github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/generator/debug_test.go (about)

     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 generator
    16  
    17  import (
    18  	"os"
    19  	"sync"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  var (
    26  	logMutex = &sync.Mutex{}
    27  )
    28  
    29  func TestDebug(t *testing.T) {
    30  	// test debugLog()
    31  	tmpFile, _ := os.CreateTemp("", "debug-test")
    32  	tmpName := tmpFile.Name()
    33  	logMutex.Lock()
    34  	defer func() {
    35  		Debug = false
    36  		// mutex for -race
    37  		logMutex.Unlock()
    38  		_ = os.Remove(tmpName)
    39  	}()
    40  
    41  	// mutex for -race
    42  	Debug = true
    43  	debugOptions()
    44  	defer func() {
    45  		generatorLogger.SetOutput(os.Stdout)
    46  	}()
    47  	generatorLogger.SetOutput(tmpFile)
    48  
    49  	debugLog("A debug")
    50  	_ = tmpFile.Close()
    51  
    52  	flushed, _ := os.Open(tmpName)
    53  	buf := make([]byte, 500)
    54  	_, _ = flushed.Read(buf)
    55  	assert.Contains(t, string(buf), "A debug")
    56  	_ = flushed.Close()
    57  
    58  	// test debugLogAsJSON()
    59  	tmpJSONFile, _ := os.CreateTemp("", "debug-test")
    60  	tmpJSONName := tmpJSONFile.Name()
    61  	defer func() {
    62  		_ = os.Remove(tmpJSONName)
    63  	}()
    64  	generatorLogger.SetOutput(tmpJSONFile)
    65  	debugLogAsJSON("A short debug")
    66  
    67  	sch := struct {
    68  		FieldOne string `json:"fieldOne"`
    69  	}{
    70  		FieldOne: "content",
    71  	}
    72  	debugLogAsJSON("A long debug:%t", true, sch)
    73  	_ = tmpJSONFile.Close()
    74  
    75  	flushed, _ = os.Open(tmpJSONName)
    76  	buf2 := make([]byte, 500)
    77  	_, _ = flushed.Read(buf2)
    78  	_ = flushed.Close()
    79  	assert.Contains(t, string(buf2), "A short debug")
    80  	assert.Contains(t, string(buf2), "A long debug:true")
    81  	assert.Contains(t, string(buf2), `"fieldOne":`)
    82  	assert.Contains(t, string(buf2), `"content"`)
    83  }
    84  
    85  func TestDebugAsJSON(t *testing.T) {
    86  	t.SkipNow()
    87  
    88  	var body struct {
    89  		A string `json:"a"`
    90  		B int    `json:"b"`
    91  	}
    92  	Debug = true
    93  	body.A = "abc"
    94  	body.B = 123
    95  	debugLogAsJSON("No arg")
    96  	debugLogAsJSON("With arg:%t", true, body)
    97  }