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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user