From a94b2e9ce3942c3b5a15f1634d18b7f1bddf9274 Mon Sep 17 00:00:00 2001 From: Tommy Graves Date: Tue, 26 Nov 2024 20:53:56 -0500 Subject: [PATCH] Support negative globs for partition globs (#92) --- internal/fs/local.go | 16 ++++++++++++++-- internal/fs/local_test.go | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/internal/fs/local.go b/internal/fs/local.go index c4f9639..e176a73 100644 --- a/internal/fs/local.go +++ b/internal/fs/local.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "sort" + "strings" doublestar "github.com/bmatcuk/doublestar/v4" @@ -52,14 +53,25 @@ func (l Local) Glob(pattern string) ([]string, error) { } func (l Local) GlobMany(patterns []string) ([]string, error) { - pathSet := make(map[string]struct{}) + pathSet := make(map[string]bool) for _, pattern := range patterns { + isNegation := false + + if strings.HasPrefix(pattern, "!") { + isNegation = true + pattern = strings.TrimPrefix(pattern, "!") + } + expandedPaths, err := l.Glob(pattern) if err != nil { return nil, errors.WithStack(err) } for _, filePath := range expandedPaths { - pathSet[filePath] = struct{}{} + if isNegation { + delete(pathSet, filePath) + } else { + pathSet[filePath] = true + } } } diff --git a/internal/fs/local_test.go b/internal/fs/local_test.go index 2309f65..a4ea1bd 100644 --- a/internal/fs/local_test.go +++ b/internal/fs/local_test.go @@ -39,6 +39,22 @@ var _ = Describe("fs.GlobMany", func() { })) }) + It("supports negation", func() { + fs := fs.Local{} + expandedPaths, _ := fs.GlobMany([]string{ + "../../test/fixtures/integration-tests/partition/*_spec.rb", + "!../../test/fixtures/integration-tests/partition/a_spec.rb", + "!../../test/fixtures/integration-tests/partition/b_spec.rb", + "../../test/fixtures/integration-tests/partition/b_spec.rb", + }) + + Expect(expandedPaths).To(Equal([]string{ + "../../test/fixtures/integration-tests/partition/b_spec.rb", + "../../test/fixtures/integration-tests/partition/c_spec.rb", + "../../test/fixtures/integration-tests/partition/d_spec.rb", + })) + }) + It("expands multiple glob patterns only returning unique paths", func() { fs := fs.Local{} expandedPaths, _ := fs.GlobMany([]string{