Skip to content

Commit c78a9f4

Browse files
committed
extract writeEventStr
1 parent b3a3394 commit c78a9f4

File tree

1 file changed

+64
-103
lines changed

1 file changed

+64
-103
lines changed

testjson/multipkg.go

Lines changed: 64 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ import (
1515
)
1616

1717
type PkgTracker struct {
18-
lastPkg string
19-
col int
18+
withWallTime bool
19+
lastPkg string
20+
col int
2021

2122
startTime time.Time
2223
pkgs map[string]*pkgLine
2324
}
2425

2526
type pkgLine struct {
26-
pkg string
27+
path string
2728
event TestEvent
2829
lastUpdate time.Time
2930
}
@@ -40,24 +41,11 @@ func shouldJoinPkgs(lastPkg, pkg string) (join bool, commonPrefix string) {
4041
return false, ""
4142
}
4243

43-
func isEmpty(event TestEvent, exec *Execution) bool {
44-
pkg := exec.Package(event.Package)
45-
switch event.Action {
46-
case ActionSkip:
47-
return true
48-
case ActionPass:
49-
if pkg.Total == 0 {
50-
return true
51-
}
52-
}
53-
return false
54-
}
55-
5644
var colorRe = regexp.MustCompile(`\x1b\[[0-9;]*m`)
5745

5846
func multiPkgNameFormat(out io.Writer, opts FormatOptions, withFailures, withWallTime bool) eventFormatterFunc {
5947
buf := bufio.NewWriter(out)
60-
pkgTracker := &PkgTracker{startTime: time.Now()}
48+
pt := &PkgTracker{startTime: time.Now(), withWallTime: withWallTime}
6149

6250
w, _, err := term.GetSize(int(os.Stdout.Fd()))
6351
if err != nil || w == 0 {
@@ -67,10 +55,10 @@ func multiPkgNameFormat(out io.Writer, opts FormatOptions, withFailures, withWal
6755
return func(event TestEvent, exec *Execution) error {
6856
if !event.PackageEvent() {
6957
if event.Action == ActionFail && withFailures {
70-
if pkgTracker.col > 0 {
58+
if pt.col > 0 {
7159
buf.WriteString("\n")
7260
}
73-
pkgTracker.col = 0
61+
pt.col = 0
7462
pkg := exec.Package(event.Package)
7563
tc := pkg.LastFailedByName(event.Test)
7664
pkg.WriteOutputTo(buf, tc.ID) // nolint:errcheck
@@ -79,57 +67,62 @@ func multiPkgNameFormat(out io.Writer, opts FormatOptions, withFailures, withWal
7967
return nil
8068
}
8169

82-
// Remove newline from shortFormatPackageEvent
70+
pkgPath := RelativePackagePath(event.Package)
71+
8372
eventStr := strings.TrimSuffix(shortFormatPackageEvent(opts, event, exec), "\n")
8473
if eventStr == "" {
8574
return nil
8675
}
87-
//eventStr = fmt.Sprintf("%d/%d %s", pkgTracker.col, w, eventStr)
8876

89-
pkgPath := RelativePackagePath(event.Package)
90-
join, commonPrefix := shouldJoinPkgs(pkgTracker.lastPkg, pkgPath)
91-
if event.Action == ActionFail {
92-
join = false
93-
}
77+
elapsed := time.Since(pt.startTime).Round(time.Millisecond)
78+
pt.writeEventStr(pkgPath, eventStr, event, w, buf, elapsed)
79+
return buf.Flush()
80+
}
81+
}
9482

95-
if join {
96-
eventStrJoin := strings.ReplaceAll(eventStr, commonPrefix, "")
97-
eventStrJoin = strings.ReplaceAll(eventStrJoin, " ", " ")
98-
noColorJoinStr := colorRe.ReplaceAllString(eventStr, "")
99-
if pkgTracker.col == 0 || pkgTracker.col+len([]rune(noColorJoinStr)) >= w {
100-
join = false
101-
eventStr = strings.ReplaceAll(eventStr, commonPrefix, "…/")
102-
} else {
103-
eventStr = eventStrJoin
104-
}
105-
}
106-
if join {
107-
buf.WriteString(" ")
108-
pkgTracker.col++
109-
} else {
110-
buf.WriteString("\n")
111-
pkgTracker.col = 0
83+
func (pt *PkgTracker) writeEventStr(pkgPath string, eventStr string, event TestEvent, w int, buf io.StringWriter, elapsed time.Duration) {
84+
join, commonPrefix := shouldJoinPkgs(pt.lastPkg, pkgPath)
85+
pt.lastPkg = pkgPath
86+
if event.Action == ActionFail {
87+
// put failures on new lines, to include full package name
88+
join = false
89+
}
11290

113-
if withWallTime {
114-
elapsed := time.Since(pkgTracker.startTime).Round(time.Millisecond)
115-
eventStr = fmtElapsed(elapsed, false) + eventStr
116-
}
91+
if join {
92+
eventStrJoin := strings.ReplaceAll(eventStr, commonPrefix, "")
93+
eventStrJoin = strings.ReplaceAll(eventStrJoin, " ", " ")
94+
noColorJoinStr := colorRe.ReplaceAllString(eventStr, "")
95+
if pt.col == 0 || pt.col+len([]rune(noColorJoinStr)) >= w {
96+
join = false
97+
eventStr = strings.ReplaceAll(eventStr, commonPrefix, "…/")
98+
} else {
99+
eventStr = eventStrJoin
117100
}
118-
pkgTracker.lastPkg = pkgPath
119-
noColorStr := colorRe.ReplaceAllString(eventStr, "")
120-
pkgTracker.col += len([]rune(noColorStr))
101+
}
102+
if join {
103+
buf.WriteString(" ")
104+
pt.col++
105+
} else {
106+
buf.WriteString("\n")
107+
pt.col = 0
121108

122-
buf.WriteString(eventStr) // nolint:errcheck
123-
return buf.Flush()
109+
if pt.withWallTime {
110+
eventStr = fmtElapsed(elapsed, false) + eventStr
111+
}
124112
}
113+
noColorStr := colorRe.ReplaceAllString(eventStr, "")
114+
pt.col += len([]rune(noColorStr))
115+
116+
buf.WriteString(eventStr) // nolint:errcheck
125117
}
126118

127119
// ---
128120

129121
func multiPkgNameFormat2(out io.Writer, opts FormatOptions, withFailures, withWallTime bool) eventFormatterFunc {
130122
pkgTracker := &PkgTracker{
131-
startTime: time.Now(),
132-
pkgs: map[string]*pkgLine{},
123+
startTime: time.Now(),
124+
withWallTime: withWallTime,
125+
pkgs: map[string]*pkgLine{},
133126
}
134127

135128
writer := dotwriter.New(out)
@@ -143,7 +136,7 @@ func multiPkgNameFormat2(out io.Writer, opts FormatOptions, withFailures, withWa
143136
failBuf := bufio.NewWriter(writer)
144137
pkg.WriteOutputTo(failBuf, tc.ID) // nolint:errcheck
145138
failBuf.Flush() // nolint:errcheck
146-
writer.Flush()
139+
writer.Flush() // nolint:errcheck
147140
writer = dotwriter.New(out)
148141
// continue to mark the package as failed early
149142
//return pkgTracker.flush(writer, opts, exec, withWallTime)
@@ -164,7 +157,7 @@ func multiPkgNameFormat2(out io.Writer, opts FormatOptions, withFailures, withWa
164157
}
165158

166159
pkgTracker.pkgs[pkgPath] = &pkgLine{
167-
pkg: pkgPath,
160+
path: pkgPath,
168161
event: event,
169162
lastUpdate: time.Now(),
170163
}
@@ -174,9 +167,9 @@ func multiPkgNameFormat2(out io.Writer, opts FormatOptions, withFailures, withWa
174167

175168
func (pt *PkgTracker) flush(writer *dotwriter.Writer, opts FormatOptions, exec *Execution,
176169
withWallTime bool) error {
177-
writer.Write([]byte("\n\n"))
170+
//writer.Write([]byte("\n"))
178171

179-
var pkgPaths []string
172+
var pkgPaths []string // nolint:prealloc
180173
for pkgPath := range pt.pkgs {
181174
pkgPaths = append(pkgPaths, pkgPath)
182175
}
@@ -195,7 +188,7 @@ func (pt *PkgTracker) flush(writer *dotwriter.Writer, opts FormatOptions, exec *
195188
lastPkg = pkgPath
196189
}
197190

198-
var groupPaths []string
191+
var groupPaths []string // nolint:prealloc
199192
for groupPath := range groupPkgs {
200193
groupPaths = append(groupPaths, groupPath)
201194
}
@@ -210,55 +203,23 @@ func (pt *PkgTracker) flush(writer *dotwriter.Writer, opts FormatOptions, exec *
210203

211204
for _, groupPath := range groupPaths {
212205
pkgs := groupPkgs[groupPath]
213-
lastPkg := ""
214-
col := 0
215-
for pkgI, pkg := range pkgs {
216-
join, commonPrefix := shouldJoinPkgs(lastPkg, pkg.pkg)
217-
lastPkg = pkg.pkg
218-
if pkg.event.Action == ActionFail {
219-
// put failures on new lines, to include full package name
220-
join = false
221-
}
222-
223-
eventStr := strings.TrimSuffix(shortFormatPackageEvent(opts, pkg.event, exec), "\n")
224-
if join {
225-
eventStrJoin := strings.ReplaceAll(eventStr, commonPrefix, "")
226-
eventStrJoin = strings.ReplaceAll(eventStrJoin, " ", " ")
227-
noColorJoinStr := colorRe.ReplaceAllString(eventStr, "")
228-
if col+len([]rune(noColorJoinStr)) >= w {
229-
join = false
230-
eventStr = strings.ReplaceAll(eventStr, commonPrefix, "…/")
231-
} else {
232-
eventStr = eventStrJoin
233-
}
234-
}
235-
if join {
236-
buf.WriteString(" ")
237-
col++
238-
} else {
239-
if pkgI > 0 {
240-
buf.WriteString("\n")
241-
}
242-
col = 0
243-
244-
if withWallTime {
245-
var lastUpdatePkg *pkgLine
246-
for _, p := range pkgs {
247-
if lastUpdatePkg == nil || p.lastUpdate.After(lastUpdatePkg.lastUpdate) {
248-
lastUpdatePkg = p
249-
}
250-
}
251-
elapsed := lastUpdatePkg.lastUpdate.Sub(pt.startTime)
252-
eventStr = fmtElapsed(elapsed, false) + eventStr
206+
pt.lastPkg = ""
207+
pt.col = 0
208+
for _, pkg := range pkgs {
209+
var lastUpdatePkg *pkgLine
210+
for _, p := range pkgs {
211+
if lastUpdatePkg == nil || p.lastUpdate.After(lastUpdatePkg.lastUpdate) {
212+
lastUpdatePkg = p
253213
}
254214
}
255-
noColorStr := colorRe.ReplaceAllString(eventStr, "")
256-
col += len([]rune(noColorStr))
215+
elapsed := lastUpdatePkg.lastUpdate.Sub(pt.startTime).Round(time.Millisecond)
257216

258-
buf.WriteString(eventStr) // nolint:errcheck
217+
event := pkg.event
218+
eventStr := strings.TrimSuffix(shortFormatPackageEvent(opts, event, exec), "\n")
219+
pt.writeEventStr(pkg.path, eventStr, event, w, buf, elapsed)
259220
}
260-
buf.WriteString("\n")
261221
}
222+
buf.WriteString("\n")
262223
buf.Flush() // nolint:errcheck
263224
PrintSummary(writer, exec, SummarizeNone)
264225
return writer.Flush()

0 commit comments

Comments
 (0)