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

Commit 1814fc3

Browse files
committed
status: Add projectConstraint & constraintsCollection
The resultant collection obtained from collectConstraints() should contain data about which project set a particular constraint on a dependency project. `projectConstraint` consists of the project and the constraint it sets on a given dependency. And `constraintsCollection` is a map to store the pair of dependency project and a collection of `projectConstraint` for different constraints. This would be used by project argument status (status for a single project), to show the importers of the project.
1 parent df694ee commit 1814fc3

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

cmd/dep/status.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana
460460
} else {
461461
bs.Constraint = gps.Any()
462462
for _, c := range cm[bs.ProjectRoot] {
463-
bs.Constraint = c.Intersect(bs.Constraint)
463+
bs.Constraint = c.Constraint.Intersect(bs.Constraint)
464464
}
465465
}
466466

@@ -643,9 +643,20 @@ func formatVersion(v gps.Version) string {
643643
return v.String()
644644
}
645645

646+
// projectConstraint stores ProjectRoot and Constraint for that project.
647+
type projectConstraint struct {
648+
Project gps.ProjectRoot
649+
Constraint gps.Constraint
650+
}
651+
652+
// constraintsCollection is a map of ProjectRoot(dependency) and a collection of
653+
// projectConstraint for the dependencies. This can be used to find constraints
654+
// on a dependency and the projects that apply those constraints.
655+
type constraintsCollection map[string][]projectConstraint
656+
646657
// collectConstraints collects constraints declared by all the dependencies.
647-
func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) map[string][]gps.Constraint {
648-
constraintCollection := make(map[string][]gps.Constraint)
658+
func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) constraintsCollection {
659+
constraintCollection := make(constraintsCollection)
649660

650661
// Get direct deps of the root project.
651662
_, directDeps, err := getDirectDependencies(sm, p)
@@ -669,7 +680,10 @@ func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) map[
669680
// Iterate through the project constraints to get individual dependency
670681
// project and constraint values.
671682
for pr, pp := range pc {
672-
constraintCollection[string(pr)] = append(constraintCollection[string(pr)], pp.Constraint)
683+
constraintCollection[string(pr)] = append(
684+
constraintCollection[string(pr)],
685+
projectConstraint{proj.Ident().ProjectRoot, pp.Constraint},
686+
)
673687
}
674688
}
675689

cmd/dep/status_test.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func TestCollectConstraints(t *testing.T) {
300300
cases := []struct {
301301
name string
302302
project dep.Project
303-
wantConstraints map[string][]gps.Constraint
303+
wantConstraints constraintsCollection
304304
}{
305305
{
306306
name: "without any constraints",
@@ -315,7 +315,7 @@ func TestCollectConstraints(t *testing.T) {
315315
},
316316
},
317317
},
318-
wantConstraints: map[string][]gps.Constraint{},
318+
wantConstraints: constraintsCollection{},
319319
},
320320
{
321321
name: "with multiple constraints",
@@ -340,10 +340,17 @@ func TestCollectConstraints(t *testing.T) {
340340
},
341341
},
342342
},
343-
wantConstraints: map[string][]gps.Constraint{
344-
"github.com/sdboyer/deptest": []gps.Constraint{ver1, ver08},
345-
"github.com/sdboyer/deptestdos": []gps.Constraint{ver2},
346-
"github.com/sdboyer/dep-test": []gps.Constraint{ver1},
343+
wantConstraints: constraintsCollection{
344+
"github.com/sdboyer/deptest": []projectConstraint{
345+
{"github.com/darkowlzz/deptest-project-1", ver1},
346+
{"github.com/darkowlzz/deptest-project-2", ver08},
347+
},
348+
"github.com/sdboyer/deptestdos": []projectConstraint{
349+
{"github.com/darkowlzz/deptest-project-2", ver2},
350+
},
351+
"github.com/sdboyer/dep-test": []projectConstraint{
352+
{"github.com/darkowlzz/deptest-project-2", ver1},
353+
},
347354
},
348355
},
349356
{
@@ -364,8 +371,10 @@ func TestCollectConstraints(t *testing.T) {
364371
},
365372
},
366373
},
367-
wantConstraints: map[string][]gps.Constraint{
368-
"github.com/sdboyer/deptest": []gps.Constraint{ver1},
374+
wantConstraints: constraintsCollection{
375+
"github.com/sdboyer/deptest": []projectConstraint{
376+
{"github.com/darkowlzz/deptest-project-1", ver1},
377+
},
369378
},
370379
},
371380
}

0 commit comments

Comments
 (0)