Skip to content

Commit e8a2056

Browse files
lahabanaonsi
authored andcommittedMar 18, 2024
add --github-output for nicer output in github actions
Leverage github's special formats to output nicer output https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#using-workflow-commands-to-access-toolkit-functions Fix #1372 Signed-off-by: Charly Molter <charly.molter@konghq.com>
1 parent 977bc6f commit e8a2056

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed
 

‎docs/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -3382,6 +3382,7 @@ When you [filter specs](#filtering-specs) using Ginkgo's various filtering mecha
33823382
Here are a grab bag of other settings:
33833383

33843384
You can disable Ginkgo's color output by running `ginkgo --no-color`.
3385+
You can also output in a format that makes it easier to read in github actions console by running `ginkgo --github-output`.
33853386

33863387
By default, Ginkgo only emits full stack traces when a spec panics. When a normal assertion failure occurs, Ginkgo simply emits the line at which the failure occurred. You can, instead, have Ginkgo always emit the full stack trace by running `ginkgo --trace`.
33873388

@@ -3706,6 +3707,8 @@ Here's why:
37063707
- `--timeout` allows you to specify a timeout for the `ginkgo` run. The default duration is one hour, which may or may not be enough!
37073708
- `--poll-progress-after` and `--poll-progress-interval` will allow you to learn where long-running specs are getting stuck. Choose a values for `X` and `Y` that are appropriate to your suite. A long-running integration suite, for example, might set `X` to `120s` and `Y` to `30s` - whereas a quicker set of unit tests might not need this setting. Note that if you precompile suites and run them from a different directory relative to your source code, you may also need to set `--source-root` to enable Ginkgo to emit source code lines when generating progress reports.
37083709

3710+
If running on Github actions: `--github-output` will make the output more readable in the Github actions console.
3711+
37093712
### Supporting Custom Suite Configuration
37103713

37113714
There are contexts where you may want to change some aspects of a suite's behavior based on user-provided configuration. There are two widely adopted means of doing this: environment variables and command-line flags.

‎reporters/default_reporter.go

+32-15
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,22 @@ func (r *DefaultReporter) WillRun(report types.SpecReport) {
182182
r.emitBlock(r.f(r.codeLocationBlock(report, "{{/}}", v.Is(types.VerbosityLevelVeryVerbose), false)))
183183
}
184184

185+
func (r *DefaultReporter) wrapTextBlock(sectionName string, fn func()) {
186+
r.emitBlock("\n")
187+
if r.conf.GithubOutput {
188+
r.emitBlock(r.fi(1, "::group::%s", sectionName))
189+
} else {
190+
r.emitBlock(r.fi(1, "{{gray}}<< %s{{/}}", sectionName))
191+
}
192+
fn()
193+
if r.conf.GithubOutput {
194+
r.emitBlock(r.fi(1, "::endgroup::"))
195+
} else {
196+
r.emitBlock(r.fi(1, "{{gray}}%s >>{{/}}", sectionName))
197+
}
198+
199+
}
200+
185201
func (r *DefaultReporter) DidRun(report types.SpecReport) {
186202
v := r.conf.Verbosity()
187203
inParallel := report.RunningInParallel
@@ -283,26 +299,23 @@ func (r *DefaultReporter) DidRun(report types.SpecReport) {
283299

284300
//Emit Stdout/Stderr Output
285301
if showSeparateStdSection {
286-
r.emitBlock("\n")
287-
r.emitBlock(r.fi(1, "{{gray}}Captured StdOut/StdErr Output >>{{/}}"))
288-
r.emitBlock(r.fi(1, "%s", report.CapturedStdOutErr))
289-
r.emitBlock(r.fi(1, "{{gray}}<< Captured StdOut/StdErr Output{{/}}"))
302+
r.wrapTextBlock("Captured StdOut/StdErr Output", func() {
303+
r.emitBlock(r.fi(1, "%s", report.CapturedStdOutErr))
304+
})
290305
}
291306

292307
if showSeparateVisibilityAlwaysReportsSection {
293-
r.emitBlock("\n")
294-
r.emitBlock(r.fi(1, "{{gray}}Report Entries >>{{/}}"))
295-
for _, entry := range report.ReportEntries.WithVisibility(types.ReportEntryVisibilityAlways) {
296-
r.emitReportEntry(1, entry)
297-
}
298-
r.emitBlock(r.fi(1, "{{gray}}<< Report Entries{{/}}"))
308+
r.wrapTextBlock("Report Entries", func() {
309+
for _, entry := range report.ReportEntries.WithVisibility(types.ReportEntryVisibilityAlways) {
310+
r.emitReportEntry(1, entry)
311+
}
312+
})
299313
}
300314

301315
if showTimeline {
302-
r.emitBlock("\n")
303-
r.emitBlock(r.fi(1, "{{gray}}Timeline >>{{/}}"))
304-
r.emitTimeline(1, report, timeline)
305-
r.emitBlock(r.fi(1, "{{gray}}<< Timeline{{/}}"))
316+
r.wrapTextBlock("Timeline", func() {
317+
r.emitTimeline(1, report, timeline)
318+
})
306319
}
307320

308321
// Emit Failure Message
@@ -405,7 +418,11 @@ func (r *DefaultReporter) emitShortFailure(indent uint, state types.SpecState, f
405418
func (r *DefaultReporter) emitFailure(indent uint, state types.SpecState, failure types.Failure, includeAdditionalFailure bool) {
406419
highlightColor := r.highlightColorForState(state)
407420
r.emitBlock(r.fi(indent, highlightColor+"[%s] %s{{/}}", r.humanReadableState(state), failure.Message))
408-
r.emitBlock(r.fi(indent, highlightColor+"In {{bold}}[%s]{{/}}"+highlightColor+" at: {{bold}}%s{{/}} {{gray}}@ %s{{/}}\n", failure.FailureNodeType, failure.Location, failure.TimelineLocation.Time.Format(types.GINKGO_TIME_FORMAT)))
421+
if r.conf.GithubOutput {
422+
r.emitBlock(r.fi(indent, "::error file=%s,line=%d::%s %s", failure.Location.FileName, failure.Location.LineNumber, failure.FailureNodeType, failure.TimelineLocation.Time.Format(types.GINKGO_TIME_FORMAT)))
423+
} else {
424+
r.emitBlock(r.fi(indent, highlightColor+"In {{bold}}[%s]{{/}}"+highlightColor+" at: {{bold}}%s{{/}} {{gray}}@ %s{{/}}\n", failure.FailureNodeType, failure.Location, failure.TimelineLocation.Time.Format(types.GINKGO_TIME_FORMAT)))
425+
}
409426
if failure.ForwardedPanic != "" {
410427
r.emitBlock("\n")
411428
r.emitBlock(r.fi(indent, highlightColor+"%s{{/}}", failure.ForwardedPanic))

‎types/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type ReporterConfig struct {
8989
VeryVerbose bool
9090
FullTrace bool
9191
ShowNodeEvents bool
92+
GithubOutput bool
9293

9394
JSONReport string
9495
JUnitReport string
@@ -331,6 +332,8 @@ var ReporterConfigFlags = GinkgoFlags{
331332
Usage: "If set, default reporter prints out the full stack trace when a failure occurs"},
332333
{KeyPath: "R.ShowNodeEvents", Name: "show-node-events", SectionKey: "output",
333334
Usage: "If set, default reporter prints node > Enter and < Exit events when specs fail"},
335+
{KeyPath: "R.GithubOutput", Name: "github-output", SectionKey: "output",
336+
Usage: "If set, default reporter prints easier to manage output in Github Actions."},
334337

335338
{KeyPath: "R.JSONReport", Name: "json-report", UsageArgument: "filename.json", SectionKey: "output",
336339
Usage: "If set, Ginkgo will generate a JSON-formatted test report at the specified location."},

0 commit comments

Comments
 (0)
Please sign in to comment.