From 61e422447d83aa73c856e428025fd8d96eb73d02 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 9 Aug 2017 00:19:08 +0530 Subject: [PATCH 1/4] fix(status): fix `Constraint` & `Version` output Replace `Constraint` & `Version` with their json alternative forms `JSONConstraint` and `JSONVersion`. --- cmd/dep/status.go | 19 +++++++++++++++++++ .../status/case1/json/stdout.txt | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 49ef9e9fec..5d35f57991 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -140,6 +140,13 @@ func (out *jsonOutput) BasicFooter() { } func (out *jsonOutput) BasicLine(bs *BasicStatus) { + if v, ok := bs.Constraint.(gps.Version); ok { + bs.JSONConstraint = formatVersion(v) + } else { + bs.JSONConstraint = bs.Constraint.String() + } + + bs.JSONVersion = formatVersion(bs.Version) out.basic = append(out.basic, bs) } @@ -246,6 +253,7 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { // BasicStatus contains all the information reported about a single dependency // in the summary/list status output mode. type BasicStatus struct { +<<<<<<< HEAD ProjectRoot string Children []string Constraint gps.Constraint @@ -254,6 +262,17 @@ type BasicStatus struct { Latest gps.Version PackageCount int hasOverride bool +======= + ProjectRoot string + Children []string + Constraint gps.Constraint `json:"-"` + Version gps.UnpairedVersion `json:"-"` + Revision gps.Revision + Latest gps.Version + PackageCount int + JSONConstraint string `json:"Constraint"` + JSONVersion string `json:"Version"` +>>>>>>> fix(status): fix `Constraint` & `Version` output } // MissingStatus contains information about all the missing packages in a project. 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 a7fd40c5be..f02b364232 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","Children":null,"Constraint":{},"Version":{},"Revision":"ff2948a2ac8f538c4ecd55962e919d1e13e74baf","Latest":"3f4c3bea144e112a69bbe5d8d01c1b09a544253f","PackageCount":1},{"ProjectRoot":"github.com/sdboyer/deptestdos","Children":null,"Constraint":{},"Version":{},"Revision":"5c607206be5decd28e6263ffffdcee067266015e","Latest":"5c607206be5decd28e6263ffffdcee067266015e","PackageCount":1}] +[{"ProjectRoot":"github.com/sdboyer/deptest","Children":null,"Revision":"ff2948a2ac8f538c4ecd55962e919d1e13e74baf","Latest":"3f4c3bea144e112a69bbe5d8d01c1b09a544253f","PackageCount":1,"Constraint":"^0.8.0","Version":"v0.8.0"},{"ProjectRoot":"github.com/sdboyer/deptestdos","Children":null,"Revision":"5c607206be5decd28e6263ffffdcee067266015e","Latest":"5c607206be5decd28e6263ffffdcee067266015e","PackageCount":1,"Constraint":"*","Version":"v2.0.0"}] From e00c4ffed2b59fdf5d1d0853534c58b5b9288522 Mon Sep 17 00:00:00 2001 From: Sunny Date: Thu, 10 Aug 2017 18:17:19 +0530 Subject: [PATCH 2/4] test(status): BasicLine output & BasicStatus func Add table tests for BasicLine output for each output type and tests for getConsolidatedConstraint() & getConsolidatedVersion(). --- cmd/dep/status.go | 61 ++++++------- cmd/dep/status_test.go | 193 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 199 insertions(+), 55 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 5d35f57991..77b06117f6 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -89,19 +89,10 @@ func (out *tableOutput) BasicFooter() { } func (out *tableOutput) BasicLine(bs *BasicStatus) { - var constraint string - if v, ok := bs.Constraint.(gps.Version); ok { - constraint = formatVersion(v) - } else { - constraint = bs.Constraint.String() - } - if bs.hasOverride { - constraint += " (override)" - } fmt.Fprintf(out.w, "%s\t%s\t%s\t%s\t%s\t%d\t\n", bs.ProjectRoot, - constraint, + bs.getConsolidatedConstraint(), formatVersion(bs.Version), formatVersion(bs.Revision), formatVersion(bs.Latest), @@ -140,12 +131,7 @@ func (out *jsonOutput) BasicFooter() { } func (out *jsonOutput) BasicLine(bs *BasicStatus) { - if v, ok := bs.Constraint.(gps.Version); ok { - bs.JSONConstraint = formatVersion(v) - } else { - bs.JSONConstraint = bs.Constraint.String() - } - + bs.JSONConstraint = bs.getConsolidatedConstraint() bs.JSONVersion = formatVersion(bs.Version) out.basic = append(out.basic, bs) } @@ -184,11 +170,7 @@ func (out *dotOutput) BasicFooter() { } func (out *dotOutput) BasicLine(bs *BasicStatus) { - version := formatVersion(bs.Revision) - if bs.Version != nil { - version = formatVersion(bs.Version) - } - out.g.createNode(bs.ProjectRoot, version, bs.Children) + out.g.createNode(bs.ProjectRoot, bs.getConsolidatedVersion(), bs.Children) } func (out *dotOutput) MissingHeader() {} @@ -253,7 +235,6 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { // BasicStatus contains all the information reported about a single dependency // in the summary/list status output mode. type BasicStatus struct { -<<<<<<< HEAD ProjectRoot string Children []string Constraint gps.Constraint @@ -262,17 +243,31 @@ type BasicStatus struct { Latest gps.Version PackageCount int hasOverride bool -======= - ProjectRoot string - Children []string - Constraint gps.Constraint `json:"-"` - Version gps.UnpairedVersion `json:"-"` - Revision gps.Revision - Latest gps.Version - PackageCount int - JSONConstraint string `json:"Constraint"` - JSONVersion string `json:"Version"` ->>>>>>> fix(status): fix `Constraint` & `Version` output +} + +func (bs *BasicStatus) getConsolidatedConstraint() string { + var constraint string + if bs.Constraint != nil { + if v, ok := bs.Constraint.(gps.Version); ok { + constraint = formatVersion(v) + } else { + constraint = bs.Constraint.String() + } + } + + if bs.hasOverride { + constraint += " (override)" + } + + return constraint +} + +func (bs *BasicStatus) getConsolidatedVersion() string { + version := formatVersion(bs.Revision) + if bs.Version != nil { + version = formatVersion(bs.Version) + } + return version } // MissingStatus contains information about all the missing packages in a project. diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index 51118ba352..ee9ee2a4be 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -7,6 +7,7 @@ package main import ( "bytes" "testing" + "text/tabwriter" "strings" @@ -34,34 +35,182 @@ func TestStatusFormatVersion(t *testing.T) { func TestBasicLine(t *testing.T) { project := dep.Project{} + aSemverConstraint, _ := gps.NewSemverConstraint("1.2.3") - var tests = []struct { - status BasicStatus - expected string + tests := []struct { + name string + status BasicStatus + wantDotStatus []string + wantJSONStatus []string + wantTableStatus []string }{ - {BasicStatus{ - Version: nil, - Revision: gps.Revision("flooboofoobooo"), - }, `[label="\nflooboo"];`}, - {BasicStatus{ - Version: gps.NewVersion("1.0.0"), - Revision: gps.Revision("flooboofoobooo"), - }, `[label="\n1.0.0"];`}, + { + name: "BasicStatus with ProjectRoot only", + status: BasicStatus{ + ProjectRoot: "github.com/foo/bar", + }, + wantDotStatus: []string{`[label="github.com/foo/bar"];`}, + wantJSONStatus: []string{`"Version":""`, `"Revision":""`}, + wantTableStatus: []string{`github.com/foo/bar 0`}, + }, + { + name: "BasicStatus with Revision", + status: BasicStatus{ + ProjectRoot: "github.com/foo/bar", + Revision: gps.Revision("flooboofoobooo"), + }, + wantDotStatus: []string{`[label="github.com/foo/bar\nflooboo"];`}, + wantJSONStatus: []string{`"Version":""`, `"Revision":"flooboofoobooo"`, `"Constraint":""`}, + wantTableStatus: []string{`github.com/foo/bar flooboo 0`}, + }, + { + name: "BasicStatus with Version and Revision", + status: BasicStatus{ + ProjectRoot: "github.com/foo/bar", + Version: gps.NewVersion("1.0.0"), + Revision: gps.Revision("flooboofoobooo"), + }, + wantDotStatus: []string{`[label="github.com/foo/bar\n1.0.0"];`}, + wantJSONStatus: []string{`"Version":"1.0.0"`, `"Revision":"flooboofoobooo"`, `"Constraint":""`}, + wantTableStatus: []string{`github.com/foo/bar 1.0.0 flooboo 0`}, + }, + { + name: "BasicStatus with Constraint, Version and Revision", + status: BasicStatus{ + ProjectRoot: "github.com/foo/bar", + Constraint: aSemverConstraint, + Version: gps.NewVersion("1.0.0"), + Revision: gps.Revision("revxyz"), + }, + wantDotStatus: []string{`[label="github.com/foo/bar\n1.0.0"];`}, + wantJSONStatus: []string{`"Revision":"revxyz"`, `"Constraint":"1.2.3"`, `"Version":"1.0.0"`}, + wantTableStatus: []string{`github.com/foo/bar 1.2.3 1.0.0 revxyz 0`}, + }, } for _, test := range tests { - var buf bytes.Buffer + t.Run(test.name, func(t *testing.T) { + var buf bytes.Buffer - out := &dotOutput{ - p: &project, - w: &buf, - } - out.BasicHeader() - out.BasicLine(&test.status) - out.BasicFooter() + dotout := &dotOutput{ + p: &project, + w: &buf, + } + dotout.BasicHeader() + dotout.BasicLine(&test.status) + dotout.BasicFooter() - if ok := strings.Contains(buf.String(), test.expected); !ok { - t.Fatalf("Did not find expected node label: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), test.status) - } + for _, wantStatus := range test.wantDotStatus { + if ok := strings.Contains(buf.String(), wantStatus); !ok { + t.Errorf("Did not find expected node status: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), wantStatus) + } + } + + buf.Reset() + + jsonout := &jsonOutput{w: &buf} + + jsonout.BasicHeader() + jsonout.BasicLine(&test.status) + jsonout.BasicFooter() + + for _, wantStatus := range test.wantJSONStatus { + if ok := strings.Contains(buf.String(), wantStatus); !ok { + t.Errorf("Did not find expected JSON status: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), wantStatus) + } + } + + buf.Reset() + + tabw := tabwriter.NewWriter(&buf, 0, 4, 2, ' ', 0) + + tableout := &tableOutput{w: tabw} + + tableout.BasicHeader() + tableout.BasicLine(&test.status) + tableout.BasicFooter() + + for _, wantStatus := range test.wantTableStatus { + if ok := strings.Contains(buf.String(), wantStatus); !ok { + t.Errorf("Did not find expected Table status: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), wantStatus) + } + } + }) + } +} + +func TestBasicStatusGetConsolidatedConstraint(t *testing.T) { + + aSemverConstraint, _ := gps.NewSemverConstraint("1.2.1") + + testCases := []struct { + name string + basicStatus BasicStatus + wantConstraint string + }{ + { + name: "empty BasicStatus", + basicStatus: BasicStatus{}, + wantConstraint: "", + }, + { + name: "BasicStatus with Any Constraint", + basicStatus: BasicStatus{ + Constraint: gps.Any(), + }, + wantConstraint: "*", + }, + { + name: "BasicStatus with Semver Constraint", + basicStatus: BasicStatus{ + Constraint: aSemverConstraint, + }, + wantConstraint: "1.2.1", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + if tc.basicStatus.getConsolidatedConstraint() != tc.wantConstraint { + t.Errorf("unexpected consolidated constraint: \n\t(GOT) %v \n\t(WNT) %v", tc.basicStatus.getConsolidatedConstraint(), tc.wantConstraint) + } + }) + } +} + +func TestBasicStatusGetConsolidatedVersion(t *testing.T) { + testCases := []struct { + name string + basicStatus BasicStatus + wantVersion string + }{ + { + name: "empty BasicStatus", + basicStatus: BasicStatus{}, + wantVersion: "", + }, + { + name: "BasicStatus with Version and Revision", + basicStatus: BasicStatus{ + Version: gps.NewVersion("1.0.0"), + Revision: gps.Revision("revxyz"), + }, + wantVersion: "1.0.0", + }, + { + name: "BasicStatus with only Revision", + basicStatus: BasicStatus{ + Revision: gps.Revision("revxyz"), + }, + wantVersion: "revxyz", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + if tc.basicStatus.getConsolidatedVersion() != tc.wantVersion { + t.Errorf("unexpected consolidated version: \n\t(GOT) %v \n\t(WNT) %v", tc.basicStatus.getConsolidatedVersion(), tc.wantVersion) + } + }) } } From 9300b6237e851ee9660a60027b770558af6fa8a0 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sat, 12 Aug 2017 16:46:44 +0530 Subject: [PATCH 3/4] fix(status): add marshalJSON() Add `rawStatus` struct and use `marshalJSON()` to convert `BasicStatus` to `rawStatus`. --- cmd/dep/status.go | 28 +++++++++++++++---- cmd/dep/status_test.go | 2 -- .../status/case1/json/stdout.txt | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 77b06117f6..c97ca04ef3 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -118,12 +118,12 @@ func (out *tableOutput) MissingFooter() { type jsonOutput struct { w io.Writer - basic []*BasicStatus + basic []*rawStatus missing []*MissingStatus } func (out *jsonOutput) BasicHeader() { - out.basic = []*BasicStatus{} + out.basic = []*rawStatus{} } func (out *jsonOutput) BasicFooter() { @@ -131,9 +131,7 @@ func (out *jsonOutput) BasicFooter() { } func (out *jsonOutput) BasicLine(bs *BasicStatus) { - bs.JSONConstraint = bs.getConsolidatedConstraint() - bs.JSONVersion = formatVersion(bs.Version) - out.basic = append(out.basic, bs) + out.basic = append(out.basic, bs.marshalJSON()) } func (out *jsonOutput) MissingHeader() { @@ -232,6 +230,15 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { return nil } +type rawStatus struct { + ProjectRoot string + Constraint string + Version string + Revision gps.Revision + Latest gps.Version + PackageCount int +} + // BasicStatus contains all the information reported about a single dependency // in the summary/list status output mode. type BasicStatus struct { @@ -270,6 +277,17 @@ func (bs *BasicStatus) getConsolidatedVersion() string { return version } +func (bs *BasicStatus) marshalJSON() *rawStatus { + return &rawStatus{ + ProjectRoot: bs.ProjectRoot, + Constraint: bs.getConsolidatedConstraint(), + Version: formatVersion(bs.Version), + Revision: bs.Revision, + Latest: bs.Latest, + PackageCount: bs.PackageCount, + } +} + // MissingStatus contains information about all the missing packages in a project. type MissingStatus struct { ProjectRoot string diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index ee9ee2a4be..a68114f8a2 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -33,7 +33,6 @@ func TestStatusFormatVersion(t *testing.T) { } func TestBasicLine(t *testing.T) { - project := dep.Project{} aSemverConstraint, _ := gps.NewSemverConstraint("1.2.3") @@ -140,7 +139,6 @@ func TestBasicLine(t *testing.T) { } func TestBasicStatusGetConsolidatedConstraint(t *testing.T) { - aSemverConstraint, _ := gps.NewSemverConstraint("1.2.1") testCases := []struct { 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 f02b364232..e995757921 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","Children":null,"Revision":"ff2948a2ac8f538c4ecd55962e919d1e13e74baf","Latest":"3f4c3bea144e112a69bbe5d8d01c1b09a544253f","PackageCount":1,"Constraint":"^0.8.0","Version":"v0.8.0"},{"ProjectRoot":"github.com/sdboyer/deptestdos","Children":null,"Revision":"5c607206be5decd28e6263ffffdcee067266015e","Latest":"5c607206be5decd28e6263ffffdcee067266015e","PackageCount":1,"Constraint":"*","Version":"v2.0.0"}] +[{"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}] From c6b462b2b5afb8b26ac720fc2e65371fff90bb2a Mon Sep 17 00:00:00 2001 From: Sunny Date: Sat, 12 Aug 2017 19:45:38 +0530 Subject: [PATCH 4/4] test(status): consolidated constraint with override --- cmd/dep/status_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/dep/status_test.go b/cmd/dep/status_test.go index a68114f8a2..35ef53e007 100644 --- a/cmd/dep/status_test.go +++ b/cmd/dep/status_test.go @@ -165,6 +165,14 @@ func TestBasicStatusGetConsolidatedConstraint(t *testing.T) { }, wantConstraint: "1.2.1", }, + { + name: "BasicStatus with Override", + basicStatus: BasicStatus{ + Constraint: aSemverConstraint, + hasOverride: true, + }, + wantConstraint: "1.2.1 (override)", + }, } for _, tc := range testCases {