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 (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
@ -12,17 +11,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestDebugLogging(t *testing.T) {
|
func TestDebugLogging(t *testing.T) {
|
||||||
// Save original GODEBUG and restore it
|
// Test cleanup handled by t.Setenv
|
||||||
originalGodebug := os.Getenv("GODEBUG")
|
defer InitDebugLogging() // Re-initialize debug system after test
|
||||||
defer func() {
|
|
||||||
if originalGodebug == "" {
|
|
||||||
os.Unsetenv("GODEBUG")
|
|
||||||
} else {
|
|
||||||
os.Setenv("GODEBUG", originalGodebug)
|
|
||||||
}
|
|
||||||
// Re-initialize debug system with original setting
|
|
||||||
InitDebugLogging()
|
|
||||||
}()
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@ -54,10 +44,8 @@ func TestDebugLogging(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
// Set GODEBUG
|
// Set GODEBUG
|
||||||
if tt.godebug == "" {
|
if tt.godebug != "" {
|
||||||
os.Unsetenv("GODEBUG")
|
t.Setenv("GODEBUG", tt.godebug)
|
||||||
} else {
|
|
||||||
os.Setenv("GODEBUG", tt.godebug)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Re-initialize debug system
|
// Re-initialize debug system
|
||||||
@ -104,16 +92,8 @@ func TestDebugLogging(t *testing.T) {
|
|||||||
|
|
||||||
func TestDebugFunctions(t *testing.T) {
|
func TestDebugFunctions(t *testing.T) {
|
||||||
// Enable debug for testing
|
// Enable debug for testing
|
||||||
originalGodebug := os.Getenv("GODEBUG")
|
t.Setenv("GODEBUG", "berlin.sneak.pkg.secret")
|
||||||
os.Setenv("GODEBUG", "berlin.sneak.pkg.secret")
|
defer InitDebugLogging() // Re-initialize after test
|
||||||
defer func() {
|
|
||||||
if originalGodebug == "" {
|
|
||||||
os.Unsetenv("GODEBUG")
|
|
||||||
} else {
|
|
||||||
os.Setenv("GODEBUG", originalGodebug)
|
|
||||||
}
|
|
||||||
InitDebugLogging()
|
|
||||||
}()
|
|
||||||
|
|
||||||
InitDebugLogging()
|
InitDebugLogging()
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package vault
|
package vault
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -11,29 +10,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestVaultOperations(t *testing.T) {
|
func TestVaultOperations(t *testing.T) {
|
||||||
// Save original environment variables
|
// Test environment will be cleaned up automatically by t.Setenv
|
||||||
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)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Set test environment variables
|
// Set test environment variables
|
||||||
testMnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
|
testMnemonic := "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
|
||||||
os.Setenv(secret.EnvMnemonic, testMnemonic)
|
t.Setenv(secret.EnvMnemonic, testMnemonic)
|
||||||
os.Setenv(secret.EnvUnlockPassphrase, "test-passphrase")
|
t.Setenv(secret.EnvUnlockPassphrase, "test-passphrase")
|
||||||
|
|
||||||
// Use in-memory filesystem
|
// Use in-memory filesystem
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
Loading…
Reference in New Issue
Block a user