@@ -95,6 +95,10 @@ type ConsoleReporter struct {
95
95
writer map [string ]io.Writer
96
96
times map [string ]time.Time
97
97
mu sync.RWMutex
98
+
99
+ // out is the writer to print to. Normally this is os.Stdout, but it can be set to a buffer for testing.
100
+ out io.Writer
101
+ now func () time.Time
98
102
}
99
103
100
104
// exclusiveWriter makes a write an exclusive resource by protecting Write calls with a mutex.
@@ -115,6 +119,8 @@ func NewConsoleReporter() *ConsoleReporter {
115
119
return & ConsoleReporter {
116
120
writer : make (map [string ]io.Writer ),
117
121
times : make (map [string ]time.Time ),
122
+ out : os .Stdout ,
123
+ now : time .Now ,
118
124
}
119
125
}
120
126
@@ -134,7 +140,7 @@ func (r *ConsoleReporter) getWriter(pkg *Package) io.Writer {
134
140
return res
135
141
}
136
142
137
- res = & exclusiveWriter {O : textio .NewPrefixWriter (os . Stdout , getRunPrefix (pkg ))}
143
+ res = & exclusiveWriter {O : textio .NewPrefixWriter (r . out , getRunPrefix (pkg ))}
138
144
r .writer [name ] = res
139
145
r .mu .Unlock ()
140
146
}
@@ -166,7 +172,7 @@ func (r *ConsoleReporter) BuildStarted(pkg *Package, status map[*Package]Package
166
172
i ++
167
173
}
168
174
sort .Slice (lines , func (i , j int ) bool { return lines [i ] < lines [j ] })
169
- tw := tabwriter .NewWriter (os . Stdout , 0 , 2 , 2 , ' ' , 0 )
175
+ tw := tabwriter .NewWriter (r . out , 0 , 2 , 2 , ' ' , 0 )
170
176
fmt .Fprintln (tw , strings .Join (lines , "" ))
171
177
tw .Flush ()
172
178
}
@@ -191,7 +197,7 @@ func (r *ConsoleReporter) PackageBuildStarted(pkg *Package) {
191
197
}
192
198
193
199
r .mu .Lock ()
194
- r .times [pkg .FullName ()] = time . Now ()
200
+ r .times [pkg .FullName ()] = r . now ()
195
201
r .mu .Unlock ()
196
202
197
203
_ , _ = io .WriteString (out , color .Sprintf ("<fg=yellow>build started</> <gray>(version %s)</>\n " , version ))
@@ -210,20 +216,32 @@ func (r *ConsoleReporter) PackageBuildFinished(pkg *Package, rep *PackageBuildRe
210
216
out := r .getWriter (pkg )
211
217
212
218
r .mu .Lock ()
213
- dur := time . Since (r .times [nme ])
219
+ dur := r . now (). Sub (r .times [nme ])
214
220
delete (r .writer , nme )
215
221
delete (r .times , nme )
216
222
r .mu .Unlock ()
217
223
224
+ // Format phase durations
225
+ var phaseDurations []string
226
+ for _ , phase := range rep .Phases {
227
+ if d := rep .PhaseDuration (phase ); d > 0 {
228
+ phaseDurations = append (phaseDurations , fmt .Sprintf ("%s: %.1fs" , phase , d .Seconds ()))
229
+ }
230
+ }
231
+ phaseDurStr := ""
232
+ if len (phaseDurations ) > 0 {
233
+ phaseDurStr = color .Sprintf (" [%s]" , strings .Join (phaseDurations , " | " ))
234
+ }
235
+
218
236
var msg string
219
- if rep .Error != nil {
237
+ if rep .Error != nil {
220
238
msg = color .Sprintf ("<red>package build failed while %sing</>\n <white>Reason:</> %s\n " , rep .LastPhase (), rep .Error )
221
239
} else {
222
240
var coverage string
223
241
if rep .TestCoverageAvailable {
224
242
coverage = color .Sprintf ("<fg=yellow>test coverage: %d%%</> <gray>(%d of %d functions have tests)</>\n " , rep .TestCoveragePercentage , rep .FunctionsWithTest , rep .FunctionsWithTest + rep .FunctionsWithoutTest )
225
243
}
226
- msg = color .Sprintf ("%s<green>package build succeded</> <gray>(%.2fs)</>\n " , coverage , dur .Seconds ())
244
+ msg = color .Sprintf ("%s<green>package build succeded</> <gray>(%.2fs)%s </>\n " , coverage , dur .Seconds (), phaseDurStr )
227
245
}
228
246
//nolint:errcheck
229
247
io .WriteString (out , msg )
0 commit comments