1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 type PathTest struct {
21 var cleantests = []PathTest{
24 {"abc/def", "abc/def"},
29 {"../../abc", "../../abc"},
33 // Empty is current dir
36 // Remove trailing slash
38 {"abc/def/", "abc/def"},
45 // Remove doubled slash
46 {"abc//def//ghi", "abc/def/ghi"},
53 {"abc/./def", "abc/def"},
54 {"/./abc/def", "/abc/def"},
58 {"abc/def/ghi/../jkl", "abc/def/jkl"},
59 {"abc/def/../ghi/../jkl", "abc/jkl"},
60 {"abc/def/..", "abc"},
61 {"abc/def/../..", "."},
62 {"/abc/def/../..", "/"},
63 {"abc/def/../../..", ".."},
64 {"/abc/def/../../..", "/"},
65 {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
69 {"abc/./../def", "def"},
70 {"abc//./../def", "def"},
71 {"abc/../../././../def", "../../def"},
74 var wincleantests = []PathTest{
78 {`c:abc\..\..\.\.\..\def`, `c:..\..\def`},
79 {`c:\abc\def\..\..`, `c:\`},
80 {`c:\..\abc`, `c:\abc`},
81 {`c:..\abc`, `c:..\abc`},
85 {`\\i\..\i\c$`, `\i\c$`},
86 {`\\i\..\I\c$`, `\I\c$`},
87 {`\\host\share\foo\..\bar`, `\\host\share\bar`},
88 {`//host/share/foo/../baz`, `\\host\share\baz`},
89 {`\\a\b\..\c`, `\\a\b\c`},
93 func TestClean(t *testing.T) {
95 if runtime.GOOS == "windows" {
96 for i := range tests {
97 tests[i].result = filepath.FromSlash(tests[i].result)
99 tests = append(tests, wincleantests...)
101 for _, test := range tests {
102 if s := filepath.Clean(test.path); s != test.result {
103 t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
105 if s := filepath.Clean(test.result); s != test.result {
106 t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
110 var ms runtime.MemStats
111 runtime.ReadMemStats(&ms)
112 allocs := -ms.Mallocs
114 for i := 0; i < rounds; i++ {
115 for _, test := range tests {
116 filepath.Clean(test.result)
119 runtime.ReadMemStats(&ms)
121 /* Fails with gccgo, which has no escape analysis.
122 if allocs >= rounds {
123 t.Errorf("Clean cleaned paths: %d allocations per test round, want zero", allocs/rounds)
128 const sep = filepath.Separator
130 var slashtests = []PathTest{
133 {"/a/b", string([]byte{sep, 'a', sep, 'b'})},
134 {"a//b", string([]byte{'a', sep, sep, 'b'})},
137 func TestFromAndToSlash(t *testing.T) {
138 for _, test := range slashtests {
139 if s := filepath.FromSlash(test.path); s != test.result {
140 t.Errorf("FromSlash(%q) = %q, want %q", test.path, s, test.result)
142 if s := filepath.ToSlash(test.result); s != test.path {
143 t.Errorf("ToSlash(%q) = %q, want %q", test.result, s, test.path)
148 type SplitListTest struct {
153 const lsep = filepath.ListSeparator
155 var splitlisttests = []SplitListTest{
157 {string([]byte{'a', lsep, 'b'}), []string{"a", "b"}},
158 {string([]byte{lsep, 'a', lsep, 'b'}), []string{"", "a", "b"}},
161 func TestSplitList(t *testing.T) {
162 for _, test := range splitlisttests {
163 if l := filepath.SplitList(test.list); !reflect.DeepEqual(l, test.result) {
164 t.Errorf("SplitList(%q) = %s, want %s", test.list, l, test.result)
169 type SplitTest struct {
170 path, dir, file string
173 var unixsplittests = []SplitTest{
175 {"a/b/", "a/b/", ""},
181 var winsplittests = []SplitTest{
184 {`c:/foo`, `c:/`, `foo`},
185 {`c:/foo/bar`, `c:/foo/`, `bar`},
186 {`//host/share`, `//host/share`, ``},
187 {`//host/share/`, `//host/share/`, ``},
188 {`//host/share/foo`, `//host/share/`, `foo`},
189 {`\\host\share`, `\\host\share`, ``},
190 {`\\host\share\`, `\\host\share\`, ``},
191 {`\\host\share\foo`, `\\host\share\`, `foo`},
194 func TestSplit(t *testing.T) {
195 var splittests []SplitTest
196 splittests = unixsplittests
197 if runtime.GOOS == "windows" {
198 splittests = append(splittests, winsplittests...)
200 for _, test := range splittests {
201 if d, f := filepath.Split(test.path); d != test.dir || f != test.file {
202 t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)
207 type JoinTest struct {
212 var jointests = []JoinTest{
218 {[]string{"a"}, "a"},
221 {[]string{"a", "b"}, "a/b"},
222 {[]string{"a", ""}, "a"},
223 {[]string{"", "b"}, "b"},
224 {[]string{"/", "a"}, "/a"},
225 {[]string{"/", ""}, "/"},
226 {[]string{"a/", "b"}, "a/b"},
227 {[]string{"a/", ""}, "a"},
228 {[]string{"", ""}, ""},
231 var winjointests = []JoinTest{
232 {[]string{`directory`, `file`}, `directory\file`},
233 {[]string{`C:\Windows\`, `System32`}, `C:\Windows\System32`},
234 {[]string{`C:\Windows\`, ``}, `C:\Windows`},
235 {[]string{`C:\`, `Windows`}, `C:\Windows`},
236 {[]string{`C:`, `Windows`}, `C:\Windows`},
237 {[]string{`\\host\share`, `foo`}, `\\host\share\foo`},
238 {[]string{`//host/share`, `foo/bar`}, `\\host\share\foo\bar`},
241 // join takes a []string and passes it to Join.
242 func join(elem []string, args ...string) string {
244 return filepath.Join(args...)
247 func TestJoin(t *testing.T) {
248 if runtime.GOOS == "windows" {
249 jointests = append(jointests, winjointests...)
251 for _, test := range jointests {
252 if p := join(test.elem); p != filepath.FromSlash(test.path) {
253 t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
258 type ExtTest struct {
262 var exttests = []ExtTest{
264 {"path.pb.go", ".go"},
266 {"a.dir/b.go", ".go"},
270 func TestExt(t *testing.T) {
271 for _, test := range exttests {
272 if x := filepath.Ext(test.path); x != test.ext {
273 t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext)
280 entries []*Node // nil if the entry is a file
310 func walkTree(n *Node, path string, f func(path string, n *Node)) {
312 for _, e := range n.entries {
313 walkTree(e, filepath.Join(path, e.name), f)
317 func makeTree(t *testing.T) {
318 walkTree(tree, tree.name, func(path string, n *Node) {
319 if n.entries == nil {
320 fd, err := os.Create(path)
322 t.Errorf("makeTree: %v", err)
332 func markTree(n *Node) { walkTree(n, "", func(path string, n *Node) { n.mark++ }) }
334 func checkMarks(t *testing.T, report bool) {
335 walkTree(tree, tree.name, func(path string, n *Node) {
336 if n.mark != 1 && report {
337 t.Errorf("node %s mark = %d; expected 1", path, n.mark)
343 // Assumes that each node name is unique. Good enough for a test.
344 // If clear is true, any incoming error is cleared before return. The errors
345 // are always accumulated, though.
346 func mark(path string, info os.FileInfo, err error, errors *[]error, clear bool) error {
348 *errors = append(*errors, err)
355 walkTree(tree, tree.name, func(path string, n *Node) {
363 func TestWalk(t *testing.T) {
365 errors := make([]error, 0, 10)
367 markFn := func(path string, info os.FileInfo, err error) error {
368 return mark(path, info, err, &errors, clear)
371 err := filepath.Walk(tree.name, markFn)
373 t.Fatalf("no error expected, found: %s", err)
375 if len(errors) != 0 {
376 t.Fatalf("unexpected errors: %s", errors)
381 // Test permission errors. Only possible if we're not root
382 // and only on some file systems (AFS, FAT). To avoid errors during
383 // all.bash on those file systems, skip during go test -short.
384 if os.Getuid() > 0 && !testing.Short() {
385 // introduce 2 errors: chmod top-level directories to 0
386 os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0)
387 os.Chmod(filepath.Join(tree.name, tree.entries[3].name), 0)
389 // 3) capture errors, expect two.
390 // mark respective subtrees manually
391 markTree(tree.entries[1])
392 markTree(tree.entries[3])
393 // correct double-marking of directory itself
394 tree.entries[1].mark--
395 tree.entries[3].mark--
396 err := filepath.Walk(tree.name, markFn)
398 t.Fatalf("expected no error return from Walk, got %s", err)
400 if len(errors) != 2 {
401 t.Errorf("expected 2 errors, got %d: %s", len(errors), errors)
403 // the inaccessible subtrees were marked manually
407 // 4) capture errors, stop after first error.
408 // mark respective subtrees manually
409 markTree(tree.entries[1])
410 markTree(tree.entries[3])
411 // correct double-marking of directory itself
412 tree.entries[1].mark--
413 tree.entries[3].mark--
414 clear = false // error will stop processing
415 err = filepath.Walk(tree.name, markFn)
417 t.Fatalf("expected error return from Walk")
419 if len(errors) != 1 {
420 t.Errorf("expected 1 error, got %d: %s", len(errors), errors)
422 // the inaccessible subtrees were marked manually
426 // restore permissions
427 os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0770)
428 os.Chmod(filepath.Join(tree.name, tree.entries[3].name), 0770)
432 if err := os.RemoveAll(tree.name); err != nil {
433 t.Errorf("removeTree: %v", err)
437 var basetests = []PathTest{
451 var winbasetests = []PathTest{
457 {`\\host\share\`, `\`},
458 {`\\host\share\a`, `a`},
459 {`\\host\share\a\b`, `b`},
462 func TestBase(t *testing.T) {
464 if runtime.GOOS == "windows" {
465 // make unix tests work on windows
466 for i := range tests {
467 tests[i].result = filepath.Clean(tests[i].result)
469 // add windows specific tests
470 tests = append(tests, winbasetests...)
472 for _, test := range tests {
473 if s := filepath.Base(test.path); s != test.result {
474 t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result)
479 var dirtests = []PathTest{
494 var windirtests = []PathTest{
499 {`c:a\b\c`, `c:a\b`},
500 {`\\host\share\`, `\\host\share\`},
501 {`\\host\share\a`, `\\host\share\`},
502 {`\\host\share\a\b`, `\\host\share\a`},
505 func TestDir(t *testing.T) {
507 if runtime.GOOS == "windows" {
508 // make unix tests work on windows
509 for i := range tests {
510 tests[i].result = filepath.Clean(tests[i].result)
512 // add windows specific tests
513 tests = append(tests, windirtests...)
515 for _, test := range tests {
516 if s := filepath.Dir(test.path); s != test.result {
517 t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result)
522 type IsAbsTest struct {
527 var isabstests = []IsAbsTest{
530 {"/usr/bin/gcc", true},
538 var winisabstests = []IsAbsTest{
547 {`\\host\share\foo`, true},
548 {`//host/share/foo/bar`, true},
551 func TestIsAbs(t *testing.T) {
552 var tests []IsAbsTest
553 if runtime.GOOS == "windows" {
554 tests = append(tests, winisabstests...)
555 // All non-windows tests should fail, because they have no volume letter.
556 for _, test := range isabstests {
557 tests = append(tests, IsAbsTest{test.path, false})
559 // All non-windows test should work as intended if prefixed with volume letter.
560 for _, test := range isabstests {
561 tests = append(tests, IsAbsTest{"c:" + test.path, test.isAbs})
567 for _, test := range tests {
568 if r := filepath.IsAbs(test.path); r != test.isAbs {
569 t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)
574 type EvalSymlinksTest struct {
575 // If dest is empty, the path is created; otherwise the dest is symlinked to the path.
579 var EvalSymlinksTestDirs = []EvalSymlinksTest{
582 {"test/dir/link3", "../../"},
583 {"test/link1", "../test"},
584 {"test/link2", "dir"},
585 {"test/linkabs", "/"},
588 var EvalSymlinksTests = []EvalSymlinksTest{
590 {"test/dir", "test/dir"},
591 {"test/dir/../..", "."},
592 {"test/link1", "test"},
593 {"test/link2", "test/dir"},
594 {"test/link1/dir", "test/dir"},
595 {"test/link2/..", "test"},
596 {"test/dir/link3", "."},
597 {"test/link2/link3/test", "test"},
598 {"test/linkabs", "/"},
601 var EvalSymlinksAbsWindowsTests = []EvalSymlinksTest{
605 // simpleJoin builds a file name from the directory and path.
606 // It does not use Join because we don't want ".." to be evaluated.
607 func simpleJoin(dir, path string) string {
608 return dir + string(filepath.Separator) + path
611 func TestEvalSymlinks(t *testing.T) {
612 tmpDir, err := ioutil.TempDir("", "evalsymlink")
614 t.Fatal("creating temp dir:", err)
616 defer os.RemoveAll(tmpDir)
618 // /tmp may itself be a symlink! Avoid the confusion, although
619 // it means trusting the thing we're testing.
620 tmpDir, err = filepath.EvalSymlinks(tmpDir)
622 t.Fatal("eval symlink for tmp dir:", err)
625 // Create the symlink farm using relative paths.
626 for _, d := range EvalSymlinksTestDirs {
628 path := simpleJoin(tmpDir, d.path)
630 err = os.Mkdir(path, 0755)
632 if runtime.GOOS != "windows" {
633 err = os.Symlink(d.dest, path)
641 var tests []EvalSymlinksTest
642 if runtime.GOOS == "windows" {
643 for _, d := range EvalSymlinksTests {
644 if d.path == d.dest {
645 // will test only real files and directories
646 tests = append(tests, d)
647 // test "canonical" names
648 d2 := EvalSymlinksTest{
649 path: strings.ToUpper(d.path),
652 tests = append(tests, d2)
656 tests = EvalSymlinksTests
659 // Evaluate the symlink farm.
660 for _, d := range tests {
661 path := simpleJoin(tmpDir, d.path)
662 dest := simpleJoin(tmpDir, d.dest)
663 if filepath.IsAbs(d.dest) {
666 if p, err := filepath.EvalSymlinks(path); err != nil {
667 t.Errorf("EvalSymlinks(%q) error: %v", d.path, err)
668 } else if filepath.Clean(p) != filepath.Clean(dest) {
669 t.Errorf("Clean(%q)=%q, want %q", path, p, dest)
674 // Test directories relative to temporary directory.
675 // The tests are run in absTestDirs[0].
676 var absTestDirs = []string{
682 // Test paths relative to temporary directory. $ expands to the directory.
683 // The tests are run in absTestDirs[0].
684 // We create absTestDirs first.
685 var absTests = []string{
690 "../a/b/./c/../../.././a",
694 "$/a/b/c/../../.././a",
697 func TestAbs(t *testing.T) {
698 oldwd, err := os.Getwd()
700 t.Fatal("Getwd failed: ", err)
702 defer os.Chdir(oldwd)
704 root, err := ioutil.TempDir("", "TestAbs")
706 t.Fatal("TempDir failed: ", err)
708 defer os.RemoveAll(root)
710 wd, err := os.Getwd()
712 t.Fatal("getwd failed: ", err)
716 t.Fatal("chdir failed: ", err)
720 for _, dir := range absTestDirs {
721 err = os.Mkdir(dir, 0777)
723 t.Fatal("Mkdir failed: ", err)
727 err = os.Chdir(absTestDirs[0])
729 t.Fatal("chdir failed: ", err)
732 for _, path := range absTests {
733 path = strings.Replace(path, "$", root, -1)
734 info, err := os.Stat(path)
736 t.Errorf("%s: %s", path, err)
740 abspath, err := filepath.Abs(path)
742 t.Errorf("Abs(%q) error: %v", path, err)
745 absinfo, err := os.Stat(abspath)
746 if err != nil || !os.SameFile(absinfo, info) {
747 t.Errorf("Abs(%q)=%q, not the same file", path, abspath)
749 if !filepath.IsAbs(abspath) {
750 t.Errorf("Abs(%q)=%q, not an absolute path", path, abspath)
752 if filepath.IsAbs(path) && abspath != filepath.Clean(path) {
753 t.Errorf("Abs(%q)=%q, isn't clean", path, abspath)
758 type RelTests struct {
759 root, path, want string
762 var reltests = []RelTests{
764 {"a/b/.", "a/b", "."},
765 {"a/b", "a/b/.", "."},
766 {"./a/b", "a/b", "."},
767 {"a/b", "./a/b", "."},
768 {"ab/cd", "ab/cde", "../cde"},
769 {"ab/cd", "ab/c", "../c"},
770 {"a/b", "a/b/c/d", "c/d"},
771 {"a/b", "a/b/../c", "../c"},
772 {"a/b/../c", "a/b", "../b"},
773 {"a/b/c", "a/c/d", "../../c/d"},
774 {"a/b", "c/d", "../../c/d"},
775 {"a/b/c/d", "a/b", "../.."},
776 {"a/b/c/d", "a/b/", "../.."},
777 {"a/b/c/d/", "a/b", "../.."},
778 {"a/b/c/d/", "a/b/", "../.."},
779 {"../../a/b", "../../a/b/c/d", "c/d"},
780 {"/a/b", "/a/b", "."},
781 {"/a/b/.", "/a/b", "."},
782 {"/a/b", "/a/b/.", "."},
783 {"/ab/cd", "/ab/cde", "../cde"},
784 {"/ab/cd", "/ab/c", "../c"},
785 {"/a/b", "/a/b/c/d", "c/d"},
786 {"/a/b", "/a/b/../c", "../c"},
787 {"/a/b/../c", "/a/b", "../b"},
788 {"/a/b/c", "/a/c/d", "../../c/d"},
789 {"/a/b", "/c/d", "../../c/d"},
790 {"/a/b/c/d", "/a/b", "../.."},
791 {"/a/b/c/d", "/a/b/", "../.."},
792 {"/a/b/c/d/", "/a/b", "../.."},
793 {"/a/b/c/d/", "/a/b/", "../.."},
794 {"/../../a/b", "/../../a/b/c/d", "c/d"},
798 // can't do purely lexically
801 {"../..", "..", "err"},
806 var winreltests = []RelTests{
807 {`C:a\b\c`, `C:a/b/d`, `..\d`},
808 {`C:\`, `D:\`, `err`},
812 func TestRel(t *testing.T) {
813 tests := append([]RelTests{}, reltests...)
814 if runtime.GOOS == "windows" {
815 for i := range tests {
816 tests[i].want = filepath.FromSlash(tests[i].want)
818 tests = append(tests, winreltests...)
820 for _, test := range tests {
821 got, err := filepath.Rel(test.root, test.path)
822 if test.want == "err" {
824 t.Errorf("Rel(%q, %q)=%q, want error", test.root, test.path, got)
829 t.Errorf("Rel(%q, %q): want %q, got error: %s", test.root, test.path, test.want, err)
831 if got != test.want {
832 t.Errorf("Rel(%q, %q)=%q, want %q", test.root, test.path, got, test.want)
837 type VolumeNameTest struct {
842 var volumenametests = []VolumeNameTest{
843 {`c:/foo/bar`, `c:`},
849 {`\\\host\share`, ``},
850 {`\\\host\\share`, ``},
855 {`\\host\share`, `\\host\share`},
856 {`//host/share`, `//host/share`},
857 {`\\host\share\`, `\\host\share`},
858 {`//host/share/`, `//host/share`},
859 {`\\host\share\foo`, `\\host\share`},
860 {`//host/share/foo`, `//host/share`},
861 {`\\host\share\\foo\\\bar\\\\baz`, `\\host\share`},
862 {`//host/share//foo///bar////baz`, `//host/share`},
863 {`\\host\share\foo\..\bar`, `\\host\share`},
864 {`//host/share/foo/../bar`, `//host/share`},
867 func TestVolumeName(t *testing.T) {
868 if runtime.GOOS != "windows" {
871 for _, v := range volumenametests {
872 if vol := filepath.VolumeName(v.path); vol != v.vol {
873 t.Errorf("VolumeName(%q)=%q, want %q", v.path, vol, v.vol)
878 func TestDriveLetterInEvalSymlinks(t *testing.T) {
879 if runtime.GOOS != "windows" {
884 t.Errorf("Current directory path %q is too short", wd)
886 lp := strings.ToLower(wd)
887 up := strings.ToUpper(wd)
888 flp, err := filepath.EvalSymlinks(lp)
890 t.Fatalf("EvalSymlinks(%q) failed: %q", lp, err)
892 fup, err := filepath.EvalSymlinks(up)
894 t.Fatalf("EvalSymlinks(%q) failed: %q", up, err)
897 t.Errorf("Results of EvalSymlinks do not match: %q and %q", flp, fup)
901 /* This test does not work gccgo, since the sources are arranged
904 func TestBug3486(t *testing.T) { // http://code.google.com/p/go/issues/detail?id=3486
905 root, err := filepath.EvalSymlinks(os.Getenv("GOROOT"))
909 lib := filepath.Join(root, "lib")
910 src := filepath.Join(root, "src")
912 filepath.Walk(root, func(pth string, info os.FileInfo, err error) error {
919 return filepath.SkipDir
926 t.Fatalf("%q not seen", src)