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:
@@ -118,10 +118,7 @@ incomplete line without brace
|
||||
|
||||
func TestGitHasUncommittedChanges_NotInGitRepo(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
originalWd, _ := os.Getwd()
|
||||
defer os.Chdir(originalWd)
|
||||
|
||||
os.Chdir(tmpDir)
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
// Should error when not in a git repo
|
||||
_, err := gitHasUncommittedChanges()
|
||||
@@ -132,10 +129,7 @@ func TestGitHasUncommittedChanges_NotInGitRepo(t *testing.T) {
|
||||
|
||||
func TestGetCurrentGitHead_NotInGitRepo(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
originalWd, _ := os.Getwd()
|
||||
defer os.Chdir(originalWd)
|
||||
|
||||
os.Chdir(tmpDir)
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
// Should error when not in a git repo
|
||||
_, err := getCurrentGitHead()
|
||||
|
||||
Reference in New Issue
Block a user