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

Commit ead19b8

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 5d885c9 commit ead19b8

File tree

3 files changed

+202
-199
lines changed

3 files changed

+202
-199
lines changed

cmd/dep/glide_importer_test.go

Lines changed: 61 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,9 @@ func newTestContext(h *test.Helper) *dep.Ctx {
3434

3535
func TestGlideConfig_Convert(t *testing.T) {
3636
testCases := map[string]struct {
37-
yaml glideYaml
38-
lock *glideLock
39-
wantConvertErr bool
40-
matchPairedVersion bool
41-
projectRoot gps.ProjectRoot
42-
wantSourceRepo string
43-
wantConstraint string
44-
wantRevision gps.Revision
45-
wantVersion string
46-
wantLockCount int
47-
wantIgnoreCount int
48-
wantIgnoredPackages []string
37+
*convertTestCase
38+
yaml glideYaml
39+
lock *glideLock
4940
}{
5041
"project": {
5142
yaml: glideYaml{
@@ -66,13 +57,14 @@ func TestGlideConfig_Convert(t *testing.T) {
6657
},
6758
},
6859
},
69-
projectRoot: "github.com/sdboyer/deptest",
70-
wantSourceRepo: "https://github.com/sdboyer/deptest.git",
71-
matchPairedVersion: true,
72-
wantConstraint: "^1.0.0",
73-
wantLockCount: 1,
74-
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
75-
wantVersion: "v1.0.0",
60+
convertTestCase: &convertTestCase{
61+
projectRoot: "github.com/sdboyer/deptest",
62+
wantSourceRepo: "https://github.com/sdboyer/deptest.git",
63+
wantConstraint: "^1.0.0",
64+
wantLockCount: 1,
65+
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
66+
wantVersion: "v1.0.0",
67+
},
7668
},
7769
"test project": {
7870
yaml: glideYaml{
@@ -91,42 +83,74 @@ func TestGlideConfig_Convert(t *testing.T) {
9183
},
9284
},
9385
},
94-
projectRoot: "github.com/sdboyer/deptest",
95-
wantLockCount: 1,
96-
wantConstraint: "^1.0.0",
97-
wantVersion: "v1.0.0",
86+
convertTestCase: &convertTestCase{
87+
projectRoot: "github.com/sdboyer/deptest",
88+
wantLockCount: 1,
89+
wantConstraint: "^1.0.0",
90+
wantVersion: "v1.0.0",
91+
},
92+
},
93+
"revision only": {
94+
yaml: glideYaml{
95+
Imports: []glidePackage{
96+
{
97+
Name: "github.com/sdboyer/deptest",
98+
},
99+
},
100+
},
101+
lock: &glideLock{
102+
Imports: []glideLockedPackage{
103+
{
104+
Name: "github.com/sdboyer/deptest",
105+
Reference: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
106+
},
107+
},
108+
},
109+
convertTestCase: &convertTestCase{
110+
projectRoot: "github.com/sdboyer/deptest",
111+
wantLockCount: 1,
112+
wantRevision: gps.Revision("ff2948a2ac8f538c4ecd55962e919d1e13e74baf"),
113+
},
98114
},
99115
"with ignored package": {
100116
yaml: glideYaml{
101117
Ignores: []string{"github.com/sdboyer/deptest"},
102118
},
103-
projectRoot: "github.com/sdboyer/deptest",
104-
wantIgnoreCount: 1,
105-
wantIgnoredPackages: []string{"github.com/sdboyer/deptest"},
119+
convertTestCase: &convertTestCase{
120+
projectRoot: "github.com/sdboyer/deptest",
121+
wantIgnoreCount: 1,
122+
wantIgnoredPackages: []string{"github.com/sdboyer/deptest"},
123+
},
106124
},
107125
"with exclude dir": {
108126
yaml: glideYaml{
109127
ExcludeDirs: []string{"samples"},
110128
},
111-
projectRoot: testProjectRoot,
112-
wantIgnoreCount: 1,
113-
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
129+
convertTestCase: &convertTestCase{
130+
projectRoot: testProjectRoot,
131+
wantIgnoreCount: 1,
132+
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
133+
},
114134
},
115135
"exclude dir ignores mismatched package name": {
116136
yaml: glideYaml{
117137
Name: "github.com/golang/mismatched-package-name",
118138
ExcludeDirs: []string{"samples"},
119139
},
120-
projectRoot: testProjectRoot,
121-
wantIgnoreCount: 1,
122-
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
140+
convertTestCase: &convertTestCase{
141+
projectRoot: testProjectRoot,
142+
wantIgnoreCount: 1,
143+
wantIgnoredPackages: []string{"github.com/golang/notexist/samples"},
144+
},
123145
},
124146
"bad input, empty package name": {
125147
yaml: glideYaml{
126148
Imports: []glidePackage{{Name: ""}},
127149
},
128-
projectRoot: testProjectRoot,
129-
wantConvertErr: true,
150+
convertTestCase: &convertTestCase{
151+
projectRoot: testProjectRoot,
152+
wantConvertErr: true,
153+
},
130154
},
131155
}
132156

@@ -147,86 +171,10 @@ func TestGlideConfig_Convert(t *testing.T) {
147171
g.lock = testCase.lock
148172
}
149173

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

0 commit comments

Comments
 (0)