t.Fatal(err)
}
defer os.RemoveAll(temp)
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(temp)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
+ chdir(t, temp)
f, err := os.Create("a")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
-
- oldwd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(tmpdir)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(oldwd)
+ chdir(t, tmpdir)
dir := filepath.Join(tmpdir, "dir")
err = os.Mkdir(dir, 0777)
}
defer os.RemoveAll(dir)
- oldwd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(dir)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(oldwd)
+ chdir(t, dir)
shareName := "GoSymbolicLinkTestShare" // hope no conflictions
sharePath := filepath.Join(dir, shareName)
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(tmpdir)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
+ chdir(t, tmpdir)
want := []string{"file1", "file2", "file3", "gopher.txt"}
sort.Strings(want)
if err != nil {
t.Fatal(err)
}
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(tmpdir)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
+ chdir(t, tmpdir)
vol := filepath.VolumeName(tmpdir)
output, err := osexec.Command("cmd", "/c", "mountvol", vol, "/L").CombinedOutput()
}
}
+// chdir changes the current working directory to the named directory,
+// and then restore the original working directory at the end of the test.
+func chdir(t *testing.T, dir string) {
+ olddir, err := os.Getwd()
+ if err != nil {
+ t.Fatalf("chdir: %v", err)
+ }
+ if err := os.Chdir(dir); err != nil {
+ t.Fatalf("chdir %s: %v", dir, err)
+ }
+
+ t.Cleanup(func() {
+ if err := os.Chdir(olddir); err != nil {
+ t.Errorf("chdir to original working directory %s: %v", olddir, err)
+ os.Exit(1)
+ }
+ })
+}
+
func TestRemoveAllLongPath(t *testing.T) {
switch runtime.GOOS {
case "aix", "darwin", "ios", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "illumos", "solaris":
t.Skip("skipping for not implemented platforms")
}
- prevDir, err := Getwd()
- if err != nil {
- t.Fatalf("Could not get wd: %s", err)
- }
-
startPath, err := os.MkdirTemp("", "TestRemoveAllLongPath-")
if err != nil {
t.Fatalf("Could not create TempDir: %s", err)
}
defer RemoveAll(startPath)
-
- err = Chdir(startPath)
- if err != nil {
- t.Fatalf("Could not chdir %s: %s", startPath, err)
- }
+ chdir(t, startPath)
// Removing paths with over 4096 chars commonly fails
for i := 0; i < 41; i++ {
}
}
- err = Chdir(prevDir)
- if err != nil {
- t.Fatalf("Could not chdir %s: %s", prevDir, err)
- }
-
err = RemoveAll(startPath)
if err != nil {
t.Errorf("RemoveAll could not remove long file path %s: %s", startPath, err)
return nil
}
+// chdir changes the current working directory to the named directory,
+// and then restore the original working directory at the end of the test.
+func chdir(t *testing.T, dir string) {
+ olddir, err := os.Getwd()
+ if err != nil {
+ t.Fatalf("getwd %s: %v", dir, err)
+ }
+ if err := os.Chdir(dir); err != nil {
+ t.Fatalf("chdir %s: %v", dir, err)
+ }
+
+ t.Cleanup(func() {
+ if err := os.Chdir(olddir); err != nil {
+ t.Errorf("restore original working directory %s: %v", olddir, err)
+ os.Exit(1)
+ }
+ })
+}
+
func chtmpdir(t *testing.T) (restore func()) {
oldwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
-
- if err := os.Chdir(tmpDir); err != nil {
- t.Fatal(err)
- }
+ chdir(t, tmpDir)
subdir := filepath.Join("a", "b")
if err := os.MkdirAll(subdir, 0777); err != nil {