forked from constabulary/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve_test.go
99 lines (86 loc) · 2.16 KB
/
resolve_test.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package gb
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestResolvePackages(t *testing.T) {
cwd := getwd(t)
root := filepath.Join(cwd, "testdata", "src")
tests := []struct {
paths []string
err error
}{
{paths: []string{"a"}},
{paths: []string{"."}, err: fmt.Errorf("failed to resolve import path %q: %q is not a package", ".", root)},
{paths: []string{"h"}, err: fmt.Errorf("failed to resolve import path %q: no buildable Go source files in %s", "h", filepath.Join(root, "blank"))},
}
for _, tt := range tests {
ctx := testContext(t)
defer ctx.Destroy()
_, err := ResolvePackages(ctx, tt.paths...)
if !sameErr(err, tt.err) {
t.Errorf("ResolvePackage(%v): want: %v, got %v", tt.paths, tt.err, err)
}
}
}
var join = filepath.Join
// makeTestData constructs
func makeTestdata(t *testing.T) string {
tempdir, err := filepath.EvalSymlinks(os.TempDir())
if err != nil {
t.Fatal(err)
}
root, err := ioutil.TempDir(tempdir, "path-test")
if err != nil {
t.Fatal(err)
}
mkdir := func(args ...string) string {
path := join(args...)
if err := os.MkdirAll(path, 0777); err != nil {
t.Fatal(err)
}
return path
}
mkfile := func(path string, content string) {
if err := ioutil.WriteFile(path, []byte(content), 0755); err != nil {
t.Fatal(err)
}
}
srcdir := mkdir(root, "src")
mkfile(join(mkdir(srcdir, "a"), "a.go"), "package a")
return root
}
func TestRelImportPath(t *testing.T) {
tests := []struct {
root, path, want string
}{
{"/project/src", "a", "a"},
// { "/project/src", "./a", "a"}, // TODO(dfc) this is relative
// { "/project/src", "a/../b", "a"}, // TODO(dfc) so is this
}
for _, tt := range tests {
got, _ := relImportPath(tt.root, tt.path)
if got != tt.want {
t.Errorf("relImportPath(%q, %q): want: %v, got: %v", tt.root, tt.path, tt.want, got)
}
}
}
func TestIsRel(t *testing.T) {
tests := []struct {
path string
want bool
}{
{".", true},
{"..", false}, // TODO(dfc) this is relative
{"a/../b", false}, // TODO(dfc) this too
}
for _, tt := range tests {
got := isRel(tt.path)
if got != tt.want {
t.Errorf("isRel(%q): want: %v, got: %v", tt.want, got)
}
}
}