From b51249c3cca00438fb6d00584ae2a7e7d7e0eb61 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 6 Aug 2017 23:19:18 +0530 Subject: [PATCH 1/5] fix(status): add constraint for locked projects Use project lock to get the constraints of projects not in manifest. --- cmd/dep/status.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index a1f9a0a8c1..656ec4e1b1 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -470,7 +470,12 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana if bs.Version != nil && bs.Version.Type() != gps.IsVersion { c, has := p.Manifest.Constraints[proj.Ident().ProjectRoot] if !has { - c.Constraint = gps.Any() + // Get constraint for locked project + for _, lockedP := range p.Lock.P { + if lockedP.Ident().ProjectRoot == proj.Ident().ProjectRoot { + c.Constraint = lockedP.Version() + } + } } // TODO: This constraint is only the constraint imposed by the // current project, not by any transitive deps. As a result, From 99f6de7d11e37d403364f96b0bc981d2b723b6d1 Mon Sep 17 00:00:00 2001 From: Sunny Date: Tue, 8 Aug 2017 18:36:43 +0530 Subject: [PATCH 2/5] fix(status): implement collectConstraints() --- cmd/dep/status.go | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 656ec4e1b1..b5ecfe24bc 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -19,7 +19,6 @@ import ( "github.com/golang/dep" "github.com/golang/dep/gps" "github.com/golang/dep/gps/paths" - "github.com/golang/dep/gps/pkgtree" "github.com/pkg/errors" ) @@ -389,7 +388,7 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana return false, 0, errors.Wrapf(err, "could not set up solver for input hashing") } - cm := collectConstraints(ptree, p, sm) + cm := collectConstraints(ctx, p, sm) // Get the project list and sort it so that the printed output users see is // deterministically ordered. (This may be superfluous if the lock is always @@ -644,7 +643,35 @@ func formatVersion(v gps.Version) string { return v.String() } -func collectConstraints(ptree pkgtree.PackageTree, p *dep.Project, sm gps.SourceManager) map[string][]gps.Constraint { - // TODO - return map[string][]gps.Constraint{} +// 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) + + // Get direct deps of the root project. + _, directDeps, err := getDirectDependencies(sm, p) + if err != nil { + ctx.Err.Println("Error getting direct deps:", err) + } + // Create a root analyzer. + rootAnalyzer := newRootAnalyzer(false, ctx, directDeps, sm) + + // Iterate through the locked projects and collect constraints of all the projects. + for _, proj := range p.Lock.Projects() { + manifest, _, err := sm.GetManifestAndLock(proj.Ident(), proj.Version(), rootAnalyzer) + if err != nil { + ctx.Err.Println("Error getting manifest and lock:", err) + continue + } + + // Get project constraints. + pc := manifest.DependencyConstraints() + + // 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) + } + } + + return constraintCollection } From 62b077caff52d00f84c30bf09458fa9eef283c7d Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 29 Oct 2017 17:36:49 +0530 Subject: [PATCH 3/5] test(status): fix failing tests due to constraints from lock --- cmd/dep/testdata/harness_tests/status/case1/json/stdout.txt | 2 +- .../testdata/harness_tests/status/case1/table/stdout.txt | 2 +- .../harness_tests/status/override_constraint/stdout.txt | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/dep/testdata/harness_tests/status/case1/json/stdout.txt b/cmd/dep/testdata/harness_tests/status/case1/json/stdout.txt index e995757921..381f3246d5 100644 --- a/cmd/dep/testdata/harness_tests/status/case1/json/stdout.txt +++ b/cmd/dep/testdata/harness_tests/status/case1/json/stdout.txt @@ -1 +1 @@ -[{"ProjectRoot":"github.com/sdboyer/deptest","Constraint":"^0.8.0","Version":"v0.8.0","Revision":"ff2948a2ac8f538c4ecd55962e919d1e13e74baf","Latest":"3f4c3bea144e112a69bbe5d8d01c1b09a544253f","PackageCount":1},{"ProjectRoot":"github.com/sdboyer/deptestdos","Constraint":"*","Version":"v2.0.0","Revision":"5c607206be5decd28e6263ffffdcee067266015e","Latest":"5c607206be5decd28e6263ffffdcee067266015e","PackageCount":1}] +[{"ProjectRoot":"github.com/sdboyer/deptest","Constraint":"^0.8.0","Version":"v0.8.0","Revision":"ff2948a2ac8f538c4ecd55962e919d1e13e74baf","Latest":"3f4c3bea144e112a69bbe5d8d01c1b09a544253f","PackageCount":1},{"ProjectRoot":"github.com/sdboyer/deptestdos","Constraint":"v2.0.0","Version":"v2.0.0","Revision":"5c607206be5decd28e6263ffffdcee067266015e","Latest":"5c607206be5decd28e6263ffffdcee067266015e","PackageCount":1}] diff --git a/cmd/dep/testdata/harness_tests/status/case1/table/stdout.txt b/cmd/dep/testdata/harness_tests/status/case1/table/stdout.txt index f77f65a927..dbe1b29360 100644 --- a/cmd/dep/testdata/harness_tests/status/case1/table/stdout.txt +++ b/cmd/dep/testdata/harness_tests/status/case1/table/stdout.txt @@ -1,3 +1,3 @@ PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED github.com/sdboyer/deptest ^0.8.0 v0.8.0 ff2948a 3f4c3be 1 -github.com/sdboyer/deptestdos * v2.0.0 5c60720 5c60720 1 +github.com/sdboyer/deptestdos v2.0.0 v2.0.0 5c60720 5c60720 1 diff --git a/cmd/dep/testdata/harness_tests/status/override_constraint/stdout.txt b/cmd/dep/testdata/harness_tests/status/override_constraint/stdout.txt index e3df72b63d..202f122e20 100644 --- a/cmd/dep/testdata/harness_tests/status/override_constraint/stdout.txt +++ b/cmd/dep/testdata/harness_tests/status/override_constraint/stdout.txt @@ -1,3 +1,3 @@ -PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED -github.com/sdboyer/deptest * (override) v0.8.1 3f4c3be ff2948a 1 -github.com/sdboyer/deptestdos * v2.0.0 5c60720 5c60720 1 +PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED +github.com/sdboyer/deptest v0.8.1 (override) v0.8.1 3f4c3be 3f4c3be 1 +github.com/sdboyer/deptestdos v2.0.0 v2.0.0 5c60720 5c60720 1 From df694ee91650ae44a9592955f4cc3aaa3919fc42 Mon Sep 17 00:00:00 2001 From: Sunny Date: Thu, 2 Nov 2017 20:10:28 +0530 Subject: [PATCH 4/5] test(status): Add TestCollectConstraints --- cmd/dep/status_test.go | 110 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index 1558343a6e..b88ca4fe74 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -6,6 +6,9 @@ package main import ( "bytes" + "io/ioutil" + "log" + "reflect" "testing" "text/tabwriter" @@ -13,6 +16,7 @@ import ( "github.com/golang/dep" "github.com/golang/dep/gps" + "github.com/golang/dep/internal/test" ) func TestStatusFormatVersion(t *testing.T) { @@ -287,3 +291,109 @@ func TestBasicStatusGetConsolidatedLatest(t *testing.T) { }) } } + +func TestCollectConstraints(t *testing.T) { + ver1, _ := gps.NewSemverConstraintIC("v1.0.0") + ver08, _ := gps.NewSemverConstraintIC("v0.8.0") + ver2, _ := gps.NewSemverConstraintIC("v2.0.0") + + cases := []struct { + name string + project dep.Project + wantConstraints map[string][]gps.Constraint + }{ + { + name: "without any constraints", + project: dep.Project{ + Lock: &dep.Lock{ + P: []gps.LockedProject{ + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")}, + gps.NewVersion("v1.0.0"), + []string{"."}, + ), + }, + }, + }, + wantConstraints: map[string][]gps.Constraint{}, + }, + { + name: "with multiple constraints", + project: dep.Project{ + Lock: &dep.Lock{ + P: []gps.LockedProject{ + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/sdboyer/deptest")}, + gps.NewVersion("v1.0.0"), + []string{"."}, + ), + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")}, + gps.NewVersion("v0.1.0"), + []string{"."}, + ), + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")}, + gps.NewBranch("master").Pair(gps.Revision("824a8d56a4c6b2f4718824a98cd6d70d3dbd4c3e")), + []string{"."}, + ), + }, + }, + }, + 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}, + }, + }, + { + name: "skip projects with invalid versions", + project: dep.Project{ + Lock: &dep.Lock{ + P: []gps.LockedProject{ + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-1")}, + gps.NewVersion("v0.1.0"), + []string{"."}, + ), + gps.NewLockedProject( + gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/darkowlzz/deptest-project-2")}, + gps.NewVersion("v1.0.0"), + []string{"."}, + ), + }, + }, + }, + wantConstraints: map[string][]gps.Constraint{ + "github.com/sdboyer/deptest": []gps.Constraint{ver1}, + }, + }, + } + + h := test.NewHelper(t) + defer h.Cleanup() + + h.TempDir("src") + pwd := h.Path(".") + discardLogger := log.New(ioutil.Discard, "", 0) + + ctx := &dep.Ctx{ + GOPATH: pwd, + Out: discardLogger, + Err: discardLogger, + } + + sm, err := ctx.SourceManager() + h.Must(err) + defer sm.Release() + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + gotConstraints := collectConstraints(ctx, &c.project, sm) + + if !reflect.DeepEqual(gotConstraints, c.wantConstraints) { + t.Fatalf("Unexpected collected constraints: \n\t(GOT): %v\n\t(WNT): %v", gotConstraints, c.wantConstraints) + } + }) + } +} From 1814fc330681b2128f797c131caed1803ddb5ea3 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 12 Nov 2017 18:53:32 +0530 Subject: [PATCH 5/5] 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. --- cmd/dep/status.go | 22 ++++++++++++++++++---- cmd/dep/status_test.go | 25 +++++++++++++++++-------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index b5ecfe24bc..a43c38f8c3 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -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) } } @@ -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) @@ -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}, + ) } } diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index b88ca4fe74..6f0eb3f4d0 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -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", @@ -315,7 +315,7 @@ func TestCollectConstraints(t *testing.T) { }, }, }, - wantConstraints: map[string][]gps.Constraint{}, + wantConstraints: constraintsCollection{}, }, { name: "with multiple constraints", @@ -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}, + }, }, }, { @@ -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}, + }, }, }, }