Skip to content

Commit 665e7ba

Browse files
committed
AroundNode supports simpler signatures too
1 parent d41f01e commit 665e7ba

File tree

11 files changed

+188
-101
lines changed

11 files changed

+188
-101
lines changed

core_dsl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ func RunSpecs(t GinkgoTestingT, description string, args ...any) bool {
330330
return passed
331331
}
332332

333-
func extractSuiteConfiguration(args []any) (Labels, SemVerConstraints, internal.AroundNodes) {
333+
func extractSuiteConfiguration(args []any) (Labels, SemVerConstraints, types.AroundNodes) {
334334
suiteLabels := Labels{}
335335
suiteSemVerConstraints := SemVerConstraints{}
336-
aroundNodes := internal.AroundNodes{}
336+
aroundNodes := types.AroundNodes{}
337337
configErrors := []error{}
338338
for _, arg := range args {
339339
switch arg := arg.(type) {
@@ -345,7 +345,7 @@ func extractSuiteConfiguration(args []any) (Labels, SemVerConstraints, internal.
345345
suiteLabels = append(suiteLabels, arg...)
346346
case SemVerConstraints:
347347
suiteSemVerConstraints = append(suiteSemVerConstraints, arg...)
348-
case internal.AroundNode:
348+
case types.AroundNodeDecorator:
349349
aroundNodes = append(aroundNodes, arg)
350350
default:
351351
configErrors = append(configErrors, types.GinkgoErrors.UnknownTypePassedToRunSpecs(arg))

decorator_dsl.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ginkgo
22

33
import (
44
"github.com/onsi/ginkgo/v2/internal"
5+
"github.com/onsi/ginkgo/v2/types"
56
)
67

78
/*
@@ -169,4 +170,6 @@ For example, each Ginkgo node runs in its own goroutine. Some linux namespace c
169170
170171
WIP
171172
*/
172-
var AroundNode = internal.BuildAroundNode
173+
func AroundNode[F types.AroundNodeAllowedFuncs](f F) types.AroundNodeDecorator {
174+
return types.AroundNode(f, types.NewCodeLocation(1))
175+
}

dsl/decorators/decorators_dsl.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package decorators
1414

1515
import (
1616
"github.com/onsi/ginkgo/v2"
17+
"github.com/onsi/ginkgo/v2/types"
1718
)
1819

1920
type Offset = ginkgo.Offset
@@ -37,4 +38,7 @@ const SuppressProgressReporting = ginkgo.SuppressProgressReporting
3738

3839
var Label = ginkgo.Label
3940
var SemVerConstraint = ginkgo.SemVerConstraint
40-
var AroundNode = ginkgo.AroundNode
41+
42+
func AroundNode[F types.AroundNodeAllowedFuncs](f F) types.AroundNodeDecorator {
43+
return types.AroundNode(f, types.NewCodeLocation(1))
44+
}

internal/around_node.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,21 @@ func ComputeAroundNodes(specs Specs) Specs {
99
for _, spec := range specs {
1010
nodes := Nodes{}
1111
currentNestingLevel := 0
12-
aroundNodes := []AroundNode{}
12+
aroundNodes := types.AroundNodes{}
1313
nestingLevelIndices := []int{}
1414
for _, node := range spec.Nodes {
1515
switch node.NodeType {
1616
case types.NodeTypeContainer:
1717
currentNestingLevel = node.NestingLevel + 1
1818
nestingLevelIndices = append(nestingLevelIndices, len(aroundNodes))
19-
aroundNodes = append(aroundNodes, node.AroundNodes...)
19+
aroundNodes = aroundNodes.Append(node.AroundNodes...)
2020
nodes = append(nodes, node)
2121
default:
2222
if currentNestingLevel > node.NestingLevel {
2323
currentNestingLevel = node.NestingLevel
2424
aroundNodes = aroundNodes[:nestingLevelIndices[currentNestingLevel]]
2525
}
26-
nodeAroundNodes := []AroundNode{}
27-
nodeAroundNodes = append(nodeAroundNodes, aroundNodes...)
28-
nodeAroundNodes = append(nodeAroundNodes, node.AroundNodes...)
29-
node.AroundNodes = nodeAroundNodes
26+
node.AroundNodes = types.AroundNodes{}.Append(aroundNodes...).Append(node.AroundNodes...)
3027
nodes = append(nodes, node)
3128
}
3229
}

internal/internal_integration/around_node_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import (
55
"time"
66

77
. "github.com/onsi/ginkgo/v2"
8-
"github.com/onsi/ginkgo/v2/internal"
98
. "github.com/onsi/ginkgo/v2/internal/test_helpers"
9+
"github.com/onsi/ginkgo/v2/types"
1010
. "github.com/onsi/gomega"
1111
)
1212

13-
func AN(run string) internal.AroundNode {
14-
return AroundNode(func(ctx context.Context, body func(ctx context.Context)) {
13+
func AN(run string) types.AroundNodeDecorator {
14+
return AroundNode(func() {
1515
rt.Run(run)
16-
body(ctx)
1716
})
1817
}
1918

@@ -133,10 +132,9 @@ var _ = Describe("The AroundNode decorator", func() {
133132
rt.Run("SBS-all")
134133
Ω(ctx.Value("wrapped")).Should(Equal("value"))
135134
Ω(data).Should(Equal([]byte("data")))
136-
}, AroundNode(func(ctx context.Context, body func(ctx context.Context)) {
135+
}, AroundNode(func(ctx context.Context) context.Context {
137136
rt.Run("SBS-around")
138-
newCtx = context.WithValue(ctx, "wrapped", "value")
139-
body(newCtx)
137+
return context.WithValue(ctx, "wrapped", "value")
140138
}))
141139
It("runs", rt.T("A"))
142140
}, AN("suite-around-1"))

internal/internal_integration/internal_integration_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func SetUpForParallel(parallelTotal int) {
8888
conf.ParallelHost = server.Address()
8989
}
9090

91-
func RunFixture(description string, callback func(), aroundNodes ...internal.AroundNode) (bool, bool) {
91+
func RunFixture(description string, callback func(), aroundNodes ...types.AroundNodeDecorator) (bool, bool) {
9292
suite := internal.NewSuite()
9393
var success, hasProgrammaticFocus bool
9494
WithSuite(suite, func() {

internal/node.go

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type Node struct {
6161
NodeTimeout time.Duration
6262
SpecTimeout time.Duration
6363
GracePeriod time.Duration
64-
AroundNodes AroundNodes
64+
AroundNodes types.AroundNodes
6565

6666
NodeIDWhereCleanupWasGenerated uint
6767
}
@@ -87,51 +87,24 @@ type FlakeAttempts uint
8787
type MustPassRepeatedly uint
8888
type Offset uint
8989
type Done chan<- any // Deprecated Done Channel for asynchronous testing
90-
type Labels []string
91-
type SemVerConstraints []string
9290
type PollProgressInterval time.Duration
9391
type PollProgressAfter time.Duration
9492
type NodeTimeout time.Duration
9593
type SpecTimeout time.Duration
9694
type GracePeriod time.Duration
9795

98-
func BuildAroundNode(f AroundNodeFunc) AroundNode {
99-
return AroundNode{
100-
Body: f,
101-
CodeLocation: types.NewCodeLocation(1),
102-
}
103-
}
104-
105-
type AroundNodeFunc func(ctx context.Context, body func(ctx context.Context))
106-
107-
type AroundNode struct {
108-
Body AroundNodeFunc
109-
CodeLocation types.CodeLocation
110-
}
111-
112-
type AroundNodes []AroundNode
96+
type Labels []string
11397

11498
func (l Labels) MatchesLabelFilter(query string) bool {
11599
return types.MustParseLabelFilter(query)(l)
116100
}
117101

102+
type SemVerConstraints []string
103+
118104
func (svc SemVerConstraints) MatchesSemVerFilter(version string) bool {
119105
return types.MustParseSemVerFilter(version)(svc)
120106
}
121107

122-
func (an AroundNodes) Clone() AroundNodes {
123-
out := make(AroundNodes, len(an))
124-
copy(out, an)
125-
return out
126-
}
127-
128-
func (an AroundNodes) Append(other ...AroundNode) AroundNodes {
129-
out := make(AroundNodes, len(an)+len(other))
130-
copy(out, an)
131-
copy(out[len(an):], other)
132-
return out
133-
}
134-
135108
func unionOf[S ~[]E, E comparable](slices ...S) S {
136109
out := S{}
137110
seen := map[E]bool{}
@@ -207,7 +180,7 @@ func isDecoration(arg any) bool {
207180
return true
208181
case t == reflect.TypeOf(GracePeriod(0)):
209182
return true
210-
case t == reflect.TypeOf(AroundNode{}):
183+
case t == reflect.TypeOf(types.AroundNodeDecorator{}):
211184
return true
212185
case t.Kind() == reflect.Slice && isSliceOfDecorations(arg):
213186
return true
@@ -349,8 +322,8 @@ func NewNode(deprecationTracker *types.DeprecationTracker, nodeType types.NodeTy
349322
if nodeType.Is(types.NodeTypeContainer) {
350323
appendError(types.GinkgoErrors.InvalidDecoratorForNodeType(node.CodeLocation, nodeType, "GracePeriod"))
351324
}
352-
case t == reflect.TypeOf(AroundNode{}):
353-
node.AroundNodes = append(node.AroundNodes, arg.(AroundNode))
325+
case t == reflect.TypeOf(types.AroundNodeDecorator{}):
326+
node.AroundNodes = append(node.AroundNodes, arg.(types.AroundNodeDecorator))
354327
case t == reflect.TypeOf(Labels{}):
355328
if !nodeType.Is(types.NodeTypesForContainerAndIt) {
356329
appendError(types.GinkgoErrors.InvalidDecoratorForNodeType(node.CodeLocation, nodeType, "Label"))

internal/node_test.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,50 +1228,6 @@ var _ = Describe("Node", func() {
12281228
})
12291229
})
12301230

1231-
var _ = Describe("AroundNodes", func() {
1232-
Describe("building an AroundNode", func() {
1233-
It("captures the code location", func() {
1234-
called := false
1235-
cl := types.NewCodeLocation(0)
1236-
an := internal.BuildAroundNode(func(ctx context.Context, body func(context.Context)) {
1237-
called = true
1238-
})
1239-
Ω(an).ShouldNot(BeZero())
1240-
cl.LineNumber += 1
1241-
Ω(an.CodeLocation).Should(Equal(cl))
1242-
an.Body(nil, nil)
1243-
Ω(called).Should(BeTrue())
1244-
})
1245-
})
1246-
1247-
Describe("Clone", func() {
1248-
It("clones the slice", func() {
1249-
an1 := internal.BuildAroundNode(nil)
1250-
an2 := internal.BuildAroundNode(nil)
1251-
an3 := internal.BuildAroundNode(nil)
1252-
original := internal.AroundNodes{an1, an2, an3}
1253-
clone := original.Clone()
1254-
Ω(original).Should(Equal(clone))
1255-
an4 := internal.BuildAroundNode(nil)
1256-
clone[2] = an4
1257-
Ω(original).Should(Equal(internal.AroundNodes{an1, an2, an3}))
1258-
Ω(clone).Should(Equal(internal.AroundNodes{an1, an2, an4}))
1259-
})
1260-
})
1261-
1262-
Describe("Append", func() {
1263-
It("appends the node to the slice", func() {
1264-
an1 := internal.BuildAroundNode(nil)
1265-
an2 := internal.BuildAroundNode(nil)
1266-
an3 := internal.BuildAroundNode(nil)
1267-
nodes := internal.AroundNodes{an1, an2}
1268-
newNodes := nodes.Append(an3)
1269-
Ω(nodes).Should(Equal(internal.AroundNodes{an1, an2}))
1270-
Ω(newNodes).Should(Equal(internal.AroundNodes{an1, an2, an3}))
1271-
})
1272-
})
1273-
})
1274-
12751231
var _ = Describe("Nodes", func() {
12761232
Describe("Clone", func() {
12771233
var n1, n2, n3, n4 Node

internal/suite.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Suite struct {
3232

3333
suiteNodes Nodes
3434
cleanupNodes Nodes
35-
aroundNodes AroundNodes
35+
aroundNodes types.AroundNodes
3636

3737
failer *Failer
3838
reporter reporters.Reporter
@@ -106,7 +106,7 @@ func (suite *Suite) BuildTree() error {
106106
return nil
107107
}
108108

109-
func (suite *Suite) Run(description string, suiteLabels Labels, suiteSemVerConstraints SemVerConstraints, suiteAroundNodes AroundNodes, suitePath string, failer *Failer, reporter reporters.Reporter, writer WriterInterface, outputInterceptor OutputInterceptor, interruptHandler interrupt_handler.InterruptHandlerInterface, client parallel_support.Client, progressSignalRegistrar ProgressSignalRegistrar, suiteConfig types.SuiteConfig) (bool, bool) {
109+
func (suite *Suite) Run(description string, suiteLabels Labels, suiteSemVerConstraints SemVerConstraints, suiteAroundNodes types.AroundNodes, suitePath string, failer *Failer, reporter reporters.Reporter, writer WriterInterface, outputInterceptor OutputInterceptor, interruptHandler interrupt_handler.InterruptHandlerInterface, client parallel_support.Client, progressSignalRegistrar ProgressSignalRegistrar, suiteConfig types.SuiteConfig) (bool, bool) {
110110
if suite.phase != PhaseBuildTree {
111111
panic("cannot run before building the tree = call suite.BuildTree() first")
112112
}
@@ -263,7 +263,7 @@ func (suite *Suite) pushCleanupNode(node Node) error {
263263

264264
node.NodeIDWhereCleanupWasGenerated = suite.currentNode.ID
265265
node.NestingLevel = suite.currentNode.NestingLevel
266-
node.AroundNodes = AroundNodes{}.Append(suite.currentNode.AroundNodes...).Append(node.AroundNodes...)
266+
node.AroundNodes = types.AroundNodes{}.Append(suite.currentNode.AroundNodes...).Append(node.AroundNodes...)
267267
suite.selectiveLock.Lock()
268268
suite.cleanupNodes = append(suite.cleanupNodes, node)
269269
suite.selectiveLock.Unlock()
@@ -897,7 +897,7 @@ func (suite *Suite) runNode(node Node, specDeadline time.Time, text string) (typ
897897
failureC <- failureFromRun
898898
}()
899899

900-
aroundNodes := AroundNodes{}.Append(suite.aroundNodes...).Append(node.AroundNodes...)
900+
aroundNodes := types.AroundNodes{}.Append(suite.aroundNodes...).Append(node.AroundNodes...)
901901
if len(aroundNodes) > 0 {
902902
i := 0
903903
var f func(context.Context)

types/around_node.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package types
2+
3+
import (
4+
"context"
5+
)
6+
7+
type AroundNodeAllowedFuncs interface {
8+
~func(context.Context, func(context.Context)) | ~func(context.Context) context.Context | ~func()
9+
}
10+
type AroundNodeFunc func(ctx context.Context, body func(ctx context.Context))
11+
12+
func AroundNode[F AroundNodeAllowedFuncs](f F, cl CodeLocation) AroundNodeDecorator {
13+
if f == nil {
14+
panic("BuildAroundNode cannot be called with a nil function.")
15+
}
16+
var aroundNodeFunc func(context.Context, func(context.Context))
17+
switch x := any(f).(type) {
18+
case func(context.Context, func(context.Context)):
19+
aroundNodeFunc = x
20+
case func(context.Context) context.Context:
21+
aroundNodeFunc = func(ctx context.Context, body func(context.Context)) {
22+
ctx = x(ctx)
23+
body(ctx)
24+
}
25+
case func():
26+
aroundNodeFunc = func(ctx context.Context, body func(context.Context)) {
27+
x()
28+
body(ctx)
29+
}
30+
}
31+
32+
return AroundNodeDecorator{
33+
Body: aroundNodeFunc,
34+
CodeLocation: cl,
35+
}
36+
}
37+
38+
type AroundNodeDecorator struct {
39+
Body AroundNodeFunc
40+
CodeLocation CodeLocation
41+
}
42+
43+
type AroundNodes []AroundNodeDecorator
44+
45+
func (an AroundNodes) Clone() AroundNodes {
46+
out := make(AroundNodes, len(an))
47+
copy(out, an)
48+
return out
49+
}
50+
51+
func (an AroundNodes) Append(other ...AroundNodeDecorator) AroundNodes {
52+
out := make(AroundNodes, len(an)+len(other))
53+
copy(out, an)
54+
copy(out[len(an):], other)
55+
return out
56+
}

0 commit comments

Comments
 (0)