From febbe3896353b8eec798833d9af4ef16427646c9 Mon Sep 17 00:00:00 2001 From: Onsi Fakhouri Date: Mon, 13 Feb 2023 13:09:47 -0700 Subject: [PATCH] don't run ReportEntries through sprintf This would break ReportEntries that included characters like % --- formatter/formatter.go | 5 ++++- formatter/formatter_test.go | 4 ++++ reporters/default_reporter.go | 2 +- reporters/default_reporter_test.go | 8 ++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/formatter/formatter.go b/formatter/formatter.go index ece35d17b..743555dde 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -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 diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 17503d7dd..036d95c71 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -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() { diff --git a/reporters/default_reporter.go b/reporters/default_reporter.go index 7a27220ca..1425825f7 100644 --- a/reporters/default_reporter.go +++ b/reporters/default_reporter.go @@ -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)) } diff --git a/reporters/default_reporter_test.go b/reporters/default_reporter_test.go index 056546430..27d357159 100644 --- a/reporters/default_reporter_test.go +++ b/reporters/default_reporter_test.go @@ -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",