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:
@@ -37,19 +37,9 @@ func TestCLIInstanceWithFs(t *testing.T) {
|
||||
func TestDetermineStateDir(t *testing.T) {
|
||||
// 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
|
||||
testEnvDir := "/test-env-dir"
|
||||
os.Setenv(secret.EnvStateDir, testEnvDir)
|
||||
t.Setenv(secret.EnvStateDir, testEnvDir)
|
||||
|
||||
stateDir := secret.DetermineStateDir("")
|
||||
if stateDir != testEnvDir {
|
||||
|
||||
@@ -28,7 +28,8 @@ func newGenerateMnemonicCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "mnemonic",
|
||||
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 {
|
||||
cli := NewCLIInstance()
|
||||
return cli.GenerateMnemonic(cmd)
|
||||
|
||||
@@ -52,8 +52,7 @@ func TestMain(m *testing.M) {
|
||||
// This test serves as both validation and documentation of the program's behavior.
|
||||
func TestSecretManagerIntegration(t *testing.T) {
|
||||
// Enable debug logging to diagnose issues
|
||||
os.Setenv("GODEBUG", "berlin.sneak.pkg.secret")
|
||||
defer os.Unsetenv("GODEBUG")
|
||||
t.Setenv("GODEBUG", "berlin.sneak.pkg.secret")
|
||||
|
||||
// Reinitialize debug logging to pick up the environment variable change
|
||||
secret.InitDebugLogging()
|
||||
@@ -66,8 +65,7 @@ func TestSecretManagerIntegration(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
|
||||
// Set environment variables for the test
|
||||
os.Setenv("SB_SECRET_STATE_DIR", tempDir)
|
||||
defer os.Unsetenv("SB_SECRET_STATE_DIR")
|
||||
t.Setenv("SB_SECRET_STATE_DIR", tempDir)
|
||||
|
||||
// Find the secret binary path (needed for tests that still use exec.Command)
|
||||
wd, err := os.Getwd()
|
||||
@@ -1570,7 +1568,7 @@ func test23ErrorHandling(t *testing.T, tempDir, secretPath, testMnemonic string,
|
||||
cmdOutput, err = cmd.CombinedOutput()
|
||||
assert.Error(t, err, "get without mnemonic should fail")
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user