| 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 validate |
| 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 |
tmpFile, _ := os.CreateTemp("", "debug-test") |
| 31 |
tmpName := tmpFile.Name() |
| 32 |
defer func() { |
| 33 |
Debug = false |
| 34 |
// mutex for -race |
| 35 |
logMutex.Unlock() |
| 36 |
os.Remove(tmpName) |
| 37 |
}() |
| 38 |
|
| 39 |
// mutex for -race |
| 40 |
logMutex.Lock() |
| 41 |
Debug = true |
| 42 |
debugOptions() |
| 43 |
defer func() { |
| 44 |
validateLogger.SetOutput(os.Stdout) |
| 45 |
}() |
| 46 |
|
| 47 |
validateLogger.SetOutput(tmpFile) |
| 48 |
|
| 49 |
debugLog("A debug") |
| 50 |
Debug = false |
| 51 |
tmpFile.Close() |
| 52 |
|
| 53 |
flushed, _ := os.Open(tmpName) |
| 54 |
buf := make([]byte, 500) |
| 55 |
_, _ = flushed.Read(buf) |
| 56 |
validateLogger.SetOutput(os.Stdout) |
| 57 |
assert.Contains(t, string(buf), "A debug") |
| 58 |
} |