Fix all usetesting linter errors

- Replace os.MkdirTemp() with t.TempDir() in test files
- Replace os.Setenv() with t.Setenv() in test files
- Remove manual environment cleanup code (t.Setenv automatically restores)
- Remove unused "os" imports from files that no longer use os package
This commit is contained in:
Jeffrey Paul 2025-06-20 08:18:38 -07:00
parent fd7ab06fb1
commit ecaa5e101b
11 changed files with 34 additions and 163 deletions

View File

@ -14,7 +14,9 @@
"Bash(make:*)", "Bash(make:*)",
"Bash(golangci-lint run:*)", "Bash(golangci-lint run:*)",
"Bash(git add:*)", "Bash(git add:*)",
"Bash(gofumpt:*)" "Bash(gofumpt:*)",
"Bash(git commit:*)",
"Bash(git push:*)"
], ],
"deny": [] "deny": []
} }

View File

@ -6,6 +6,11 @@ prioritized from most critical (top) to least critical (bottom).
## Code Cleanups ## Code Cleanups
* none of the integration tests should be searching for a binary or trying
to execute another process. the integration tests cannot make another
process or depend on a compiled file, they must do all of their testing in
the current (test) process.
* we shouldn't be passing around a statedir, it should be read from the * we shouldn't be passing around a statedir, it should be read from the
environment or default. environment or default.

View File

@ -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 {

View File

@ -28,7 +28,8 @@ func newGenerateMnemonicCmd() *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "mnemonic", Use: "mnemonic",
Short: "Generate a random BIP39 mnemonic phrase", Short: "Generate a random BIP39 mnemonic phrase",
Long: `Generate a cryptographically secure random BIP39 mnemonic phrase that can be used with 'secret init' or 'secret import'.`, Long: `Generate a cryptographically secure random BIP39 mnemonic phrase that can be used with ` +
`'secret init' or 'secret import'.`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cli := NewCLIInstance() cli := NewCLIInstance()
return cli.GenerateMnemonic(cmd) return cli.GenerateMnemonic(cmd)

View File

@ -52,8 +52,7 @@ func TestMain(m *testing.M) {
// This test serves as both validation and documentation of the program's behavior. // This test serves as both validation and documentation of the program's behavior.
func TestSecretManagerIntegration(t *testing.T) { func TestSecretManagerIntegration(t *testing.T) {
// Enable debug logging to diagnose issues // Enable debug logging to diagnose issues
os.Setenv("GODEBUG", "berlin.sneak.pkg.secret") t.Setenv("GODEBUG", "berlin.sneak.pkg.secret")
defer os.Unsetenv("GODEBUG")
// Reinitialize debug logging to pick up the environment variable change // Reinitialize debug logging to pick up the environment variable change
secret.InitDebugLogging() secret.InitDebugLogging()
@ -66,8 +65,7 @@ func TestSecretManagerIntegration(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
// Set environment variables for the test // Set environment variables for the test
os.Setenv("SB_SECRET_STATE_DIR", tempDir) t.Setenv("SB_SECRET_STATE_DIR", tempDir)
defer os.Unsetenv("SB_SECRET_STATE_DIR")
// Find the secret binary path (needed for tests that still use exec.Command) // Find the secret binary path (needed for tests that still use exec.Command)
wd, err := os.Getwd() wd, err := os.Getwd()
@ -1570,7 +1568,7 @@ func test23ErrorHandling(t *testing.T, tempDir, secretPath, testMnemonic string,
cmdOutput, err = cmd.CombinedOutput() cmdOutput, err = cmd.CombinedOutput()
assert.Error(t, err, "get without mnemonic should fail") assert.Error(t, err, "get without mnemonic should fail")
assert.Contains(t, string(cmdOutput), "failed to unlock", "should indicate unlock failure") assert.Contains(t, string(cmdOutput), "failed to unlock", "should indicate unlock failure")
os.Setenv("SB_SECRET_MNEMONIC", unsetMnemonic) t.Setenv("SB_SECRET_MNEMONIC", unsetMnemonic)
// Invalid secret names (already tested in test 12) // Invalid secret names (already tested in test 12)

View File

@ -3,7 +3,6 @@ package secret
import ( import (
"bytes" "bytes"
"log/slog" "log/slog"
"os"
"strings" "strings"
"syscall" "syscall"
"testing" "testing"
@ -12,14 +11,8 @@ import (
) )
func TestDebugLogging(t *testing.T) { func TestDebugLogging(t *testing.T) {
// Save original GODEBUG and restore it // Original GODEBUG will be restored automatically by t.Setenv
originalGodebug := os.Getenv("GODEBUG")
defer func() { defer func() {
if originalGodebug == "" {
os.Unsetenv("GODEBUG")
} else {
os.Setenv("GODEBUG", originalGodebug)
}
// Re-initialize debug system with original setting // Re-initialize debug system with original setting
InitDebugLogging() InitDebugLogging()
}() }()
@ -55,9 +48,9 @@ func TestDebugLogging(t *testing.T) {
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", "")
} else { } else {
os.Setenv("GODEBUG", tt.godebug) t.Setenv("GODEBUG", tt.godebug)
} }
// Re-initialize debug system // Re-initialize debug system
@ -104,14 +97,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 func() { defer func() {
if originalGodebug == "" {
os.Unsetenv("GODEBUG")
} else {
os.Setenv("GODEBUG", originalGodebug)
}
InitDebugLogging() InitDebugLogging()
}() }()

View File

@ -19,11 +19,7 @@ func TestPassphraseUnlockerWithRealFS(t *testing.T) {
} }
// Create a temporary directory for our tests // Create a temporary directory for our tests
tempDir, err := os.MkdirTemp("", "secret-passphrase-test-") tempDir := t.TempDir()
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir) // Clean up after test
// Use the real filesystem // Use the real filesystem
fs := afero.NewOsFs() fs := afero.NewOsFs()
@ -131,18 +127,8 @@ func TestPassphraseUnlockerWithRealFS(t *testing.T) {
} }
}) })
// Save original environment variables and set test ones // Set test environment variables
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) {

View File

@ -131,11 +131,7 @@ func TestPGPUnlockerWithRealFS(t *testing.T) {
} }
// Create a temporary directory for our tests // Create a temporary directory for our tests
tempDir, err := os.MkdirTemp("", "secret-pgp-test-") tempDir := t.TempDir()
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir) // Clean up after test
// Create a temporary GNUPGHOME // Create a temporary GNUPGHOME
gnupgHomeDir := filepath.Join(tempDir, "gnupg") gnupgHomeDir := filepath.Join(tempDir, "gnupg")
@ -143,20 +139,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"
@ -182,7 +166,7 @@ Passphrase: ` + testPassphrase + `
// Generate GPG key with batch mode // Generate GPG key with batch mode
t.Log("Generating GPG key...") t.Log("Generating GPG key...")
_, err = runGPGWithPassphrase(gnupgHomeDir, testPassphrase, _, err := runGPGWithPassphrase(gnupgHomeDir, testPassphrase,
[]string{"--gen-key", batchFile}, nil) []string{"--gen-key", batchFile}, nil)
if err != nil { if err != nil {
t.Fatalf("Failed to generate GPG key: %v", err) t.Fatalf("Failed to generate GPG key: %v", err)
@ -224,15 +208,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 +216,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

View File

@ -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()

View File

@ -13,11 +13,7 @@ import (
func TestVaultWithRealFilesystem(t *testing.T) { func TestVaultWithRealFilesystem(t *testing.T) {
// Create a temporary directory for our tests // Create a temporary directory for our tests
tempDir, err := os.MkdirTemp("", "secret-test-") tempDir := t.TempDir()
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir) // Clean up after test
// Use the real filesystem // Use the real filesystem
fs := afero.NewOsFs() fs := afero.NewOsFs()
@ -25,28 +21,9 @@ func TestVaultWithRealFilesystem(t *testing.T) {
// Test mnemonic // 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"
// Save original environment variables
oldMnemonic := os.Getenv(secret.EnvMnemonic)
oldPassphrase := os.Getenv(secret.EnvUnlockPassphrase)
// Set test environment variables // Set test environment variables
os.Setenv(secret.EnvMnemonic, testMnemonic) t.Setenv(secret.EnvMnemonic, testMnemonic)
os.Setenv(secret.EnvUnlockPassphrase, "test-passphrase") t.Setenv(secret.EnvUnlockPassphrase, "test-passphrase")
// 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 symlink handling // Test symlink handling
t.Run("SymlinkHandling", func(t *testing.T) { t.Run("SymlinkHandling", func(t *testing.T) {

View File

@ -1,7 +1,6 @@
package vault package vault
import ( import (
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -11,29 +10,10 @@ import (
) )
func TestVaultOperations(t *testing.T) { 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)
}
}()
// 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()