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

Commit 6d355e0

Browse files
authored
Merge pull request #653 from jmank88/mkbridge
de-globalize mkBridge function var
2 parents 8021755 + b91b9c9 commit 6d355e0

File tree

5 files changed

+27
-34
lines changed

5 files changed

+27
-34
lines changed

internal/gps/bridge.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ type bridge struct {
7575
down bool
7676
}
7777

78-
// Global factory func to create a bridge. This exists solely to allow tests to
79-
// override it with a custom bridge and sm.
80-
var mkBridge = func(s *solver, sm SourceManager, down bool) sourceBridge {
78+
// mkBridge creates a bridge
79+
func mkBridge(s *solver, sm SourceManager, down bool) *bridge {
8180
return &bridge{
8281
sm: sm,
8382
s: s,

internal/gps/hash_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestHashInputs(t *testing.T) {
2222
Manifest: fix.rootmanifest(),
2323
ProjectAnalyzer: naiveAnalyzer{},
2424
stdLibFn: func(string) bool { return false },
25+
mkBridgeFn: overrideMkBridge,
2526
}
2627

2728
s, err := Prepare(params, newdepspecSM(fix.ds, nil))
@@ -74,6 +75,7 @@ func TestHashInputsReqsIgs(t *testing.T) {
7475
Manifest: rm,
7576
ProjectAnalyzer: naiveAnalyzer{},
7677
stdLibFn: func(string) bool { return false },
78+
mkBridgeFn: overrideMkBridge,
7779
}
7880

7981
s, err := Prepare(params, newdepspecSM(fix.ds, nil))
@@ -204,6 +206,7 @@ func TestHashInputsOverrides(t *testing.T) {
204206
Manifest: rm,
205207
ProjectAnalyzer: naiveAnalyzer{},
206208
stdLibFn: func(string) bool { return false },
209+
mkBridgeFn: overrideMkBridge,
207210
}
208211

209212
table := []struct {

internal/gps/rootdata_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestRootdataExternalImports(t *testing.T) {
1818
Manifest: fix.rootmanifest(),
1919
ProjectAnalyzer: naiveAnalyzer{},
2020
stdLibFn: func(string) bool { return false },
21+
mkBridgeFn: overrideMkBridge,
2122
}
2223

2324
is, err := Prepare(params, newdepspecSM(fix.ds, nil))
@@ -72,6 +73,7 @@ func TestGetApplicableConstraints(t *testing.T) {
7273
Manifest: fix.rootmanifest(),
7374
ProjectAnalyzer: naiveAnalyzer{},
7475
stdLibFn: func(string) bool { return false },
76+
mkBridgeFn: overrideMkBridge,
7577
}
7678

7779
is, err := Prepare(params, newdepspecSM(fix.ds, nil))

internal/gps/solve_test.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,12 @@ var fixtorun string
2626
// TODO(sdboyer) regression test ensuring that locks with only revs for projects don't cause errors
2727
func init() {
2828
flag.StringVar(&fixtorun, "gps.fix", "", "A single fixture to run in TestBasicSolves or TestBimodalSolves")
29-
mkBridge(nil, nil, false)
30-
overrideMkBridge()
3129
}
3230

33-
// sets the mkBridge global func to one that allows virtualized RootDirs
34-
func overrideMkBridge() {
35-
// For all tests, override the base bridge with the depspecBridge that skips
36-
// verifyRootDir calls
37-
mkBridge = func(s *solver, sm SourceManager, down bool) sourceBridge {
38-
return &depspecBridge{
39-
&bridge{
40-
sm: sm,
41-
s: s,
42-
down: down,
43-
vlists: make(map[ProjectIdentifier][]Version),
44-
},
45-
}
46-
}
31+
// overrideMkBridge overrides the base bridge with the depspecBridge that skips
32+
// verifyRootDir calls
33+
func overrideMkBridge(s *solver, sm SourceManager, down bool) sourceBridge {
34+
return &depspecBridge{mkBridge(s, sm, down)}
4735
}
4836

4937
type testlogger struct {
@@ -73,6 +61,7 @@ func fixSolve(params SolveParameters, sm SourceManager, t *testing.T) (Solution,
7361
// always return false, otherwise it would identify pretty much all of
7462
// our fixtures as being stdlib and skip everything
7563
params.stdLibFn = func(string) bool { return false }
64+
params.mkBridgeFn = overrideMkBridge
7665
s, err := Prepare(params, sm)
7766
if err != nil {
7867
return nil, err
@@ -312,7 +301,9 @@ func TestBadSolveOpts(t *testing.T) {
312301
fix.ds[0].n = ProjectRoot(pn)
313302

314303
sm := newdepspecSM(fix.ds, nil)
315-
params := SolveParameters{}
304+
params := SolveParameters{
305+
mkBridgeFn: overrideMkBridge,
306+
}
316307

317308
_, err := Prepare(params, nil)
318309
if err == nil {
@@ -430,14 +421,7 @@ func TestBadSolveOpts(t *testing.T) {
430421

431422
// swap out the test mkBridge override temporarily, just to make sure we get
432423
// the right error
433-
mkBridge = func(s *solver, sm SourceManager, down bool) sourceBridge {
434-
return &bridge{
435-
sm: sm,
436-
s: s,
437-
down: down,
438-
vlists: make(map[ProjectIdentifier][]Version),
439-
}
440-
}
424+
params.mkBridgeFn = nil
441425

442426
_, err = Prepare(params, sm)
443427
if err == nil {
@@ -454,7 +438,4 @@ func TestBadSolveOpts(t *testing.T) {
454438
} else if !strings.Contains(err.Error(), "is a file, not a directory") {
455439
t.Error("Prepare should have given error on file as RootDir, but gave:", err)
456440
}
457-
458-
// swap them back...not sure if this matters, but just in case
459-
overrideMkBridge()
460441
}

internal/gps/solver.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ type SolveParameters struct {
9696
// stdLibFn is the function to use to recognize standard library import paths.
9797
// Only overridden for tests. Defaults to paths.IsStandardImportPath if nil.
9898
stdLibFn func(string) bool
99+
100+
// mkBridgeFn is the function to use to create sourceBridges.
101+
// Only overridden for tests (so we can run with virtual RootDir).
102+
// Defaults to mkBridge if nil.
103+
mkBridgeFn func(*solver, SourceManager, bool) sourceBridge
99104
}
100105

101106
// solver is a CDCL-style constraint solver with satisfiability conditions
@@ -285,9 +290,12 @@ func Prepare(params SolveParameters, sm SourceManager) (Solver, error) {
285290
}
286291

287292
// Set up the bridge and ensure the root dir is in good, working order
288-
// before doing anything else. (This call is stubbed out in tests, via
289-
// overriding mkBridge(), so we can run with virtual RootDir.)
290-
s.b = mkBridge(s, sm, params.Downgrade)
293+
// before doing anything else.
294+
if params.mkBridgeFn == nil {
295+
s.b = mkBridge(s, sm, params.Downgrade)
296+
} else {
297+
s.b = params.mkBridgeFn(s, sm, params.Downgrade)
298+
}
291299
err = s.b.verifyRootDir(params.RootDir)
292300
if err != nil {
293301
return nil, err

0 commit comments

Comments
 (0)