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

Commit

Permalink
status: Add projectConstraint & constraintsCollection
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
darkowlzz committed Nov 12, 2017
1 parent 50cac16 commit 8d2d1cd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
22 changes: 18 additions & 4 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana
} else {
bs.Constraint = gps.Any()
for _, c := range cm[bs.ProjectRoot] {
bs.Constraint = c.Intersect(bs.Constraint)
bs.Constraint = c.Constraint.Intersect(bs.Constraint)
}
}

Expand Down Expand Up @@ -643,9 +643,20 @@ func formatVersion(v gps.Version) string {
return v.String()
}

// projectConstraint stores ProjectRoot and Constraint for that project.
type projectConstraint struct {
Project gps.ProjectRoot
Constraint gps.Constraint
}

// constraintsCollection is a map of ProjectRoot(dependency) and a collection of
// projectConstraint for the dependencies. This can be used to find constraints
// on a dependency and the projects that apply those constraints.
type constraintsCollection map[string][]projectConstraint

// collectConstraints collects constraints declared by all the dependencies.
func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) map[string][]gps.Constraint {
constraintCollection := make(map[string][]gps.Constraint)
func collectConstraints(ctx *dep.Ctx, p *dep.Project, sm gps.SourceManager) constraintsCollection {
constraintCollection := make(constraintsCollection)

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

Expand Down
25 changes: 17 additions & 8 deletions cmd/dep/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func TestCollectConstraints(t *testing.T) {
cases := []struct {
name string
project dep.Project
wantConstraints map[string][]gps.Constraint
wantConstraints constraintsCollection
}{
{
name: "without any constraints",
Expand All @@ -315,7 +315,7 @@ func TestCollectConstraints(t *testing.T) {
},
},
},
wantConstraints: map[string][]gps.Constraint{},
wantConstraints: constraintsCollection{},
},
{
name: "with multiple constraints",
Expand All @@ -340,10 +340,17 @@ func TestCollectConstraints(t *testing.T) {
},
},
},
wantConstraints: map[string][]gps.Constraint{
"github.com/sdboyer/deptest": []gps.Constraint{ver1, ver08},
"github.com/sdboyer/deptestdos": []gps.Constraint{ver2},
"github.com/sdboyer/dep-test": []gps.Constraint{ver1},
wantConstraints: constraintsCollection{
"github.com/sdboyer/deptest": []projectConstraint{
{"github.com/darkowlzz/deptest-project-1", ver1},
{"github.com/darkowlzz/deptest-project-2", ver08},
},
"github.com/sdboyer/deptestdos": []projectConstraint{
{"github.com/darkowlzz/deptest-project-2", ver2},
},
"github.com/sdboyer/dep-test": []projectConstraint{
{"github.com/darkowlzz/deptest-project-2", ver1},
},
},
},
{
Expand All @@ -364,8 +371,10 @@ func TestCollectConstraints(t *testing.T) {
},
},
},
wantConstraints: map[string][]gps.Constraint{
"github.com/sdboyer/deptest": []gps.Constraint{ver1},
wantConstraints: constraintsCollection{
"github.com/sdboyer/deptest": []projectConstraint{
{"github.com/darkowlzz/deptest-project-1", ver1},
},
},
},
}
Expand Down

0 comments on commit 8d2d1cd

Please sign in to comment.