t.Skip("scanning file system takes too long")
}
maybeSkip(t)
- where, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- defer func() {
- if err := os.Chdir(where); err != nil {
- t.Fatal(err)
- }
- }()
- if err := os.Chdir(filepath.Join(buildCtx.GOROOT, "src", "text")); err != nil {
- t.Fatal(err)
- }
+ t.Chdir(filepath.Join(buildCtx.GOROOT, "src", "text"))
+
var b strings.Builder
var flagSet flag.FlagSet
- err = do(&b, &flagSet, []string{"./template"})
+ err := do(&b, &flagSet, []string{"./template"})
if err != nil {
t.Errorf("unexpected error %q from ./template", err)
}
tg := testgo(t)
defer tg.cleanup()
- wd, err := os.Getwd()
- tg.check(err)
tg.makeTempdir()
- tg.check(os.Chdir(tg.tempdir))
- defer func() { tg.check(os.Chdir(wd)) }()
+ t.Chdir(tg.tempdir)
tg.check(os.Remove(tg.tempdir))
t.Helper()
// Create a temporary directory and chdir to it.
- prevwd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
cwd = filepath.Join(t.TempDir(), "root")
if err := os.Mkdir(cwd, 0777); err != nil {
t.Fatal(err)
}
- if err := os.Chdir(cwd); err != nil {
- t.Fatal(err)
- }
- t.Cleanup(func() {
- if err := os.Chdir(prevwd); err != nil {
- t.Fatal(err)
- }
- })
+ t.Chdir(cwd)
a := txtar.Parse([]byte(config))
for _, f := range a.Files {
ar.addFile(goodbyeFile.Reset())
ar.a.File().Close()
// Now extract one file. We chdir to the directory of the archive for simplicity.
- pwd, err := os.Getwd()
- if err != nil {
- t.Fatal("os.Getwd: ", err)
- }
- err = os.Chdir(dir)
- if err != nil {
- t.Fatal("os.Chdir: ", err)
- }
- defer func() {
- err := os.Chdir(pwd)
- if err != nil {
- t.Fatal("os.Chdir: ", err)
- }
- }()
+ t.Chdir(dir)
ar = openArchive(name, os.O_RDONLY, []string{goodbyeFile.name})
ar.scan(ar.extractContents)
ar.a.File().Close()
}
func TestWalkDir(t *testing.T) {
- tmpDir := t.TempDir()
-
- origDir, err := os.Getwd()
- if err != nil {
- t.Fatal("finding working dir:", err)
- }
- if err = os.Chdir(tmpDir); err != nil {
- t.Fatal("entering temp dir:", err)
- }
- defer os.Chdir(origDir)
+ t.Chdir(t.TempDir())
fsys := makeTree()
errors := make([]error, 0, 10)
return mark(entry, err, &errors, clear)
}
// Expect no errors.
- err = WalkDir(fsys, ".", markFn)
+ err := WalkDir(fsys, ".", markFn)
if err != nil {
t.Fatalf("no error expected, found: %s", err)
}
if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0777); err != nil {
t.Fatal(err)
}
- chdir(t, tmpDir)
- t.Setenv("PWD", tmpDir)
+ t.Chdir(tmpDir)
t.Logf(". is %#q", tmpDir)
origPath := os.Getenv(pathVar)
return cmd
}
-func chdir(t *testing.T, dir string) {
- t.Helper()
-
- prev, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- if err := os.Chdir(dir); err != nil {
- t.Fatal(err)
- }
- t.Logf("Chdir(%#q)", dir)
-
- t.Cleanup(func() {
- if err := os.Chdir(prev); err != nil {
- // Couldn't chdir back to the original working directory.
- // panic instead of t.Fatal so that we don't run other tests
- // in an unexpected location.
- panic("couldn't restore working directory: " + err.Error())
- }
- })
-}
-
var helperCommandUsed sync.Map
var helperCommands = map[string]func(...string){
// Not parallel: uses Chdir and Setenv.
tmp := t.TempDir()
- chdir(t, tmp)
+ t.Chdir(tmp)
f, err := os.OpenFile("exec_me", os.O_CREATE|os.O_EXCL, 0700)
if err != nil {
t.Setenv("PATH", pathVar)
t.Logf("set PATH=%s", pathVar)
- chdir(t, root)
+ t.Chdir(root)
if !testing.Short() && !(tt.skipCmdExeCheck || errors.Is(tt.wantErr, exec.ErrDot)) {
// Check that cmd.exe, which is our source of ground truth,
t.Setenv("PATH", pathVar)
t.Logf("set PATH=%s", pathVar)
- chdir(t, root)
+ t.Chdir(root)
cmd := exec.Command(tt.arg0, "printpath")
cmd.Dir = filepath.Join(root, tt.dir)
}
func TestStatError(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
path := "no-such-file"
func TestStatSymlinkLoop(t *testing.T) {
testenv.MustHaveSymlink(t)
-
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
err := Symlink("x", "y")
if err != nil {
func TestHardLink(t *testing.T) {
testenv.MustHaveLink(t)
+ t.Chdir(t.TempDir())
- defer chtmpdir(t)()
from, to := "hardlinktestfrom", "hardlinktestto"
file, err := Create(to)
if err != nil {
}
}
-// chtmpdir changes the working directory to a new temporary directory and
-// provides a cleanup function.
-func chtmpdir(t *testing.T) func() {
- oldwd, err := Getwd()
- if err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- d, err := MkdirTemp("", "test")
- if err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- if err := Chdir(d); err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- return func() {
- if err := Chdir(oldwd); err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- RemoveAll(d)
- }
-}
-
func TestSymlink(t *testing.T) {
testenv.MustHaveSymlink(t)
+ t.Chdir(t.TempDir())
- defer chtmpdir(t)()
from, to := "symlinktestfrom", "symlinktestto"
file, err := Create(to)
if err != nil {
func TestLongSymlink(t *testing.T) {
testenv.MustHaveSymlink(t)
+ t.Chdir(t.TempDir())
- defer chtmpdir(t)()
s := "0123456789abcdef"
// Long, but not too long: a common limit is 255.
s = s + s + s + s + s + s + s + s + s + s + s + s + s + s + s
}
func TestRename(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
from, to := "renamefrom", "renameto"
file, err := Create(from)
}
func TestRenameOverwriteDest(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
from, to := "renamefrom", "renameto"
toData := []byte("to")
}
func TestRenameFailed(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
from, to := "renamefrom", "renameto"
err := Rename(from, to)
}
func TestRenameNotExisting(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
from, to := "doesnt-exist", "dest"
Mkdir(to, 0777)
}
func TestRenameToDirFailed(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
from, to := "renamefrom", "renameto"
Mkdir(from, 0777)
for _, test := range tests {
pt.Run(test.name, func(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
if err := test.create(); err != nil {
t.Fatalf("failed to create test file: %s", err)
if err != nil {
t.Fatalf("Getwd: %s", err)
}
- defer Chdir(wd)
+ t.Chdir(".") // Ensure wd is restored after the test.
fd, err := Open(".")
if err != nil {
}
func TestChdirAndGetwd(t *testing.T) {
- fd, err := Open(".")
- if err != nil {
- t.Fatalf("Open .: %s", err)
- }
+ t.Chdir(t.TempDir()) // Ensure wd is restored after the test.
+
// These are chosen carefully not to be symlinks on a Mac
// (unlike, say, /var, /etc), except /tmp, which we handle below.
dirs := []string{"/", "/usr/bin", "/tmp"}
dirs = nil
for _, dir := range []string{t.TempDir(), t.TempDir()} {
// Expand symlinks so path equality tests work.
- dir, err = filepath.EvalSymlinks(dir)
+ dir, err := filepath.EvalSymlinks(dir)
if err != nil {
t.Fatalf("EvalSymlinks: %v", err)
}
dirs = append(dirs, dir)
}
}
- oldwd := Getenv("PWD")
for mode := 0; mode < 2; mode++ {
for _, d := range dirs {
+ var err error
if mode == 0 {
err = Chdir(d)
} else {
Setenv("PWD", "/tmp")
}
pwd, err1 := Getwd()
- Setenv("PWD", oldwd)
- err2 := fd.Chdir()
- if err2 != nil {
- // We changed the current directory and cannot go back.
- // Don't let the tests continue; they'll scribble
- // all over some other directory.
- fmt.Fprintf(Stderr, "fchdir back to dot failed: %s\n", err2)
- Exit(1)
- }
if err != nil {
- fd.Close()
t.Fatalf("Chdir %s: %s", d, err)
}
if err1 != nil {
- fd.Close()
t.Fatalf("Getwd in %s: %s", d, err1)
}
if !equal(pwd, d) {
- fd.Close()
t.Fatalf("Getwd returned %q want %q", pwd, d)
}
}
}
- fd.Close()
}
// Test that Chdir+Getwd is program-wide.
done := make(chan struct{})
d := t.TempDir()
- oldwd, err := Getwd()
- if err != nil {
- t.Fatalf("Getwd: %v", err)
- }
- defer func() {
- if err := Chdir(oldwd); err != nil {
- // It's not safe to continue with tests if we can't get back to
- // the original working directory.
- panic(err)
- }
- }()
+ t.Chdir(d)
// Note the deferred Wait must be called after the deferred close(done),
// to ensure the N goroutines have been released even if the main goroutine
}
}(i)
}
+ var err error
if err = Chdir(d); err != nil {
t.Fatalf("Chdir: %v", err)
}
// Verify that WriteAt doesn't work in append mode.
func TestWriteAtInAppendMode(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
f, err := OpenFile("write_at_in_append_mode.txt", O_APPEND|O_CREATE, 0666)
if err != nil {
t.Fatalf("OpenFile: %v", err)
}
func TestAppend(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
const f = "append.txt"
s := writeFile(t, f, O_CREATE|O_TRUNC|O_RDWR, "new")
if s != "new" {
}
func TestSameFile(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
fa, err := Create("a")
if err != nil {
t.Fatalf("Create(a): %v", err)
//
// Regression test for go.dev/issue/60181
func TestIssue60181(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
want := "hello gopher"
// For TestRawConnReadWrite.
type syscallDescriptor = syscall.Handle
-// 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 TestSameWindowsFile(t *testing.T) {
- temp := t.TempDir()
- chdir(t, temp)
+ t.Chdir(t.TempDir())
f, err := os.Create("a")
if err != nil {
func testDirLinks(t *testing.T, tests []dirLinkTest) {
tmpdir := t.TempDir()
- chdir(t, tmpdir)
+ t.Chdir(tmpdir)
dir := filepath.Join(tmpdir, "dir")
err := os.Mkdir(dir, 0777)
const _NERR_ServerNotStarted = syscall.Errno(2114)
dir := t.TempDir()
- chdir(t, dir)
+ t.Chdir(dir)
pid := os.Getpid()
shareName := fmt.Sprintf("GoSymbolicLinkTestShare%d", pid)
t.Skip("skipping: WSL not detected")
}
- temp := t.TempDir()
- chdir(t, temp)
+ t.Chdir(t.TempDir())
const target = "target"
const link = "link"
}
func TestStatDir(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
f, err := os.Open(".")
if err != nil {
func TestOpenVolumeName(t *testing.T) {
tmpdir := t.TempDir()
- chdir(t, tmpdir)
+ t.Chdir(tmpdir)
want := []string{"file1", "file2", "file3", "gopher.txt"}
slices.Sort(want)
if err != nil {
t.Fatal(err)
}
- defer func() {
- if err := os.Chdir(oldwd); err != nil {
- t.Fatal(err)
- }
- }()
- if err := os.Chdir(temp); err != nil {
- t.Fatal(err)
- }
+ t.Chdir(temp)
t.Logf("Chdir(%#q)", temp)
wdRelDir := filepath.VolumeName(temp) + `dir\sub` // no backslash after volume.
} else {
want = relTarget
}
- chdir(t, tmpdir)
+ t.Chdir(tmpdir)
link = filepath.Base(link)
target = relTarget
} else {
// Test that RemoveAll doesn't hang with long relative paths.
// See go.dev/issue/36375.
tmp := t.TempDir()
- chdir(t, tmp)
+ t.Chdir(tmp)
dir := filepath.Join(tmp, "foo", "bar", strings.Repeat("a", 150), strings.Repeat("b", 150))
err := os.MkdirAll(dir, 0755)
if err != nil {
}
func TestLongPathRel(t *testing.T) {
- chdir(t, t.TempDir())
+ t.Chdir(t.TempDir())
target := strings.Repeat("b\\", 300)
testLongPathAbs(t, target)
}
// test relative paths
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(tmpDir)
- if err != nil {
- t.Fatal(err)
- }
- defer func() {
- err := os.Chdir(wd)
- if err != nil {
- t.Fatal(err)
- }
- }()
+ t.Chdir(tmpDir)
for _, test := range tests {
err := test.globRel("")
if err != nil {
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.Fatalf("chtmpdir: %v", err)
- }
- d, err := os.MkdirTemp("", "test")
- if err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- if err := os.Chdir(d); err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- return func() {
- if err := os.Chdir(oldwd); err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- os.RemoveAll(d)
- }
-}
-
// tempDirCanonical returns a temporary directory for the test to use, ensuring
// that the returned path does not contain symlinks.
func tempDirCanonical(t *testing.T) string {
}
func testWalk(t *testing.T, walk func(string, fs.WalkDirFunc) error, errVisit int) {
- if runtime.GOOS == "ios" {
- restore := chtmpdir(t)
- defer restore()
- }
-
- tmpDir := t.TempDir()
-
- origDir, err := os.Getwd()
- if err != nil {
- t.Fatal("finding working dir:", err)
- }
- if err = os.Chdir(tmpDir); err != nil {
- t.Fatal("entering temp dir:", err)
- }
- defer os.Chdir(origDir)
+ t.Chdir(t.TempDir())
makeTree(t)
errors := make([]error, 0, 10)
return mark(d, err, &errors, clear)
}
// Expect no errors.
- err = walk(tree.name, markFn)
+ err := walk(tree.name, markFn)
if err != nil {
t.Fatalf("no error expected, found: %s", err)
}
}
func testEvalSymlinksAfterChdir(t *testing.T, wd, path, want string) {
- cwd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- defer func() {
- err := os.Chdir(cwd)
- if err != nil {
- t.Fatal(err)
- }
- }()
-
- err = os.Chdir(wd)
- if err != nil {
- t.Fatal(err)
- }
-
+ t.Chdir(wd)
have, err := filepath.EvalSymlinks(path)
if err != nil {
t.Errorf("EvalSymlinks(%q) in %q directory error: %v", path, wd, err)
func TestEvalSymlinksIsNotExist(t *testing.T) {
testenv.MustHaveSymlink(t)
-
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
_, err := filepath.EvalSymlinks("notexist")
if !os.IsNotExist(err) {
// Issue 57905.
func TestRelativeSymlinkToAbsolute(t *testing.T) {
testenv.MustHaveSymlink(t)
- // Not parallel: uses os.Chdir.
+ // Not parallel: uses t.Chdir.
tmpDir := t.TempDir()
- chdir(t, tmpDir)
+ t.Chdir(tmpDir)
// Create "link" in the current working directory as a symlink to an arbitrary
// absolute path. On macOS, this path is likely to begin with a symlink
func TestAbs(t *testing.T) {
root := t.TempDir()
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal("getwd failed: ", err)
- }
- err = os.Chdir(root)
- if err != nil {
- t.Fatal("chdir failed: ", err)
- }
- defer os.Chdir(wd)
+ t.Chdir(root)
for _, dir := range absTestDirs {
- err = os.Mkdir(dir, 0777)
+ err := os.Mkdir(dir, 0777)
if err != nil {
t.Fatal("Mkdir failed: ", err)
}
tests = append(slices.Clip(tests), extra...)
}
- err = os.Chdir(absTestDirs[0])
+ err := os.Chdir(absTestDirs[0])
if err != nil {
t.Fatal("chdir failed: ", err)
}
// a valid path, so it can't be used with os.Stat.
func TestAbsEmptyString(t *testing.T) {
root := t.TempDir()
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal("getwd failed: ", err)
- }
- err = os.Chdir(root)
- if err != nil {
- t.Fatal("chdir failed: ", err)
- }
- defer os.Chdir(wd)
+ t.Chdir(root)
info, err := os.Stat(root)
if err != nil {
func testWalkSymlink(t *testing.T, mklink func(target, link string) error) {
tmpdir := t.TempDir()
+ t.Chdir(tmpdir)
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
-
- err = os.Chdir(tmpdir)
- if err != nil {
- t.Fatal(err)
- }
-
- err = mklink(tmpdir, "link")
+ err := mklink(tmpdir, "link")
if err != nil {
t.Fatal(err)
}
// Issue 30520 part 2.
func TestEvalSymlinksAboveRootChdir(t *testing.T) {
testenv.MustHaveSymlink(t)
-
- tmpDir, err := os.MkdirTemp("", "TestEvalSymlinksAboveRootChdir")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
- chdir(t, tmpDir)
+ t.Chdir(t.TempDir())
subdir := filepath.Join("a", "b")
if err := os.MkdirAll(subdir, 0777); err != nil {
}
func TestEscaping(t *testing.T) {
- dir1 := t.TempDir()
- dir2 := t.TempDir()
- chdir(t, dir1)
+ dir := t.TempDir()
+ t.Chdir(t.TempDir())
for _, p := range []string{
- filepath.Join(dir2, "x"),
+ filepath.Join(dir, "x"),
} {
if !filepath.IsLocal(p) {
continue
if err != nil {
f.Close()
}
- ents, err := os.ReadDir(dir2)
+ ents, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
- defer func() {
- err := os.Chdir(cwd)
- if err != nil {
- t.Fatal(err)
- }
- }()
+ t.Chdir(".") // Ensure cwd is restored after the test.
tmpVol := filepath.VolumeName(ctmp)
if len(tmpVol) != 2 {
tmpdir := t.TempDir()
- dir0, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(dir0)
-
const src = `
#include <stdint.h>
#include <windows.h>
}
`
srcname := "nojack.c"
- err = os.WriteFile(filepath.Join(tmpdir, srcname), []byte(src), 0)
+ err := os.WriteFile(filepath.Join(tmpdir, srcname), []byte(src), 0)
if err != nil {
t.Fatal(err)
}
// ("nojack.dll") Think of this as the user double-clicking an
// installer from their Downloads directory where a browser
// silently downloaded some malicious DLLs.
- os.Chdir(tmpdir)
+ t.Chdir(tmpdir)
// First before we can load a DLL from the current directory,
// loading it only as "nojack.dll", without an absolute path.
"unsafe"
)
-// chtmpdir changes the working directory to a new temporary directory and
-// provides a cleanup function. Used when PWD is read-only.
-func chtmpdir(t *testing.T) func() {
- oldwd, err := os.Getwd()
- if err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- d, err := os.MkdirTemp("", "test")
- if err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- if err := os.Chdir(d); err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- return func() {
- if err := os.Chdir(oldwd); err != nil {
- t.Fatalf("chtmpdir: %v", err)
- }
- os.RemoveAll(d)
- }
-}
-
func touch(t *testing.T, name string) {
f, err := os.Create(name)
if err != nil {
)
func TestFaccessat(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
touch(t, "file1")
err := syscall.Faccessat(_AT_FDCWD, "file1", _R_OK, 0)
}
func TestFchmodat(t *testing.T) {
- defer chtmpdir(t)()
+ t.Chdir(t.TempDir())
touch(t, "file1")
os.Symlink("file1", "symlink1")
func TestGetwd_DoesNotPanicWhenPathIsLong(t *testing.T) {
// Regression test for https://github.com/golang/go/issues/60051.
+ tmp := t.TempDir()
+ t.Chdir(tmp)
// The length of a filename is also limited, so we can't reproduce the
// crash by creating a single directory with a very long name; we need two
// layers.
a200 := strings.Repeat("a", 200)
- dirname := filepath.Join(t.TempDir(), a200, a200)
+ dirname := filepath.Join(tmp, a200, a200)
err := os.MkdirAll(dirname, 0o700)
if err != nil {
if err != nil {
t.Skipf("Chdir failed: %v", err)
}
- // Change out of the temporary directory so that we don't inhibit its
- // removal during test cleanup.
- defer os.Chdir(`\`)
syscall.Getwd()
}