Skip to content

Commit 47984f8

Browse files
committed
Validate report test output
1 parent 98e0a04 commit 47984f8

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

command/report.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Options:
4545
-full-message=false Include full commit message
4646
-terminal-off=false Exclude time spent in terminal (Terminal plug-in is required)
4747
-color=false Always output color even if no terminal is detected, i.e 'gtm report -color | less -R'
48+
-testing=false This is used for automated testing to force default test path
4849
4950
Commit Limiting:
5051
@@ -73,7 +74,7 @@ Options:
7374
// Run executes report command with args
7475
func (c ReportCmd) Run(args []string) int {
7576
var limit int
76-
var color, terminalOff, fullMessage bool
77+
var color, terminalOff, fullMessage, testing bool
7778
var today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth, thisYear, lastYear, all bool
7879
var before, after, message, author, tags, format string
7980
cmdFlags := flag.NewFlagSet("report", flag.ContinueOnError)
@@ -96,6 +97,7 @@ func (c ReportCmd) Run(args []string) int {
9697
cmdFlags.StringVar(&message, "message", "", "")
9798
cmdFlags.StringVar(&tags, "tags", "", "")
9899
cmdFlags.BoolVar(&all, "all", false, "")
100+
cmdFlags.BoolVar(&testing, "testing", false, "")
99101
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
100102
if err := cmdFlags.Parse(args); err != nil {
101103
return 1
@@ -123,7 +125,7 @@ func (c ReportCmd) Run(args []string) int {
123125
projCommits := []report.ProjectCommits{}
124126

125127
switch {
126-
case !isMinGW && !isatty.IsTerminal(os.Stdin.Fd()):
128+
case !testing && !isMinGW && !isatty.IsTerminal(os.Stdin.Fd()):
127129
scanner := bufio.NewScanner(os.Stdin)
128130
for scanner.Scan() {
129131
if !sha1Regex.MatchString(scanner.Text()) {
@@ -140,7 +142,7 @@ func (c ReportCmd) Run(args []string) int {
140142

141143
projCommits = append(projCommits, report.ProjectCommits{Path: curProjPath, Commits: commits})
142144

143-
case len(cmdFlags.Args()) > 0:
145+
case !testing && len(cmdFlags.Args()) > 0:
144146
for _, a := range cmdFlags.Args() {
145147
if !sha1Regex.MatchString(a) {
146148
c.Ui.Error(fmt.Sprintf("%s %s", invalidSHA1, a))

command/report_test.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ func TestReportDefaultOptions(t *testing.T) {
3333
ui := new(cli.MockUi)
3434
c := ReportCmd{Ui: ui}
3535

36-
args := []string{""}
36+
args := []string{"-testing=true"}
3737
rc := c.Run(args)
3838

3939
if rc != 0 {
4040
t.Errorf("gtm report(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
4141
}
42+
43+
want := "2m 40s 89% [m] event/event.go"
44+
if !strings.Contains(ui.OutputWriter.String(), want) {
45+
t.Errorf("gtm report(%+v), want %s got %s, %s", args, want, ui.OutputWriter.String(), ui.ErrorWriter.String())
46+
}
4247
}
4348

4449
func TestReportSummary(t *testing.T) {
@@ -63,12 +68,17 @@ func TestReportSummary(t *testing.T) {
6368
ui := new(cli.MockUi)
6469
c := ReportCmd{Ui: ui}
6570

66-
args := []string{"-format", "summary"}
71+
args := []string{"-format", "summary", "-testing=true"}
6772
rc := c.Run(args)
6873

6974
if rc != 0 {
7075
t.Errorf("gtm report(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
7176
}
77+
78+
want := "3m 0s This is a commit"
79+
if !strings.Contains(ui.OutputWriter.String(), want) {
80+
t.Errorf("gtm report(%+v), want %s got %s, %s", args, want, ui.OutputWriter.String(), ui.ErrorWriter.String())
81+
}
7282
}
7383

7484
func TestReportAll(t *testing.T) {
@@ -93,6 +103,7 @@ func TestReportAll(t *testing.T) {
93103
ui := new(cli.MockUi)
94104
c := ReportCmd{Ui: ui}
95105

106+
// TODO: in order to test output of multi-project reporting, we need the ability to mock the project index
96107
args := []string{"-all"}
97108
rc := c.Run(args)
98109

@@ -123,12 +134,17 @@ func TestReportTimelineHours(t *testing.T) {
123134
ui := new(cli.MockUi)
124135
c := ReportCmd{Ui: ui}
125136

126-
args := []string{"-format", "timeline-hours"}
137+
args := []string{"-format", "timeline-hours", "-testing=true"}
127138
rc := c.Run(args)
128139

129140
if rc != 0 {
130141
t.Errorf("gtm report(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
131142
}
143+
144+
want := "Sun Mar 20"
145+
if !strings.Contains(ui.OutputWriter.String(), want) {
146+
t.Errorf("gtm report(%+v), want %s got %s, %s", args, want, ui.OutputWriter.String(), ui.ErrorWriter.String())
147+
}
132148
}
133149

134150
func TestReportTimelineCommits(t *testing.T) {
@@ -153,12 +169,17 @@ func TestReportTimelineCommits(t *testing.T) {
153169
ui := new(cli.MockUi)
154170
c := ReportCmd{Ui: ui}
155171

156-
args := []string{"-format", "timeline-commits"}
172+
args := []string{"-format", "timeline-commits", "-testing=true"}
157173
rc := c.Run(args)
158174

159175
if rc != 0 {
160176
t.Errorf("gtm report(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
161177
}
178+
179+
want := "Wed Mar 06"
180+
if !strings.Contains(ui.OutputWriter.String(), want) {
181+
t.Errorf("gtm report(%+v), want %s got %s, %s", args, want, ui.OutputWriter.String(), ui.ErrorWriter.String())
182+
}
162183
}
163184

164185
func TestReportFiles(t *testing.T) {
@@ -183,12 +204,17 @@ func TestReportFiles(t *testing.T) {
183204
ui := new(cli.MockUi)
184205
c := ReportCmd{Ui: ui}
185206

186-
args := []string{"-format", "files"}
207+
args := []string{"-format", "files", "-testing=true"}
187208
rc := c.Run(args)
188209

189210
if rc != 0 {
190211
t.Errorf("gtm report(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
191212
}
213+
214+
want := "3m 0s"
215+
if !strings.Contains(ui.OutputWriter.String(), want) {
216+
t.Errorf("gtm report(%+v), want %s got %s, %s", args, want, ui.OutputWriter.String(), ui.ErrorWriter.String())
217+
}
192218
}
193219

194220
func TestReportInvalidOption(t *testing.T) {

0 commit comments

Comments
 (0)