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 Dec 24, 2017
1 parent c98bfea commit 99aeaae
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
58 changes: 49 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 @@ -747,6 +748,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 @@ -881,6 +886,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 @@ -896,6 +902,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 @@ -940,10 +947,37 @@ 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)

// Sort for consistent result.
sort.Sort(byProject(pcs))

// 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 pcs {
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 @@ -987,7 +1021,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 @@ -1019,13 +1053,19 @@ 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, ccerrs := collectConstraints(ctx, p, sm)
// If found any errors, print to stderr.
if len(ccerrs) > 0 {
if ctx.Verbose {
for _, e := range ccerrs {
ctx.Err.Println(e)
}
} else {
ctx.Out.Println("Got errors while collecting constraints. Rerun with `-v` flag to see details.")
}
return errors.New("errors while collecting constraints")
}

// Collect list of packages in target projects.
pkgs := make(map[gps.ProjectRoot][]string)
Expand Down Expand Up @@ -1173,7 +1213,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 @@ -482,6 +482,9 @@ func TestValidateFlags(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 @@ -490,11 +493,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 @@ -518,7 +524,8 @@ func TestProjectStatusString(t *testing.T) {
wantString: `
PROJECT: github.com/x/y
VERSION: v1.0
CONSTRAINTS: []
CONSTRAINTS: ^1.0.0(gh.com/f/b)
^0.5.0(btb.com/x/y)
SOURCE: github.com/x/y
ALT SOURCE: https://github.com/z/y
PUB VERSION: branches: dev, master
Expand Down Expand Up @@ -546,7 +553,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 99aeaae

Please sign in to comment.