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

Commit 1b53890

Browse files
committed
Add test for using variables in templates
1 parent 4b3ad2c commit 1b53890

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

cmd/dep/dep

-11.6 MB
Binary file not shown.

cmd/dep/status.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,7 @@ func (out *templateOutput) BasicHeader() error { return nil }
245245
func (out *templateOutput) BasicFooter() error { return nil }
246246

247247
func (out *templateOutput) BasicLine(bs *BasicStatus) error {
248-
data := struct {
249-
ProjectRoot, Constraint, Version, Revision, Latest string
250-
PackageCount int
251-
}{
248+
data := rawStatus{
252249
ProjectRoot: bs.ProjectRoot,
253250
Constraint: bs.getConsolidatedConstraint(),
254251
Version: bs.getConsolidatedVersion(),

cmd/dep/status_test.go

+50-27
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,40 @@ func TestBasicLine(t *testing.T) {
4343
project := dep.Project{}
4444
aSemverConstraint, _ := gps.NewSemverConstraint("1.2.3")
4545

46+
templateString := "PR:{{.ProjectRoot}}, Const:{{.Constraint}}, Ver:{{.Version}}, Rev:{{.Revision}}, Lat:{{.Latest}}, PkgCt:{{.PackageCount}}"
47+
equalityTestTemplate := `{{if eq .Constraint "1.2.3"}}Constraint is 1.2.3{{end}}|{{if eq .Version "flooboo"}}Version is flooboo{{end}}|{{if eq .Latest "unknown"}}Latest is unknown{{end}}`
48+
4649
tests := []struct {
47-
name string
48-
status BasicStatus
49-
wantDotStatus []string
50-
wantJSONStatus []string
51-
wantTableStatus []string
52-
wantTemplateStatus []string
50+
name string
51+
status BasicStatus
52+
wantDotStatus []string
53+
wantJSONStatus []string
54+
wantTableStatus []string
55+
wantTemplateStatus []string
56+
wantEqTemplateStatus []string
5357
}{
5458
{
5559
name: "BasicStatus with ProjectRoot only",
5660
status: BasicStatus{
5761
ProjectRoot: "github.com/foo/bar",
5862
},
59-
wantDotStatus: []string{`[label="github.com/foo/bar"];`},
60-
wantJSONStatus: []string{`"Version":""`, `"Revision":""`},
61-
wantTableStatus: []string{`github.com/foo/bar 0`},
62-
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:, Ver:, Rev:, Lat:, PkgCt:0`},
63+
wantDotStatus: []string{`[label="github.com/foo/bar"];`},
64+
wantJSONStatus: []string{`"Version":""`, `"Revision":""`},
65+
wantTableStatus: []string{`github.com/foo/bar 0`},
66+
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:, Ver:, Rev:, Lat:, PkgCt:0`},
67+
wantEqTemplateStatus: []string{`||`},
6368
},
6469
{
6570
name: "BasicStatus with Revision",
6671
status: BasicStatus{
6772
ProjectRoot: "github.com/foo/bar",
6873
Revision: gps.Revision("flooboofoobooo"),
6974
},
70-
wantDotStatus: []string{`[label="github.com/foo/bar\nflooboo"];`},
71-
wantJSONStatus: []string{`"Version":""`, `"Revision":"flooboofoobooo"`, `"Constraint":""`},
72-
wantTableStatus: []string{`github.com/foo/bar flooboo 0`},
73-
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:, Ver:flooboo, Rev:flooboofoobooo, Lat:, PkgCt:0`},
75+
wantDotStatus: []string{`[label="github.com/foo/bar\nflooboo"];`},
76+
wantJSONStatus: []string{`"Version":""`, `"Revision":"flooboofoobooo"`, `"Constraint":""`},
77+
wantTableStatus: []string{`github.com/foo/bar flooboo 0`},
78+
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:, Ver:flooboo, Rev:flooboofoobooo, Lat:, PkgCt:0`},
79+
wantEqTemplateStatus: []string{`|Version is flooboo|`},
7480
},
7581
{
7682
name: "BasicStatus with Version and Revision",
@@ -79,10 +85,11 @@ func TestBasicLine(t *testing.T) {
7985
Version: gps.NewVersion("1.0.0"),
8086
Revision: gps.Revision("flooboofoobooo"),
8187
},
82-
wantDotStatus: []string{`[label="github.com/foo/bar\n1.0.0"];`},
83-
wantJSONStatus: []string{`"Version":"1.0.0"`, `"Revision":"flooboofoobooo"`, `"Constraint":""`},
84-
wantTableStatus: []string{`github.com/foo/bar 1.0.0 flooboo 0`},
85-
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:, Ver:1.0.0, Rev:flooboofoobooo, Lat:, PkgCt:0`},
88+
wantDotStatus: []string{`[label="github.com/foo/bar\n1.0.0"];`},
89+
wantJSONStatus: []string{`"Version":"1.0.0"`, `"Revision":"flooboofoobooo"`, `"Constraint":""`},
90+
wantTableStatus: []string{`github.com/foo/bar 1.0.0 flooboo 0`},
91+
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:, Ver:1.0.0, Rev:flooboofoobooo, Lat:, PkgCt:0`},
92+
wantEqTemplateStatus: []string{`||`},
8693
},
8794
{
8895
name: "BasicStatus with Constraint, Version and Revision",
@@ -92,21 +99,23 @@ func TestBasicLine(t *testing.T) {
9299
Version: gps.NewVersion("1.0.0"),
93100
Revision: gps.Revision("revxyz"),
94101
},
95-
wantDotStatus: []string{`[label="github.com/foo/bar\n1.0.0"];`},
96-
wantJSONStatus: []string{`"Revision":"revxyz"`, `"Constraint":"1.2.3"`, `"Version":"1.0.0"`},
97-
wantTableStatus: []string{`github.com/foo/bar 1.2.3 1.0.0 revxyz 0`},
98-
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:1.2.3, Ver:1.0.0, Rev:revxyz, Lat:, PkgCt:0`},
102+
wantDotStatus: []string{`[label="github.com/foo/bar\n1.0.0"];`},
103+
wantJSONStatus: []string{`"Revision":"revxyz"`, `"Constraint":"1.2.3"`, `"Version":"1.0.0"`},
104+
wantTableStatus: []string{`github.com/foo/bar 1.2.3 1.0.0 revxyz 0`},
105+
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:1.2.3, Ver:1.0.0, Rev:revxyz, Lat:, PkgCt:0`},
106+
wantEqTemplateStatus: []string{`Constraint is 1.2.3||`},
99107
},
100108
{
101109
name: "BasicStatus with update error",
102110
status: BasicStatus{
103111
ProjectRoot: "github.com/foo/bar",
104112
hasError: true,
105113
},
106-
wantDotStatus: []string{`[label="github.com/foo/bar"];`},
107-
wantJSONStatus: []string{`"Version":""`, `"Revision":""`, `"Latest":"unknown"`},
108-
wantTableStatus: []string{`github.com/foo/bar unknown 0`},
109-
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:, Ver:, Rev:, Lat:unknown, PkgCt:0`},
114+
wantDotStatus: []string{`[label="github.com/foo/bar"];`},
115+
wantJSONStatus: []string{`"Version":""`, `"Revision":""`, `"Latest":"unknown"`},
116+
wantTableStatus: []string{`github.com/foo/bar unknown 0`},
117+
wantTemplateStatus: []string{`PR:github.com/foo/bar, Const:, Ver:, Rev:, Lat:unknown, PkgCt:0`},
118+
wantEqTemplateStatus: []string{`||Latest is unknown`},
110119
},
111120
}
112121

@@ -159,7 +168,7 @@ func TestBasicLine(t *testing.T) {
159168
}
160169

161170
buf.Reset()
162-
template, _ := template.New("status").Parse("PR:{{.ProjectRoot}}, Const:{{.Constraint}}, Ver:{{.Version}}, Rev:{{.Revision}}, Lat:{{.Latest}}, PkgCt:{{.PackageCount}}")
171+
template, _ := template.New("status").Parse(templateString)
163172
templateout := &templateOutput{w: &buf, tmpl: template}
164173
templateout.BasicHeader()
165174
templateout.BasicLine(&test.status)
@@ -170,6 +179,20 @@ func TestBasicLine(t *testing.T) {
170179
t.Errorf("Did not find expected template status: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), wantStatus)
171180
}
172181
}
182+
183+
// The following test is to ensure that certain fields usable with string operations such as .eq
184+
buf.Reset()
185+
template, _ = template.New("status").Parse(equalityTestTemplate)
186+
templateout = &templateOutput{w: &buf, tmpl: template}
187+
templateout.BasicHeader()
188+
templateout.BasicLine(&test.status)
189+
templateout.BasicFooter()
190+
191+
for _, wantStatus := range test.wantEqTemplateStatus {
192+
if ok := strings.Contains(buf.String(), wantStatus); !ok {
193+
t.Errorf("Did not find expected template status: \n\t(GOT) %v \n\t(WNT) %v", buf.String(), wantStatus)
194+
}
195+
}
173196
})
174197
}
175198
}

0 commit comments

Comments
 (0)