Skip to content

Commit febbe38

Browse files
committed
don't run ReportEntries through sprintf
This would break ReportEntries that included characters like %
1 parent 11a4860 commit febbe38

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

formatter/formatter.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ func (f Formatter) Fi(indentation uint, format string, args ...interface{}) stri
120120
}
121121

122122
func (f Formatter) Fiw(indentation uint, maxWidth uint, format string, args ...interface{}) string {
123-
out := fmt.Sprintf(f.style(format), args...)
123+
out := f.style(format)
124+
if len(args) > 0 {
125+
out = fmt.Sprintf(out, args...)
126+
}
124127

125128
if indentation == 0 && maxWidth == 0 {
126129
return out

formatter/formatter_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ var _ = Describe("Formatter", func() {
123123
It("transforms the color information and sprintfs", func() {
124124
Ω(f.F("{{green}}hi there {{cyan}}%d {{yellow}}%s{{/}}", 3, "wise men")).Should(Equal("\x1b[38;5;10mhi there \x1b[38;5;14m3 \x1b[38;5;11mwise men\x1b[0m"))
125125
})
126+
127+
It("avoids sprintf if there are no additional arguments", func() {
128+
Ω(f.F("{{green}}hi there {{cyan}}%d {{yellow}}%s{{/}}")).Should(Equal("\x1b[38;5;10mhi there \x1b[38;5;14m%d \x1b[38;5;11m%s\x1b[0m"))
129+
})
126130
})
127131

128132
Describe("Fi", func() {

reporters/default_reporter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ func (r *DefaultReporter) EmitReportEntry(entry types.ReportEntry) {
528528
}
529529

530530
func (r *DefaultReporter) emitReportEntry(indent uint, entry types.ReportEntry) {
531-
r.emitBlock(r.fi(indent, "{{bold}}"+entry.Name+"{{gray}} - %s @ %s{{/}}", entry.Location, entry.Time.Format(types.GINKGO_TIME_FORMAT)))
531+
r.emitBlock(r.fi(indent, "{{bold}}"+entry.Name+"{{gray}} "+fmt.Sprintf("- %s @ %s{{/}}", entry.Location, entry.Time.Format(types.GINKGO_TIME_FORMAT))))
532532
if representation := entry.StringRepresentation(); representation != "" {
533533
r.emitBlock(r.fi(indent+1, representation))
534534
}

reporters/default_reporter_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,14 @@ var _ = Describe("DefaultReporter", func() {
26422642
" {{coral}}Is beautiful{{/}}",
26432643
"",
26442644
),
2645+
//correctly handling reports that have format string components
2646+
Entry("emits the report without running it through sprintf",
2647+
C(Verbose),
2648+
RE("my %f report", cl0, "{{green}}my report http://example.com/?q=%d%3%%{{/}}", cl0),
2649+
spr(" {{bold}}my %%f report{{gray}} - cl0.go:12 @ %s{{/}}", FORMATTED_TIME),
2650+
" {{green}}my report http://example.com/?q=%d%3%%{{/}}",
2651+
"",
2652+
),
26452653
)
26462654

26472655
DescribeTable("EmitSpecEvent",

0 commit comments

Comments
 (0)