Skip to content

Commit 59bc751

Browse files
pohlyonsi
authored andcommitted
CurrentTreeConstructionNodeReport: fix for nested container nodes
In the following tree, the code cleaning up after the inner container node set a nil current construction node report, causing CurrentTreeConstructionNodeReport to panic with "CurrentConstructionNodeReport may only be called during construction of the spec tree" when called by the transformer for the following It: var _ = Describe("outer", func() { Context("inner", func() { ... }) It("works", func() { ... }) }) The fix is to restore the old value instead of setting to nil.
1 parent f331739 commit 59bc751

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

internal/node_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,28 @@ var _ = Describe("ConstructionNodeReport", func() {
21192119
actual := CurrentTreeConstructionNodeReport()
21202120
expect := newConstructionNodeReport(expectDescribeReport, []container{{"outer", outerLine + 1, []string{}, []string{}}, {"inner", outerLine + 2, []string{}, []string{}}})
21212121
expectEqual(actual, expect)
2122+
2123+
// The transformer runs while constructing the following It node.
2124+
// The assertion must be set up outside of it, after removing the transformer.
2125+
// The report must be the same.
2126+
remove := AddTreeConstructionNodeArgsTransformer(func(nodeType types.NodeType, offset Offset, text string, args []any) (string, []any, []error) {
2127+
actual = CurrentTreeConstructionNodeReport()
2128+
return text, args, nil
2129+
})
2130+
It("", func() {})
2131+
remove()
2132+
expectEqual(actual, expect)
2133+
})
2134+
2135+
var actual ConstructionNodeReport
2136+
expect := newConstructionNodeReport(expectDescribeReport, []container{{"outer", outerLine + 1, []string{}, []string{}}})
2137+
remove := AddTreeConstructionNodeArgsTransformer(func(nodeType types.NodeType, offset Offset, text string, args []any) (string, []any, []error) {
2138+
actual = CurrentTreeConstructionNodeReport()
2139+
return text, args, nil
21222140
})
2141+
It("", func() {})
2142+
remove()
2143+
expectEqual(actual, expect)
21232144
})
21242145
})
21252146

internal/suite.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,12 @@ func (suite *Suite) PushNode(node Node) error {
208208

209209
// Ensure that code running in the body of the container node
210210
// has access to information about the current container node(s).
211+
// The current one (nil in top-level container nodes, non-nil in an
212+
// embedded container node) gets restored when the node is done.
213+
oldConstructionNodeReport := suite.currentConstructionNodeReport
211214
suite.currentConstructionNodeReport = constructionNodeReportForTreeNode(suite.tree)
212215
defer func() {
213-
suite.currentConstructionNodeReport = nil
216+
suite.currentConstructionNodeReport = oldConstructionNodeReport
214217
}()
215218

216219
node.Body(nil)

0 commit comments

Comments
 (0)