mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
path/filepath: replace os.MkdirTemp with T.TempDir
Add the tempDirCanonical function, for tests that need a temporary directory that does not contain symlinks. Updates #45402 Change-Id: I3d08ef32ef911331544acce3d7d013b4c3382960 Reviewed-on: https://go-review.googlesource.com/c/go/+/308011 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
parent
6382ec1aba
commit
11f159456b
3 changed files with 28 additions and 88 deletions
|
|
@ -181,12 +181,7 @@ var globSymlinkTests = []struct {
|
||||||
func TestGlobSymlink(t *testing.T) {
|
func TestGlobSymlink(t *testing.T) {
|
||||||
testenv.MustHaveSymlink(t)
|
testenv.MustHaveSymlink(t)
|
||||||
|
|
||||||
tmpDir, err := os.MkdirTemp("", "globsymlink")
|
tmpDir := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatal("creating temp dir:", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpDir)
|
|
||||||
|
|
||||||
for _, tt := range globSymlinkTests {
|
for _, tt := range globSymlinkTests {
|
||||||
path := Join(tmpDir, tt.path)
|
path := Join(tmpDir, tt.path)
|
||||||
dest := Join(tmpDir, tt.dest)
|
dest := Join(tmpDir, tt.dest)
|
||||||
|
|
@ -267,18 +262,7 @@ func TestWindowsGlob(t *testing.T) {
|
||||||
t.Skipf("skipping windows specific test")
|
t.Skipf("skipping windows specific test")
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpDir, err := os.MkdirTemp("", "TestWindowsGlob")
|
tmpDir := tempDirCanonical(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpDir)
|
|
||||||
|
|
||||||
// /tmp may itself be a symlink
|
|
||||||
tmpDir, err = EvalSymlinks(tmpDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("eval symlink for tmp dir:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(tmpDir) < 3 {
|
if len(tmpDir) < 3 {
|
||||||
t.Fatalf("tmpDir path %q is too short", tmpDir)
|
t.Fatalf("tmpDir path %q is too short", tmpDir)
|
||||||
}
|
}
|
||||||
|
|
@ -323,15 +307,13 @@ func TestWindowsGlob(t *testing.T) {
|
||||||
// test absolute paths
|
// test absolute paths
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
var p string
|
var p string
|
||||||
err = test.globAbs(tmpDir, tmpDir)
|
if err := test.globAbs(tmpDir, tmpDir); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
// test C:\*Documents and Settings\...
|
// test C:\*Documents and Settings\...
|
||||||
p = tmpDir
|
p = tmpDir
|
||||||
p = strings.Replace(p, `:\`, `:\*`, 1)
|
p = strings.Replace(p, `:\`, `:\*`, 1)
|
||||||
err = test.globAbs(tmpDir, p)
|
if err := test.globAbs(tmpDir, p); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
// test C:\Documents and Settings*\...
|
// test C:\Documents and Settings*\...
|
||||||
|
|
@ -339,8 +321,7 @@ func TestWindowsGlob(t *testing.T) {
|
||||||
p = strings.Replace(p, `:\`, `:`, 1)
|
p = strings.Replace(p, `:\`, `:`, 1)
|
||||||
p = strings.Replace(p, `\`, `*\`, 1)
|
p = strings.Replace(p, `\`, `*\`, 1)
|
||||||
p = strings.Replace(p, `:`, `:\`, 1)
|
p = strings.Replace(p, `:`, `:\`, 1)
|
||||||
err = test.globAbs(tmpDir, p)
|
if err := test.globAbs(tmpDir, p); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -449,6 +449,19 @@ func chtmpdir(t *testing.T) (restore func()) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
dir := t.TempDir()
|
||||||
|
|
||||||
|
cdir, err := filepath.EvalSymlinks(dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("tempDirCanonical: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cdir
|
||||||
|
}
|
||||||
|
|
||||||
func TestWalk(t *testing.T) {
|
func TestWalk(t *testing.T) {
|
||||||
walk := func(root string, fn fs.WalkDirFunc) error {
|
walk := func(root string, fn fs.WalkDirFunc) error {
|
||||||
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
|
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
|
||||||
|
|
|
||||||
|
|
@ -37,12 +37,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
|
||||||
perm fs.FileMode = 0700
|
perm fs.FileMode = 0700
|
||||||
)
|
)
|
||||||
|
|
||||||
tmp, err := os.MkdirTemp("", "testWinSplitListTestIsValid")
|
tmp := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("TempDir failed: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmp)
|
|
||||||
|
|
||||||
for i, d := range tt.result {
|
for i, d := range tt.result {
|
||||||
if d == "" {
|
if d == "" {
|
||||||
continue
|
continue
|
||||||
|
|
@ -57,12 +52,12 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
|
||||||
t.Errorf("%d,%d: %#q already exists", ti, i, d)
|
t.Errorf("%d,%d: %#q already exists", ti, i, d)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = os.MkdirAll(dd, perm); err != nil {
|
if err := os.MkdirAll(dd, perm); err != nil {
|
||||||
t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err)
|
t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fn, data := filepath.Join(dd, cmdfile), []byte("@echo "+d+"\r\n")
|
fn, data := filepath.Join(dd, cmdfile), []byte("@echo "+d+"\r\n")
|
||||||
if err = os.WriteFile(fn, data, perm); err != nil {
|
if err := os.WriteFile(fn, data, perm); err != nil {
|
||||||
t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err)
|
t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -103,18 +98,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
|
||||||
func TestWindowsEvalSymlinks(t *testing.T) {
|
func TestWindowsEvalSymlinks(t *testing.T) {
|
||||||
testenv.MustHaveSymlink(t)
|
testenv.MustHaveSymlink(t)
|
||||||
|
|
||||||
tmpDir, err := os.MkdirTemp("", "TestWindowsEvalSymlinks")
|
tmpDir := tempDirCanonical(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpDir)
|
|
||||||
|
|
||||||
// /tmp may itself be a symlink! Avoid the confusion, although
|
|
||||||
// it means trusting the thing we're testing.
|
|
||||||
tmpDir, err = filepath.EvalSymlinks(tmpDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(tmpDir) < 3 {
|
if len(tmpDir) < 3 {
|
||||||
t.Fatalf("tmpDir path %q is too short", tmpDir)
|
t.Fatalf("tmpDir path %q is too short", tmpDir)
|
||||||
|
|
@ -161,18 +145,7 @@ func TestWindowsEvalSymlinks(t *testing.T) {
|
||||||
// TestEvalSymlinksCanonicalNames verify that EvalSymlinks
|
// TestEvalSymlinksCanonicalNames verify that EvalSymlinks
|
||||||
// returns "canonical" path names on windows.
|
// returns "canonical" path names on windows.
|
||||||
func TestEvalSymlinksCanonicalNames(t *testing.T) {
|
func TestEvalSymlinksCanonicalNames(t *testing.T) {
|
||||||
tmp, err := os.MkdirTemp("", "evalsymlinkcanonical")
|
ctmp := tempDirCanonical(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal("creating temp dir:", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmp)
|
|
||||||
|
|
||||||
// os.MkdirTemp might return "non-canonical" name.
|
|
||||||
cTmpName, err := filepath.EvalSymlinks(tmp)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("EvalSymlinks(%q) error: %v", tmp, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dirs := []string{
|
dirs := []string{
|
||||||
"test",
|
"test",
|
||||||
"test/dir",
|
"test/dir",
|
||||||
|
|
@ -181,7 +154,7 @@ func TestEvalSymlinksCanonicalNames(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range dirs {
|
for _, d := range dirs {
|
||||||
dir := filepath.Join(cTmpName, d)
|
dir := filepath.Join(ctmp, d)
|
||||||
err := os.Mkdir(dir, 0755)
|
err := os.Mkdir(dir, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -417,25 +390,8 @@ func TestToNorm(t *testing.T) {
|
||||||
{".", `\\localhost\c$`, `\\localhost\c$`},
|
{".", `\\localhost\c$`, `\\localhost\c$`},
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp, err := os.MkdirTemp("", "testToNorm")
|
ctmp := tempDirCanonical(t)
|
||||||
if err != nil {
|
if err := os.MkdirAll(strings.ReplaceAll(testPath, "{{tmp}}", ctmp), 0777); err != nil {
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
err := os.RemoveAll(tmp)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// os.MkdirTemp might return "non-canonical" name.
|
|
||||||
ctmp, err := filepath.EvalSymlinks(tmp)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.MkdirAll(strings.ReplaceAll(testPath, "{{tmp}}", ctmp), 0777)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -526,20 +482,10 @@ func TestNTNamespaceSymlink(t *testing.T) {
|
||||||
t.Skip("skipping test because mklink command does not support junctions")
|
t.Skip("skipping test because mklink command does not support junctions")
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpdir, err := os.MkdirTemp("", "TestNTNamespaceSymlink")
|
tmpdir := tempDirCanonical(t)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
// Make sure tmpdir is not a symlink, otherwise tests will fail.
|
|
||||||
tmpdir, err = filepath.EvalSymlinks(tmpdir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
vol := filepath.VolumeName(tmpdir)
|
vol := filepath.VolumeName(tmpdir)
|
||||||
output, err = exec.Command("cmd", "/c", "mountvol", vol, "/L").CombinedOutput()
|
output, err := exec.Command("cmd", "/c", "mountvol", vol, "/L").CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to run mountvol %v /L: %v %q", vol, err, output)
|
t.Fatalf("failed to run mountvol %v /L: %v %q", vol, err, output)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue