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:
@@ -36,9 +36,7 @@ func TestInstallHooks(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Change to temp directory
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
// Initialize a real git repo (required for git rev-parse)
|
||||
if err := exec.Command("git", "init").Run(); err != nil {
|
||||
@@ -85,9 +83,7 @@ func TestInstallHooksBackup(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Change to temp directory
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
// Initialize a real git repo (required for git rev-parse)
|
||||
if err := exec.Command("git", "init").Run(); err != nil {
|
||||
@@ -135,9 +131,7 @@ func TestInstallHooksForce(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Change to temp directory first, then init
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
// Initialize a real git repo (required for git rev-parse)
|
||||
if err := exec.Command("git", "init").Run(); err != nil {
|
||||
@@ -175,9 +169,7 @@ func TestUninstallHooks(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Change to temp directory first, then init
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
// Initialize a real git repo (required for git rev-parse)
|
||||
if err := exec.Command("git", "init").Run(); err != nil {
|
||||
@@ -215,9 +207,7 @@ func TestHooksCheckGitHooks(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Change to temp directory first, then init
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
// Initialize a real git repo (required for git rev-parse)
|
||||
if err := exec.Command("git", "init").Run(); err != nil {
|
||||
@@ -263,9 +253,7 @@ func TestInstallHooksShared(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Change to temp directory
|
||||
oldWd, _ := os.Getwd()
|
||||
defer os.Chdir(oldWd)
|
||||
os.Chdir(tmpDir)
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
// Initialize a real git repo (needed for git config command)
|
||||
if err := exec.Command("git", "init").Run(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user