File: focus_test.go

package info (click to toggle)
golang-github-onsi-ginkgo-v2 2.15.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 4,112 kB
  • sloc: javascript: 59; sh: 14; makefile: 7
file content (325 lines) | stat: -rw-r--r-- 13,611 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package internal_test

import (
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"

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

var _ = Describe("Focus", func() {
	Describe("ApplyNestedFocusToTree", func() {
		It("unfocuses parent nodes that have a focused child node somewhere in their tree", func() {
			tree := TN(N(ntCon, "root", Focus), //should lose focus
				TN(N(ntCon, "A", Focus), //should stay focused
					TN(N(ntIt)),
					TN(N(ntIt)),
				),
				TN(N(ntCon),
					TN(N(ntIt)),
					TN(N(ntIt, "B", Focus)), //should stay focused
				),
				TN(N(ntCon, "C", Focus), //should lose focus
					TN(N(ntIt)),
					TN(N(ntIt, "D", Focus)), //should stay focused
				),
				TN(N(ntCon, "E", Focus), //should lose focus
					TN(N(ntIt)),
					TN(N(ntCon),
						TN(N(ntIt)),
						TN(N(ntIt, "F", Focus)), // should stay focused
					),
				),
				TN(N(ntCon, "G", Focus), //should lose focus
					TN(N(ntIt)),
					TN(N(ntCon, "H", Focus), //should lose focus
						TN(N(ntIt)),
						TN(N(ntIt, "I", Focus)), //should stay focused
					),
					TN(N(ntCon, "J", Focus), // should stay focused
						TN(N(ntIt)),
					),
				),
			)

			internal.ApplyNestedFocusPolicyToTree(tree)
			Ω(mustFindNodeWithText(tree, "root").MarkedFocus).Should(BeFalse())
			Ω(mustFindNodeWithText(tree, "A").MarkedFocus).Should(BeTrue())
			Ω(mustFindNodeWithText(tree, "B").MarkedFocus).Should(BeTrue())
			Ω(mustFindNodeWithText(tree, "C").MarkedFocus).Should(BeFalse())
			Ω(mustFindNodeWithText(tree, "D").MarkedFocus).Should(BeTrue())
			Ω(mustFindNodeWithText(tree, "E").MarkedFocus).Should(BeFalse())
			Ω(mustFindNodeWithText(tree, "F").MarkedFocus).Should(BeTrue())
			Ω(mustFindNodeWithText(tree, "G").MarkedFocus).Should(BeFalse())
			Ω(mustFindNodeWithText(tree, "H").MarkedFocus).Should(BeFalse())
			Ω(mustFindNodeWithText(tree, "I").MarkedFocus).Should(BeTrue())
			Ω(mustFindNodeWithText(tree, "J").MarkedFocus).Should(BeTrue())
		})

		It("does not unfocus parent nodes if a focused child is the child of a pending child", func() {
			tree := TN(N(ntCon),
				TN(N(ntCon, "A", Focus), //should stay focused
					TN(N(ntIt)),
					TN(N(ntCon, "B", Pending), //should stay pending
						TN(N(ntIt)),
						TN(N(ntIt, "C", Focus)), //should stay focused
					),
				),
			)

			internal.ApplyNestedFocusPolicyToTree(tree)
			Ω(mustFindNodeWithText(tree, "A").MarkedFocus).Should(BeTrue())
			Ω(mustFindNodeWithText(tree, "B").MarkedPending).Should(BeTrue())
			Ω(mustFindNodeWithText(tree, "C").MarkedFocus).Should(BeTrue())
		})
	})

	Describe("ApplyFocusToSpecs", func() {
		var specs Specs
		var description string
		var suiteLabels Labels
		var conf types.SuiteConfig

		harvestSkips := func(specs Specs) []bool {
			out := []bool{}
			for _, spec := range specs {
				out = append(out, spec.Skip)
			}
			return out
		}

		BeforeEach(func() {
			description = "Silmarillion Suite"
			suiteLabels = Labels{"SuiteLabel", "TopLevelLabel"}
			conf = types.SuiteConfig{}
		})

		Context("when there are specs with nodes marked pending", func() {
			BeforeEach(func() {
				specs = Specs{
					S(N(), N()),
					S(N(), N()),
					S(N(), N(Pending)),
					S(N(), N()),
					S(N(Pending)),
				}
			})

			It("skips those specs", func() {
				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
				Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, true, false, true}))
				Ω(hasProgrammaticFocus).Should(BeFalse())
			})
		})

		Context("when there are specs with nodes marked focused", func() {
			BeforeEach(func() {
				specs = Specs{
					S(N(), N()),
					S(N(), N()),
					S(N(), N(Focus)),
					S(N()),
					S(N(Focus)),
				}
			})
			It("skips any other specs and notes that it has programmatic focus", func() {
				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
				Ω(harvestSkips(specs)).Should(Equal([]bool{true, true, false, true, false}))
				Ω(hasProgrammaticFocus).Should(BeTrue())
			})

			Context("when the specs with nodes marked focused also have nodes marked pending ", func() {
				BeforeEach(func() {
					specs = Specs{
						S(N(), N()),
						S(N(), N()),
						S(N(Pending), N(Focus)),
						S(N()),
					}
				})
				It("does not skip any other specs and notes that it does not have programmatic focus", func() {
					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
					Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, true, false}))
					Ω(hasProgrammaticFocus).Should(BeFalse())
				})
			})
		})

		Context("when there are focus strings and/or skip strings configured", func() {
			BeforeEach(func() {
				specs = Specs{
					S(N("blue"), N("dragon")),
					S(N("blue"), N("Dragon")),
					S(N("red dragon"), N()),
					S(N("green dragon"), N()),
					S(N(Pending), N("blue Dragon")),
					S(N("yellow dragon")),
					S(N("yellow dragon")),
				}
			})

			Context("when there are focus strings configured", func() {
				BeforeEach(func() {
					conf.FocusStrings = []string{"blue [dD]ra", "(red|green) dragon"}
				})

				It("overrides any programmatic focus, runs only specs that match the focus string, and continues to skip specs with nodes marked pending", func() {
					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
					Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, false, false, true, true, true}))
					Ω(hasProgrammaticFocus).Should(BeFalse())
				})

				It("includes the description string in the search", func() {
					conf.FocusStrings = []string{"Silmaril"}
					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
					Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, false, false, true, false, false}))
					Ω(hasProgrammaticFocus).Should(BeFalse())
				})
			})

			Context("when there are skip strings configured", func() {
				BeforeEach(func() {
					conf.SkipStrings = []string{"blue [dD]ragon", "red dragon"}
				})

				It("overrides any programmatic focus, and runs specs that don't match the skip strings, and continues to skip specs with nodes marked pending", func() {
					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
					Ω(harvestSkips(specs)).Should(Equal([]bool{true, true, true, false, true, false, false}))
					Ω(hasProgrammaticFocus).Should(BeFalse())
				})

				It("includes the description string in the search", func() {
					conf.SkipStrings = []string{"Silmaril"}
					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
					Ω(harvestSkips(specs)).Should(Equal([]bool{true, true, true, true, true, true, true}))
					Ω(hasProgrammaticFocus).Should(BeFalse())
				})
			})

			Context("when skip and focus are configured", func() {
				BeforeEach(func() {
					conf.FocusStrings = []string{"blue [dD]ragon", "(red|green) dragon"}
					conf.SkipStrings = []string{"red dragon", "Dragon"}
				})

				It("ORs both together", func() {
					specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
					Ω(harvestSkips(specs)).Should(Equal([]bool{false, true, true, false, true, true, true}))
					Ω(hasProgrammaticFocus).Should(BeFalse())
				})
			})
		})

		Context("when configured to focus/skip files", func() {
			BeforeEach(func() {
				specs = Specs{
					S(N(CL("file_a", 1))),               //include because "file_:1" is in FocusFiles
					S(N(CL("file_b", 3, "file_b", 15))), //include because "file_:15-21" is in FocusFiles
					S(N(CL("file_b", 17))),              //skip because "_b:17" is in SkipFiles
					S(N(CL("file_b", 20), Pending)),     //skip because spec is flagged pending
					S(N(CL("c", 3))),                    //skip because "c" is not in FocusFiles
					S(N(CL("d", 17))),                   //include because "d " is in FocusFiles
				}

				conf.FocusFiles = []string{"file_:1,15-21", "d"}
				conf.SkipFiles = []string{"_b:17"}
			})

			It("applies a file-based focus and skip filter", func() {
				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
				Ω(harvestSkips(specs)).Should(Equal([]bool{false, false, true, true, true, false}))
				Ω(hasProgrammaticFocus).Should(BeFalse())
			})
		})

		Context("when configured with a label filter", func() {
			BeforeEach(func() {
				conf.LabelFilter = "(cat || cow) && !fish"
				specs = Specs{
					S(N(ntCon, Label("cat", "dog")), N(ntIt, "A", Label("fish"))),  //skip because fish
					S(N(ntCon, Label("cat", "dog")), N(ntIt, "B", Label("apple"))), //include because has cat and not fish
					S(N(ntCon, Label("dog")), N(ntIt, "C", Label("apple"))),        //skip because no cat or cow
					S(N(ntCon, Label("cow")), N(ntIt, "D", Label("fish"))),         //skip because fish
					S(N(ntCon, Label("cow")), N(ntIt, "E")),                        //include because cow and no fish
					S(N(ntCon, Label("cow")), N(ntIt, "F", Pending)),               //skip because pending
				}
			})

			It("applies the label filters", func() {
				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
				Ω(harvestSkips(specs)).Should(Equal([]bool{true, false, true, true, false, true}))
				Ω(hasProgrammaticFocus).Should(BeFalse())

			})
		})

		Context("when configured with a label filter that filters on the suite level label", func() {
			BeforeEach(func() {
				conf.LabelFilter = "cat && TopLevelLabel"
				specs = Specs{
					S(N(ntCon, Label("cat", "dog")), N(ntIt, "A", Label("fish"))), //include because cat and suite has TopLevelLabel
					S(N(ntCon, Label("dog")), N(ntIt, "B", Label("apple"))),       //skip because no cat
				}
			})
			It("honors the suite level label", func() {
				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
				Ω(harvestSkips(specs)).Should(Equal([]bool{false, true}))
				Ω(hasProgrammaticFocus).Should(BeFalse())
			})
		})

		Context("when configured with focus/skip files, focus/skip strings, and label filters", func() {
			BeforeEach(func() {
				specs = Specs{
					S(N("dog", CL("file_a", 1), Label("brown"))),                   //include because "file_:1" is in FocusFiles and "dog" is in FocusStrings and has "brown" label
					S(N("dog", CL("file_a", 1), Label("white"))),                   //skip because does not have "brown" label
					S(N("dog cat", CL("file_b", 3, "file_b", 15), Label("brown"))), //skip because "file_:15-21" is in FocusFiles but "cat" is in SkipStirngs
					S(N("fish", CL("file_b", 17), Label("brown"))),                 //skip because "_b:17" is in SkipFiles, even though "fish" is in FocusStrings
					S(N("biscuit", CL("file_b", 20), Pending, Label("brown"))),     //skip because spec is flagged pending
					S(N("pony", CL("c", 3), Label("brown"))),                       //skip because "c" is not in FocusFiles or FocusStrings
					S(N("goat", CL("d", 17), Label("brown"))),                      //skip because "goat" is in FocusStrings but "d" is not in FocusFiles
				}

				conf.FocusFiles = []string{"file_:1,15-21"}
				conf.SkipFiles = []string{"_b:17"}
				conf.FocusStrings = []string{"goat", "dog", "fish", "biscuit"}
				conf.SkipStrings = []string{"cat"}
				conf.LabelFilter = "brown"
			})

			It("applies all filters", func() {
				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
				Ω(harvestSkips(specs)).Should(Equal([]bool{false, true, true, true, true, true, true}))
				Ω(hasProgrammaticFocus).Should(BeFalse())
			})
		})

		Context("when configured with focus/skip files, focus/skip strings, and label filters and there is a programmatic focus", func() {
			BeforeEach(func() {
				specs = Specs{
					S(N("dog", CL("file_a", 1), Label("brown"))),                   //skip because "file_:1" is in FocusFiles and "dog" is in FocusStrings and has "brown" label but a different spec has a programmatic focus
					S(N("dog", CL("file_a", 17), Label("brown"), Focus)),           //include because "file_:15-21" is in FocusFiles and "dog" is in FocusStrings and has "brown" label
					S(N("dog", CL("file_a", 1), Label("white"), Focus)),            //skip because does not have "brown" label
					S(N("dog cat", CL("file_b", 3, "file_b", 15), Label("brown"))), //skip because "file_:15-21" is in FocusFiles but "cat" is in SkipStirngs
					S(N("fish", CL("file_b", 17), Label("brown"))),                 //skip because "_b:17" is in SkipFiles, even though "fish" is in FocusStrings
					S(N("biscuit", CL("file_b", 20), Pending, Label("brown"))),     //skip because spec is flagged pending
					S(N("pony", CL("c", 3), Label("brown"))),                       //skip because "c" is not in FocusFiles or FocusStrings
					S(N("goat", CL("d", 17), Label("brown"))),                      //skip because "goat" is in FocusStrings but "d" is not in FocusFiles
				}

				conf.FocusFiles = []string{"file_:1,15-21"}
				conf.SkipFiles = []string{"_b:17"}
				conf.FocusStrings = []string{"goat", "dog", "fish", "biscuit"}
				conf.SkipStrings = []string{"cat"}
				conf.LabelFilter = "brown"
			})

			It("applies all filters", func() {
				specs, hasProgrammaticFocus := internal.ApplyFocusToSpecs(specs, description, suiteLabels, conf)
				Ω(harvestSkips(specs)).Should(Equal([]bool{true, false, true, true, true, true, true, true}))
				Ω(hasProgrammaticFocus).Should(BeTrue())
			})
		})
	})
})