Skip to content

Commit

Permalink
Add SpecContext to ReportAfterSuite callback body.
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenenosenko authored and onsi committed Feb 27, 2024
1 parent 9c771cd commit 5ff9d7f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
46 changes: 39 additions & 7 deletions internal/internal_integration/report_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
)

var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite nodes", func() {
var failInReportBeforeSuiteA, failInReportAfterSuiteA, interruptSuiteB bool
var failInReportBeforeSuiteA, failInReportAfterSuiteA, timeoutInReportAfterSuiteC, interruptSuiteB bool
var fixture func()

BeforeEach(func() {
failInReportBeforeSuiteA = false
failInReportAfterSuiteA = false
timeoutInReportAfterSuiteC = false
interruptSuiteB = false
conf.RandomSeed = 17
fixture = func() {
Expand Down Expand Up @@ -61,6 +62,19 @@ var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite node
writer.Print("gw-report-after-suite-B")
outputInterceptor.AppendInterceptedOutput("out-report-after-suite-B")
})
ReportAfterSuite("Report C", func(ctx SpecContext, report Report) {
timeout := 200 * time.Millisecond
if timeoutInReportAfterSuiteC {
timeout = timeout + 1*time.Second
}
rt.RunWithData("report-after-suite-C", "report", report)
writer.Print("gw-report-after-suite-C")
outputInterceptor.AppendInterceptedOutput("out-report-after-suite-C")
select {
case <-ctx.Done():
case <-time.After(timeout):
}
}, NodeTimeout(500*time.Millisecond))
AfterSuite(rt.T("after-suite", func() {
writer.Print("gw-after-suite")
F("fail in after-suite")
Expand All @@ -87,7 +101,7 @@ var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite node
"before-suite",
"A", "B", "C",
"after-suite",
"report-after-suite-A", "report-after-suite-B",
"report-after-suite-A", "report-after-suite-B", "report-after-suite-C",
))
})

Expand Down Expand Up @@ -159,7 +173,7 @@ var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite node
It("doesn't run any specs - just reporting functions", func() {
Ω(rt).Should(HaveTracked(
"report-before-suite-A", "report-before-suite-B",
"report-after-suite-A", "report-after-suite-B",
"report-after-suite-A", "report-after-suite-B", "report-after-suite-C",
))
})

Expand All @@ -176,6 +190,23 @@ var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite node
})
})

Context("when a ReportAfterSuiteContext times out", func() {
BeforeEach(func() {
timeoutInReportAfterSuiteC = true
success, _ := RunFixture("report-after-suite-C-timed-out", fixture)
Ω(success).Should(BeFalse())
})

It("reports on the failure, to Ginkgo's reporter and any subsequent reporters", func() {
Ω(reporter.Did.Find("Report C")).Should(HaveTimedOut(
types.NodeTypeReportAfterSuite,
"A node timeout occurred",
CapturedGinkgoWriterOutput("gw-report-after-suite-C"),
CapturedStdOutput("out-report-after-suite-C"),
))
})
})

Context("when a ReportAfterSuite node fails", func() {
BeforeEach(func() {
failInReportAfterSuiteA = true
Expand All @@ -189,7 +220,7 @@ var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite node
"before-suite",
"A", "B", "C",
"after-suite",
"report-after-suite-A", "report-after-suite-B",
"report-after-suite-A", "report-after-suite-B", "report-after-suite-C",
))
})

Expand Down Expand Up @@ -220,6 +251,7 @@ var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite node
"A", "B", "C",
"after-suite",
"report-after-suite-A",
"report-after-suite-C",
))
})
})
Expand Down Expand Up @@ -259,7 +291,7 @@ var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite node
"before-suite",
"A", "B", "C",
"after-suite",
"report-after-suite-A", "report-after-suite-B",
"report-after-suite-A", "report-after-suite-B", "report-after-suite-C",
))
})

Expand Down Expand Up @@ -309,7 +341,7 @@ var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite node

Context("when a non-primary proc disappears before it reports", func() {
BeforeEach(func() {
close(exitChannels[2]) //proc 2 disappears before reporting
close(exitChannels[2]) // proc 2 disappears before reporting
success, _ := RunFixture("disappearing-proc-2", fixture)
Ω(success).Should(BeFalse())
})
Expand Down Expand Up @@ -342,7 +374,7 @@ var _ = Describe("Sending reports to ReportBeforeSuite and ReportAfterSuite node
It("only runs the reporting nodes", func() {
Ω(rt).Should(HaveTracked(
"report-before-suite-A", "report-before-suite-B",
"report-after-suite-A", "report-after-suite-B",
"report-after-suite-A", "report-after-suite-B", "report-after-suite-C",
))
})

Expand Down
4 changes: 2 additions & 2 deletions internal/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"fmt"
"reflect"
"sort"
"time"

"sync"
"time"

"github.com/onsi/ginkgo/v2/types"
)
Expand Down Expand Up @@ -337,6 +336,7 @@ func NewNode(deprecationTracker *types.DeprecationTracker, nodeType types.NodeTy
node.ReportSuiteBody = func(_ SpecContext, r types.Report) { fn(r) }
} else {
node.ReportSuiteBody = arg.(func(SpecContext, types.Report))
node.HasContext = true
}
} else {
appendError(types.GinkgoErrors.MultipleBodyFunctions(node.CodeLocation, nodeType))
Expand Down
20 changes: 17 additions & 3 deletions reporting_dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,22 @@ func ReportBeforeSuite(body func(Report), args ...interface{}) bool {
}

/*
ReportAfterSuite nodes are run at the end of the suite. ReportAfterSuite nodes take a function that receives a suite Report.
ReportAfterSuite nodes are run at the end of the suite. ReportAfterSuite nodes execute at the suite's conclusion,
accepting a function that can either receives Report or both SpecContext and Report for interruptible behavior.
Example Usage:
ReportAfterSuite("Non-interruptible ReportAfterSuite", func(r Report) { })
ReportAfterSuite("Interruptible ReportAfterSuite", func(ctx SpecContext, r Report) { })
These nodes must be placed at the top-level of your test suite, ensuring they're not nested within Context, Describe, or When nodes, to maintain clear, hierarchical test structures.
In parallel test execution, Ginkgo ensures a singular ReportAfterSuite node runs, aggregating reports across all nodes for consistency.
ReportAfterSuite supports generating detailed suite reports programmatically and via CLI flags (--json-report, --junit-report, and --teamcity-report) for various report formats. However, nesting other Ginkgo nodes within ReportAfterSuite's closure is not permitted.
They are called at the end of the suite, after all specs have run and any AfterSuite or SynchronizedAfterSuite nodes, and are passed in the final report for the suite.
ReportAftersuite nodes must be created at the top-level (i.e. not nested in a Context/Describe/When node)
ReportAfterSuite nodes must be created at the top-level (i.e. not nested in a Context/Describe/When node)
When running in parallel, Ginkgo ensures that only one of the parallel nodes runs the ReportAfterSuite and that it is passed a report that is aggregated across
all parallel nodes
Expand All @@ -134,8 +146,10 @@ You cannot nest any other Ginkgo nodes within a ReportAfterSuite node's closure.
You can learn more about ReportAfterSuite here: https://onsi.github.io/ginkgo/#generating-reports-programmatically
You can learn more about Ginkgo's reporting infrastructure, including generating reports with the CLI here: https://onsi.github.io/ginkgo/#generating-machine-readable-reports
You can learn about interruptible nodes here: https://onsi.github.io/ginkgo/#spec-timeouts-and-interruptible-nodes
*/
func ReportAfterSuite(text string, body func(Report), args ...interface{}) bool {
func ReportAfterSuite(text string, body any, args ...interface{}) bool {
combinedArgs := []interface{}{body}
combinedArgs = append(combinedArgs, args...)
return pushNode(internal.NewNode(deprecationTracker, types.NodeTypeReportAfterSuite, text, combinedArgs...))
Expand Down

0 comments on commit 5ff9d7f

Please sign in to comment.