Skip to content

Commit

Permalink
Support negative globs for partition globs (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
TAGraves authored Nov 27, 2024
1 parent db3e97a commit a94b2e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/fs/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"sort"
"strings"

doublestar "github.com/bmatcuk/doublestar/v4"

Expand Down Expand Up @@ -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
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions internal/fs/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit a94b2e9

Please sign in to comment.