From 8d5d393b4302a6cd00507b891598e69b5af22065 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 29 Nov 2017 16:23:12 +0530 Subject: [PATCH] status: make project status compatible with collectConstraints --- cmd/dep/status.go | 51 +++++++++++++++---- cmd/dep/status_test.go | 21 +++++--- .../status/project_status/stdout.txt | 2 +- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index f87bd43a90..d530e8ff25 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -23,6 +23,7 @@ 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" ) @@ -685,6 +686,10 @@ type projectConstraint struct { Constraint gps.Constraint } +func (pc projectConstraint) String() string { + return fmt.Sprintf("%s(%s)", pc.Constraint.String(), string(pc.Project)) +} + // 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. @@ -768,6 +773,7 @@ func (pv pubVersions) TabString() string { return buf.String() } +// projectImporters stores a map of project names that import a specific project. type projectImporters map[string]bool func (pi projectImporters) String() string { @@ -783,6 +789,7 @@ func (pi projectImporters) String() string { return strings.Join(projects, ", ") } +// packageImporters stores a map of package and projects that import them. type packageImporters map[string][]string func (pi packageImporters) TabString() string { @@ -827,10 +834,41 @@ func (pi packageImporters) TabString() string { return buf.String() } +// projectConstraints is a slice of projectConstraint +type projectConstraints []projectConstraint + +func (pcs projectConstraints) TabString() string { + var buf bytes.Buffer + w := bufio.NewWriter(&buf) + + // Create a list of constraints string to be printed and sort them. + var constList []string + for _, pc := range pcs { + constList = append(constList, pc.String()) + } + sort.Strings(constList) + + // Count lines and add newlines("\n") and tabs("\t"), compatible with + // tabwriter. + // ^0.5.0(btb.com/x/y)\n \t^1.0.0(gh.com/f/b)\t \t^1.5.0(gh.com/a/c) + count := 0 + for _, c := range constList { + count++ + if count > 1 { + fmt.Fprintf(w, "\n \t") + } + + fmt.Fprintf(w, "%s", c) + } + w.Flush() + + return buf.String() +} + type projectStatus struct { Project string Version string - Constraints []string + Constraints projectConstraints Source string AltSource string PubVersions pubVersions @@ -874,7 +912,7 @@ func (ps projectStatus) String() string { "PACKAGE IMPORTERS:\t%s\n"+ "UPSTREAM EXISTS:\t%s\n"+ "UPSTREAM VERSION EXISTS:\t%s", - ps.Project, ps.Version, ps.Constraints, ps.Source, ps.AltSource, + ps.Project, ps.Version, ps.Constraints.TabString(), ps.Source, ps.AltSource, ps.PubVersions.TabString(), ps.Revision, ps.LatestAllowed, ps.SourceType, strings.Join(ps.Packages, ", "), ps.ProjectImporters, ps.PackageImporters.TabString(), upstreamExists, upstreamVersionExists, @@ -906,13 +944,8 @@ func runProjectStatus(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.Source prs = append(prs, pr) } - // This ptree would not be required with the new collectConstraints() implementation. - ptree, err := p.ParseRootPackageTree() - if err != nil { - return err - } // Collect all the constraints. - cc := collectConstraints(ptree, p, sm) + cc := collectConstraints(ctx, p, sm) // Collect list of packages in target projects. pkgs := make(map[gps.ProjectRoot][]string) @@ -1060,7 +1093,7 @@ func runProjectStatus(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.Source // CONSTRAINTS constraints := cc[string(pr)] for _, c := range constraints { - projStatus.Constraints = append(projStatus.Constraints, c.String()) + projStatus.Constraints = append(projStatus.Constraints, c) } } } diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index e3996fb260..d68de6e1b1 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -408,6 +408,9 @@ func TestCollectConstraints(t *testing.T) { } func TestProjectStatusString(t *testing.T) { + ver1, _ := gps.NewSemverConstraintIC("v1.0.0") + ver05, _ := gps.NewSemverConstraintIC("v0.5.0") + testCases := []struct { name string ps projectStatus @@ -416,11 +419,14 @@ func TestProjectStatusString(t *testing.T) { { name: "basic projectStatus", ps: projectStatus{ - Project: "github.com/x/y", - Version: "v1.0", - Constraints: nil, - Source: "github.com/x/y", - AltSource: "https://github.com/z/y", + Project: "github.com/x/y", + Version: "v1.0", + Constraints: projectConstraints{ + {"gh.com/f/b", ver1}, + {"btb.com/x/y", ver05}, + }, + Source: "github.com/x/y", + AltSource: "https://github.com/z/y", PubVersions: pubVersions{ "semvers": []string{"v0.5", "v0.7", "v1.0", "v1.5"}, "branches": []string{"master", "dev"}, @@ -444,7 +450,8 @@ func TestProjectStatusString(t *testing.T) { wantString: ` PROJECT: github.com/x/y VERSION: v1.0 -CONSTRAINTS: [] +CONSTRAINTS: ^0.5.0(btb.com/x/y) + ^1.0.0(gh.com/f/b) SOURCE: github.com/x/y ALT SOURCE: https://github.com/z/y PUB VERSION: branches: dev, master @@ -472,7 +479,7 @@ UPSTREAM VERSION EXISTS: yes`, wantString: ` PROJECT: VERSION: -CONSTRAINTS: [] +CONSTRAINTS: SOURCE: ALT SOURCE: PUB VERSION: diff --git a/cmd/dep/testdata/harness_tests/status/project_status/stdout.txt b/cmd/dep/testdata/harness_tests/status/project_status/stdout.txt index 7dfe5a3ed8..34942c7449 100644 --- a/cmd/dep/testdata/harness_tests/status/project_status/stdout.txt +++ b/cmd/dep/testdata/harness_tests/status/project_status/stdout.txt @@ -1,7 +1,7 @@ PROJECT: github.com/sdboyer/deptest VERSION: v1.0.0 -CONSTRAINTS: [] +CONSTRAINTS: SOURCE: github.com/sdboyer/deptest ALT SOURCE: PUB VERSION: branches: master