Skip to content

Commit

Permalink
don't run ReportEntries through sprintf
Browse files Browse the repository at this point in the history
This would break ReportEntries that included characters like %
  • Loading branch information
onsi committed Feb 13, 2023
1 parent 11a4860 commit febbe38
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ func (f Formatter) Fi(indentation uint, format string, args ...interface{}) stri
}

func (f Formatter) Fiw(indentation uint, maxWidth uint, format string, args ...interface{}) string {
out := fmt.Sprintf(f.style(format), args...)
out := f.style(format)
if len(args) > 0 {
out = fmt.Sprintf(out, args...)
}

if indentation == 0 && maxWidth == 0 {
return out
Expand Down
4 changes: 4 additions & 0 deletions formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ var _ = Describe("Formatter", func() {
It("transforms the color information and sprintfs", func() {
Ω(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"))
})

It("avoids sprintf if there are no additional arguments", func() {
Ω(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"))
})
})

Describe("Fi", func() {
Expand Down
2 changes: 1 addition & 1 deletion reporters/default_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ func (r *DefaultReporter) EmitReportEntry(entry types.ReportEntry) {
}

func (r *DefaultReporter) emitReportEntry(indent uint, entry types.ReportEntry) {
r.emitBlock(r.fi(indent, "{{bold}}"+entry.Name+"{{gray}} - %s @ %s{{/}}", entry.Location, entry.Time.Format(types.GINKGO_TIME_FORMAT)))
r.emitBlock(r.fi(indent, "{{bold}}"+entry.Name+"{{gray}} "+fmt.Sprintf("- %s @ %s{{/}}", entry.Location, entry.Time.Format(types.GINKGO_TIME_FORMAT))))
if representation := entry.StringRepresentation(); representation != "" {
r.emitBlock(r.fi(indent+1, representation))
}
Expand Down
8 changes: 8 additions & 0 deletions reporters/default_reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2642,6 +2642,14 @@ var _ = Describe("DefaultReporter", func() {
" {{coral}}Is beautiful{{/}}",
"",
),
//correctly handling reports that have format string components
Entry("emits the report without running it through sprintf",
C(Verbose),
RE("my %f report", cl0, "{{green}}my report http://example.com/?q=%d%3%%{{/}}", cl0),
spr(" {{bold}}my %%f report{{gray}} - cl0.go:12 @ %s{{/}}", FORMATTED_TIME),
" {{green}}my report http://example.com/?q=%d%3%%{{/}}",
"",
),
)

DescribeTable("EmitSpecEvent",
Expand Down

0 comments on commit febbe38

Please sign in to comment.