File: around_node_test.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.27.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,340 kB
  • sloc: javascript: 65; makefile: 23; sh: 14
file content (217 lines) | stat: -rw-r--r-- 7,722 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package internal_integration_test

import (
	"context"
	"time"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/ginkgo/v2/internal/test_helpers"
	"github.com/onsi/ginkgo/v2/types"
	. "github.com/onsi/gomega"
)

func AN(run string) types.AroundNodeDecorator {
	return AroundNode(func() {
		rt.Run(run)
	})
}

var _ = Describe("The AroundNode decorator", func() {
	Context("when applied to individual nodes", func() {
		It("is scoped to run just around that node", func() {
			success, hPF := RunFixture("around node test", func() {
				BeforeSuite(rt.T("before-suite"), AN("before-suite-around"))
				Describe("container", func() {
					BeforeEach(rt.T("before-each"), AN("before-each-around"))
					It("runs", rt.T("it"), AN("it-around"), AroundNode(func(ctx context.Context, body func(ctx context.Context)) {
						rt.Run("it-around-2-before")
						body(ctx)
						rt.Run("it-around-2-after")
					}))
				})
			})

			Ω(success).Should(BeTrue())
			Ω(rt).Should(HaveTracked(
				"before-suite-around", "before-suite",
				"before-each-around", "before-each",
				"it-around", "it-around-2-before", "it", "it-around-2-after",
			))
			Ω(hPF).Should(BeFalse())
		})
	})

	Context("when DeferCleanup is called", func() {
		It("attaches the caller's AroundNode", func() {
			success, _ := RunFixture("around node test with defer cleanup", func() {
				It("A", AN("A-around"), func() {
					rt.Run("A")
					DeferCleanup(AN("DC-around"), func() {
						rt.Run("DC")
					})
				})
			})
			Ω(success).Should(BeTrue())
			Ω(rt).Should(HaveTracked(
				"A-around", "A",
				"A-around", "DC-around", "DC",
			))
		})
	})

	Context("when included in a hierarchy", func() {
		It("correctly tracks nodes in the hierarchy", func() {
			success, _ := RunFixture("around node test with hierarchy", func() {
				Describe("outer", AN("outer-around"), AN("outer-around-2"), func() {
					It("A", AN("A-around"), rt.T("A"))
					Describe("inner", AN("inner-around"), func() {
						It("B", AN("B-around"), rt.T("B", func() {
							DeferCleanup(AN("DC-around"), rt.T("DC"))
						}))
					})
					BeforeEach(AN("before-each-around"), rt.T("before-each"))
				})
				AfterEach(AN("after-each-around"), rt.T("after-each"))
			})

			Ω(success).Should(BeTrue())
			Ω(rt).Should(HaveTracked(
				"outer-around", "outer-around-2", "before-each-around", "before-each",
				"outer-around", "outer-around-2", "A-around", "A",
				"after-each-around", "after-each",
				"outer-around", "outer-around-2", "before-each-around", "before-each",
				"outer-around", "outer-around-2", "inner-around", "B-around", "B",
				"after-each-around", "after-each",
				"outer-around", "outer-around-2", "inner-around", "B-around", "DC-around", "DC",
			))
		})
	})

	Context("when applied to RunSpecs", func() {
		It("runs for all nodes, including the suite-level nodes", func() {
			success, hPF := RunFixture("around node test with suite-level around", func() {
				BeforeSuite(rt.T("before-suite"), AN("before-suite-around"))
				Describe("container", AN("container-around"), func() {
					BeforeEach(rt.T("before-each"), AN("before-each-around"))
					It("runs", rt.T("it"), AN("it-around"), AroundNode(func(ctx context.Context, body func(ctx context.Context)) {
						rt.Run("it-around-2-before")
						body(ctx)
						rt.Run("it-around-2-after")
					}))
				})
			}, AN("suite-around-1"), AN("suite-around-2"))

			Ω(success).Should(BeTrue())
			Ω(rt).Should(HaveTracked(
				"suite-around-1", "suite-around-2", "before-suite-around", "before-suite",
				"suite-around-1", "suite-around-2", "container-around", "before-each-around", "before-each",
				"suite-around-1", "suite-around-2", "container-around", "it-around", "it-around-2-before", "it", "it-around-2-after",
			))
			Ω(hPF).Should(BeFalse())
		})
	})

	Context("when it modifies the context", func() {
		var newCtx context.Context
		It("provides the node with a wrapped version of the context that can, nonetheless, be accessed and unwrapped", AroundNode(func(ctx context.Context, body func(ctx context.Context)) {
			newCtx = context.WithValue(ctx, "wrapped", "value")
			body(newCtx)
		}), func(ctx SpecContext) {
			Ω(ctx.Value("wrapped")).Should(Equal("value"))
			Ω(ctx).ShouldNot(Equal(newCtx))
			Ω(ctx.WrappedContext()).Should(Equal(newCtx))
		})

		It("works with the more complex suite setup nodes too", func() {
			succes, _ := RunFixture("around node test with complex context", func() {
				SynchronizedBeforeSuite(func(ctx SpecContext) []byte {
					rt.Run("SBS-primary")
					Ω(ctx.Value("wrapped")).Should(Equal("value"))
					return []byte("data")
				}, func(ctx SpecContext, data []byte) {
					rt.Run("SBS-all")
					Ω(ctx.Value("wrapped")).Should(Equal("value"))
					Ω(data).Should(Equal([]byte("data")))
				}, AroundNode(func(ctx context.Context) context.Context {
					rt.Run("SBS-around")
					return context.WithValue(ctx, "wrapped", "value")
				}))
				It("runs", rt.T("A"))
			}, AN("suite-around-1"))
			Ω(succes).Should(BeTrue())
			Ω(rt).Should(HaveTracked(
				"suite-around-1", "SBS-around", "SBS-primary",
				"suite-around-1", "SBS-around", "SBS-all",
				"suite-around-1", "A",
			))
			Ω(reporter.Did).Should(HaveEach(HavePassed()))
		})

		It("should still allow timeouts", func(ctx SpecContext) {
			c := make(chan struct{})
			success, _ := RunFixture("around node test with timeout", func() {
				It("A",
					AroundNode(func(ctx context.Context, body func(ctx context.Context)) {
						ctx, cancel := context.WithTimeout(ctx, 100*time.Minute)
						defer cancel()
						ctx = context.WithValue(ctx, "timeout", "value")
						body(ctx)
					}),
					func(ctx SpecContext) {
						Ω(ctx.Value("timeout")).Should(Equal("value"))
						<-ctx.Done()
						close(c)
					},
					NodeTimeout(100*time.Millisecond))
			})

			Ω(success).Should(BeFalse())
			Ω(reporter.Did.Find("A")).Should(HaveTimedOut())
			Eventually(c, ctx).Should(BeClosed())
		}, NodeTimeout(time.Second))

	})

	Context("when the user fails to call the body function", func() {
		It("fails", func() {
			success, _ := RunFixture("around node test that fails to call the body function", func() {
				It("A", AroundNode(func(ctx context.Context, body func(ctx context.Context)) {}), func() {
					rt.Run("A")
				})
			})
			Ω(success).Should(BeFalse())
			Ω(reporter.Did.Find("A")).Should(HaveFailed("An AroundNode failed to call the passed in function."))
			Ω(rt).Should(HaveTrackedNothing())
		})
	})

	Context("when the user passes in a nil context", func() {
		It("fails", func() {
			success, _ := RunFixture("around node test that passes in a nil context", func() {
				It("A", AroundNode(func(ctx context.Context, body func(ctx context.Context)) {
					body(nil)
				}), func() {
					rt.Run("A")
				})
			})
			Ω(success).Should(BeFalse())
			Ω(reporter.Did.Find("A")).Should(HaveFailed("An AroundNode failed to pass a valid Ginkgo SpecContext in.  You must always pass in a context derived from the context passed to you."))
			Ω(rt).Should(HaveTrackedNothing())
		})
	})

	Context("when the user passes in a context that does not inherit from the chain", func() {
		It("fails", func() {
			success, _ := RunFixture("around node test that passes in a context that does not inherit from the chain", func() {
				It("A", AroundNode(func(ctx context.Context, body func(ctx context.Context)) {
					body(context.Background())
				}), func() {
					rt.Run("A")
				})
			})
			Ω(success).Should(BeFalse())
			Ω(reporter.Did.Find("A")).Should(HaveFailed("An AroundNode failed to pass a valid Ginkgo SpecContext in.  You must always pass in a context derived from the context passed to you."))
			Ω(rt).Should(HaveTrackedNothing())
		})
	})
})