How to use ParseLabelFilter method of types Package

Best Ginkgo code snippet using types.ParseLabelFilter

Run Ginkgo automation tests on LambdaTest cloud grid

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

label_filter_test.go

Source: label_filter_test.go Github

copy
1package types_test
2
3import (
4	"fmt"
5	"reflect"
6	"strings"
7
8	. "github.com/onsi/ginkgo/v2"
9	. "github.com/onsi/gomega"
10
11	"github.com/onsi/ginkgo/v2/types"
12)
13
14var _ = Describe("LabelFilter", func() {
15	BeforeEach(func() {
16		types.DEBUG_LABEL_FILTER_PARSING = false
17	})
18
19	DescribeTable("Catching and communicating syntax errors",
20		func(filter string, location int, message string) {
21			_, err := types.ParseLabelFilter(filter)
22			Ω(err).Should(MatchError(types.GinkgoErrors.SyntaxErrorParsingLabelFilter(filter, location, message)))
23		},
24		func(filter string, location int, message string) string {
25			return fmt.Sprintf("%s => %s", filter, message)
26		},
27		Entry(nil, "(A && B) || ((C && D) && E", 12, "Mismatched '(' - could not find matching ')'."),
28		Entry(nil, "A || B) && C", 6, "Mismatched ')' - could not find matching '('."),
29		Entry(nil, "A && (   )", 9, "Found empty '()' group."),
30		Entry(nil, "A && (((   )))", 11, "Found empty '()' group."),
31		Entry(nil, "A && /[a/", 5, "RegExp compilation error: error parsing regexp: missing closing ]: `[a`"),
32		Entry(nil, "A &&", -1, "Unexpected EOF."),
33		Entry(nil, "A & B", 2, "Invalid token '&'.  Did you mean '&&'?"),
34		Entry(nil, "A | B", 2, "Invalid token '|'.  Did you mean '||'?"),
35		Entry(nil, "(A) B", 4, "Found two adjacent labels.  You need an operator between them."),
36		Entry(nil, "A (B)", 2, "Invalid token '('."),
37		Entry(nil, "A !B", 2, "Invalid token '!'."),
38		Entry(nil, "A !B", 2, "Invalid token '!'."),
39		Entry(nil, " && B", 1, "Operator '&&' missing left hand operand."),
40		Entry(nil, " || B", 1, "Operator '||' missing left hand operand."),
41		Entry(nil, "&&", 0, "Operator '&&' missing left hand operand."),
42		Entry(nil, "&& || B", 0, "Operator '&&' missing left hand operand."),
43	)
44
45	type matchingLabels []string
46	type nonMatchingLabels []string
47
48	M := func(l ...string) matchingLabels {
49		return matchingLabels(l)
50	}
51
52	NM := func(l ...string) nonMatchingLabels {
53		return nonMatchingLabels(l)
54	}
55
56	DescribeTable("Generating correct LabelFilter",
57		func(filter string, samples ...interface{}) {
58			lf, err := types.ParseLabelFilter(filter)
59			Ω(err).ShouldNot(HaveOccurred())
60			for _, sample := range samples {
61				switch reflect.TypeOf(sample) {
62				case reflect.TypeOf(matchingLabels{}):
63					labels := []string(sample.(matchingLabels))
64					Ω(lf(labels)).Should(BeTrue(), strings.Join(labels, ","))
65				case reflect.TypeOf(nonMatchingLabels{}):
66					labels := []string(sample.(nonMatchingLabels))
67					Ω(lf(labels)).Should(BeFalse(), strings.Join(labels, ","))
68				}
69			}
70		},
71		Entry("A single label", "cat",
72			M("cat"), M("cat", "dog"), M("dog", "cat"),
73			NM(), NM("cow"),
74		),
75		Entry("Trimming whitespace", "  cat  ",
76			M("cat"), M("cat", "dog"), M("dog", "cat"),
77			NM(), NM("cow"),
78		),
79		Entry("Ignoring case", "cat",
80			M("CAT"),
81		),
82		Entry("A simple ||", "cat || dog ",
83			M("cat"), M("cat", "cow", "dog"), M("dog", "cow", "cat"), M("dog"),
84			NM(), NM("cow"), NM("cat dog"),
85		),
86		Entry("A simple ||", "cat||dog ",
87			M("cat"), M("cat", "cow", "dog"), M("dog", "cow", "cat"), M("dog"),
88			NM(), NM("cow"),
89		),
90		Entry("A simple ,", "cat, dog ",
91			M("cat"), M("cat", "cow", "dog"), M("dog", "cow", "cat"), M("dog"),
92			NM(), NM("cow"),
93		),
94		Entry("Multiple ORs ,", "cat,dog||cow,fruit ",
95			M("cat"), M("cat", "cow", "dog"), M("dog"), M("fruit"), M("cow", "aardvark"),
96			NM(), NM("aardvark"),
97		),
98		Entry("A simple NOT", "!cat",
99			M("dog"), M(),
100			NM("cat"), NM("cat", "dog"),
101		),
102		Entry("A double negative", "!!cat",
103			M("cat"), M("cat", "dog"),
104			NM(), NM("dog"),
105		),
106		Entry("A simple AND", "cat && dog",
107			M("cat", "dog"), M("cat", "dog", "cow"),
108			NM(), NM("cat"), NM("dog"), NM("cow"), NM("cat dog"),
109		),
110		Entry("Multiple ANDs", "cat && dog && cow fruit",
111			M("cat", "dog", "cow fruit"), M("cat", "dog", "cow fruit", "aardvark"),
112			NM(), NM("cat", "dog", "cow", "fruit"),
113		),
114		Entry("&& has > precedence than ||", "cat || dog && cow",
115			M("cat"), M("dog", "cow"),
116			NM(), NM("dog"),
117		),
118		Entry("&& has > precedence than || but () overrides", "(cat || dog) && cow",
119			M("cat", "cow"), M("dog", "cow"),
120			NM(), NM("dog"), NM("cat"), NM("cow"), NM("cat", "dog"),
121		),
122		Entry("&& has > precedence than ||", "cat && dog || cow",
123			M("cat", "dog"), M("cow"),
124			NM(), NM("cat"), NM("dog"),
125		),
126		Entry("&& has > precedence than || but () overrides", "cat && (dog || cow)",
127			M("cat", "dog"), M("cat", "cow"),
128			NM(), NM("cat"), NM("dog"), NM("cow"),
129		),
130		Entry("! has > precedence than &&", "!cat && dog",
131			M("dog"), M("dog", "cow"),
132			NM(), NM("cat", "dog"), NM("cat"), NM("cow"),
133		),
134		Entry("! has > precedence than && but () overrides", "!(cat && dog)",
135			M(), M("cow"), M("cat"), M("dog"), M("dog", "cow"),
136			NM("cat", "dog"), NM("cat", "dog", "cow"),
137		),
138		Entry("! has > precedence than ||", "!cat || dog",
139			M(), M("dog"), M("cow"),
140			NM("cat"), NM("cat", "cow"),
141		),
142		Entry("! has > precedence than || but () overrides", "!(cat || dog)",
143			M(), M("cow"),
144			NM("cat"), NM("dog"), NM("cat", "dog"), NM("cat", "dog", "cow"),
145		),
146		Entry("it can handle multiple groups", "(!(cat || dog) && fruit) || (cow && !aardvark)",
147			M("cow"), M("fruit"), M("fruit", "cow", "aardvark"), M("cow", "dog", "fruit"),
148			NM(), NM("cow", "aardvark"), NM("cat", "fruit"), NM("dog", "fruit"), NM("dog", "cat", "fruit"), NM("cat", "fruit", "cow", "aardvark"),
149		),
150		Entry("Coalescing groups", "(((cat)))",
151			M("cat"), M("cat", "dog"), M("dog", "cat"),
152			NM(), NM("cow"),
153		),
154		Entry("Comping whitespace around a simple group", "  (cat)  ",
155			M("cat"), M("cat", "dog"), M("dog", "cat"),
156			NM(), NM("cow"),
157		),
158		Entry("Supporting regular expressions", "/c[ao]/ && dog",
159			M("dog", "cat"), M("dog", "cow"), M("cat", "cow", "dog"), M("dog", "orca"),
160			NM("dog"), NM("cow"), NM("cat"), NM("dog", "fruit"), NM("dog", "cup"),
161		),
162	)
163
164	cl := types.NewCodeLocation(0)
165	DescribeTable("Validating Labels",
166		func(label string, expected string, expectedError error) {
167			out, err := types.ValidateAndCleanupLabel(label, cl)
168			Ω(out).Should(Equal(expected))
169			if expectedError == nil {
170				Ω(err).Should(BeNil())
171			} else {
172				Ω(err).Should(Equal(expectedError))
173			}
174		},
175		func(label string, expected string, expectedError error) string {
176			return label
177		},
178		Entry(nil, "cow", "cow", nil),
179		Entry(nil, "  cow dog  ", "cow dog", nil),
180		Entry(nil, "", "", types.GinkgoErrors.InvalidEmptyLabel(cl)),
181		Entry(nil, "   ", "", types.GinkgoErrors.InvalidEmptyLabel(cl)),
182		Entry(nil, "cow&", "", types.GinkgoErrors.InvalidLabel("cow&", cl)),
183		Entry(nil, "cow|", "", types.GinkgoErrors.InvalidLabel("cow|", cl)),
184		Entry(nil, "cow,", "", types.GinkgoErrors.InvalidLabel("cow,", cl)),
185		Entry(nil, "cow(", "", types.GinkgoErrors.InvalidLabel("cow(", cl)),
186		Entry(nil, "cow()", "", types.GinkgoErrors.InvalidLabel("cow()", cl)),
187		Entry(nil, "cow)", "", types.GinkgoErrors.InvalidLabel("cow)", cl)),
188		Entry(nil, "cow/", "", types.GinkgoErrors.InvalidLabel("cow/", cl)),
189	)
190})
191
Full Screen

focus.go

Source: focus.go Github

copy
1package internal
2
3import (
4	"regexp"
5	"strings"
6
7	"github.com/onsi/ginkgo/v2/types"
8)
9
10/*
11	If a container marked as focus has a descendant that is also marked as focus, Ginkgo's policy is to
12	unmark the container's focus.  This gives developers a more intuitive experience when debugging specs.
13	It is common to focus a container to just run a subset of specs, then identify the specific specs within the container to focus -
14	this policy allows the developer to simply focus those specific specs and not need to go back and turn the focus off of the container:
15
16	As a common example, consider:
17
18		FDescribe("something to debug", function() {
19			It("works", function() {...})
20			It("works", function() {...})
21			FIt("doesn't work", function() {...})
22			It("works", function() {...})
23		})
24
25	here the developer's intent is to focus in on the `"doesn't work"` spec and not to run the adjacent specs in the focused `"something to debug"` container.
26	The nested policy applied by this function enables this behavior.
27*/
28func ApplyNestedFocusPolicyToTree(tree *TreeNode) {
29	var walkTree func(tree *TreeNode) bool
30	walkTree = func(tree *TreeNode) bool {
31		if tree.Node.MarkedPending {
32			return false
33		}
34		hasFocusedDescendant := false
35		for _, child := range tree.Children {
36			childHasFocus := walkTree(child)
37			hasFocusedDescendant = hasFocusedDescendant || childHasFocus
38		}
39		tree.Node.MarkedFocus = tree.Node.MarkedFocus && !hasFocusedDescendant
40		return tree.Node.MarkedFocus || hasFocusedDescendant
41	}
42
43	walkTree(tree)
44}
45
46/*
47	Ginkgo supports focussing specs using `FIt`, `FDescribe`, etc. - this is called "programmatic focus"
48	It also supports focussing specs using regular expressions on the command line (`-focus=`, `-skip=`) that match against spec text
49	and file filters (`-focus-files=`, `-skip-files=`) that match against code locations for nodes in specs.
50
51	If any of the CLI flags are provided they take precedence.  The file filters run first followed by the regex filters.
52
53	This function sets the `Skip` property on specs by applying Ginkgo's focus policy:
54	- If there are no CLI arguments and no programmatic focus, do nothing.
55	- If there are no CLI arguments but a spec somewhere has programmatic focus, skip any specs that have no programmatic focus.
56	- If there are CLI arguments parse them and skip any specs that either don't match the focus filters or do match the skip filters.
57
58	*Note:* specs with pending nodes are Skipped when created by NewSpec.
59*/
60func ApplyFocusToSpecs(specs Specs, description string, suiteLabels Labels, suiteConfig types.SuiteConfig) (Specs, bool) {
61	focusString := strings.Join(suiteConfig.FocusStrings, "|")
62	skipString := strings.Join(suiteConfig.SkipStrings, "|")
63
64	hasFocusCLIFlags := focusString != "" || skipString != "" || len(suiteConfig.SkipFiles) > 0 || len(suiteConfig.FocusFiles) > 0 || suiteConfig.LabelFilter != ""
65
66	type SkipCheck func(spec Spec) bool
67
68	// by default, skip any specs marked pending
69	skipChecks := []SkipCheck{func(spec Spec) bool { return spec.Nodes.HasNodeMarkedPending() }}
70	hasProgrammaticFocus := false
71
72	if !hasFocusCLIFlags {
73		// check for programmatic focus
74		for _, spec := range specs {
75			if spec.Nodes.HasNodeMarkedFocus() && !spec.Nodes.HasNodeMarkedPending() {
76				skipChecks = append(skipChecks, func(spec Spec) bool { return !spec.Nodes.HasNodeMarkedFocus() })
77				hasProgrammaticFocus = true
78				break
79			}
80		}
81	}
82
83	if suiteConfig.LabelFilter != "" {
84		labelFilter, _ := types.ParseLabelFilter(suiteConfig.LabelFilter)
85		skipChecks = append(skipChecks, func(spec Spec) bool { 
86			return !labelFilter(UnionOfLabels(suiteLabels, spec.Nodes.UnionOfLabels())) 
87		})
88	}
89
90	if len(suiteConfig.FocusFiles) > 0 {
91		focusFilters, _ := types.ParseFileFilters(suiteConfig.FocusFiles)
92		skipChecks = append(skipChecks, func(spec Spec) bool { return !focusFilters.Matches(spec.Nodes.CodeLocations()) })
93	}
94
95	if len(suiteConfig.SkipFiles) > 0 {
96		skipFilters, _ := types.ParseFileFilters(suiteConfig.SkipFiles)
97		skipChecks = append(skipChecks, func(spec Spec) bool { return skipFilters.Matches(spec.Nodes.CodeLocations()) })
98	}
99
100	if focusString != "" {
101		// skip specs that don't match the focus string
102		re := regexp.MustCompile(focusString)
103		skipChecks = append(skipChecks, func(spec Spec) bool { return !re.MatchString(description + " " + spec.Text()) })
104	}
105
106	if skipString != "" {
107		// skip specs that match the skip string
108		re := regexp.MustCompile(skipString)
109		skipChecks = append(skipChecks, func(spec Spec) bool { return re.MatchString(description + " " + spec.Text()) })
110	}
111
112	// skip specs if shouldSkip() is true.  note that we do nothing if shouldSkip() is false to avoid overwriting skip status established by the node's pending status
113	processedSpecs := Specs{}
114	for _, spec := range specs {
115		for _, skipCheck := range skipChecks {
116			if skipCheck(spec) {
117				spec.Skip = true
118				break
119			}
120		}
121		processedSpecs = append(processedSpecs, spec)
122	}
123
124	return processedSpecs, hasProgrammaticFocus
125}
126
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

Most used method in

Trigger ParseLabelFilter code on LambdaTest Cloud Grid

Execute automation tests with ParseLabelFilter on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)