Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

### Fixes
- Report invalid `--focus` and `--skip` regular expressions as configuration errors instead of panicking.

### Maintenance

Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3095,6 +3095,8 @@ It("likes fish", func() {...})

then `ginkgo --focus=dog --focus=fish --skip=cat --skip=purple` will only run `"likes dogs"`, `"likes dog fish"`, and `"likes fish"`.

Invalid regular expressions cause a configuration error and a non-zero exit before suite setup or spec bodies run.

The description-based `--focus` and `--skip` flags were Ginkgo's original command-line based filtering mechanism and will continue to be supported - however, we recommend using labels when possible as the label filter language is more flexible and easier to reason about.

#### Combining Filters
Expand Down
72 changes: 72 additions & 0 deletions integration/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration_test

import (
"path/filepath"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -16,6 +17,16 @@ var _ = Describe("Filter", func() {
fm.MountFixture("filter")
})

overrideTextFilters := func(config string) {
fm.WriteFile("filter", "filter_suite_test.go", strings.Replace(
fm.ContentOf("filter", "filter_suite_test.go"),
`RunSpecs(t, "FilterFixture Suite", Label("TopLevelLabel"))`,
`suiteConfig, _ := GinkgoConfiguration()
`+config+`
RunSpecs(t, "FilterFixture Suite", Label("TopLevelLabel"), suiteConfig)`, 1,
))
}

It("honors the focus, skip, focus-file and skip-file flags", func() {
session := startGinkgo(fm.PathTo("filter"),
"--focus=dog", "--focus=fish",
Expand Down Expand Up @@ -84,6 +95,67 @@ var _ = Describe("Filter", func() {
}
})

DescribeTable("should reject invalid text filters before running the suite", func(args []string, config string) {
if config != "" {
overrideTextFilters(config)
}
fm.AppendToFile("filter", "filter_suite_test.go", `
var _ = BeforeSuite(func() { println("FILTER_SUITE_BODY_RAN") })
`)
session := startGinkgo(fm.PathTo("filter"), append(args, "--no-color")...)
Eventually(session).Should(gexec.Exit(1))
output := string(session.Out.Contents()) + string(session.Err.Contents())
Ω(output).ShouldNot(ContainSubstring("panic:"))
Ω(output).Should(ContainSubstring("Ginkgo detected configuration issues:"))
Ω(output).Should(ContainSubstring("error parsing regexp"))
Ω(output).ShouldNot(ContainSubstring("FILTER_SUITE_BODY_RAN"))
if len(args) > 1 {
Ω(output).Should(ContainSubstring("Invalid --focus regular expression:"))
Ω(output).Should(ContainSubstring("\nInvalid --skip regular expression:"))
}
},
Entry("with an invalid focus regexp", []string{"--focus=["}, ""),
Entry("with an invalid skip regexp", []string{"--skip=["}, ""),
Entry("with invalid focus and skip regexps", []string{"--focus=[", "--skip=["}, ""),
Entry("with an invalid programmatic focus regexp", []string{}, `suiteConfig.FocusStrings = []string{"["}`),
Entry("with an invalid programmatic skip regexp", []string{}, `suiteConfig.SkipStrings = []string{"["}`),
)

It("should preserve regexp semantics across repeated text filters", func() {
session := startGinkgo(fm.PathTo("filter"),
"--focus=(dog", "--focus=fish)",
"--skip=(cat", "--skip=fish)",
"--json-report=report.json",
)
Eventually(session).Should(gexec.Exit(0))
specs := Reports(fm.LoadJSONReports("filter", "report.json")[0].SpecReports)
for _, spec := range specs {
if spec.LeafNodeText == "dog" {
Ω(spec).Should(HavePassed())
} else {
Ω(spec).Should(SatisfyAny(HaveBeenSkipped(), BePending()))
}
}
})

It("should allow suites to override invalid text filters", func() {
overrideTextFilters(`suiteConfig.FocusStrings = []string{"dog"}
suiteConfig.SkipStrings = []string{"cat"}`)
session := startGinkgo(fm.PathTo("filter"),
"--focus=[", "--skip=[",
"--json-report=report.json",
)
Eventually(session).Should(gexec.Exit(0))
specs := Reports(fm.LoadJSONReports("filter", "report.json")[0].SpecReports)
for _, spec := range specs {
if strings.Contains(spec.LeafNodeText, "dog") && spec.LeafNodeText != "pending dog" {
Ω(spec).Should(HavePassed())
} else {
Ω(spec).Should(SatisfyAny(HaveBeenSkipped(), BePending()))
}
}
})

It("errors if the file-filter format is wrong", func() {
session := startGinkgo(fm.PathTo("filter"), "--focus-file=foo:bar", "--skip-file=")
Eventually(session).Should(gexec.Exit(1))
Expand Down
16 changes: 16 additions & 0 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ package types

import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -442,6 +444,20 @@ func VetConfig(flagSet GinkgoFlagSet, suiteConfig SuiteConfig, reporterConfig Re
errors = append(errors, GinkgoErrors.SleepOnFailureInParallelConfiguration())
}

if len(suiteConfig.FocusStrings) > 0 {
_, err := regexp.Compile(strings.Join(suiteConfig.FocusStrings, "|"))
if err != nil {
errors = append(errors, fmt.Errorf("Invalid --focus regular expression: %w\n", err))
}
}

if len(suiteConfig.SkipStrings) > 0 {
_, err := regexp.Compile(strings.Join(suiteConfig.SkipStrings, "|"))
if err != nil {
errors = append(errors, fmt.Errorf("Invalid --skip regular expression: %w\n", err))
}
}

if len(suiteConfig.FocusFiles) > 0 {
_, err := ParseFileFilters(suiteConfig.FocusFiles)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions types/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,28 @@ var _ = Describe("Config", func() {
})
})

Describe("text filter errors", func() {
DescribeTable("should reject invalid text filter regexps", func(focus, skip []string, messages []string) {
suiteConf.FocusStrings = focus
suiteConf.SkipStrings = skip
errors := types.VetConfig(flagSet, suiteConf, repConf)
Ω(errors).Should(HaveLen(len(messages)))
for i, message := range messages {
Ω(errors[i]).Should(MatchError(ContainSubstring(message)))
}
},
Entry("with an invalid focus regexp", []string{"["}, []string{}, []string{"missing closing ]"}),
Entry("with an invalid skip regexp", []string{}, []string{"("}, []string{"missing closing )"}),
Entry("with invalid focus and skip regexps", []string{"["}, []string{"("}, []string{"missing closing ]", "missing closing )"}),
)

It("should accept text filters that form valid regexps when ORed together", func() {
suiteConf.FocusStrings = []string{"(dog", "fish)"}
suiteConf.SkipStrings = []string{"(cat", "purple)"}
Ω(types.VetConfig(flagSet, suiteConf, repConf)).Should(BeEmpty())
})
})

Describe("file filter errors", func() {
Context("with an invalid --focus-file and/or --skip-file", func() {
BeforeEach(func() {
Expand Down
Loading