test: replace manual os.Chdir with t.Chdir in tests (#457)

Replaces manual working directory save/restore patterns
with Go's built-in `t.Chdir()` helper across 23 test files.

The manual pattern involved calling `os.Getwd()` to save
the original directory, using `defer os.Chdir(origWd)` for
restoration, and manually handling errors during directory
changes. This boilerplate has been replaced with single
`t.Chdir(path)` calls that handle cleanup automatically.

The `t.Chdir()` method automatically restores the working
directory when the test completes, eliminating the need for
manual defer statements and error handling.

Total:
~75 instances replaced (assuming Claude's math is right)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Abhinav Gupta
2025-12-04 11:21:43 -08:00
committed by GitHub
parent f4b8a7ad4f
commit ac8ef9b9e3
23 changed files with 113 additions and 612 deletions

View File

@@ -106,12 +106,7 @@ func TestTrackBdVersion_NoBeadsDir(t *testing.T) {
// Change to temp directory with no .beads
tmpDir := t.TempDir()
origWd, _ := os.Getwd()
defer os.Chdir(origWd)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change to temp dir: %v", err)
}
t.Chdir(tmpDir)
// trackBdVersion should silently succeed
trackBdVersion()
@@ -137,11 +132,7 @@ func TestTrackBdVersion_FirstRun(t *testing.T) {
}
// Change to temp directory
origWd, _ := os.Getwd()
defer os.Chdir(origWd)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change to temp dir: %v", err)
}
t.Chdir(tmpDir)
// Save original state
origUpgradeDetected := versionUpgradeDetected
@@ -180,11 +171,7 @@ func TestTrackBdVersion_UpgradeDetection(t *testing.T) {
}
// Change to temp directory
origWd, _ := os.Getwd()
defer os.Chdir(origWd)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change to temp dir: %v", err)
}
t.Chdir(tmpDir)
// Create minimal metadata.json so FindBeadsDir can find the directory (bd-420)
metadataPath := filepath.Join(beadsDir, "metadata.json")
@@ -238,11 +225,7 @@ func TestTrackBdVersion_SameVersion(t *testing.T) {
}
// Change to temp directory
origWd, _ := os.Getwd()
defer os.Chdir(origWd)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change to temp dir: %v", err)
}
t.Chdir(tmpDir)
// Create .local_version with current version
localVersionPath := filepath.Join(beadsDir, localVersionFile)