forked from d2fn/gopack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_test.go
168 lines (137 loc) · 3.9 KB
/
model_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"io/ioutil"
"os"
"path"
"testing"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func setupTestPwd() {
dir, _ := ioutil.TempDir("", "gopack-config-")
os.Setenv("GOPACK_APP_CONFIG", dir)
setPwd()
}
func createPath(path string) {
err := os.MkdirAll(path, 0700)
check(err)
}
func createScmDep(scm string, project string, paths ...string) *Dep {
dep := &Dep{Import: project}
scmPath := path.Join(dep.Src(), scm)
createPath(scmPath)
for _, p := range paths {
createPath(path.Join(scmPath, p))
}
return dep
}
func TestGit(t *testing.T) {
setupTestPwd()
dep := createScmDep(HiddenGit, "github.com/d2fn/gopack")
scm, err := NewScm(dep)
if _, ok := scm.(Git); !ok {
t.Error("Expected scm to be git but it was %s.\n%v", scm, err)
}
}
func TestHg(t *testing.T) {
setupTestPwd()
dep := createScmDep(HiddenHg, "code.google.com/p/go")
scm, err := NewScm(dep)
if _, ok := scm.(Hg); !ok {
t.Errorf("Expected scm to be hg but it was %s.\n%v", scm, err)
}
}
func TestUnknownScm(t *testing.T) {
setupTestPwd()
dep := createScmDep(HiddenSvn, "code.google.com/p/project")
scm, err := NewScm(dep)
if _, ok := scm.(Svn); !ok {
t.Errorf("Expected scm to be svn but it was %s.\n%v", scm, err)
}
}
func TestSubPackages(t *testing.T) {
setupTestPwd()
dep := createScmDep(HiddenHg, "code.google.com/p/go", "path/filepath", "io")
dep.Import = "code.google.com/p/go/path"
scm, err := NewScm(dep)
if _, ok := scm.(Hg); !ok {
t.Errorf("Expected scm to be hg but it was %s.\n%v", scm, err)
}
}
func TestTransitiveDependencies(t *testing.T) {
setupTestPwd()
setupEnv()
fixture := `
[deps.testgopack]
import = "github.com/calavera/testGoPack"
branch = "master"
`
createFixtureConfig(pwd, fixture)
config := NewConfig(pwd)
dependencies, _ := config.LoadDependencyModel(NewGraph())
loadTransitiveDependencies(dependencies)
dep := path.Join(pwd, VendorDir, "src", "github.com", "calavera", "testGoPack")
if _, err := os.Stat(dep); os.IsNotExist(err) {
t.Errorf("Expected dependency github.com/calavera/testGoPack to be in vendor %s\n", pwd)
}
dep = path.Join(pwd, VendorDir, "src", "github.com", "d2fn", "gopack")
if _, err := os.Stat(dep); os.IsNotExist(err) {
t.Errorf("Expected dependency github.com/d2fn/gopack to be in vendor %s\n", pwd)
}
}
func TestScm(t *testing.T) {
setupTestPwd()
setupEnv()
fixture := `
[deps.testpewp]
import = "github.com/calavera/testGoPack"
branch = "master"
scm = "git"
source = "https://github.com/calavera/testGoPack.git"
[deps.testnopro]
import = "github.com/nu7hatch/gouuid"
branch = "master"
`
createFixtureConfig(pwd, fixture)
config := NewConfig(pwd)
dependencies, _ := config.LoadDependencyModel(NewGraph())
if len(dependencies.DepList) > 2 {
t.Fatalf("WHOA buddy, shoulda had 2 deps, had %d instead", len(dependencies.DepList))
}
if dependencies.DepList[0].Scm != "git" {
t.Fatalf("Scm should have been git, was %s", dependencies.DepList[0])
}
if dependencies.DepList[1].Scm != "go" {
t.Fatalf("Scm should have been go, was %s", dependencies.DepList[1])
}
loadTransitiveDependencies(dependencies)
dep := path.Join(pwd, VendorDir, "src", "github.com", "calavera", "testGoPack")
if _, err := os.Stat(dep); os.IsNotExist(err) {
t.Errorf("Expected dependency github.com/calavera/testGoPack to be in vendor %s\n", pwd)
}
}
func TestScmAndSourceRequired(t *testing.T) {
setupTestPwd()
setupEnv()
fixtures := []string{`
[deps.testpewp]
import = "github.com/pewp/libnosource"
branch = "master"
scm = "git"`,
`[deps.testnopro]
import = "github.com/pewp/libnopro"
branch = "master"
source = "[email protected]:pewp/libpewp.git"
`}
for _, fixture := range fixtures {
createFixtureConfig(pwd, fixture)
config := NewConfig(pwd)
dependencies, err := config.LoadDependencyModel(NewGraph())
if err == nil {
t.Fatalf("Supposed to have failed due to lacking Source or Scm - %s", dependencies.DepList[0])
}
}
}