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 15package spec 16 17import ( 18 "io/ioutil" 19 "os" 20 "sync" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24) 25 26var ( 27 logMutex = &sync.Mutex{} 28) 29 30func TestDebug(t *testing.T) { 31 tmpFile, _ := ioutil.TempFile("", "debug-test") 32 tmpName := tmpFile.Name() 33 defer func() { 34 Debug = false 35 // mutex for -race 36 logMutex.Unlock() 37 _ = os.Remove(tmpName) 38 }() 39 40 // mutex for -race 41 logMutex.Lock() 42 Debug = true 43 debugOptions() 44 defer func() { 45 specLogger.SetOutput(os.Stdout) 46 }() 47 48 specLogger.SetOutput(tmpFile) 49 50 debugLog("A debug") 51 Debug = false 52 _ = tmpFile.Close() 53 54 flushed, _ := os.Open(tmpName) 55 buf := make([]byte, 500) 56 _, _ = flushed.Read(buf) 57 specLogger.SetOutput(os.Stdout) 58 assert.Contains(t, string(buf), "A debug") 59} 60