How to use HaveRunWithData method of test_helpers Package

Best Ginkgo code snippet using test_helpers.HaveRunWithData

Run Ginkgo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

program_test.go

Source: program_test.go Github

copy
1package command_test
2
3import (
4	"fmt"
5	"strings"
6
7	. "github.com/onsi/ginkgo/v2"
8	"github.com/onsi/ginkgo/v2/formatter"
9	. "github.com/onsi/ginkgo/v2/internal/test_helpers"
10	"github.com/onsi/ginkgo/v2/types"
11	. "github.com/onsi/gomega"
12	"github.com/onsi/gomega/gbytes"
13
14	"github.com/onsi/ginkgo/v2/ginkgo/command"
15)
16
17var _ = Describe("Program", func() {
18	var program command.Program
19	var rt *RunTracker
20	var buf *gbytes.Buffer
21
22	BeforeEach(func() {
23		rt = NewRunTracker()
24		defaultCommand := command.Command{Name: "alpha", Usage: "alpha usage", ShortDoc: "such usage!", Command: rt.C("alpha")}
25
26		fs, err := types.NewGinkgoFlagSet(
27			types.GinkgoFlags{
28				{Name: "decay-rate", KeyPath: "Rate", Usage: "set the decay rate, in years"},
29				{DeprecatedName: "old", KeyPath: "Old"},
30			},
31			&(struct {
32				Rate float64
33				Old  bool
34			}{Rate: 17.0}),
35			types.GinkgoFlagSections{},
36		)
37		Ω(err).ShouldNot(HaveOccurred())
38		commands := []command.Command{
39			{Name: "beta", Flags: fs, Usage: "beta usage", ShortDoc: "such usage!", Command: rt.C("beta")},
40			{Name: "gamma", Command: rt.C("gamma")},
41			{Name: "zeta", Command: rt.C("zeta", func() {
42				command.Abort(command.AbortDetails{Error: fmt.Errorf("Kaboom!"), ExitCode: 17})
43			})},
44		}
45
46		deprecatedCommands := []command.DeprecatedCommand{
47			{Name: "delta", Deprecation: types.Deprecation{Message: "delta is for deprecated"}},
48		}
49
50		formatter.SingletonFormatter.ColorMode = formatter.ColorModePassthrough
51
52		buf = gbytes.NewBuffer()
53		program = command.Program{
54			Name:               "omicron",
55			Heading:            "Omicron v2.0.0",
56			Commands:           commands,
57			DefaultCommand:     defaultCommand,
58			DeprecatedCommands: deprecatedCommands,
59
60			Exiter: func(code int) {
61				rt.RunWithData("exit", "Code", code)
62			},
63			OutWriter: buf,
64			ErrWriter: buf,
65		}
66	})
67
68	Context("when called with no subcommand", func() {
69		BeforeEach(func() {
70			program.RunAndExit([]string{"omicron"}) //os.Args always includes the name of the program as the first element
71		})
72
73		It("runs the default command", func() {
74			Ω(rt).Should(HaveTracked("alpha", "exit"))
75			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
76			Ω(buf.Contents()).Should(BeEmpty())
77		})
78	})
79
80	Context("when called with the default command's name", func() {
81		BeforeEach(func() {
82			program.RunAndExit([]string{"omicron", "alpha", "args1", "args2"})
83		})
84
85		It("runs the default command", func() {
86			Ω(rt).Should(HaveTracked("alpha", "exit"))
87			Ω(rt).Should(HaveRunWithData("alpha", "Args", []string{"args1", "args2"}))
88			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
89			Ω(buf.Contents()).Should(BeEmpty())
90		})
91	})
92
93	Context("when called with a subcommand", func() {
94		BeforeEach(func() {
95			program.RunAndExit([]string{"omicron", "beta"})
96		})
97
98		It("runs that subcommand", func() {
99			Ω(rt).Should(HaveTracked("beta", "exit"))
100			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
101			Ω(buf.Contents()).Should(BeEmpty())
102		})
103	})
104
105	Context("when called with an unkown subcommand", func() {
106		BeforeEach(func() {
107			program.RunAndExit([]string{"omicron", "xi"})
108		})
109
110		It("calls the default command with arguments", func() {
111			Ω(rt).Should(HaveTracked("alpha", "exit"))
112			Ω(rt).Should(HaveRunWithData("alpha", "Args", []string{"xi"}))
113			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
114			Ω(buf.Contents()).Should(BeEmpty())
115		})
116	})
117
118	Context("when passed arguments and additional arguments", func() {
119		BeforeEach(func() {
120			program.RunAndExit([]string{"omicron", "gamma", "arg1", "-arg2", "--", "addArg1", "addArg2"})
121		})
122		It("passes both in", func() {
123			Ω(rt).Should(HaveTracked("gamma", "exit"))
124			Ω(rt).Should(HaveRunWithData("gamma", "Args", []string{"arg1", "-arg2"}, "AdditionalArgs", []string{"addArg1", "addArg2"}))
125			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
126			Ω(buf.Contents()).Should(BeEmpty())
127		})
128	})
129
130	DescribeTable("Emitting help when asked",
131		func(args []string) {
132			program.RunAndExit(args)
133			Ω(rt).Should(HaveTracked("exit"))
134			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
135			//HavePrefix to avoid trailing whitespace causing failures
136			Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
137				"Omicron v2.0.0",
138				"{{gray}}--------------{{/}}",
139				"For usage information for a command, run {{bold}}omicron help COMMAND{{/}}.",
140				"For usage information for the default command, run {{bold}}omicron help omicron{{/}} or {{bold}}omicron help alpha{{/}}.",
141				"",
142				"The following commands are available:",
143				"  {{bold}}omicron{{/}} or omicron {{bold}}alpha{{/}} - {{gray}}alpha usage{{/}}",
144				"    such usage!",
145				"  {{bold}}beta{{/}} - {{gray}}beta usage{{/}}",
146				"    such usage!",
147				"  {{bold}}gamma{{/}} - {{gray}}{{/}}",
148				"  {{bold}}zeta{{/}} - {{gray}}{{/}}",
149			}, "\n")))
150		},
151		func(args []string) string {
152			return fmt.Sprintf("with %s", args[1])
153		},
154		Entry(nil, []string{"omicron", "help"}),
155		Entry(nil, []string{"omicron", "-help"}),
156		Entry(nil, []string{"omicron", "--help"}),
157		Entry(nil, []string{"omicron", "-h"}),
158		Entry(nil, []string{"omicron", "--h"}),
159	)
160
161	DescribeTable("Emitting help for the default command",
162		func(args []string) {
163			program.RunAndExit(args)
164			Ω(rt).Should(HaveTracked("exit"))
165			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
166			Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
167				"{{bold}}alpha usage{{/}}",
168				"{{gray}}-----------{{/}}",
169				"such usage!",
170			}, "\n")))
171		},
172		func(args []string) string {
173			return fmt.Sprintf("with %s %s", args[1], args[2])
174		},
175		Entry(nil, []string{"omicron", "help", "omicron"}),
176		Entry(nil, []string{"omicron", "-help", "omicron"}),
177		Entry(nil, []string{"omicron", "--help", "omicron"}),
178		Entry(nil, []string{"omicron", "-h", "omicron"}),
179		Entry(nil, []string{"omicron", "--h", "omicron"}),
180		Entry(nil, []string{"omicron", "help", "alpha"}),
181		Entry(nil, []string{"omicron", "-help", "alpha"}),
182		Entry(nil, []string{"omicron", "--help", "alpha"}),
183		Entry(nil, []string{"omicron", "-h", "alpha"}),
184		Entry(nil, []string{"omicron", "--h", "alpha"}),
185		Entry(nil, []string{"omicron", "alpha", "-help"}),
186		Entry(nil, []string{"omicron", "alpha", "--help"}),
187		Entry(nil, []string{"omicron", "alpha", "-h"}),
188		Entry(nil, []string{"omicron", "alpha", "--h"}),
189	)
190
191	DescribeTable("Emitting help for a known subcommand",
192		func(args []string) {
193			program.RunAndExit(args)
194			Ω(rt).Should(HaveTracked("exit"))
195			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
196			Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
197				"{{bold}}beta usage{{/}}",
198				"{{gray}}----------{{/}}",
199				"such usage!",
200				"",
201				"  --decay-rate{{/}} [float] {{gray}}{{/}}",
202				"    {{light-gray}}set the decay rate, in years{{/}}",
203			}, "\n")))
204		},
205		func(args []string) string {
206			return fmt.Sprintf("with %s %s", args[1], args[2])
207		},
208		Entry(nil, []string{"omicron", "help", "beta"}),
209		Entry(nil, []string{"omicron", "-help", "beta"}),
210		Entry(nil, []string{"omicron", "--help", "beta"}),
211		Entry(nil, []string{"omicron", "-h", "beta"}),
212		Entry(nil, []string{"omicron", "--h", "beta"}),
213		Entry(nil, []string{"omicron", "beta", "-help"}),
214		Entry(nil, []string{"omicron", "beta", "--help"}),
215		Entry(nil, []string{"omicron", "beta", "-h"}),
216		Entry(nil, []string{"omicron", "beta", "--h"}),
217	)
218
219	DescribeTable("Emitting help for an unknown subcommand",
220		func(args []string) {
221			program.RunAndExit(args)
222			Ω(rt).Should(HaveTracked("exit"))
223			Ω(rt).Should(HaveRunWithData("exit", "Code", 1))
224			Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
225				"{{red}}Unknown Command: {{bold}}xi{{/}}",
226				"",
227				"Omicron v2.0.0",
228				"{{gray}}--------------{{/}}",
229				"For usage information for a command, run {{bold}}omicron help COMMAND{{/}}.",
230				"For usage information for the default command, run {{bold}}omicron help omicron{{/}} or {{bold}}omicron help alpha{{/}}.",
231				"",
232				"The following commands are available:",
233				"  {{bold}}omicron{{/}} or omicron {{bold}}alpha{{/}} - {{gray}}alpha usage{{/}}",
234				"    such usage!",
235				"  {{bold}}beta{{/}} - {{gray}}beta usage{{/}}",
236				"    such usage!",
237				"  {{bold}}gamma{{/}} - {{gray}}{{/}}",
238				"  {{bold}}zeta{{/}} - {{gray}}{{/}}",
239			}, "\n")))
240		},
241		func(args []string) string {
242			return fmt.Sprintf("with %s %s", args[1], args[2])
243		},
244		Entry(nil, []string{"omicron", "help", "xi"}),
245		Entry(nil, []string{"omicron", "-help", "xi"}),
246		Entry(nil, []string{"omicron", "--help", "xi"}),
247		Entry(nil, []string{"omicron", "-h", "xi"}),
248		Entry(nil, []string{"omicron", "--h", "xi"}),
249		Entry(nil, []string{"omicron", "xi", "-help"}),
250		Entry(nil, []string{"omicron", "xi", "--help"}),
251		Entry(nil, []string{"omicron", "xi", "-h"}),
252		Entry(nil, []string{"omicron", "xi", "--h"}),
253	)
254	Context("when called with a deprecated command", func() {
255		BeforeEach(func() {
256			program.RunAndExit([]string{"omicron", "delta"})
257		})
258
259		It("lets the user know the command is deprecated", func() {
260			Ω(rt).Should(HaveTracked("exit"))
261			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
262			Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
263				"{{light-yellow}}You're using deprecated Ginkgo functionality:{{/}}",
264				"{{light-yellow}}============================================={{/}}",
265				"  {{yellow}}delta is for deprecated{{/}}",
266			}, "\n")))
267		})
268	})
269
270	Context("when a deprecated flag is used", func() {
271		BeforeEach(func() {
272			program.RunAndExit([]string{"omicron", "beta", "-old"})
273		})
274
275		It("lets the user know a deprecated flag was used", func() {
276			Ω(rt).Should(HaveTracked("beta", "exit"))
277			Ω(rt).Should(HaveRunWithData("exit", "Code", 0))
278			Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
279				"{{light-yellow}}You're using deprecated Ginkgo functionality:{{/}}",
280				"{{light-yellow}}============================================={{/}}",
281				"  {{yellow}}--old is deprecated{{/}}",
282			}, "\n")))
283		})
284	})
285
286	Context("when an unkown flag is used", func() {
287		BeforeEach(func() {
288			program.RunAndExit([]string{"omicron", "beta", "-zanzibar"})
289		})
290
291		It("emits usage for the associated subcommand", func() {
292			Ω(rt).Should(HaveTracked("exit"))
293			Ω(rt).Should(HaveRunWithData("exit", "Code", 1))
294			Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
295				"{{red}}{{bold}}omicron beta{{/}} {{red}}failed{{/}}",
296				"  flag provided but not defined: -zanzibar",
297				"",
298				"{{bold}}beta usage{{/}}",
299				"{{gray}}----------{{/}}",
300				"such usage!",
301				"",
302				"  --decay-rate{{/}} [float] {{gray}}{{/}}",
303				"    {{light-gray}}set the decay rate, in years{{/}}",
304			}, "\n")))
305		})
306	})
307
308	Context("when a subcommand aborts", func() {
309		BeforeEach(func() {
310			program.RunAndExit([]string{"omicron", "zeta"})
311		})
312
313		It("emits information about the error", func() {
314			Ω(rt).Should(HaveTracked("zeta", "exit"))
315			Ω(rt).Should(HaveRunWithData("exit", "Code", 17))
316			Ω(string(buf.Contents())).Should(HavePrefix(strings.Join([]string{
317				"{{red}}{{bold}}omicron zeta{{/}} {{red}}failed{{/}}",
318				"  Kaboom!",
319			}, "\n")))
320		})
321	})
322
323})
324
Full Screen

synchronized_suite_nodes_test.go

Source: synchronized_suite_nodes_test.go Github

copy
1package internal_integration_test
2
3import (
4	. "github.com/onsi/ginkgo/v2"
5	. "github.com/onsi/ginkgo/v2/internal/test_helpers"
6	"github.com/onsi/ginkgo/v2/types"
7	. "github.com/onsi/gomega"
8	"github.com/onsi/gomega/gbytes"
9)
10
11var _ = Describe("Synchronized Suite Nodes", func() {
12	var failInBeforeSuiteProc1, failInBeforeSuiteAllProcs, failInAfterSuiteAllProcs, failInAfterSuiteProc1 bool
13	var fixture func()
14
15	BeforeEach(func() {
16		failInBeforeSuiteProc1, failInBeforeSuiteAllProcs, failInAfterSuiteAllProcs, failInAfterSuiteProc1 = false, false, false, false
17		fixture = func() {
18			SynchronizedBeforeSuite(func() []byte {
19				outputInterceptor.AppendInterceptedOutput("before-suite-proc-1")
20				rt.Run("before-suite-proc-1")
21				if failInBeforeSuiteProc1 {
22					F("fail-in-before-suite-proc-1", cl)
23				}
24				return []byte("hey there")
25			}, func(data []byte) {
26				outputInterceptor.AppendInterceptedOutput("before-suite-all-procs")
27				rt.RunWithData("before-suite-all-procs", "data", string(data))
28				if failInBeforeSuiteAllProcs {
29					F("fail-in-before-suite-all-procs", cl)
30				}
31			})
32			It("test", rt.T("test"))
33			SynchronizedAfterSuite(func() {
34				outputInterceptor.AppendInterceptedOutput("after-suite-all-procs")
35				rt.Run("after-suite-all-procs")
36				if failInAfterSuiteAllProcs {
37					F("fail-in-after-suite-all-procs", cl)
38				}
39			}, func() {
40				outputInterceptor.AppendInterceptedOutput("after-suite-proc-1")
41				rt.Run("after-suite-proc-1")
42				if failInAfterSuiteProc1 {
43					F("fail-in-after-suite-proc-1", cl)
44				}
45			})
46		}
47	})
48
49	Describe("when running in series", func() {
50		BeforeEach(func() {
51			conf.ParallelTotal = 1
52			conf.ParallelProcess = 1
53		})
54
55		Describe("happy path", func() {
56			BeforeEach(func() {
57				success, _ := RunFixture("happy-path", fixture)
58				Ω(success).Should(BeTrue())
59			})
60
61			It("runs all the functions", func() {
62				Ω(rt).Should(HaveTracked(
63					"before-suite-proc-1", "before-suite-all-procs",
64					"test",
65					"after-suite-all-procs", "after-suite-proc-1",
66				))
67			})
68
69			It("reports on the SynchronizedBeforeSuite and SynchronizedAfterSuite as having passed", func() {
70				befReports := reporter.Did.WithLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)
71				Ω(befReports).Should(HaveLen(1))
72				Ω(befReports[0]).Should(HavePassed())
73
74				aftReports := reporter.Did.WithLeafNodeType(types.NodeTypeSynchronizedAfterSuite)
75				Ω(aftReports).Should(HaveLen(1))
76				Ω(aftReports[0]).Should(HavePassed())
77			})
78
79			It("passes data between the two SynchronizedBeforeSuite functions", func() {
80				Ω(rt).Should(HaveRunWithData("before-suite-all-procs", "data", "hey there"))
81			})
82		})
83
84		Describe("when the SynchronizedBeforeSuite proc1 function fails", func() {
85			BeforeEach(func() {
86				failInBeforeSuiteProc1 = true
87				success, _ := RunFixture("fail in SynchronizedBeforeSuite proc1", fixture)
88				Ω(success).Should(BeFalse())
89			})
90
91			It("doens't run the allProcs function or any of the tests", func() {
92				Ω(rt).Should(HaveTracked(
93					"before-suite-proc-1",
94					"after-suite-all-procs", "after-suite-proc-1",
95				))
96			})
97
98			It("reports on the SynchronizedBeforeSuite and SynchronizedAfterSuite correctly", func() {
99				Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HaveFailed("fail-in-before-suite-proc-1"))
100				Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedAfterSuite)).Should(HavePassed())
101			})
102		})
103
104		Describe("when the SynchronizedBeforeSuite allProcs function fails", func() {
105			BeforeEach(func() {
106				failInBeforeSuiteAllProcs = true
107				success, _ := RunFixture("fail in SynchronizedBeforeSuite allProcs", fixture)
108				Ω(success).Should(BeFalse())
109			})
110
111			It("doesn't run the tests", func() {
112				Ω(rt).Should(HaveTracked(
113					"before-suite-proc-1", "before-suite-all-procs",
114					"after-suite-all-procs", "after-suite-proc-1",
115				))
116				Ω(rt).Should(HaveRunWithData("before-suite-all-procs", "data", "hey there"))
117			})
118
119			It("reports on the SynchronizedBeforeSuite and SynchronizedAfterSuite correctly", func() {
120				Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HaveFailed("fail-in-before-suite-all-procs"))
121				Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedAfterSuite)).Should(HavePassed())
122			})
123		})
124
125		Describe("when the SynchronizedAfterSuite allProcs function fails", func() {
126			BeforeEach(func() {
127				failInAfterSuiteAllProcs = true
128				success, _ := RunFixture("fail in SynchronizedAfterSuite allProcs", fixture)
129				Ω(success).Should(BeFalse())
130			})
131
132			It("nonetheless runs the proc-1 function", func() {
133				Ω(rt).Should(HaveTracked(
134					"before-suite-proc-1", "before-suite-all-procs",
135					"test",
136					"after-suite-all-procs", "after-suite-proc-1",
137				))
138				Ω(rt).Should(HaveRunWithData("before-suite-all-procs", "data", "hey there"))
139			})
140
141			It("reports on the SynchronizedBeforeSuite and SynchronizedAfterSuite correctly", func() {
142				Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HavePassed())
143				Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedAfterSuite)).Should(HaveFailed("fail-in-after-suite-all-procs"))
144			})
145		})
146
147		Describe("when the SynchronizedAfterSuite proc1 function fails", func() {
148			BeforeEach(func() {
149				failInAfterSuiteProc1 = true
150				success, _ := RunFixture("fail in SynchronizedAfterSuite proc1", fixture)
151				Ω(success).Should(BeFalse())
152			})
153
154			It("will have run everything", func() {
155				Ω(rt).Should(HaveTracked(
156					"before-suite-proc-1", "before-suite-all-procs",
157					"test",
158					"after-suite-all-procs", "after-suite-proc-1",
159				))
160				Ω(rt).Should(HaveRunWithData("before-suite-all-procs", "data", "hey there"))
161			})
162
163			It("reports on the SynchronizedBeforeSuite and SynchronizedAfterSuite correctly", func() {
164				Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HavePassed())
165				Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedAfterSuite)).Should(HaveFailed("fail-in-after-suite-proc-1"))
166			})
167		})
168	})
169
170	Describe("when running in parallel", func() {
171		var serverOutputBuffer *gbytes.Buffer
172
173		BeforeEach(func() {
174			SetUpForParallel(2)
175			serverOutputBuffer = gbytes.NewBuffer()
176			server.SetOutputDestination(serverOutputBuffer)
177		})
178
179		Describe("when running as proc 1", func() {
180			BeforeEach(func() {
181				conf.ParallelProcess = 1
182			})
183
184			Describe("happy path", func() {
185				BeforeEach(func() {
186					close(exitChannels[2]) //trigger proc 2 exiting so the proc1 after suite runs
187					success, _ := RunFixture("happy-path", fixture)
188					Ω(success).Should(BeTrue())
189				})
190
191				It("runs all the functions", func() {
192					Ω(rt).Should(HaveTracked(
193						"before-suite-proc-1", "before-suite-all-procs",
194						"test",
195						"after-suite-all-procs", "after-suite-proc-1",
196					))
197				})
198
199				It("reports on the SynchronizedBeforeSuite and SynchronizedAfterSuite as having passed", func() {
200					Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HavePassed())
201					Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedAfterSuite)).Should(HavePassed())
202				})
203
204				It("passes data between the two SynchronizedBeforeSuite functions and up to the server", func() {
205					Ω(rt).Should(HaveRunWithData("before-suite-all-procs", "data", "hey there"))
206					state, data, err := client.BlockUntilSynchronizedBeforeSuiteData()
207					Ω(state).Should(Equal(types.SpecStatePassed))
208					Ω(data).Should(Equal([]byte("hey there")))
209					Ω(err).ShouldNot(HaveOccurred())
210				})
211
212				It("emits the output of the proc-1 BeforeSuite function and the proc-1 AfterSuite fnction", func() {
213					Ω(string(serverOutputBuffer.Contents())).Should(Equal("before-suite-proc-1after-suite-proc-1"))
214					Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HavePassed(CapturedStdOutput("before-suite-proc-1before-suite-all-procs")))
215					Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedAfterSuite)).Should(HavePassed(CapturedStdOutput("after-suite-all-procsafter-suite-proc-1")))
216				})
217			})
218
219			Describe("when the BeforeSuite proc1 function fails", func() {
220				BeforeEach(func() {
221					close(exitChannels[2]) //trigger proc 2 exiting so the proc1 after suite runs
222					failInBeforeSuiteProc1 = true
223					success, _ := RunFixture("happy-path", fixture)
224					Ω(success).Should(BeFalse())
225				})
226
227				It("tells the server", func() {
228					state, data, err := client.BlockUntilSynchronizedBeforeSuiteData()
229					Ω(state).Should(Equal(types.SpecStateFailed))
230					Ω(data).Should(BeNil())
231					Ω(err).ShouldNot(HaveOccurred())
232				})
233			})
234
235			Describe("waiting for all procs to finish before running the AfterSuite proc 1 function", func() {
236				It("waits for the server to give it the all clear", func() {
237					done := make(chan interface{})
238					go func() {
239						defer GinkgoRecover()
240						success, _ := RunFixture("happy-path", fixture)
241						Ω(success).Should(BeTrue())
242						close(done)
243					}()
244					Consistently(done).ShouldNot(BeClosed())
245					close(exitChannels[2])
246					Eventually(done).Should(BeClosed())
247				})
248			})
249		})
250
251		Describe("when running as another proc", func() {
252			BeforeEach(func() {
253				conf.ParallelProcess = 2
254			})
255
256			Describe("happy path", func() {
257				BeforeEach(func() {
258					client.PostSynchronizedBeforeSuiteCompleted(types.SpecStatePassed, []byte("hola hola"))
259					success, _ := RunFixture("happy-path", fixture)
260					Ω(success).Should(BeTrue())
261				})
262
263				It("runs all the all-procs functions", func() {
264					Ω(rt).Should(HaveTracked(
265						"before-suite-all-procs",
266						"test",
267						"after-suite-all-procs",
268					))
269				})
270
271				It("reports on the SynchronizedBeforeSuite and SynchronizedAfterSuite as having passed", func() {
272					Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HavePassed())
273					Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedAfterSuite)).Should(HavePassed())
274				})
275
276				It("gets data for the SynchronizedBeforeSuite all procs function from the server", func() {
277					Ω(rt).Should(HaveRunWithData("before-suite-all-procs", "data", "hola hola"))
278				})
279			})
280
281			Describe("waiting for the data from proc 1", func() {
282				It("waits for the server to give it the data", func() {
283					done := make(chan interface{})
284					go func() {
285						defer GinkgoRecover()
286						success, _ := RunFixture("happy-path", fixture)
287						Ω(success).Should(BeTrue())
288						close(done)
289					}()
290					Consistently(done).ShouldNot(BeClosed())
291					client.PostSynchronizedBeforeSuiteCompleted(types.SpecStatePassed, []byte("hola hola"))
292					Eventually(done).Should(BeClosed())
293					Ω(rt).Should(HaveRunWithData("before-suite-all-procs", "data", "hola hola"))
294				})
295			})
296
297			Describe("when proc 1 fails the SynchronizedBeforeSuite proc1 function", func() {
298				It("fails and only runs the after suite", func() {
299					done := make(chan interface{})
300					go func() {
301						defer GinkgoRecover()
302						success, _ := RunFixture("happy-path", fixture)
303						Ω(success).Should(BeFalse())
304						close(done)
305					}()
306					Consistently(done).ShouldNot(BeClosed())
307					client.PostSynchronizedBeforeSuiteCompleted(types.SpecStateFailed, nil)
308					Eventually(done).Should(BeClosed())
309
310					Ω(rt).Should(HaveTracked("after-suite-all-procs"))
311
312					Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HaveFailed(types.GinkgoErrors.SynchronizedBeforeSuiteFailedOnProc1().Error()))
313				})
314			})
315
316			Describe("when the proc1 SynchronizedBeforeSuite function Skips()", func() {
317				It("fails and only runs the after suite", func() {
318					done := make(chan interface{})
319					go func() {
320						defer GinkgoRecover()
321						success, _ := RunFixture("happy-path", fixture)
322						Ω(success).Should(BeTrue())
323						close(done)
324					}()
325					Consistently(done).ShouldNot(BeClosed())
326					client.PostSynchronizedBeforeSuiteCompleted(types.SpecStateSkipped, nil)
327					Eventually(done).Should(BeClosed())
328
329					Ω(rt).Should(HaveTracked("after-suite-all-procs"))
330
331					Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HaveBeenSkipped())
332				})
333			})
334
335			Describe("when proc 1 disappears before the proc 1 function returns", func() {
336				It("fails and only runs the after suite", func() {
337					done := make(chan interface{})
338					go func() {
339						defer GinkgoRecover()
340						success, _ := RunFixture("happy-path", fixture)
341						Ω(success).Should(BeFalse())
342						close(done)
343					}()
344					Consistently(done).ShouldNot(BeClosed())
345					close(exitChannels[1])
346					Eventually(done).Should(BeClosed())
347
348					Ω(rt).Should(HaveTracked("after-suite-all-procs"))
349
350					Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeSynchronizedBeforeSuite)).Should(HaveFailed(types.GinkgoErrors.SynchronizedBeforeSuiteDisappearedOnProc1().Error()))
351				})
352			})
353		})
354	})
355})
356
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)