diff options
| author | Hiroshi Ioka <hirochachacha@gmail.com> | 2016-10-24 08:52:04 +0900 |
|---|---|---|
| committer | Alex Brainman <alex.brainman@gmail.com> | 2016-10-25 01:57:05 +0000 |
| commit | 643c6b3c74409871d7f96cbad145dabdefbbc8c1 (patch) | |
| tree | f28c162c4cbe5b2156b5ba2d689c99fc2664014a /src/path/filepath/path_windows_test.go | |
| parent | 02c1d8a1589b47f12deaaa63d4a6a084640f4389 (diff) | |
| download | go-643c6b3c74409871d7f96cbad145dabdefbbc8c1.tar.xz | |
path/filepath: make TestToNorm robust
The old code leaves garbages in a temporary directory because it
cannot remove the current working directory on windows.
The new code changes the directory before calling os.Remove.
Furthermore, the old code assumes that ioutil.TempDir (os.TempDir)
doesn't return a relative path nor an UNC path.
If it isn't the case, the new code calls t.Fatal earlier for preventing
ambiguous errors.
Finally, the old code reassigns the variable which is used by the defer
function. It could cause unexpected results, so avoid that.
Change-Id: I5fc3902059ecaf18dc1341ecc4979d1206034cd7
Reviewed-on: https://go-review.googlesource.com/31790
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Diffstat (limited to 'src/path/filepath/path_windows_test.go')
| -rw-r--r-- | src/path/filepath/path_windows_test.go | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/src/path/filepath/path_windows_test.go b/src/path/filepath/path_windows_test.go index 6393629ccc..73e74be8d3 100644 --- a/src/path/filepath/path_windows_test.go +++ b/src/path/filepath/path_windows_test.go @@ -352,38 +352,47 @@ func TestToNorm(t *testing.T) { {`{{tmp}}\test`, `FOO\BAR`, `foo\bar`}, } - cwd, err := os.Getwd() + tmp, err := ioutil.TempDir("", "testToNorm") if err != nil { t.Fatal(err) } defer func() { - err := os.Chdir(cwd) + err := os.RemoveAll(tmp) if err != nil { t.Fatal(err) } }() - tmp, err := ioutil.TempDir("", "testToNorm") + // ioutil.TempDir might return "non-canonical" name. + ctmp, err := filepath.EvalSymlinks(tmp) if err != nil { t.Fatal(err) } - defer os.RemoveAll(tmp) - // ioutil.TempDir might return "non-canonical" name. - tmp, err = filepath.EvalSymlinks(tmp) + err = os.MkdirAll(strings.Replace(testPath, "{{tmp}}", ctmp, -1), 0777) if err != nil { t.Fatal(err) } - err = os.MkdirAll(strings.Replace(testPath, "{{tmp}}", tmp, -1), 0777) + cwd, err := os.Getwd() if err != nil { t.Fatal(err) } + defer func() { + err := os.Chdir(cwd) + if err != nil { + t.Fatal(err) + } + }() + + tmpVol := filepath.VolumeName(ctmp) + if len(tmpVol) != 2 { + t.Fatalf("unexpected temp volume name %q", tmpVol) + } - tmpVol := filepath.VolumeName(tmp) - tmpNoVol := tmp[len(tmpVol):] + tmpNoVol := ctmp[len(tmpVol):] - replacer := strings.NewReplacer("{{tmp}}", tmp, "{{tmpvol}}", tmpVol, "{{tmpnovol}}", tmpNoVol) + replacer := strings.NewReplacer("{{tmp}}", ctmp, "{{tmpvol}}", tmpVol, "{{tmpnovol}}", tmpNoVol) for _, test := range testsDir { wd := replacer.Replace(test.wd) |
