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