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

@@ -369,10 +369,6 @@ func TestBatchCreateIssues(t *testing.T) {
// TestFindDatabasePathIntegration tests the database discovery
func TestFindDatabasePathIntegration(t *testing.T) {
// Save original working directory
originalWd, _ := os.Getwd()
defer os.Chdir(originalWd)
// Create temporary directory with .beads
tmpDir, err := os.MkdirTemp("", "beads-find-*")
if err != nil {
@@ -388,7 +384,7 @@ func TestFindDatabasePathIntegration(t *testing.T) {
f.Close()
// Change to temp directory
os.Chdir(tmpDir)
t.Chdir(tmpDir)
// Should find the database
found := beads.FindDatabasePath()

View File

@@ -58,17 +58,8 @@ func TestFindAllDatabases(t *testing.T) {
t.Fatal(err)
}
// Save original working directory
origDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(origDir)
// Change to subdir and test FindAllDatabases
if err := os.Chdir(subdir); err != nil {
t.Fatal(err)
}
t.Chdir(subdir)
databases := FindAllDatabases()
@@ -118,17 +109,8 @@ func TestFindAllDatabases_Single(t *testing.T) {
t.Fatal(err)
}
// Save original working directory
origDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(origDir)
// Change to tmpDir and test
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
t.Chdir(tmpDir)
databases := FindAllDatabases()
@@ -150,17 +132,8 @@ func TestFindAllDatabases_None(t *testing.T) {
}
defer os.RemoveAll(tmpDir)
// Save original working directory
origDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(origDir)
// Change to tmpDir and test
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
t.Chdir(tmpDir)
databases := FindAllDatabases()

View File

@@ -61,17 +61,8 @@ func TestFindAllDatabases_SymlinkDeduplication(t *testing.T) {
t.Fatal(err)
}
// Save original working directory
origDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(origDir)
// Change to subdir (which is inside the symlinked directory)
if err := os.Chdir(subdir); err != nil {
t.Fatal(err)
}
t.Chdir(subdir)
// Call FindAllDatabases
databases := FindAllDatabases()
@@ -150,16 +141,8 @@ func TestFindAllDatabases_MultipleSymlinksToSameDB(t *testing.T) {
t.Fatal(err)
}
// Save and change directory
origDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(origDir)
if err := os.Chdir(workdir); err != nil {
t.Fatal(err)
}
// Change to working directory
t.Chdir(workdir)
// Find databases
databases := FindAllDatabases()

View File

@@ -30,16 +30,14 @@ func TestFindDatabasePathEnvVar(t *testing.T) {
}
func TestFindDatabasePathInTree(t *testing.T) {
// Save original env var and working directory
// Save original env var
originalEnv := os.Getenv("BEADS_DB")
originalWd, _ := os.Getwd()
defer func() {
if originalEnv != "" {
os.Setenv("BEADS_DB", originalEnv)
} else {
os.Unsetenv("BEADS_DB")
}
os.Chdir(originalWd)
}()
// Clear env var
@@ -73,10 +71,7 @@ func TestFindDatabasePathInTree(t *testing.T) {
t.Fatalf("Failed to create subdirectory: %v", err)
}
err = os.Chdir(subDir)
if err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
t.Chdir(subDir)
// Should find the database in the parent directory tree
result := FindDatabasePath()
@@ -97,16 +92,14 @@ func TestFindDatabasePathInTree(t *testing.T) {
}
func TestFindDatabasePathNotFound(t *testing.T) {
// Save original env var and working directory
// Save original env var
originalEnv := os.Getenv("BEADS_DB")
originalWd, _ := os.Getwd()
defer func() {
if originalEnv != "" {
os.Setenv("BEADS_DB", originalEnv)
} else {
os.Unsetenv("BEADS_DB")
}
os.Chdir(originalWd)
}()
// Clear env var
@@ -119,10 +112,7 @@ func TestFindDatabasePathNotFound(t *testing.T) {
}
defer os.RemoveAll(tmpDir)
err = os.Chdir(tmpDir)
if err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
t.Chdir(tmpDir)
// Should return empty string (no database found)
result := FindDatabasePath()
@@ -366,14 +356,12 @@ func TestHasBeadsProjectFiles(t *testing.T) {
func TestFindBeadsDirSkipsDaemonRegistry(t *testing.T) {
// Save original state
originalEnv := os.Getenv("BEADS_DIR")
originalWd, _ := os.Getwd()
defer func() {
if originalEnv != "" {
os.Setenv("BEADS_DIR", originalEnv)
} else {
os.Unsetenv("BEADS_DIR")
}
os.Chdir(originalWd)
}()
os.Unsetenv("BEADS_DIR")
@@ -394,9 +382,7 @@ func TestFindBeadsDirSkipsDaemonRegistry(t *testing.T) {
}
// Change to temp dir
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
t.Chdir(tmpDir)
// Should NOT find the daemon-only directory
result := FindBeadsDir()
@@ -465,14 +451,12 @@ func TestFindDatabasePathHomeDefault(t *testing.T) {
// creating the file and just verify the function doesn't crash
originalEnv := os.Getenv("BEADS_DB")
originalWd, _ := os.Getwd()
defer func() {
if originalEnv != "" {
os.Setenv("BEADS_DB", originalEnv)
} else {
os.Unsetenv("BEADS_DB")
}
os.Chdir(originalWd)
}()
os.Unsetenv("BEADS_DB")
@@ -484,10 +468,7 @@ func TestFindDatabasePathHomeDefault(t *testing.T) {
}
defer os.RemoveAll(tmpDir)
err = os.Chdir(tmpDir)
if err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
t.Chdir(tmpDir)
// Call FindDatabasePath - it might return home dir default or empty string
result := FindDatabasePath()
@@ -691,14 +672,12 @@ func TestFollowRedirect(t *testing.T) {
func TestFindDatabasePathWithRedirect(t *testing.T) {
// Save original state
originalEnv := os.Getenv("BEADS_DIR")
originalWd, _ := os.Getwd()
defer func() {
if originalEnv != "" {
os.Setenv("BEADS_DIR", originalEnv)
} else {
os.Unsetenv("BEADS_DIR")
}
os.Chdir(originalWd)
}()
os.Unsetenv("BEADS_DIR")
@@ -733,9 +712,7 @@ func TestFindDatabasePathWithRedirect(t *testing.T) {
// Change to project directory
projectDir := filepath.Join(tmpDir, "project")
if err := os.Chdir(projectDir); err != nil {
t.Fatal(err)
}
t.Chdir(projectDir)
// FindDatabasePath should follow the redirect
result := FindDatabasePath()
@@ -753,14 +730,12 @@ func TestFindDatabasePathWithRedirect(t *testing.T) {
func TestFindBeadsDirWithRedirect(t *testing.T) {
// Save original state
originalEnv := os.Getenv("BEADS_DIR")
originalWd, _ := os.Getwd()
defer func() {
if originalEnv != "" {
os.Setenv("BEADS_DIR", originalEnv)
} else {
os.Unsetenv("BEADS_DIR")
}
os.Chdir(originalWd)
}()
os.Unsetenv("BEADS_DIR")
@@ -794,9 +769,7 @@ func TestFindBeadsDirWithRedirect(t *testing.T) {
// Change to project directory
projectDir := filepath.Join(tmpDir, "project")
if err := os.Chdir(projectDir); err != nil {
t.Fatal(err)
}
t.Chdir(projectDir)
// FindBeadsDir should follow the redirect
result := FindBeadsDir()