-
Notifications
You must be signed in to change notification settings - Fork 1
/
importer_test.go
100 lines (93 loc) · 1.85 KB
/
importer_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
100
package importer
import (
"go/build"
"reflect"
"runtime"
"testing"
"github.com/pkg/errors"
)
func TestSrcDirImporterImport(t *testing.T) {
i := &srcdirImporter{
Context: &build.Default,
root: runtime.GOROOT(),
}
tests := []struct {
Importer
setup func(*testing.T) func(*testing.T)
path string
err error
}{{
Importer: i,
path: "",
err: &importErr{
msg: "invalid import path",
},
}, {
Importer: i,
path: ".",
err: &importErr{
path: ".",
msg: "relative import not supported",
},
}, {
Importer: i,
path: "..",
err: &importErr{
path: "..",
msg: "relative import not supported",
},
}, {
Importer: i,
path: "/math",
err: &importErr{
path: "/math",
msg: "cannot import absolute path",
},
}}
for i, tt := range tests {
if tt.setup != nil {
teardown := tt.setup(t)
defer teardown(t)
}
_, err := tt.Import(tt.path)
if tt.err == nil && err != nil {
t.Errorf("%d: srcdirImporter.Import(%q): err, got %v, want %v", i, tt.path, err, nil)
continue
}
err = errors.Cause(err)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("%d: srcdirImporter.Import(%q): err, got %v, want %v", i, tt.path, err, tt.err)
continue
}
}
}
func TestGOROOT(t *testing.T) {
i := GOROOT(&build.Default)
tests := []struct {
Importer
path string
err error
}{{
Importer: i,
path: "math",
}, {
Importer: i,
path: "math/abs.go",
err: &importErr{
path: "math/abs.go",
msg: "not a directory",
},
}}
for i, tt := range tests {
_, err := tt.Import(tt.path)
if tt.err == nil && err != nil {
t.Errorf("%d: GOROOT().Import(%q): err, got %v, want %v", i, tt.path, err, nil)
continue
}
err = errors.Cause(err)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("%d: GOROOT().Import(%q): err, got %v, want %v", i, tt.path, err, tt.err)
continue
}
}
}