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:
@@ -26,15 +26,7 @@ func TestLocalOnlyMode(t *testing.T) {
|
||||
runGitCmd(t, tempDir, "config", "user.name", "Test User")
|
||||
|
||||
// Change to temp directory so git commands run in the test repo
|
||||
oldDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get working dir: %v", err)
|
||||
}
|
||||
defer os.Chdir(oldDir)
|
||||
|
||||
if err := os.Chdir(tempDir); err != nil {
|
||||
t.Fatalf("Failed to change to temp dir: %v", err)
|
||||
}
|
||||
t.Chdir(tempDir)
|
||||
|
||||
// Verify no remote exists
|
||||
cmd := exec.Command("git", "remote")
|
||||
@@ -112,15 +104,7 @@ func TestWithRemote(t *testing.T) {
|
||||
runGitCmd(t, tempDir, "clone", remoteDir, cloneDir)
|
||||
|
||||
// Change to clone directory
|
||||
oldDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get working dir: %v", err)
|
||||
}
|
||||
defer os.Chdir(oldDir)
|
||||
|
||||
if err := os.Chdir(cloneDir); err != nil {
|
||||
t.Fatalf("Failed to change to clone dir: %v", err)
|
||||
}
|
||||
t.Chdir(cloneDir)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user