test: fix failing debug_test.go on Windows (#2464)

* test: fix failing debug_test.go on Windows

--- FAIL: TestDebug (1.52s)
    testing.go:1097: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestDebug1334287763\001\gopass.log: The process cannot access the file because it is being used by another process.
--- FAIL: TestDebugSecret (1.90s)
    testing.go:1097: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestDebugSecret2374267425\001\gopass.log: The process cannot access the file because it is being used by another process.
--- FAIL: TestDebugFilter (1.77s)
    testing.go:1097: TempDir RemoveAll cleanup: remove C:\Users\RUNNER~1\AppData\Local\Temp\TestDebugFilter836798913\001\gopass.log: The process cannot access the file because it is being used by another process.

The gopass.log needs to be closed before it can be deleted on Windows.

Fixes: f584544 ("Increase test coverage (#2461)")
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* test(debug): close logFile in initDebug

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2022-12-11 19:27:20 +08:00 committed by GitHub
parent f58454452f
commit eecb7e1c10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,9 +20,10 @@ var (
)
var opts struct {
logger *log.Logger
funcs map[string]bool
files map[string]bool
logger *log.Logger
funcs map[string]bool
files map[string]bool
logFile *os.File
}
var logFn = doNotLog
@ -31,6 +32,10 @@ var logFn = doNotLog
var enabled = initDebug()
func initDebug() bool {
if opts.logFile != nil {
_ = opts.logFile.Close()
}
if os.Getenv("GOPASS_DEBUG") == "" && os.Getenv("GOPASS_DEBUG_LOG") == "" {
logFn = doNotLog
@ -62,6 +67,7 @@ func initDebugLogger() {
// seek to the end of the file (offset, whence [2 = end])
_, err := f.Seek(0, 2)
if err != nil {
_ = f.Close()
fmt.Fprintf(Stderr, "unable to seek to end of %v: %v\n", debugfile, err)
os.Exit(3)
}
@ -76,6 +82,7 @@ func initDebugLogger() {
os.Exit(2)
}
opts.logFile = f
opts.logger = log.New(f, "", log.Ldate|log.Lmicroseconds)
}