From eecb7e1c1020249bd0feb6e72d19931c68490dab Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sun, 11 Dec 2022 19:27:20 +0800 Subject: [PATCH] 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 * test(debug): close logFile in initDebug Signed-off-by: Eng Zer Jun Signed-off-by: Eng Zer Jun --- pkg/debug/debug.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/debug/debug.go b/pkg/debug/debug.go index 653e7844..15613bf3 100644 --- a/pkg/debug/debug.go +++ b/pkg/debug/debug.go @@ -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) }