-
Notifications
You must be signed in to change notification settings - Fork 16
/
config_test.go
217 lines (181 loc) · 5.06 KB
/
config_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"io/ioutil"
"os"
"path"
"testing"
)
func createFixtureConfig(dir string, config string) {
err := ioutil.WriteFile(path.Join(dir, "gopack.config"), []byte(config), 0644)
check(err)
}
func setupTestConfig(fixture string) *Config {
setupTestPwd()
setupEnv()
createFixtureConfig(pwd, fixture)
return NewConfig(pwd)
}
func TestNewConfig(t *testing.T) {
config := setupTestConfig(`
repo = "github.com/d2fn/gopack"
[deps.testgopack]
import = "github.com/calavera/testGoPack"
branch = "master"
`)
if config.Repository == "" {
t.Error("Expected repository to not be empty.")
}
if config.DepsTree == nil {
t.Error("Expected dependency tree to not be empty.")
}
}
func TestInitRepoWithoutRepo(t *testing.T) {
config := setupTestConfig(`
[deps.testgopack]
import = "github.com/calavera/testGoPack"
branch = "master"
`)
graph := NewGraph()
config.InitRepo(graph)
src := path.Join(pwd, VendorDir, "src")
_, err := os.Stat(src)
if !os.IsNotExist(err) {
t.Errorf("Expected vendor to not exist in %s\n", pwd)
}
}
func TestInitRepo(t *testing.T) {
config := setupTestConfig(`repo = "github.com/d2fn/gopack"`)
graph := NewGraph()
config.InitRepo(graph)
dep := path.Join(pwd, VendorDir, "src", "github.com", "d2fn", "gopack")
stat, err := os.Stat(dep)
if os.IsNotExist(err) || (stat.Mode()&os.ModeSymlink != 0) {
t.Errorf("Expected repository %s to be linked in vendor %s\n", config.Repository, pwd)
}
if graph.Search(config.Repository) == nil {
t.Errorf("Expected repository %s to be in the dependencies graph\n", config.Repository)
}
}
func TestWriteChecksum(t *testing.T) {
config := setupTestConfig(`
[deps.testgopack]
import = "github.com/calavera/testGoPack"
branch = "master"
`)
config.WriteChecksum()
path := path.Join(pwd, GopackChecksum)
_, err := ioutil.ReadFile(path)
if err != nil && os.IsNotExist(err) {
t.Errorf("Expected checksum file %s to exist", path)
}
}
func TestFetchDependenciesWithoutChecksum(t *testing.T) {
config := setupTestConfig(`
[deps.testgopack]
import = "github.com/calavera/testGoPack"
branch = "master"
`)
if cfg, _ := config.LoadDependencyModel(NewGraph()); !cfg.AllDepsNeedFetching() {
t.Errorf("Expected to load all the dependencies when there is no checksum")
}
}
func TestFetchDependenciesWithoutChanges(t *testing.T) {
config := setupTestConfig(`
[deps.testgopack]
import = "github.com/calavera/testGoPack"
commit = "182cae2ee3926a960223d8db4998aa9d57c89788"
`)
config.WriteChecksum()
deps, _ := config.LoadDependencyModel(NewGraph())
if deps.AnyDepsNeedFetching() {
t.Errorf("Expected to not load any dependency with commit flag")
}
}
func TestFetchDependenciesWithBranch(t *testing.T) {
config := setupTestConfig(`
[deps.testgopack]
import = "github.com/calavera/testGoPack"
branch = "master"
`)
config.WriteChecksum()
deps, _ := config.LoadDependencyModel(NewGraph())
if len(deps.DepList) != 1 {
t.Errorf("Expected to load any dependency with branch flag")
}
}
func TestFetchDependenciesWithChanges(t *testing.T) {
config := setupTestConfig(`
[deps.testgopack]
import = "github.com/calavera/testGoPack"
commit = "182cae2ee3926a960223d8db4998aa9d57c89788"
`)
config.WriteChecksum()
config.Checksum = nil
fixture := `
[deps.testgopack]
import = "github.com/calavera/testGoPack"
commit = "182cae2ee3926a960223d8db4998aa9d57c89788"
[deps.foo]
import = "github.com/calavera/foo"
branch = "master"
`
createFixtureConfig(pwd, fixture)
deps, _ := config.LoadDependencyModel(NewGraph())
if len(deps.DepList) != 1 {
t.Errorf("Expected to load only the new dependencies")
}
}
func TestFetchWithCommitSpecs(t *testing.T) {
config := setupTestConfig(`
[deps.testgopack]
import = "github.com/calavera/testGoPack"
commit = "182cae2ee3926a960223d8db4998aa9d57c89788"
[deps.foo]
import = "github.com/calavera/foo"
branch = "master"
`)
config.WriteChecksum()
deps, _ := config.LoadDependencyModel(NewGraph())
if deps.DepList[0].fetch {
t.Errorf("Expected to not fetch the commit dependencies")
}
if !deps.DepList[1].fetch {
t.Errorf("Expected to fetch the branch dependencies")
}
}
func TestFetchWithTagSpecs(t *testing.T) {
config := setupTestConfig(`
[deps.testgopack]
import = "github.com/calavera/testGoPack"
tag = "v1.0.0"
[deps.foo]
import = "github.com/calavera/foo"
branch = "master"
`)
config.WriteChecksum()
deps, _ := config.LoadDependencyModel(NewGraph())
if deps.DepList[0].fetch {
t.Errorf("Expected to not fetch the tag dependencies")
}
if !deps.DepList[1].fetch {
t.Errorf("Expected to fetch the branch dependencies")
}
}
func TestFetchWithMixedSpecsIgnoringOrder(t *testing.T) {
config := setupTestConfig(`
[deps.foo]
import = "github.com/calavera/foo"
branch = "master"
[deps.testgopack]
import = "github.com/calavera/testGoPack"
commit = "182cae2ee3926a960223d8db4998aa9d57c89788"
`)
config.WriteChecksum()
deps, _ := config.LoadDependencyModel(NewGraph())
if !deps.DepList[0].fetch {
t.Errorf("Expected to not fetch the commit dependencies")
}
if deps.DepList[1].fetch {
t.Errorf("Expected to fetch the branch dependencies")
}
}