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

Commit 1550da3

Browse files
authored
Merge pull request #1869 from tinnefeld/issue-1452-collect-constraints-from-root-project
CollectConstraints from root project as well
2 parents 902d09c + 88d9067 commit 1550da3

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

cmd/dep/status.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,9 +984,9 @@ type projectConstraint struct {
984984
// on a dependency and the projects that apply those constraints.
985985
type constraintsCollection map[string][]projectConstraint
986986

987-
// collectConstraints collects constraints declared by all the dependencies.
988-
// It returns constraintsCollection and a slice of errors encountered while
989-
// collecting the constraints, if any.
987+
// collectConstraints collects constraints declared by all the dependencies and
988+
// constraints from the root project. It returns constraintsCollection and
989+
// a slice of errors encountered while collecting the constraints, if any.
990990
func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (constraintsCollection, []error) {
991991
logger := ctx.Err
992992
if !ctx.Verbose {
@@ -1068,6 +1068,29 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) (con
10681068
}
10691069
}
10701070

1071+
// Incorporate constraints set in the manifest of the root project.
1072+
if p.Manifest != nil {
1073+
1074+
// Iterate through constraints in the manifest, append if it is a
1075+
// direct dependency
1076+
for pr, pp := range p.Manifest.Constraints {
1077+
if _, ok := directDeps[pr]; !ok {
1078+
continue
1079+
}
1080+
1081+
// Mark constraints coming from the manifest as "root"
1082+
tempCC := append(
1083+
constraintCollection[string(pr)],
1084+
projectConstraint{"root", pp.Constraint},
1085+
)
1086+
1087+
// Sort the inner projectConstraint slice by Project string.
1088+
// Required for consistent returned value.
1089+
sort.Sort(byProject(tempCC))
1090+
constraintCollection[string(pr)] = tempCC
1091+
}
1092+
}
1093+
10711094
return constraintCollection, errs
10721095
}
10731096

cmd/dep/status_test.go

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ func TestCollectConstraints(t *testing.T) {
351351
cases := []struct {
352352
name string
353353
lock dep.Lock
354+
manifest dep.Manifest
354355
wantConstraints constraintsCollection
355356
wantErr bool
356357
}{
@@ -368,7 +369,7 @@ func TestCollectConstraints(t *testing.T) {
368369
wantConstraints: constraintsCollection{},
369370
},
370371
{
371-
name: "with multiple constraints",
372+
name: "with multiple constraints from dependencies",
372373
lock: dep.Lock{
373374
P: []gps.LockedProject{
374375
gps.NewLockedProject(
@@ -401,6 +402,53 @@ func TestCollectConstraints(t *testing.T) {
401402
},
402403
},
403404
},
405+
{
406+
name: "with multiple constraints from dependencies and root project",
407+
lock: dep.Lock{
408+
P: []gps.LockedProject{
409+
gps.NewLockedProject(
410+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
411+
gps.NewVersion("v1.0.0"),
412+
[]string{"."},
413+
),
414+
gps.NewLockedProject(
415+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")},
416+
gps.NewVersion("v0.1.0"),
417+
[]string{"."},
418+
),
419+
gps.NewLockedProject(
420+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")},
421+
gps.NewBranch("master").Pair(gps.Revision("824a8d56a4c6b2f4718824a98cd6d70d3dbd4c3e")),
422+
[]string{"."},
423+
),
424+
},
425+
},
426+
manifest: dep.Manifest{
427+
Constraints: map[gps.ProjectRoot]gps.ProjectProperties{
428+
gps.ProjectRoot("github.com/sdboyer/deptest"): {
429+
Constraint: gps.Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f"),
430+
},
431+
},
432+
Ovr: make(gps.ProjectConstraints),
433+
PruneOptions: gps.CascadingPruneOptions{
434+
DefaultOptions: gps.PruneNestedVendorDirs,
435+
PerProjectOptions: make(map[gps.ProjectRoot]gps.PruneOptionSet),
436+
},
437+
},
438+
wantConstraints: constraintsCollection{
439+
"github.com/sdboyer/deptestdos": []projectConstraint{
440+
{"github.com/darkowlzz/deptest-project-2", ver2},
441+
},
442+
"github.com/sdboyer/dep-test": []projectConstraint{
443+
{"github.com/darkowlzz/deptest-project-2", ver1},
444+
},
445+
"github.com/sdboyer/deptest": []projectConstraint{
446+
{"github.com/darkowlzz/deptest-project-1", ver1},
447+
{"github.com/darkowlzz/deptest-project-2", ver08},
448+
{"root", gps.Revision("3f4c3bea144e112a69bbe5d8d01c1b09a544253f")},
449+
},
450+
},
451+
},
404452
{
405453
name: "skip projects with invalid versions",
406454
lock: dep.Lock{
@@ -444,6 +492,31 @@ func TestCollectConstraints(t *testing.T) {
444492
},
445493
},
446494
},
495+
{
496+
name: "skip ineffective constraint from manifest",
497+
lock: dep.Lock{
498+
P: []gps.LockedProject{
499+
gps.NewLockedProject(
500+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")},
501+
gps.NewVersion("v1.0.0"),
502+
[]string{"."},
503+
),
504+
},
505+
},
506+
manifest: dep.Manifest{
507+
Constraints: map[gps.ProjectRoot]gps.ProjectProperties{
508+
gps.ProjectRoot("github.com/darkowlzz/deptest-project-1"): {
509+
Constraint: ver1,
510+
},
511+
},
512+
Ovr: make(gps.ProjectConstraints),
513+
PruneOptions: gps.CascadingPruneOptions{
514+
DefaultOptions: gps.PruneNestedVendorDirs,
515+
PerProjectOptions: make(map[gps.ProjectRoot]gps.PruneOptionSet),
516+
},
517+
},
518+
wantConstraints: constraintsCollection{},
519+
},
447520
}
448521

449522
h := test.NewHelper(t)
@@ -474,6 +547,7 @@ func TestCollectConstraints(t *testing.T) {
474547
for _, c := range cases {
475548
t.Run(c.name, func(t *testing.T) {
476549
p.Lock = &c.lock
550+
p.Manifest = &c.manifest
477551
gotConstraints, err := collectConstraints(ctx, p, sm)
478552
if len(err) > 0 && !c.wantErr {
479553
t.Fatalf("unexpected errors while collecting constraints: %v", err)

0 commit comments

Comments
 (0)