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()

View File

@@ -105,30 +105,23 @@ flush-debounce: 15s
t.Fatalf("failed to write config file: %v", err)
}
// Change to tmp directory so config file is discovered
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
defer os.Chdir(origDir)
// Create .beads directory
beadsDir := filepath.Join(tmpDir, ".beads")
if err := os.MkdirAll(beadsDir, 0750); err != nil {
t.Fatalf("failed to create .beads directory: %v", err)
}
// Move config to .beads directory
beadsConfigPath := filepath.Join(beadsDir, "config.yaml")
if err := os.Rename(configPath, beadsConfigPath); err != nil {
t.Fatalf("failed to move config file: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("failed to change directory: %v", err)
}
// Change to tmp directory so config file is discovered
t.Chdir(tmpDir)
// Initialize viper
var err error
err = Initialize()
if err != nil {
t.Fatalf("Initialize() returned error: %v", err)
@@ -169,17 +162,10 @@ func TestConfigPrecedence(t *testing.T) {
}
// Change to tmp directory
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
defer os.Chdir(origDir)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("failed to change directory: %v", err)
}
t.Chdir(tmpDir)
// Test 1: Config file value (json: false)
var err error
err = Initialize()
if err != nil {
t.Fatalf("Initialize() returned error: %v", err)
@@ -288,17 +274,10 @@ repos:
}
// Change to tmp directory
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
defer os.Chdir(origDir)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("failed to change directory: %v", err)
}
t.Chdir(tmpDir)
// Initialize viper
var err error
err = Initialize()
if err != nil {
t.Fatalf("Initialize() returned error: %v", err)
@@ -365,17 +344,10 @@ repos:
}
// Change to tmp directory
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
defer os.Chdir(origDir)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("failed to change directory: %v", err)
}
t.Chdir(tmpDir)
// Initialize viper
var err error
err = Initialize()
if err != nil {
t.Fatalf("Initialize() returned error: %v", err)

View File

@@ -297,28 +297,13 @@ func setupBenchServer(b *testing.B) (*Server, *Client, func(), string) {
time.Sleep(100 * time.Millisecond)
// Change to tmpDir so client's os.Getwd() finds the test database
originalWd, err := os.Getwd()
if err != nil {
cancel()
server.Stop()
store.Close()
os.RemoveAll(tmpDir)
b.Fatalf("Failed to get working directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
cancel()
server.Stop()
store.Close()
os.RemoveAll(tmpDir)
b.Fatalf("Failed to change directory: %v", err)
}
b.Chdir(tmpDir)
client, err := TryConnect(socketPath)
if err != nil {
cancel()
server.Stop()
store.Close()
os.Chdir(originalWd)
os.RemoveAll(tmpDir)
b.Fatalf("Failed to connect client: %v", err)
}
@@ -331,7 +316,6 @@ func setupBenchServer(b *testing.B) (*Server, *Client, func(), string) {
cancel()
server.Stop()
store.Close()
os.Chdir(originalWd) // Restore original working directory
os.RemoveAll(tmpDir)
}

View File

@@ -68,21 +68,7 @@ func setupTestServerWithStore(t *testing.T) (*Server, *Client, *sqlitestorage.SQ
}
}
originalWd, err := os.Getwd()
if err != nil {
cancel()
server.Stop()
store.Close()
os.RemoveAll(tmpDir)
t.Fatalf("Failed to get working directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
cancel()
server.Stop()
store.Close()
os.RemoveAll(tmpDir)
t.Fatalf("Failed to change directory: %v", err)
}
t.Chdir(tmpDir)
client, err := TryConnect(socketPath)
if err != nil {
@@ -108,7 +94,6 @@ func setupTestServerWithStore(t *testing.T) (*Server, *Client, *sqlitestorage.SQ
cancel()
server.Stop()
store.Close()
os.Chdir(originalWd)
os.RemoveAll(tmpDir)
}

View File

@@ -76,22 +76,8 @@ func setupTestServer(t *testing.T) (*Server, *Client, func()) {
}
}
// Change to tmpDir so client's os.Getwd() finds the test database
originalWd, err := os.Getwd()
if err != nil {
cancel()
server.Stop()
store.Close()
os.RemoveAll(tmpDir)
t.Fatalf("Failed to get working directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
cancel()
server.Stop()
store.Close()
os.RemoveAll(tmpDir)
t.Fatalf("Failed to change directory: %v", err)
}
// Change to tmpDir so client's os.Getwd() finds the test database.
t.Chdir(tmpDir)
client, err := TryConnect(socketPath)
if err != nil {
@@ -118,7 +104,6 @@ func setupTestServer(t *testing.T) (*Server, *Client, func()) {
cancel()
server.Stop()
store.Close()
os.Chdir(originalWd) // Restore original working directory
os.RemoveAll(tmpDir)
}
@@ -445,9 +430,6 @@ func TestConcurrentRequests(t *testing.T) {
}
func TestDatabaseHandshake(t *testing.T) {
// Save original directory and change to a temp directory for test isolation
origDir, _ := os.Getwd()
// Create two separate databases and daemons
tmpDir1, err := os.MkdirTemp("", "bd-test-db1-*")
if err != nil {
@@ -492,9 +474,8 @@ func TestDatabaseHandshake(t *testing.T) {
time.Sleep(100 * time.Millisecond)
// Test 1: Client with correct ExpectedDB should succeed
// Change to tmpDir1 so cwd resolution doesn't find other databases
os.Chdir(tmpDir1)
defer os.Chdir(origDir)
// Change to tmpDir1 so cwd resolution doesn't find other databases.
t.Chdir(tmpDir1)
client1, err := TryConnect(socketPath1)
if err != nil {

View File

@@ -6,7 +6,6 @@ package rpc
import (
"context"
"encoding/json"
"os"
"testing"
"time"
@@ -117,14 +116,7 @@ func TestVersionCompatibility(t *testing.T) {
defer func() { ClientVersion = originalClientVersion }()
// Change to tmpDir so client's os.Getwd() finds the test database
originalWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
defer os.Chdir(originalWd)
t.Chdir(tmpDir)
client, err := TryConnect(socketPath)
if err != nil {
@@ -202,14 +194,7 @@ func TestHealthCheckIncludesVersionInfo(t *testing.T) {
time.Sleep(100 * time.Millisecond)
// Change to tmpDir so client's os.Getwd() finds the test database
originalWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
defer os.Chdir(originalWd)
t.Chdir(tmpDir)
client, err := TryConnect(socketPath)
if err != nil {
@@ -266,14 +251,7 @@ func TestIncompatibleVersionInHealth(t *testing.T) {
time.Sleep(100 * time.Millisecond)
// Change to tmpDir so client's os.Getwd() finds the test database
originalWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
defer os.Chdir(originalWd)
t.Chdir(tmpDir)
client, err := TryConnect(socketPath)
if err != nil {
@@ -386,14 +364,7 @@ func TestPingAndHealthBypassVersionCheck(t *testing.T) {
time.Sleep(100 * time.Millisecond)
// Change to tmpDir so client's os.Getwd() finds the test database
originalWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
defer os.Chdir(originalWd)
t.Chdir(tmpDir)
client, err := TryConnect(socketPath)
if err != nil {
@@ -460,14 +431,7 @@ func TestMetricsOperation(t *testing.T) {
time.Sleep(100 * time.Millisecond)
// Change to tmpDir so client's os.Getwd() finds the test database
originalWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
defer os.Chdir(originalWd)
t.Chdir(tmpDir)
client, err := TryConnect(socketPath)
if err != nil {