fix: replace remaining os.Setenv with t.Setenv in tests
Replace all os.Setenv calls with t.Setenv in test functions to ensure proper test environment cleanup and better test isolation. This leaves only legitimate application code and helper functions using os.Setenv.
This commit is contained in:
parent
c6935d8f0f
commit
c450e1c13d
@ -37,19 +37,9 @@ func TestCLIInstanceWithFs(t *testing.T) {
|
|||||||
func TestDetermineStateDir(t *testing.T) {
|
func TestDetermineStateDir(t *testing.T) {
|
||||||
// Test the determineStateDir function from the secret package
|
// Test the determineStateDir function from the secret package
|
||||||
|
|
||||||
// Save original environment and restore it after test
|
|
||||||
originalStateDir := os.Getenv(secret.EnvStateDir)
|
|
||||||
defer func() {
|
|
||||||
if originalStateDir == "" {
|
|
||||||
os.Unsetenv(secret.EnvStateDir)
|
|
||||||
} else {
|
|
||||||
os.Setenv(secret.EnvStateDir, originalStateDir)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Test with environment variable set
|
// Test with environment variable set
|
||||||
testEnvDir := "/test-env-dir"
|
testEnvDir := "/test-env-dir"
|
||||||
os.Setenv(secret.EnvStateDir, testEnvDir)
|
t.Setenv(secret.EnvStateDir, testEnvDir)
|
||||||
|
|
||||||
stateDir := secret.DetermineStateDir("")
|
stateDir := secret.DetermineStateDir("")
|
||||||
if stateDir != testEnvDir {
|
if stateDir != testEnvDir {
|
||||||
|
@ -131,18 +131,8 @@ func TestPassphraseUnlockerWithRealFS(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Save original environment variables and set test ones
|
// Set test environment variable (cleaned up automatically)
|
||||||
oldPassphrase := os.Getenv(secret.EnvUnlockPassphrase)
|
t.Setenv(secret.EnvUnlockPassphrase, testPassphrase)
|
||||||
os.Setenv(secret.EnvUnlockPassphrase, testPassphrase)
|
|
||||||
|
|
||||||
// Clean up after test
|
|
||||||
defer func() {
|
|
||||||
if oldPassphrase != "" {
|
|
||||||
os.Setenv(secret.EnvUnlockPassphrase, oldPassphrase)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv(secret.EnvUnlockPassphrase)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Test getting identity from environment variable
|
// Test getting identity from environment variable
|
||||||
t.Run("GetIdentityFromEnv", func(t *testing.T) {
|
t.Run("GetIdentityFromEnv", func(t *testing.T) {
|
||||||
|
@ -143,20 +143,8 @@ func TestPGPUnlockerWithRealFS(t *testing.T) {
|
|||||||
t.Fatalf("Failed to create GNUPGHOME: %v", err)
|
t.Fatalf("Failed to create GNUPGHOME: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save original GNUPGHOME
|
|
||||||
origGnupgHome := os.Getenv("GNUPGHOME")
|
|
||||||
|
|
||||||
// Set new GNUPGHOME
|
// Set new GNUPGHOME
|
||||||
os.Setenv("GNUPGHOME", gnupgHomeDir)
|
t.Setenv("GNUPGHOME", gnupgHomeDir)
|
||||||
|
|
||||||
// Clean up environment after test
|
|
||||||
defer func() {
|
|
||||||
if origGnupgHome != "" {
|
|
||||||
os.Setenv("GNUPGHOME", origGnupgHome)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv("GNUPGHOME")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Test passphrase for GPG key
|
// Test passphrase for GPG key
|
||||||
testPassphrase := "test123"
|
testPassphrase := "test123"
|
||||||
@ -224,15 +212,7 @@ Passphrase: ` + testPassphrase + `
|
|||||||
t.Logf("Generated GPG fingerprint: %s", fingerprint)
|
t.Logf("Generated GPG fingerprint: %s", fingerprint)
|
||||||
|
|
||||||
// Set the GPG_AGENT_INFO to empty to ensure gpg-agent doesn't interfere
|
// Set the GPG_AGENT_INFO to empty to ensure gpg-agent doesn't interfere
|
||||||
oldAgentInfo := os.Getenv("GPG_AGENT_INFO")
|
t.Setenv("GPG_AGENT_INFO", "")
|
||||||
os.Setenv("GPG_AGENT_INFO", "")
|
|
||||||
defer func() {
|
|
||||||
if oldAgentInfo != "" {
|
|
||||||
os.Setenv("GPG_AGENT_INFO", oldAgentInfo)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv("GPG_AGENT_INFO")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Use the real filesystem
|
// Use the real filesystem
|
||||||
fs := afero.NewOsFs()
|
fs := afero.NewOsFs()
|
||||||
@ -240,28 +220,9 @@ Passphrase: ` + testPassphrase + `
|
|||||||
// Test data
|
// Test data
|
||||||
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"
|
||||||
|
|
||||||
// Save original environment variable
|
|
||||||
oldMnemonic := os.Getenv(secret.EnvMnemonic)
|
|
||||||
oldGPGKeyID := os.Getenv(secret.EnvGPGKeyID)
|
|
||||||
|
|
||||||
// Set test environment variables
|
// Set test environment variables
|
||||||
os.Setenv(secret.EnvMnemonic, testMnemonic)
|
t.Setenv(secret.EnvMnemonic, testMnemonic)
|
||||||
os.Setenv(secret.EnvGPGKeyID, keyID)
|
t.Setenv(secret.EnvGPGKeyID, keyID)
|
||||||
|
|
||||||
// Clean up after test
|
|
||||||
defer func() {
|
|
||||||
if oldMnemonic != "" {
|
|
||||||
os.Setenv(secret.EnvMnemonic, oldMnemonic)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv(secret.EnvMnemonic)
|
|
||||||
}
|
|
||||||
|
|
||||||
if oldGPGKeyID != "" {
|
|
||||||
os.Setenv(secret.EnvGPGKeyID, oldGPGKeyID)
|
|
||||||
} else {
|
|
||||||
os.Unsetenv(secret.EnvGPGKeyID)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Set up vault structure for testing
|
// Set up vault structure for testing
|
||||||
stateDir := tempDir
|
stateDir := tempDir
|
||||||
|
@ -128,19 +128,9 @@ func TestPerSecretKeyFunctionality(t *testing.T) {
|
|||||||
// Create an in-memory filesystem for testing
|
// Create an in-memory filesystem for testing
|
||||||
fs := afero.NewMemMapFs()
|
fs := afero.NewMemMapFs()
|
||||||
|
|
||||||
// Set up test environment variables
|
|
||||||
oldMnemonic := os.Getenv(EnvMnemonic)
|
|
||||||
defer func() {
|
|
||||||
if oldMnemonic == "" {
|
|
||||||
os.Unsetenv(EnvMnemonic)
|
|
||||||
} else {
|
|
||||||
os.Setenv(EnvMnemonic, oldMnemonic)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Set test mnemonic for direct encryption/decryption
|
// Set test mnemonic for direct encryption/decryption
|
||||||
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(EnvMnemonic, testMnemonic)
|
t.Setenv(EnvMnemonic, testMnemonic)
|
||||||
|
|
||||||
// Set up a test vault structure
|
// Set up a test vault structure
|
||||||
baseDir := "/test-config/berlin.sneak.pkg.secret"
|
baseDir := "/test-config/berlin.sneak.pkg.secret"
|
||||||
@ -312,9 +302,7 @@ func TestSecretGetValueWithEnvMnemonicUsesVaultDerivationIndex(t *testing.T) {
|
|||||||
|
|
||||||
// Set up test mnemonic
|
// Set up test mnemonic
|
||||||
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"
|
||||||
originalEnv := os.Getenv(EnvMnemonic)
|
t.Setenv(EnvMnemonic, testMnemonic)
|
||||||
os.Setenv(EnvMnemonic, testMnemonic)
|
|
||||||
defer os.Setenv(EnvMnemonic, originalEnv)
|
|
||||||
|
|
||||||
// Create temporary directory for vaults
|
// Create temporary directory for vaults
|
||||||
fs := afero.NewOsFs()
|
fs := afero.NewOsFs()
|
||||||
|
Loading…
Reference in New Issue
Block a user