|
| 1 | +package internal_integration_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + "github.com/onsi/ginkgo/v2/internal" |
| 9 | + . "github.com/onsi/ginkgo/v2/internal/test_helpers" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | +) |
| 12 | + |
| 13 | +func AN(run string) internal.AroundNode { |
| 14 | + return AroundNode(func(ctx context.Context, body func(ctx context.Context)) { |
| 15 | + rt.Run(run) |
| 16 | + body(ctx) |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +var _ = Describe("The AroundNode decorator", func() { |
| 21 | + Context("when applied to individual nodes", func() { |
| 22 | + It("is scoped to run just around that node", func() { |
| 23 | + success, hPF := RunFixture("around node test", func() { |
| 24 | + BeforeSuite(rt.T("before-suite"), AN("before-suite-around")) |
| 25 | + Describe("container", func() { |
| 26 | + BeforeEach(rt.T("before-each"), AN("before-each-around")) |
| 27 | + It("runs", rt.T("it"), AN("it-around"), AroundNode(func(ctx context.Context, body func(ctx context.Context)) { |
| 28 | + rt.Run("it-around-2-before") |
| 29 | + body(ctx) |
| 30 | + rt.Run("it-around-2-after") |
| 31 | + })) |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + Ω(success).Should(BeTrue()) |
| 36 | + Ω(rt).Should(HaveTracked( |
| 37 | + "before-suite-around", "before-suite", |
| 38 | + "before-each-around", "before-each", |
| 39 | + "it-around", "it-around-2-before", "it", "it-around-2-after", |
| 40 | + )) |
| 41 | + Ω(hPF).Should(BeFalse()) |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + Context("when DeferCleanup is called", func() { |
| 46 | + It("attaches the caller's AroundNode", func() { |
| 47 | + success, _ := RunFixture("around node test with defer cleanup", func() { |
| 48 | + It("A", AN("A-around"), func() { |
| 49 | + rt.Run("A") |
| 50 | + DeferCleanup(AN("DC-around"), func() { |
| 51 | + rt.Run("DC") |
| 52 | + }) |
| 53 | + }) |
| 54 | + }) |
| 55 | + Ω(success).Should(BeTrue()) |
| 56 | + Ω(rt).Should(HaveTracked( |
| 57 | + "A-around", "A", |
| 58 | + "A-around", "DC-around", "DC", |
| 59 | + )) |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + Context("when included in a hierarchy", func() { |
| 64 | + It("correctly tracks nodes in the hierarchy", func() { |
| 65 | + success, _ := RunFixture("around node test with hierarchy", func() { |
| 66 | + Describe("outer", AN("outer-around"), AN("outer-around-2"), func() { |
| 67 | + It("A", AN("A-around"), rt.T("A")) |
| 68 | + Describe("inner", AN("inner-around"), func() { |
| 69 | + It("B", AN("B-around"), rt.T("B", func() { |
| 70 | + DeferCleanup(AN("DC-around"), rt.T("DC")) |
| 71 | + })) |
| 72 | + }) |
| 73 | + BeforeEach(AN("before-each-around"), rt.T("before-each")) |
| 74 | + }) |
| 75 | + AfterEach(AN("after-each-around"), rt.T("after-each")) |
| 76 | + }) |
| 77 | + |
| 78 | + Ω(success).Should(BeTrue()) |
| 79 | + Ω(rt).Should(HaveTracked( |
| 80 | + "outer-around", "outer-around-2", "before-each-around", "before-each", |
| 81 | + "outer-around", "outer-around-2", "A-around", "A", |
| 82 | + "after-each-around", "after-each", |
| 83 | + "outer-around", "outer-around-2", "before-each-around", "before-each", |
| 84 | + "outer-around", "outer-around-2", "inner-around", "B-around", "B", |
| 85 | + "after-each-around", "after-each", |
| 86 | + "outer-around", "outer-around-2", "inner-around", "B-around", "DC-around", "DC", |
| 87 | + )) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + Context("when applied to RunSpecs", func() { |
| 92 | + It("runs for all nodes, including the suite-level nodes", func() { |
| 93 | + success, hPF := RunFixture("around node test with suite-level around", func() { |
| 94 | + BeforeSuite(rt.T("before-suite"), AN("before-suite-around")) |
| 95 | + Describe("container", AN("container-around"), func() { |
| 96 | + BeforeEach(rt.T("before-each"), AN("before-each-around")) |
| 97 | + It("runs", rt.T("it"), AN("it-around"), AroundNode(func(ctx context.Context, body func(ctx context.Context)) { |
| 98 | + rt.Run("it-around-2-before") |
| 99 | + body(ctx) |
| 100 | + rt.Run("it-around-2-after") |
| 101 | + })) |
| 102 | + }) |
| 103 | + }, AN("suite-around-1"), AN("suite-around-2")) |
| 104 | + |
| 105 | + Ω(success).Should(BeTrue()) |
| 106 | + Ω(rt).Should(HaveTracked( |
| 107 | + "suite-around-1", "suite-around-2", "before-suite-around", "before-suite", |
| 108 | + "suite-around-1", "suite-around-2", "container-around", "before-each-around", "before-each", |
| 109 | + "suite-around-1", "suite-around-2", "container-around", "it-around", "it-around-2-before", "it", "it-around-2-after", |
| 110 | + )) |
| 111 | + Ω(hPF).Should(BeFalse()) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + Context("when it modifies the context", func() { |
| 116 | + var newCtx context.Context |
| 117 | + 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)) { |
| 118 | + newCtx = context.WithValue(ctx, "wrapped", "value") |
| 119 | + body(newCtx) |
| 120 | + }), func(ctx SpecContext) { |
| 121 | + Ω(ctx.Value("wrapped")).Should(Equal("value")) |
| 122 | + Ω(ctx).ShouldNot(Equal(newCtx)) |
| 123 | + Ω(ctx.WrappedContext()).Should(Equal(newCtx)) |
| 124 | + }) |
| 125 | + |
| 126 | + It("works with the more complex suite setup nodes too", func() { |
| 127 | + succes, _ := RunFixture("around node test with complex context", func() { |
| 128 | + SynchronizedBeforeSuite(func(ctx SpecContext) []byte { |
| 129 | + rt.Run("SBS-primary") |
| 130 | + Ω(ctx.Value("wrapped")).Should(Equal("value")) |
| 131 | + return []byte("data") |
| 132 | + }, func(ctx SpecContext, data []byte) { |
| 133 | + rt.Run("SBS-all") |
| 134 | + Ω(ctx.Value("wrapped")).Should(Equal("value")) |
| 135 | + Ω(data).Should(Equal([]byte("data"))) |
| 136 | + }, AroundNode(func(ctx context.Context, body func(ctx context.Context)) { |
| 137 | + rt.Run("SBS-around") |
| 138 | + newCtx = context.WithValue(ctx, "wrapped", "value") |
| 139 | + body(newCtx) |
| 140 | + })) |
| 141 | + It("runs", rt.T("A")) |
| 142 | + }, AN("suite-around-1")) |
| 143 | + Ω(succes).Should(BeTrue()) |
| 144 | + Ω(rt).Should(HaveTracked( |
| 145 | + "suite-around-1", "SBS-around", "SBS-primary", |
| 146 | + "suite-around-1", "SBS-around", "SBS-all", |
| 147 | + "suite-around-1", "A", |
| 148 | + )) |
| 149 | + Ω(reporter.Did).Should(HaveEach(HavePassed())) |
| 150 | + }) |
| 151 | + |
| 152 | + It("should still allow timeouts", func(ctx SpecContext) { |
| 153 | + c := make(chan struct{}) |
| 154 | + success, _ := RunFixture("around node test with timeout", func() { |
| 155 | + It("A", |
| 156 | + AroundNode(func(ctx context.Context, body func(ctx context.Context)) { |
| 157 | + ctx, cancel := context.WithTimeout(ctx, 100*time.Minute) |
| 158 | + defer cancel() |
| 159 | + ctx = context.WithValue(ctx, "timeout", "value") |
| 160 | + body(ctx) |
| 161 | + }), |
| 162 | + func(ctx SpecContext) { |
| 163 | + Ω(ctx.Value("timeout")).Should(Equal("value")) |
| 164 | + <-ctx.Done() |
| 165 | + close(c) |
| 166 | + }, |
| 167 | + NodeTimeout(100*time.Millisecond)) |
| 168 | + }) |
| 169 | + |
| 170 | + Ω(success).Should(BeFalse()) |
| 171 | + Ω(reporter.Did.Find("A")).Should(HaveTimedOut()) |
| 172 | + Eventually(c, ctx).Should(BeClosed()) |
| 173 | + }, NodeTimeout(time.Second)) |
| 174 | + |
| 175 | + }) |
| 176 | + |
| 177 | + Context("when the user fails to call the body function", func() { |
| 178 | + It("fails", func() { |
| 179 | + success, _ := RunFixture("around node test that fails to call the body function", func() { |
| 180 | + It("A", AroundNode(func(ctx context.Context, body func(ctx context.Context)) {}), func() { |
| 181 | + rt.Run("A") |
| 182 | + }) |
| 183 | + }) |
| 184 | + Ω(success).Should(BeFalse()) |
| 185 | + Ω(reporter.Did.Find("A")).Should(HaveFailed("An AroundNode failed to call the passed in function.")) |
| 186 | + Ω(rt).Should(HaveTrackedNothing()) |
| 187 | + }) |
| 188 | + }) |
| 189 | + |
| 190 | + Context("when the user passes in a nil context", func() { |
| 191 | + It("fails", func() { |
| 192 | + success, _ := RunFixture("around node test that passes in a nil context", func() { |
| 193 | + It("A", AroundNode(func(ctx context.Context, body func(ctx context.Context)) { |
| 194 | + body(nil) |
| 195 | + }), func() { |
| 196 | + rt.Run("A") |
| 197 | + }) |
| 198 | + }) |
| 199 | + Ω(success).Should(BeFalse()) |
| 200 | + Ω(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.")) |
| 201 | + Ω(rt).Should(HaveTrackedNothing()) |
| 202 | + }) |
| 203 | + }) |
| 204 | + |
| 205 | + Context("when the user passes in a context that does not inherit from the chain", func() { |
| 206 | + It("fails", func() { |
| 207 | + success, _ := RunFixture("around node test that passes in a context that does not inherit from the chain", func() { |
| 208 | + It("A", AroundNode(func(ctx context.Context, body func(ctx context.Context)) { |
| 209 | + body(context.Background()) |
| 210 | + }), func() { |
| 211 | + rt.Run("A") |
| 212 | + }) |
| 213 | + }) |
| 214 | + Ω(success).Should(BeFalse()) |
| 215 | + Ω(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.")) |
| 216 | + Ω(rt).Should(HaveTrackedNothing()) |
| 217 | + }) |
| 218 | + }) |
| 219 | +}) |
0 commit comments