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:
parent
b736789ecb
commit
08a42b16dd
@ -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()
|
||||
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user