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

Commit 99aeaae

Browse files
committed
status: make project status compatible with collectConstraints
1 parent c98bfea commit 99aeaae

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

cmd/dep/status.go

+49-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/golang/dep"
2424
"github.com/golang/dep/gps"
2525
"github.com/golang/dep/gps/paths"
26+
"github.com/golang/dep/gps/pkgtree"
2627
"github.com/pkg/errors"
2728
)
2829

@@ -747,6 +748,10 @@ type projectConstraint struct {
747748
Constraint gps.Constraint
748749
}
749750

751+
func (pc projectConstraint) String() string {
752+
return fmt.Sprintf("%s(%s)", pc.Constraint.String(), string(pc.Project))
753+
}
754+
750755
// constraintsCollection is a map of ProjectRoot(dependency) and a collection of
751756
// projectConstraint for the dependencies. This can be used to find constraints
752757
// on a dependency and the projects that apply those constraints.
@@ -881,6 +886,7 @@ func (pv pubVersions) TabString() string {
881886
return buf.String()
882887
}
883888

889+
// projectImporters stores a map of project names that import a specific project.
884890
type projectImporters map[string]bool
885891

886892
func (pi projectImporters) String() string {
@@ -896,6 +902,7 @@ func (pi projectImporters) String() string {
896902
return strings.Join(projects, ", ")
897903
}
898904

905+
// packageImporters stores a map of package and projects that import them.
899906
type packageImporters map[string][]string
900907

901908
func (pi packageImporters) TabString() string {
@@ -940,10 +947,37 @@ func (pi packageImporters) TabString() string {
940947
return buf.String()
941948
}
942949

950+
// projectConstraints is a slice of projectConstraint
951+
type projectConstraints []projectConstraint
952+
953+
func (pcs projectConstraints) TabString() string {
954+
var buf bytes.Buffer
955+
w := bufio.NewWriter(&buf)
956+
957+
// Sort for consistent result.
958+
sort.Sort(byProject(pcs))
959+
960+
// Count lines and add newlines("\n") and tabs("\t"), compatible with
961+
// tabwriter.
962+
// ^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)
963+
count := 0
964+
for _, c := range pcs {
965+
count++
966+
if count > 1 {
967+
fmt.Fprintf(w, "\n \t")
968+
}
969+
970+
fmt.Fprintf(w, "%s", c)
971+
}
972+
w.Flush()
973+
974+
return buf.String()
975+
}
976+
943977
type projectStatus struct {
944978
Project string
945979
Version string
946-
Constraints []string
980+
Constraints projectConstraints
947981
Source string
948982
AltSource string
949983
PubVersions pubVersions
@@ -987,7 +1021,7 @@ func (ps projectStatus) String() string {
9871021
"PACKAGE IMPORTERS:\t%s\n"+
9881022
"UPSTREAM EXISTS:\t%s\n"+
9891023
"UPSTREAM VERSION EXISTS:\t%s",
990-
ps.Project, ps.Version, ps.Constraints, ps.Source, ps.AltSource,
1024+
ps.Project, ps.Version, ps.Constraints.TabString(), ps.Source, ps.AltSource,
9911025
ps.PubVersions.TabString(), ps.Revision, ps.LatestAllowed, ps.SourceType,
9921026
strings.Join(ps.Packages, ", "), ps.ProjectImporters,
9931027
ps.PackageImporters.TabString(), upstreamExists, upstreamVersionExists,
@@ -1019,13 +1053,19 @@ func runProjectStatus(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.Source
10191053
prs = append(prs, pr)
10201054
}
10211055

1022-
// This ptree would not be required with the new collectConstraints() implementation.
1023-
ptree, err := p.ParseRootPackageTree()
1024-
if err != nil {
1025-
return err
1026-
}
10271056
// Collect all the constraints.
1028-
cc := collectConstraints(ptree, p, sm)
1057+
cc, ccerrs := collectConstraints(ctx, p, sm)
1058+
// If found any errors, print to stderr.
1059+
if len(ccerrs) > 0 {
1060+
if ctx.Verbose {
1061+
for _, e := range ccerrs {
1062+
ctx.Err.Println(e)
1063+
}
1064+
} else {
1065+
ctx.Out.Println("Got errors while collecting constraints. Rerun with `-v` flag to see details.")
1066+
}
1067+
return errors.New("errors while collecting constraints")
1068+
}
10291069

10301070
// Collect list of packages in target projects.
10311071
pkgs := make(map[gps.ProjectRoot][]string)
@@ -1173,7 +1213,7 @@ func runProjectStatus(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.Source
11731213
// CONSTRAINTS
11741214
constraints := cc[string(pr)]
11751215
for _, c := range constraints {
1176-
projStatus.Constraints = append(projStatus.Constraints, c.String())
1216+
projStatus.Constraints = append(projStatus.Constraints, c)
11771217
}
11781218
}
11791219
}

cmd/dep/status_test.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ func TestValidateFlags(t *testing.T) {
482482
}
483483

484484
func TestProjectStatusString(t *testing.T) {
485+
ver1, _ := gps.NewSemverConstraintIC("v1.0.0")
486+
ver05, _ := gps.NewSemverConstraintIC("v0.5.0")
487+
485488
testCases := []struct {
486489
name string
487490
ps projectStatus
@@ -490,11 +493,14 @@ func TestProjectStatusString(t *testing.T) {
490493
{
491494
name: "basic projectStatus",
492495
ps: projectStatus{
493-
Project: "github.com/x/y",
494-
Version: "v1.0",
495-
Constraints: nil,
496-
Source: "github.com/x/y",
497-
AltSource: "https://github.com/z/y",
496+
Project: "github.com/x/y",
497+
Version: "v1.0",
498+
Constraints: projectConstraints{
499+
{"gh.com/f/b", ver1},
500+
{"btb.com/x/y", ver05},
501+
},
502+
Source: "github.com/x/y",
503+
AltSource: "https://github.com/z/y",
498504
PubVersions: pubVersions{
499505
"semvers": []string{"v0.5", "v0.7", "v1.0", "v1.5"},
500506
"branches": []string{"master", "dev"},
@@ -518,7 +524,8 @@ func TestProjectStatusString(t *testing.T) {
518524
wantString: `
519525
PROJECT: github.com/x/y
520526
VERSION: v1.0
521-
CONSTRAINTS: []
527+
CONSTRAINTS: ^1.0.0(gh.com/f/b)
528+
^0.5.0(btb.com/x/y)
522529
SOURCE: github.com/x/y
523530
ALT SOURCE: https://github.com/z/y
524531
PUB VERSION: branches: dev, master
@@ -546,7 +553,7 @@ UPSTREAM VERSION EXISTS: yes`,
546553
wantString: `
547554
PROJECT:
548555
VERSION:
549-
CONSTRAINTS: []
556+
CONSTRAINTS:
550557
SOURCE:
551558
ALT SOURCE:
552559
PUB VERSION:

cmd/dep/testdata/harness_tests/status/project_status/stdout.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
PROJECT: github.com/sdboyer/deptest
33
VERSION: v1.0.0
4-
CONSTRAINTS: []
4+
CONSTRAINTS:
55
SOURCE: github.com/sdboyer/deptest
66
ALT SOURCE:
77
PUB VERSION: branches: master

0 commit comments

Comments
 (0)