...

Source file src/github.com/go-openapi/analysis/internal/debug/debug_test.go

Documentation: github.com/go-openapi/analysis/internal/debug

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package debug
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestDebug(t *testing.T) {
    26  	tmpFile, err := os.CreateTemp("", "debug-test")
    27  	require.NoError(t, err)
    28  
    29  	output = tmpFile
    30  
    31  	tmpName := tmpFile.Name()
    32  	defer func() {
    33  		_ = os.Remove(tmpName)
    34  	}()
    35  
    36  	testLogger := GetLogger("test", true)
    37  
    38  	testLogger("A debug: %s", "a string")
    39  	tmpFile.Close()
    40  
    41  	flushed, err := os.ReadFile(tmpName)
    42  	require.NoError(t, err)
    43  
    44  	assert.Contains(t, string(flushed), "A debug: a string")
    45  
    46  	tmpEmptyFile, err := os.CreateTemp("", "debug-test")
    47  	require.NoError(t, err)
    48  	tmpEmpty := tmpEmptyFile.Name()
    49  	defer func() {
    50  		_ = os.Remove(tmpEmpty)
    51  	}()
    52  
    53  	testLogger = GetLogger("test", false)
    54  	testLogger("A debug: %s", "a string")
    55  	tmpFile.Close()
    56  
    57  	flushed, err = os.ReadFile(tmpEmpty)
    58  	require.NoError(t, err)
    59  
    60  	assert.Empty(t, flushed)
    61  }
    62  

View as plain text