From 08a42b16ddcf7c0876c8a4c678a8913451923c06 Mon Sep 17 00:00:00 2001 From: sneak Date: Fri, 20 Jun 2025 09:13:01 -0700 Subject: [PATCH] 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. --- internal/secret/debug_test.go | 32 ++++++-------------------------- internal/vault/vault_test.go | 24 +++--------------------- 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/internal/secret/debug_test.go b/internal/secret/debug_test.go index 35180f5..2eb8d1f 100644 --- a/internal/secret/debug_test.go +++ b/internal/secret/debug_test.go @@ -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() diff --git a/internal/vault/vault_test.go b/internal/vault/vault_test.go index 0724942..0d2d310 100644 --- a/internal/vault/vault_test.go +++ b/internal/vault/vault_test.go @@ -1,7 +1,6 @@ package vault import ( - "os" "path/filepath" "testing" @@ -11,29 +10,12 @@ import ( ) func TestVaultOperations(t *testing.T) { - // Save original environment variables - oldMnemonic := os.Getenv(secret.EnvMnemonic) - oldPassphrase := os.Getenv(secret.EnvUnlockPassphrase) - - // Clean up after test - defer func() { - if oldMnemonic != "" { - os.Setenv(secret.EnvMnemonic, oldMnemonic) - } else { - os.Unsetenv(secret.EnvMnemonic) - } - - if oldPassphrase != "" { - os.Setenv(secret.EnvUnlockPassphrase, oldPassphrase) - } else { - os.Unsetenv(secret.EnvUnlockPassphrase) - } - }() + // Test environment will be cleaned up automatically by t.Setenv // Set test environment variables testMnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" - os.Setenv(secret.EnvMnemonic, testMnemonic) - os.Setenv(secret.EnvUnlockPassphrase, "test-passphrase") + t.Setenv(secret.EnvMnemonic, testMnemonic) + t.Setenv(secret.EnvUnlockPassphrase, "test-passphrase") // Use in-memory filesystem fs := afero.NewMemMapFs()