-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg_test.go
79 lines (66 loc) · 1.88 KB
/
pkg_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
package depth
import (
"go/build"
"sort"
"testing"
)
func TestPkg_CleanName(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"strings", "strings"},
{"net/http", "net/http"},
{"github.com/KyleBanks/depth", "github.com/KyleBanks/depth"},
{"C", ""},
{"golang_org/x/anything", "vendor/golang_org/x/anything"},
}
for _, tt := range tests {
p := Pkg{Name: tt.input}
out := p.cleanName()
if out != tt.expected {
t.Fatalf("Unexpected cleanName, expected=%v, got=%v", tt.expected, out)
}
}
}
func TestPkg_AddDepImportSeen(t *testing.T) {
var m MockImporter
var tr Tree
tr.Importer = m
testName := "test"
testSrcDir := "src/testing"
var expectedIm build.ImportMode
p := Pkg{Tree: &tr}
m.ImportFn = func(name, srcDir string, im build.ImportMode) (*build.Package, error) {
if name != testName {
t.Fatalf("Unexpected name provided, expected=%v, got=%v", testName, name)
}
if srcDir != testSrcDir {
t.Fatalf("Unexpected srcDir provided, expected=%v, got=%v", testSrcDir, srcDir)
}
if im != expectedIm {
t.Fatalf("Unexpected ImportMode provided, expected=%v, got=%v", expectedIm, im)
}
return &build.Package{}, nil
}
// Hasn't seen the import
p.addDep(m, testName, testSrcDir, false)
// Has seen the import
expectedIm = build.FindOnly
p.addDep(m, testName, testSrcDir, false)
}
func TestByInternalAndName(t *testing.T) {
pkgs := []Pkg{
Pkg{Internal: true, Name: "net/http"},
Pkg{Internal: false, Name: "github.com/KyleBanks/depth"},
Pkg{Internal: true, Name: "strings"},
Pkg{Internal: false, Name: "github.com/KyleBanks/commuter"},
}
expected := []string{"net/http", "strings", "github.com/KyleBanks/commuter", "github.com/KyleBanks/depth"}
sort.Sort(byInternalAndName(pkgs))
for i, e := range expected {
if pkgs[i].Name != e {
t.Fatalf("Unexpected Pkg at index %v, expected=%v, got=%v", i, e, pkgs[i].Name)
}
}
}