Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package filepath_test | 5 package filepath_test |
6 | 6 |
7 import ( | 7 import ( |
8 "os" | 8 "os" |
9 "path/filepath" | 9 "path/filepath" |
10 "reflect" | 10 "reflect" |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
565 } | 565 } |
566 testEvalSymlinks(t, tests) | 566 testEvalSymlinks(t, tests) |
567 } | 567 } |
568 | 568 |
569 // Test paths relative to $GOROOT/src | 569 // Test paths relative to $GOROOT/src |
570 var abstests = []string{ | 570 var abstests = []string{ |
571 "../AUTHORS", | 571 "../AUTHORS", |
572 "pkg/../../AUTHORS", | 572 "pkg/../../AUTHORS", |
573 "Make.pkg", | 573 "Make.pkg", |
574 "pkg/Makefile", | 574 "pkg/Makefile", |
575 | 575 » ".", |
576 » // Already absolute | |
577 "$GOROOT/src/Make.pkg", | 576 "$GOROOT/src/Make.pkg", |
578 "$GOROOT/src/../src/Make.pkg", | 577 "$GOROOT/src/../src/Make.pkg", |
578 "$GOROOT/misc/cgo", | |
579 "$GOROOT", | |
579 } | 580 } |
580 | 581 |
581 func TestAbs(t *testing.T) { | 582 func TestAbs(t *testing.T) { |
582 oldwd, err := os.Getwd() | 583 oldwd, err := os.Getwd() |
583 if err != nil { | 584 if err != nil { |
584 t.Fatal("Getwd failed: " + err.String()) | 585 t.Fatal("Getwd failed: " + err.String()) |
585 } | 586 } |
586 defer os.Chdir(oldwd) | 587 defer os.Chdir(oldwd) |
587 goroot := os.Getenv("GOROOT") | 588 goroot := os.Getenv("GOROOT") |
588 cwd := filepath.Join(goroot, "src") | 589 cwd := filepath.Join(goroot, "src") |
589 os.Chdir(cwd) | 590 os.Chdir(cwd) |
590 for _, path := range abstests { | 591 for _, path := range abstests { |
591 path = strings.Replace(path, "$GOROOT", goroot, -1) | 592 path = strings.Replace(path, "$GOROOT", goroot, -1) |
593 info, err := os.Stat(path) | |
594 if err != nil { | |
595 t.Errorf("%s: %s", path, err) | |
596 } | |
597 | |
592 abspath, err := filepath.Abs(path) | 598 abspath, err := filepath.Abs(path) |
593 if err != nil { | 599 if err != nil { |
594 t.Errorf("Abs(%q) error: %v", path, err) | 600 t.Errorf("Abs(%q) error: %v", path, err) |
595 } | 601 } |
596 info, err := os.Stat(path) | |
597 if err != nil { | |
598 t.Errorf("%s: %s", path, err) | |
599 } | |
600 absinfo, err := os.Stat(abspath) | 602 absinfo, err := os.Stat(abspath) |
601 if err != nil || absinfo.Ino != info.Ino { | 603 if err != nil || absinfo.Ino != info.Ino { |
602 t.Errorf("Abs(%q)=%q, not the same file", path, abspath) | 604 t.Errorf("Abs(%q)=%q, not the same file", path, abspath) |
603 } | 605 } |
604 if !filepath.IsAbs(abspath) { | 606 if !filepath.IsAbs(abspath) { |
605 t.Errorf("Abs(%q)=%q, not an absolute path", path, abspa th) | 607 t.Errorf("Abs(%q)=%q, not an absolute path", path, abspa th) |
606 } | 608 } |
607 if filepath.IsAbs(path) && abspath != filepath.Clean(path) { | 609 if filepath.IsAbs(path) && abspath != filepath.Clean(path) { |
608 t.Errorf("Abs(%q)=%q, isn't clean", path, abspath) | 610 t.Errorf("Abs(%q)=%q, isn't clean", path, abspath) |
609 } | 611 } |
610 } | 612 } |
611 } | 613 } |
612 | 614 |
615 type RelTests struct { | |
616 root, path, want string | |
617 } | |
618 | |
619 var reltests = []RelTests{ | |
620 {"a/b", "a/b", "."}, | |
r
2011/10/03 19:53:47
you should include at least one example with eleme
niemeyer
2011/10/04 00:59:34
There were already entries in lines 625, 626, 636
| |
621 {"a/b/.", "a/b", "."}, | |
622 {"a/b", "a/b/.", "."}, | |
623 {"./a/b", "a/b", "."}, | |
624 {"a/b", "./a/b", "."}, | |
625 {"a/b", "a/bc", "../bc"}, | |
626 {"a/bc", "a/b", "../b"}, | |
627 {"a/b", "a/b/c/d", "c/d"}, | |
628 {"a/b", "a/b/../c", "../c"}, | |
629 {"a/b/../c", "a/b", "../b"}, | |
630 {"a/b/c", "a/c/d", "../../c/d"}, | |
631 {"a/b", "c/d", "../../c/d"}, | |
632 {"../../a/b", "../../a/b/c/d", "c/d"}, | |
633 {"/a/b", "/a/b", "."}, | |
634 {"/a/b/.", "/a/b", "."}, | |
635 {"/a/b", "/a/b/.", "."}, | |
636 {"/a/b", "/a/bc", "../bc"}, | |
637 {"/a/bc", "/a/b", "../b"}, | |
638 {"/a/b", "/a/b/c/d", "c/d"}, | |
639 {"/a/b", "/a/b/../c", "../c"}, | |
640 {"/a/b/../c", "/a/b", "../b"}, | |
641 {"/a/b/c", "/a/c/d", "../../c/d"}, | |
642 {"/a/b", "/c/d", "../../c/d"}, | |
643 {"/../../a/b", "/../../a/b/c/d", "c/d"}, | |
644 {".", "a/b", "a/b"}, | |
645 {".", "..", ".."}, | |
646 | |
647 // can't do purely lexically | |
648 {"..", ".", "err"}, | |
649 {"..", "a", "err"}, | |
650 {"../..", "..", "err"}, | |
651 {"a", "/a", "err"}, | |
652 {"/a", "a", "err"}, | |
653 } | |
654 | |
655 var winreltests = []RelTests{ | |
656 {`C:a\b\c`, `C:a/b/d`, `..\d`}, | |
657 {`C:\`, `D:\`, `err`}, | |
658 {`C:`, `D:`, `err`}, | |
659 } | |
660 | |
661 func TestRel(t *testing.T) { | |
662 tests := append([]RelTests{}, reltests...) | |
663 if runtime.GOOS == "windows" { | |
664 tests = append(tests, winreltests...) | |
665 } | |
666 for _, test := range tests { | |
667 got, err := filepath.Rel(test.root, test.path) | |
668 if test.want == "err" { | |
669 if err == nil { | |
670 t.Errorf("Rel(%q, %q)=%q, want error", test.root , test.path, got) | |
671 } | |
672 continue | |
673 } | |
674 if err != nil { | |
675 t.Errorf("Rel(%q, %q): want %q, got error: %s", test.roo t, test.path, test.want, err) | |
676 } | |
677 if got != test.want { | |
678 t.Errorf("Rel(%q, %q)=%q, want %q", test.root, test.path , got, test.want) | |
679 } | |
680 } | |
681 } | |
682 | |
613 type VolumeNameTest struct { | 683 type VolumeNameTest struct { |
614 path string | 684 path string |
615 vol string | 685 vol string |
616 } | 686 } |
617 | 687 |
618 var volumenametests = []VolumeNameTest{ | 688 var volumenametests = []VolumeNameTest{ |
619 {`c:/foo/bar`, `c:`}, | 689 {`c:/foo/bar`, `c:`}, |
620 {`c:`, `c:`}, | 690 {`c:`, `c:`}, |
621 {``, ``}, | 691 {``, ``}, |
622 {`\\\host`, ``}, | 692 {`\\\host`, ``}, |
(...skipping 19 matching lines...) Expand all Loading... | |
642 func TestVolumeName(t *testing.T) { | 712 func TestVolumeName(t *testing.T) { |
643 if runtime.GOOS != "windows" { | 713 if runtime.GOOS != "windows" { |
644 return | 714 return |
645 } | 715 } |
646 for _, v := range volumenametests { | 716 for _, v := range volumenametests { |
647 if vol := filepath.VolumeName(v.path); vol != v.vol { | 717 if vol := filepath.VolumeName(v.path); vol != v.vol { |
648 t.Errorf("VolumeName(%q)=%q, want %q", v.path, vol, v.vo l) | 718 t.Errorf("VolumeName(%q)=%q, want %q", v.path, vol, v.vo l) |
649 } | 719 } |
650 } | 720 } |
651 } | 721 } |
OLD | NEW |