Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit c123f59

Browse files
committed
Apply common validations to all import conversion unit tests
* Remove matchVersionPair flag and validate the cast anytime a * wantRevision was specified. * Always validate lock properties when a lock is generated.
1 parent 044c4ef commit c123f59

File tree

3 files changed

+201
-197
lines changed

3 files changed

+201
-197
lines changed

cmd/dep/glide_importer_test.go

Lines changed: 60 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,9 @@ func newTestContext(h *test.Helper) *dep.Ctx {
3636

3737
func TestGlideConfig_Convert(t *testing.T) {
3838
testCases := map[string]struct {
39-
yaml glideYaml
40-
lock *glideLock
41-
wantConvertErr bool
42-
matchPairedVersion bool
43-
projectRoot gps.ProjectRoot
44-
wantSourceRepo string
45-
wantConstraint string
46-
wantRevision gps.Revision
47-
wantVersion string
48-
wantLockCount int
49-
wantIgnoreCount int
50-
wantIgnoredPackages []string
39+
*convertTestCase
40+
yaml glideYaml
41+
lock *glideLock
5142
}{
5243
"project": {
5344
yaml: glideYaml{
@@ -68,13 +59,14 @@ func TestGlideConfig_Convert(t *testing.T) {
6859
},
6960
},
7061
},
71-
projectRoot: "github.com/sdboyer/deptest",
72-
wantSourceRepo: "https://github.com/sdboyer/deptest.git",
73-
matchPairedVersion: true,
74-
wantConstraint: "^1.0.0",
75-
wantLockCount: 1,
76-
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
77-
wantVersion: "v1.0.0",
62+
convertTestCase: &convertTestCase{
63+
projectRoot: "github.com/sdboyer/deptest",
64+
wantSourceRepo: "https://github.com/sdboyer/deptest.git",
65+
wantConstraint: "^1.0.0",
66+
wantLockCount: 1,
67+
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
68+
wantVersion: "v1.0.0",
69+
},
7870
},
7971
"test project": {
8072
yaml: glideYaml{
@@ -93,42 +85,74 @@ func TestGlideConfig_Convert(t *testing.T) {
9385
},
9486
},
9587
},
96-
projectRoot: "github.com/sdboyer/deptest",
97-
wantLockCount: 1,
98-
wantConstraint: "^1.0.0",
99-
wantVersion: "v1.0.0",
88+
convertTestCase: &convertTestCase{
89+
projectRoot: "github.com/sdboyer/deptest",
90+
wantLockCount: 1,
91+
wantConstraint: "^1.0.0",
92+
wantVersion: "v1.0.0",
93+
},
94+
},
95+
"revision only": {
96+
yaml: glideYaml{
97+
Imports: []glidePackage{
98+
{
99+
Name: "github.com/sdboyer/deptest",
100+
},
101+
},
102+
},
103+
lock: &glideLock{
104+
Imports: []glideLockedPackage{
105+
{
106+
Name: "github.com/sdboyer/deptest",
107+
Reference: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
108+
},
109+
},
110+
},
111+
convertTestCase: &convertTestCase{
112+
projectRoot: "github.com/sdboyer/deptest",
113+
wantLockCount: 1,
114+
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
115+
},
100116
},
101117
"with ignored package": {
102118
yaml: glideYaml{
103119
Ignores: []string{"github.com/sdboyer/deptest"},
104120
},
105-
projectRoot: "github.com/sdboyer/deptest",
106-
wantIgnoreCount: 1,
107-
wantIgnoredPackages: []string{"github.com/sdboyer/deptest"},
121+
convertTestCase: &convertTestCase{
122+
projectRoot: "github.com/sdboyer/deptest",
123+
wantIgnoreCount: 1,
124+
wantIgnoredPackages: []string{"github.com/sdboyer/deptest"},
125+
},
108126
},
109127
"with exclude dir": {
110128
yaml: glideYaml{
111129
ExcludeDirs: []string{"samples"},
112130
},
113-
projectRoot: testGlideProjectRoot,
114-
wantIgnoreCount: 1,
115-
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
131+
convertTestCase: &convertTestCase{
132+
projectRoot: testGlideProjectRoot,
133+
wantIgnoreCount: 1,
134+
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
135+
},
116136
},
117137
"exclude dir ignores mismatched package name": {
118138
yaml: glideYaml{
119139
Name: "github.com/golang/mismatched-package-name",
120140
ExcludeDirs: []string{"samples"},
121141
},
122-
projectRoot: testGlideProjectRoot,
123-
wantIgnoreCount: 1,
124-
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
142+
convertTestCase: &convertTestCase{
143+
projectRoot: testGlideProjectRoot,
144+
wantIgnoreCount: 1,
145+
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
146+
},
125147
},
126148
"bad input, empty package name": {
127149
yaml: glideYaml{
128150
Imports: []glidePackage{{Name: ""}},
129151
},
130-
projectRoot: testGlideProjectRoot,
131-
wantConvertErr: true,
152+
convertTestCase: &convertTestCase{
153+
projectRoot: testGlideProjectRoot,
154+
wantConvertErr: true,
155+
},
132156
},
133157
}
134158

@@ -149,87 +173,11 @@ func TestGlideConfig_Convert(t *testing.T) {
149173
g.lock = testCase.lock
150174
}
151175

152-
manifest, lock, err := g.convert(testCase.projectRoot)
176+
manifest, lock, convertErr := g.convert(testCase.projectRoot)
177+
err := validateConvertTestCase(testCase.convertTestCase, manifest, lock, convertErr)
153178
if err != nil {
154-
if testCase.wantConvertErr {
155-
return
156-
}
157-
158179
t.Fatal(err)
159180
}
160-
161-
// Lock checks.
162-
if lock != nil && len(lock.P) != testCase.wantLockCount {
163-
t.Fatalf("Expected lock to have %d project(s), got %d",
164-
testCase.wantLockCount,
165-
len(lock.P))
166-
}
167-
168-
// Ignored projects checks.
169-
if len(manifest.Ignored) != testCase.wantIgnoreCount {
170-
t.Fatalf("Expected manifest to have %d ignored project(s), got %d",
171-
testCase.wantIgnoreCount,
172-
len(manifest.Ignored))
173-
}
174-
175-
if !equalSlice(manifest.Ignored, testCase.wantIgnoredPackages) {
176-
t.Fatalf("Expected manifest to have ignore %s, got %s",
177-
strings.Join(testCase.wantIgnoredPackages, ", "),
178-
strings.Join(manifest.Ignored, ", "))
179-
}
180-
181-
// Constraints checks below. Skip if there is no want constraint.
182-
if testCase.wantConstraint == "" {
183-
return
184-
}
185-
186-
d, ok := manifest.Constraints[testCase.projectRoot]
187-
if !ok {
188-
t.Fatalf("Expected the manifest to have a dependency for '%s' but got none",
189-
testCase.projectRoot)
190-
}
191-
192-
v := d.Constraint.String()
193-
if v != testCase.wantConstraint {
194-
t.Fatalf("Expected manifest constraint to be %s, got %s", testCase.wantConstraint, v)
195-
}
196-
197-
p := lock.P[0]
198-
199-
if p.Ident().ProjectRoot != testCase.projectRoot {
200-
t.Fatalf("Expected the lock to have a project for '%s' but got '%s'",
201-
testCase.projectRoot,
202-
p.Ident().ProjectRoot)
203-
}
204-
205-
if p.Ident().Source != testCase.wantSourceRepo {
206-
t.Fatalf("Expected locked source to be %s, got '%s'", testCase.wantSourceRepo, p.Ident().Source)
207-
}
208-
209-
lv := p.Version()
210-
lpv, ok := lv.(gps.PairedVersion)
211-
212-
if !ok {
213-
if testCase.matchPairedVersion {
214-
t.Fatalf("Expected locked version to be PairedVersion but got %T", lv)
215-
}
216-
217-
return
218-
}
219-
220-
ver := lpv.String()
221-
if ver != testCase.wantVersion {
222-
t.Fatalf("Expected locked version to be '%s', got %s", testCase.wantVersion, ver)
223-
}
224-
225-
if testCase.wantRevision != "" {
226-
rev := lpv.Revision()
227-
if rev != testCase.wantRevision {
228-
t.Fatalf("Expected locked revision to be '%s', got %s",
229-
testCase.wantRevision,
230-
rev)
231-
}
232-
}
233181
})
234182
}
235183
}

0 commit comments

Comments
 (0)