fix: replace os.Setenv with t.Setenv in tests (usetesting)

Replace os.Setenv calls with t.Setenv in test functions to ensure
proper test environment cleanup and better test isolation.
This commit is contained in:
2025-06-20 09:13:01 -07:00
parent b736789ecb
commit 08a42b16dd
2 changed files with 9 additions and 47 deletions

View File

@@ -3,7 +3,6 @@ package secret
import (
"bytes"
"log/slog"
"os"
"strings"
"syscall"
"testing"
@@ -12,17 +11,8 @@ import (
)
func TestDebugLogging(t *testing.T) {
// Save original GODEBUG and restore it
originalGodebug := os.Getenv("GODEBUG")
defer func() {
if originalGodebug == "" {
os.Unsetenv("GODEBUG")
} else {
os.Setenv("GODEBUG", originalGodebug)
}
// Re-initialize debug system with original setting
InitDebugLogging()
}()
// Test cleanup handled by t.Setenv
defer InitDebugLogging() // Re-initialize debug system after test
tests := []struct {
name string
@@ -54,10 +44,8 @@ func TestDebugLogging(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set GODEBUG
if tt.godebug == "" {
os.Unsetenv("GODEBUG")
} else {
os.Setenv("GODEBUG", tt.godebug)
if tt.godebug != "" {
t.Setenv("GODEBUG", tt.godebug)
}
// Re-initialize debug system
@@ -104,16 +92,8 @@ func TestDebugLogging(t *testing.T) {
func TestDebugFunctions(t *testing.T) {
// Enable debug for testing
originalGodebug := os.Getenv("GODEBUG")
os.Setenv("GODEBUG", "berlin.sneak.pkg.secret")
defer func() {
if originalGodebug == "" {
os.Unsetenv("GODEBUG")
} else {
os.Setenv("GODEBUG", originalGodebug)
}
InitDebugLogging()
}()
t.Setenv("GODEBUG", "berlin.sneak.pkg.secret")
defer InitDebugLogging() // Re-initialize after test
InitDebugLogging()