forked from golang/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve_test.go
259 lines (223 loc) · 7.23 KB
/
solve_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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gps
import (
"bytes"
"context"
"fmt"
"log"
"reflect"
"sort"
"testing"
"github.com/golang/dep/internal/test"
)
// overrideMkBridge overrides the base bridge with the depspecBridge that skips
// verifyRootDir calls
func overrideMkBridge(s *solver, sm SourceManager, down bool) sourceBridge {
return &depspecBridge{mkBridge(s, sm, down)}
}
func fixSolve(params SolveParameters, sm SourceManager, t *testing.T) (Solution, error) {
// Trace unconditionally; by passing the trace through t.Log(), the testing
// system will decide whether or not to actually show the output (based on
// -v, or selectively on test failure).
params.TraceLogger = log.New(test.Writer{TB: t}, "", 0)
// always return false, otherwise it would identify pretty much all of
// our fixtures as being stdlib and skip everything
params.stdLibFn = func(string) bool { return false }
params.mkBridgeFn = overrideMkBridge
s, err := Prepare(params, sm)
if err != nil {
return nil, err
}
return s.Solve(context.Background())
}
// Test all the basic table fixtures.
//
// Or, just the one named in the fix arg.
func TestBasicSolves(t *testing.T) {
// sort them by their keys so we get stable output
names := make([]string, 0, len(basicFixtures))
for n := range basicFixtures {
names = append(names, n)
}
sort.Strings(names)
for _, n := range names {
n := n
t.Run(n, func(t *testing.T) {
t.Parallel()
solveBasicsAndCheck(basicFixtures[n], t)
})
}
}
func solveBasicsAndCheck(fix basicFixture, t *testing.T) (res Solution, err error) {
sm := newdepspecSM(fix.ds, nil)
if fix.broken != "" {
t.Skip(fix.broken)
}
params := SolveParameters{
RootDir: string(fix.ds[0].n),
RootPackageTree: fix.rootTree(),
Manifest: fix.rootmanifest(),
Lock: dummyLock{},
Downgrade: fix.downgrade,
ChangeAll: fix.changeall,
ToChange: fix.changelist,
ProjectAnalyzer: naiveAnalyzer{},
}
if fix.l != nil {
params.Lock = fix.l
}
res, err = fixSolve(params, sm, t)
return fixtureSolveSimpleChecks(fix, res, err, t)
}
// Test all the bimodal table fixtures.
//
// Or, just the one named in the fix arg.
func TestBimodalSolves(t *testing.T) {
// sort them by their keys so we get stable output
names := make([]string, 0, len(bimodalFixtures))
for n := range bimodalFixtures {
names = append(names, n)
}
sort.Strings(names)
for _, n := range names {
n := n
t.Run(n, func(t *testing.T) {
t.Parallel()
solveBimodalAndCheck(bimodalFixtures[n], t)
})
}
}
func solveBimodalAndCheck(fix bimodalFixture, t *testing.T) (res Solution, err error) {
sm := newbmSM(fix)
if fix.broken != "" {
t.Skip(fix.broken)
}
params := SolveParameters{
RootDir: string(fix.ds[0].n),
RootPackageTree: fix.rootTree(),
Manifest: fix.rootmanifest(),
Lock: dummyLock{},
Downgrade: fix.downgrade,
ChangeAll: fix.changeall,
ProjectAnalyzer: naiveAnalyzer{},
}
if fix.l != nil {
params.Lock = fix.l
}
res, err = fixSolve(params, sm, t)
return fixtureSolveSimpleChecks(fix, res, err, t)
}
func fixtureSolveSimpleChecks(fix specfix, soln Solution, err error, t *testing.T) (Solution, error) {
ppi := func(id ProjectIdentifier) string {
// need this so we can clearly tell if there's a Source or not
if id.Source == "" {
return string(id.ProjectRoot)
}
return fmt.Sprintf("%s (from %s)", id.ProjectRoot, id.Source)
}
pv := func(v Version) string {
if pv, ok := v.(PairedVersion); ok {
return fmt.Sprintf("%s (%s)", pv.Unpair(), pv.Revision())
}
return v.String()
}
fixfail := fix.failure()
if err != nil {
if fixfail == nil {
t.Errorf("Solve failed unexpectedly:\n%s", err)
} else if !(fixfail.Error() == err.Error()) {
// TODO(sdboyer) reflect.DeepEqual works for now, but once we start
// modeling more complex cases, this should probably become more robust
t.Errorf("Failure mismatch:\n\t(GOT): %s\n\t(WNT): %s", err, fixfail)
}
} else if fixfail != nil {
var buf bytes.Buffer
fmt.Fprintf(&buf, "Solver succeeded, but expecting failure:\n%s\nProjects in solution:", fixfail)
for _, p := range soln.Projects() {
fmt.Fprintf(&buf, "\n\t- %s at %s", ppi(p.Ident()), p.Version())
}
t.Error(buf.String())
} else {
r := soln.(solution)
if fix.maxTries() > 0 && r.Attempts() > fix.maxTries() {
t.Errorf("Solver completed in %v attempts, but expected %v or fewer", r.att, fix.maxTries())
}
// Dump result projects into a map for easier interrogation
rp := make(map[ProjectIdentifier]LockedProject)
for _, lp := range r.p {
rp[lp.pi] = lp
}
fixlen, rlen := len(fix.solution()), len(rp)
if fixlen != rlen {
// Different length, so they definitely disagree
t.Errorf("Solver reported %v package results, result expected %v", rlen, fixlen)
}
// Whether or not len is same, still have to verify that results agree
// Walk through fixture/expected results first
for id, flp := range fix.solution() {
if lp, exists := rp[id]; !exists {
t.Errorf("Project %q expected but missing from results", ppi(id))
} else {
// delete result from map so we skip it on the reverse pass
delete(rp, id)
if flp.Version() != lp.Version() {
t.Errorf("Expected version %q of project %q, but actual version was %q", pv(flp.Version()), ppi(id), pv(lp.Version()))
}
if !reflect.DeepEqual(lp.pkgs, flp.pkgs) {
t.Errorf("Package list was not not as expected for project %s@%s:\n\t(GOT) %s\n\t(WNT) %s", ppi(id), pv(lp.Version()), lp.pkgs, flp.pkgs)
}
}
}
// Now walk through remaining actual results
for id, lp := range rp {
if _, exists := fix.solution()[id]; !exists {
t.Errorf("Unexpected project %s@%s present in results, with pkgs:\n\t%s", ppi(id), pv(lp.Version()), lp.pkgs)
}
}
}
return soln, err
}
// This tests that, when a root lock is underspecified (has only a version) we
// don't allow a match on that version from a rev in the manifest. We may allow
// this in the future, but disallow it for now because going from an immutable
// requirement to a mutable lock automagically is a bad direction that could
// produce weird side effects.
func TestRootLockNoVersionPairMatching(t *testing.T) {
fix := basicFixture{
n: "does not match unpaired lock versions with paired real versions",
ds: []depspec{
mkDepspec("root 0.0.0", "foo *"), // foo's constraint rewritten below to foorev
mkDepspec("foo 1.0.0", "bar 1.0.0"),
mkDepspec("foo 1.0.1 foorev", "bar 1.0.1"),
mkDepspec("foo 1.0.2 foorev", "bar 1.0.2"),
mkDepspec("bar 1.0.0"),
mkDepspec("bar 1.0.1"),
mkDepspec("bar 1.0.2"),
},
l: mklock(
"foo 1.0.1",
),
r: mksolution(
"foo 1.0.2 foorev",
"bar 1.0.2",
),
}
pd := fix.ds[0].deps[0]
pd.Constraint = Revision("foorev")
fix.ds[0].deps[0] = pd
sm := newdepspecSM(fix.ds, nil)
l2 := make(fixLock, 1)
copy(l2, fix.l)
l2[0].v = nil
params := SolveParameters{
RootDir: string(fix.ds[0].n),
RootPackageTree: fix.rootTree(),
Manifest: fix.rootmanifest(),
Lock: l2,
ProjectAnalyzer: naiveAnalyzer{},
}
res, err := fixSolve(params, sm, t)
fixtureSolveSimpleChecks(fix, res, err, t)
}