1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
package formula
import (
"path/filepath"
"runtime"
"testing"
)
// This function and its tests are being submitted upstream through:
//
// http://codereview.appspot.com/4981049
//
// Meanwhile, we'll inline them here.
type RelTests struct {
root, path, want string
}
var reltests = []RelTests{
{"a/b", "a/b", "."},
{"a/b/.", "a/b", "."},
{"a/b", "a/b/.", "."},
{"./a/b", "a/b", "."},
{"a/b", "./a/b", "."},
{"a/b", "a/bc", "../bc"},
{"a/bc", "a/b", "../b"},
{"a/b", "a/b/c/d", "c/d"},
{"a/b", "a/b/../c", "../c"},
{"a/b/../c", "a/b", "../b"},
{"a/b/c", "a/c/d", "../../c/d"},
{"a/b", "c/d", "../../c/d"},
{"../../a/b", "../../a/b/c/d", "c/d"},
{"/a/b", "/a/b", "."},
{"/a/b/.", "/a/b", "."},
{"/a/b", "/a/b/.", "."},
{"/a/b", "/a/bc", "../bc"},
{"/a/bc", "/a/b", "../b"},
{"/a/b", "/a/b/c/d", "c/d"},
{"/a/b", "/a/b/../c", "../c"},
{"/a/b/../c", "/a/b", "../b"},
{"/a/b/c", "/a/c/d", "../../c/d"},
{"/a/b", "/c/d", "../../c/d"},
{"/../../a/b", "/../../a/b/c/d", "c/d"},
{".", "a/b", "a/b"},
{".", "..", ".."},
// can't do purely lexically
{"..", ".", "err"},
{"..", "a", "err"},
{"../..", "..", "err"},
{"a", "/a", "err"},
{"/a", "a", "err"},
}
var winreltests = []RelTests{
{`C:a\b\c`, `C:a/b/d`, `..\d`},
{`C:\`, `D:\`, `err`},
{`C:`, `D:`, `err`},
}
func TestRel(t *testing.T) {
tests := append([]RelTests{}, reltests...)
if runtime.GOOS == "windows" {
tests = append(tests, winreltests...)
}
for _, test := range tests {
got, err := filepath.Rel(test.root, test.path)
if test.want == "err" {
if err == nil {
t.Errorf("Rel(%q, %q)=%q, want error", test.root, test.path, got)
}
continue
}
if err != nil {
t.Errorf("Rel(%q, %q): want %q, got error: %s", test.root, test.path, test.want, err)
}
if got != test.want {
t.Errorf("Rel(%q, %q)=%q, want %q", test.root, test.path, got, test.want)
}
}
}
|