-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcheckout_pathspec_test.go
More file actions
60 lines (50 loc) · 1.6 KB
/
Copy pathcheckout_pathspec_test.go
File metadata and controls
60 lines (50 loc) · 1.6 KB
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
package main
import (
"path/filepath"
"testing"
)
const (
testRepoRoot = "/repo"
testFile0 = "file0"
testDirFile1 = "dir1/file1"
testSubDir1 = "/repo/dir1"
)
func TestResolvePathspec(t *testing.T) {
t.Parallel()
root := testRepoRoot
tests := []struct {
name string
cwd string
spec string
want string
wantErr bool
}{
{name: "file at root from root", cwd: testRepoRoot, spec: testFile0, want: testFile0},
{name: "subdir file from root", cwd: testRepoRoot, spec: testDirFile1, want: testDirFile1},
{name: "dotdot back to root from subdir", cwd: testSubDir1, spec: "../file0", want: testFile0},
{name: "sibling via dotdot", cwd: testSubDir1, spec: "../dir2/file2", want: "dir2/file2"},
{name: "complex relative", cwd: testSubDir1, spec: "../dir1/../dir1/file1", want: testDirFile1},
{name: "directory pathspec", cwd: testRepoRoot, spec: "dir1", want: "dir1"},
{name: "parent escape from root", cwd: testRepoRoot, spec: "../Makefile", wantErr: true},
{name: "parent file from subdir", cwd: testSubDir1, spec: "../file0", want: testFile0},
{name: "escape from subdir", cwd: testSubDir1, spec: "../../file0", wantErr: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := resolvePathspec(filepath.FromSlash(root), filepath.FromSlash(tc.cwd), tc.spec)
if tc.wantErr {
if err == nil {
t.Fatalf("expected error, got %q", got)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != filepath.FromSlash(tc.want) {
t.Fatalf("got %q want %q", got, tc.want)
}
})
}
}