aboutsummaryrefslogtreecommitdiff
path: root/src/path/filepath/path_windows_test.go
diff options
context:
space:
mode:
authorHiroshi Ioka <hirochachacha@gmail.com>2016-03-17 17:24:19 +0900
committerAlex Brainman <alex.brainman@gmail.com>2016-04-05 00:39:25 +0000
commitc4dda7e5a830e86b597d34483fd7787723b34f2f (patch)
tree4eaa52b6498fef232876525319f58b3031ede2bf /src/path/filepath/path_windows_test.go
parent9db7ef561462606085759a2f8a93b7224fdfd2fc (diff)
downloadgo-c4dda7e5a830e86b597d34483fd7787723b34f2f.tar.xz
path/filepath: normalize output of EvalSymlinks on windows
Current implementation uses GetShortPathName and GetLongPathName to get a normalized path. That approach sometimes fails because user can disable short path name anytime. This CL provides an alternative approach suggested by MSDN. https://msdn.microsoft.com/en-us/library/windows/desktop/aa364989(v=vs.85).aspx Fixes #13980 Change-Id: Icf4afe4c9c4b507fc110c1483bf8db2c3f606b0a Reviewed-on: https://go-review.googlesource.com/20860 Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Run-TryBot: Alex Brainman <alex.brainman@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/path/filepath/path_windows_test.go')
-rw-r--r--src/path/filepath/path_windows_test.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/path/filepath/path_windows_test.go b/src/path/filepath/path_windows_test.go
index f086035e5f..b47cdfdb96 100644
--- a/src/path/filepath/path_windows_test.go
+++ b/src/path/filepath/path_windows_test.go
@@ -279,3 +279,59 @@ func TestEvalSymlinksCanonicalNamesWith8dot3Disabled(t *testing.T) {
}
TestEvalSymlinksCanonicalNames(t)
}
+
+func TestToNorm(t *testing.T) {
+ stubBase := func(path string) (string, error) {
+ vol := filepath.VolumeName(path)
+ path = path[len(vol):]
+
+ if strings.Contains(path, "/") {
+ return "", fmt.Errorf("invalid path is given to base: %s", vol+path)
+ }
+
+ if path == "" || path == "." || path == `\` {
+ return "", fmt.Errorf("invalid path is given to base: %s", vol+path)
+ }
+
+ i := strings.LastIndexByte(path, filepath.Separator)
+ if i == len(path)-1 { // trailing '\' is invalid
+ return "", fmt.Errorf("invalid path is given to base: %s", vol+path)
+ }
+ if i == -1 {
+ return strings.ToUpper(path), nil
+ }
+
+ return strings.ToUpper(path[i+1:]), nil
+ }
+
+ // On this test, toNorm should be same as string.ToUpper(filepath.Clean(path)) except empty string.
+ tests := []struct {
+ arg string
+ want string
+ }{
+ {"", ""},
+ {".", "."},
+ {"./foo/bar", `FOO\BAR`},
+ {"/", `\`},
+ {"/foo/bar", `\FOO\BAR`},
+ {"/foo/bar/baz/qux", `\FOO\BAR\BAZ\QUX`},
+ {"foo/bar", `FOO\BAR`},
+ {"C:/foo/bar", `C:\FOO\BAR`},
+ {"C:foo/bar", `C:FOO\BAR`},
+ {"c:/foo/bar", `C:\FOO\BAR`},
+ {"C:/foo/bar", `C:\FOO\BAR`},
+ {"C:/foo/bar/", `C:\FOO\BAR`},
+ {`C:\foo\bar`, `C:\FOO\BAR`},
+ {`C:\foo/bar\`, `C:\FOO\BAR`},
+ {"C:/ふー/バー", `C:\ふー\バー`},
+ }
+
+ for _, test := range tests {
+ got, err := filepath.ToNorm(test.arg, stubBase)
+ if err != nil {
+ t.Errorf("unexpected toNorm error, arg: %s, err: %v\n", test.arg, err)
+ } else if got != test.want {
+ t.Errorf("toNorm error, arg: %s, want: %s, got: %s\n", test.arg, test.want, got)
+ }
+ }
+}