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

Commit f1aaf31

Browse files
darkowlzzibrasho
authored andcommitted
test(status): BasicLine output & BasicStatus func
Add table tests for BasicLine output for each output type and tests for getConsolidatedConstraint() & getConsolidatedVersion().
1 parent 49fd655 commit f1aaf31

File tree

2 files changed

+199
-55
lines changed

2 files changed

+199
-55
lines changed

cmd/dep/status.go

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,10 @@ func (out *tableOutput) BasicFooter() {
8989
}
9090

9191
func (out *tableOutput) BasicLine(bs *BasicStatus) {
92-
var constraint string
93-
if v, ok := bs.Constraint.(gps.Version); ok {
94-
constraint = formatVersion(v)
95-
} else {
96-
constraint = bs.Constraint.String()
97-
}
98-
if bs.hasOverride {
99-
constraint += " (override)"
100-
}
10192
fmt.Fprintf(out.w,
10293
"%s\t%s\t%s\t%s\t%s\t%d\t\n",
10394
bs.ProjectRoot,
104-
constraint,
95+
bs.getConsolidatedConstraint(),
10596
formatVersion(bs.Version),
10697
formatVersion(bs.Revision),
10798
formatVersion(bs.Latest),
@@ -140,12 +131,7 @@ func (out *jsonOutput) BasicFooter() {
140131
}
141132

142133
func (out *jsonOutput) BasicLine(bs *BasicStatus) {
143-
if v, ok := bs.Constraint.(gps.Version); ok {
144-
bs.JSONConstraint = formatVersion(v)
145-
} else {
146-
bs.JSONConstraint = bs.Constraint.String()
147-
}
148-
134+
bs.JSONConstraint = bs.getConsolidatedConstraint()
149135
bs.JSONVersion = formatVersion(bs.Version)
150136
out.basic = append(out.basic, bs)
151137
}
@@ -184,11 +170,7 @@ func (out *dotOutput) BasicFooter() {
184170
}
185171

186172
func (out *dotOutput) BasicLine(bs *BasicStatus) {
187-
version := formatVersion(bs.Revision)
188-
if bs.Version != nil {
189-
version = formatVersion(bs.Version)
190-
}
191-
out.g.createNode(bs.ProjectRoot, version, bs.Children)
173+
out.g.createNode(bs.ProjectRoot, bs.getConsolidatedVersion(), bs.Children)
192174
}
193175

194176
func (out *dotOutput) MissingHeader() {}
@@ -253,7 +235,6 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
253235
// BasicStatus contains all the information reported about a single dependency
254236
// in the summary/list status output mode.
255237
type BasicStatus struct {
256-
<<<<<<< HEAD
257238
ProjectRoot string
258239
Children []string
259240
Constraint gps.Constraint
@@ -262,17 +243,31 @@ type BasicStatus struct {
262243
Latest gps.Version
263244
PackageCount int
264245
hasOverride bool
265-
=======
266-
ProjectRoot string
267-
Children []string
268-
Constraint gps.Constraint `json:"-"`
269-
Version gps.UnpairedVersion `json:"-"`
270-
Revision gps.Revision
271-
Latest gps.Version
272-
PackageCount int
273-
JSONConstraint string `json:"Constraint"`
274-
JSONVersion string `json:"Version"`
275-
>>>>>>> fix(status): fix `Constraint` & `Version` output
246+
}
247+
248+
func (bs *BasicStatus) getConsolidatedConstraint() string {
249+
var constraint string
250+
if bs.Constraint != nil {
251+
if v, ok := bs.Constraint.(gps.Version); ok {
252+
constraint = formatVersion(v)
253+
} else {
254+
constraint = bs.Constraint.String()
255+
}
256+
}
257+
258+
if bs.hasOverride {
259+
constraint += " (override)"
260+
}
261+
262+
return constraint
263+
}
264+
265+
func (bs *BasicStatus) getConsolidatedVersion() string {
266+
version := formatVersion(bs.Revision)
267+
if bs.Version != nil {
268+
version = formatVersion(bs.Version)
269+
}
270+
return version
276271
}
277272

278273
// MissingStatus contains information about all the missing packages in a project.

cmd/dep/status_test.go

Lines changed: 171 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77
import (
88
"bytes"
99
"testing"
10+
"text/tabwriter"
1011

1112
"strings"
1213

@@ -34,34 +35,182 @@ func TestStatusFormatVersion(t *testing.T) {
3435
func TestBasicLine(t *testing.T) {
3536

3637
project := dep.Project{}
38+
aSemverConstraint, _ := gps.NewSemverConstraint("1.2.3")
3739

38-
var tests = []struct {
39-
status BasicStatus
40-
expected string
40+
tests := []struct {
41+
name string
42+
status BasicStatus
43+
wantDotStatus []string
44+
wantJSONStatus []string
45+
wantTableStatus []string
4146
}{
42-
{BasicStatus{
43-
Version: nil,
44-
Revision: gps.Revision("flooboofoobooo"),
45-
}, `[label="\nflooboo"];`},
46-
{BasicStatus{
47-
Version: gps.NewVersion("1.0.0"),
48-
Revision: gps.Revision("flooboofoobooo"),
49-
}, `[label="\n1.0.0"];`},
47+
{
48+
name: "BasicStatus with ProjectRoot only",
49+
status: BasicStatus{
50+
ProjectRoot: "github.com/foo/bar",
51+
},
52+
wantDotStatus: []string{`[label="github.com/foo/bar"];`},
53+
wantJSONStatus: []string{`"Version":""`, `"Revision":""`},
54+
wantTableStatus: []string{`github.com/foo/bar 0`},
55+
},
56+
{
57+
name: "BasicStatus with Revision",
58+
status: BasicStatus{
59+
ProjectRoot: "github.com/foo/bar",
60+
Revision: gps.Revision("flooboofoobooo"),
61+
},
62+
wantDotStatus: []string{`[label="github.com/foo/bar\nflooboo"];`},
63+
wantJSONStatus: []string{`"Version":""`, `"Revision":"flooboofoobooo"`, `"Constraint":""`},
64+
wantTableStatus: []string{`github.com/foo/bar flooboo 0`},
65+
},
66+
{
67+
name: "BasicStatus with Version and Revision",
68+
status: BasicStatus{
69+
ProjectRoot: "github.com/foo/bar",
70+
Version: gps.NewVersion("1.0.0"),
71+
Revision: gps.Revision("flooboofoobooo"),
72+
},
73+
wantDotStatus: []string{`[label="github.com/foo/bar\n1.0.0"];`},
74+
wantJSONStatus: []string{`"Version":"1.0.0"`, `"Revision":"flooboofoobooo"`, `"Constraint":""`},
75+
wantTableStatus: []string{`github.com/foo/bar 1.0.0 flooboo 0`},
76+
},
77+
{
78+
name: "BasicStatus with Constraint, Version and Revision",
79+
status: BasicStatus{
80+
ProjectRoot: "github.com/foo/bar",
81+
Constraint: aSemverConstraint,
82+
Version: gps.NewVersion("1.0.0"),
83+
Revision: gps.Revision("revxyz"),
84+
},
85+
wantDotStatus: []string{`[label="github.com/foo/bar\n1.0.0"];`},
86+
wantJSONStatus: []string{`"Revision":"revxyz"`, `"Constraint":"1.2.3"`, `"Version":"1.0.0"`},
87+
wantTableStatus: []string{`github.com/foo/bar 1.2.3 1.0.0 revxyz 0`},
88+
},
5089
}
5190

5291
for _, test := range tests {
53-
var buf bytes.Buffer
92+
t.Run(test.name, func(t *testing.T) {
93+
var buf bytes.Buffer
5494

55-
out := &dotOutput{
56-
p: &project,
57-
w: &buf,
58-
}
59-
out.BasicHeader()
60-
out.BasicLine(&test.status)
61-
out.BasicFooter()
95+
dotout := &dotOutput{
96+
p: &project,
97+
w: &buf,
98+
}
99+
dotout.BasicHeader()
100+
dotout.BasicLine(&test.status)
101+
dotout.BasicFooter()
62102

63-
if ok := strings.Contains(buf.String(), test.expected); !ok {
64-
t.Fatalf("Did not find expected node label: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), test.status)
65-
}
103+
for _, wantStatus := range test.wantDotStatus {
104+
if ok := strings.Contains(buf.String(), wantStatus); !ok {
105+
t.Errorf("Did not find expected node status: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), wantStatus)
106+
}
107+
}
108+
109+
buf.Reset()
110+
111+
jsonout := &jsonOutput{w: &buf}
112+
113+
jsonout.BasicHeader()
114+
jsonout.BasicLine(&test.status)
115+
jsonout.BasicFooter()
116+
117+
for _, wantStatus := range test.wantJSONStatus {
118+
if ok := strings.Contains(buf.String(), wantStatus); !ok {
119+
t.Errorf("Did not find expected JSON status: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), wantStatus)
120+
}
121+
}
122+
123+
buf.Reset()
124+
125+
tabw := tabwriter.NewWriter(&buf, 0, 4, 2, ' ', 0)
126+
127+
tableout := &tableOutput{w: tabw}
128+
129+
tableout.BasicHeader()
130+
tableout.BasicLine(&test.status)
131+
tableout.BasicFooter()
132+
133+
for _, wantStatus := range test.wantTableStatus {
134+
if ok := strings.Contains(buf.String(), wantStatus); !ok {
135+
t.Errorf("Did not find expected Table status: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), wantStatus)
136+
}
137+
}
138+
})
139+
}
140+
}
141+
142+
func TestBasicStatusGetConsolidatedConstraint(t *testing.T) {
143+
144+
aSemverConstraint, _ := gps.NewSemverConstraint("1.2.1")
145+
146+
testCases := []struct {
147+
name string
148+
basicStatus BasicStatus
149+
wantConstraint string
150+
}{
151+
{
152+
name: "empty BasicStatus",
153+
basicStatus: BasicStatus{},
154+
wantConstraint: "",
155+
},
156+
{
157+
name: "BasicStatus with Any Constraint",
158+
basicStatus: BasicStatus{
159+
Constraint: gps.Any(),
160+
},
161+
wantConstraint: "*",
162+
},
163+
{
164+
name: "BasicStatus with Semver Constraint",
165+
basicStatus: BasicStatus{
166+
Constraint: aSemverConstraint,
167+
},
168+
wantConstraint: "1.2.1",
169+
},
170+
}
171+
172+
for _, tc := range testCases {
173+
t.Run(tc.name, func(t *testing.T) {
174+
if tc.basicStatus.getConsolidatedConstraint() != tc.wantConstraint {
175+
t.Errorf("unexpected consolidated constraint: \n\t(GOT) %v \n\t(WNT) %v", tc.basicStatus.getConsolidatedConstraint(), tc.wantConstraint)
176+
}
177+
})
178+
}
179+
}
180+
181+
func TestBasicStatusGetConsolidatedVersion(t *testing.T) {
182+
testCases := []struct {
183+
name string
184+
basicStatus BasicStatus
185+
wantVersion string
186+
}{
187+
{
188+
name: "empty BasicStatus",
189+
basicStatus: BasicStatus{},
190+
wantVersion: "",
191+
},
192+
{
193+
name: "BasicStatus with Version and Revision",
194+
basicStatus: BasicStatus{
195+
Version: gps.NewVersion("1.0.0"),
196+
Revision: gps.Revision("revxyz"),
197+
},
198+
wantVersion: "1.0.0",
199+
},
200+
{
201+
name: "BasicStatus with only Revision",
202+
basicStatus: BasicStatus{
203+
Revision: gps.Revision("revxyz"),
204+
},
205+
wantVersion: "revxyz",
206+
},
207+
}
208+
209+
for _, tc := range testCases {
210+
t.Run(tc.name, func(t *testing.T) {
211+
if tc.basicStatus.getConsolidatedVersion() != tc.wantVersion {
212+
t.Errorf("unexpected consolidated version: \n\t(GOT) %v \n\t(WNT) %v", tc.basicStatus.getConsolidatedVersion(), tc.wantVersion)
213+
}
214+
})
66215
}
67216
}

0 commit comments

Comments
 (0)