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

@@ -172,9 +172,7 @@ func TestCheckAndAutoImport_EmptyDatabaseNoGit(t *testing.T) {
}()
// Change to temp dir (no git repo)
oldWd, _ := os.Getwd()
defer os.Chdir(oldWd)
os.Chdir(tmpDir)
t.Chdir(tmpDir)
result := checkAndAutoImport(ctx, store)
if result {
@@ -191,9 +189,7 @@ func TestFindBeadsDir(t *testing.T) {
}
// Change to tmpDir
oldWd, _ := os.Getwd()
defer os.Chdir(oldWd)
os.Chdir(tmpDir)
t.Chdir(tmpDir)
found := findBeadsDir()
if found == "" {
@@ -211,9 +207,7 @@ func TestFindBeadsDir_NotFound(t *testing.T) {
// Create temp directory without .beads
tmpDir := t.TempDir()
oldWd, _ := os.Getwd()
defer os.Chdir(oldWd)
os.Chdir(tmpDir)
t.Chdir(tmpDir)
found := findBeadsDir()
// findBeadsDir walks up to root, so it might find .beads in parent dirs
@@ -237,9 +231,7 @@ func TestFindBeadsDir_ParentDirectory(t *testing.T) {
}
// Change to subdir
oldWd, _ := os.Getwd()
defer os.Chdir(oldWd)
os.Chdir(subDir)
t.Chdir(subDir)
found := findBeadsDir()
if found == "" {
@@ -256,9 +248,7 @@ func TestFindBeadsDir_ParentDirectory(t *testing.T) {
func TestCheckGitForIssues_NoGitRepo(t *testing.T) {
// Change to temp dir (not a git repo)
tmpDir := t.TempDir()
oldWd, _ := os.Getwd()
defer os.Chdir(oldWd)
os.Chdir(tmpDir)
t.Chdir(tmpDir)
count, path := checkGitForIssues()
if count != 0 {
@@ -272,9 +262,7 @@ func TestCheckGitForIssues_NoGitRepo(t *testing.T) {
func TestCheckGitForIssues_NoBeadsDir(t *testing.T) {
// Use current directory which has git but change to somewhere without .beads
tmpDir := t.TempDir()
oldWd, _ := os.Getwd()
defer os.Chdir(oldWd)
os.Chdir(tmpDir)
t.Chdir(tmpDir)
count, path := checkGitForIssues()
if count != 0 || path != "" {