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

Commit

Permalink
status: make project status compatible with collectConstraints
Browse files Browse the repository at this point in the history
  • Loading branch information
darkowlzz committed Nov 29, 2017
1 parent 7de341c commit fea4256
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
51 changes: 42 additions & 9 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
21 changes: 14 additions & 7 deletions cmd/dep/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"},
Expand All @@ -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
Expand Down Expand Up @@ -472,7 +479,7 @@ UPSTREAM VERSION EXISTS: yes`,
wantString: `
PROJECT:
VERSION:
CONSTRAINTS: []
CONSTRAINTS:
SOURCE:
ALT SOURCE:
PUB VERSION:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit fea4256

Please sign in to comment.