-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Open
Labels
NeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.TestingAn issue that has been verified to require only test changes, not just a test failure.An issue that has been verified to require only test changes, not just a test failure.help wanted
Milestone
Description
What version of Go are you using (go version
)?
$ go version go version go1.16.2 linux/amd64
I have noted that the tests that need to change the current working directory use the following pattern:
- call
os.Getwd
to get the current working directory - some code
- call
os.Chdir
to change the current working directory - some code
- call
os.Chdir
to restore the original working directory
An example is:
https://github.com/golang/go/blob/master/src/os/removeall_test.go#L159
The code should probably use defer
, using a support function like:
// chdir changes the current working directory to the named directory and
// returns a function that, when called, restores the original working
// directory.
func chdir(t *testing.T, dir string) func() {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("chdir %s: %v", dir, err)
}
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
return func() {
if err := os.Chdir(wd); err != nil {
t.Fatalf("restoring working directory: %v", err)
}
}
}
The new pattern is:
- call
defer chdir(dir)()
- some code
This is more readable and ensures that the working directory is restored in case of test failures.
dmitshur, lucastheisen, meetwithabhishek and kucukaslan
Metadata
Metadata
Assignees
Labels
NeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.TestingAn issue that has been verified to require only test changes, not just a test failure.An issue that has been verified to require only test changes, not just a test failure.help wanted