debug_test.go 1.3 KB

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