Skip to content

Commit

Permalink
feat: allow multiple ignore patterns
Browse files Browse the repository at this point in the history
* IgnoreSourceFiles now accepts multiple patterns

If at least one pattern matches, the files are filtered.

Fixes #14

* docs: fix typo

---------

Co-authored-by: Guilherme J. Tramontina <[email protected]>
  • Loading branch information
Bios-Marcel and gtramontina authored Aug 1, 2023
1 parent 6a8ebab commit 957d080
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 30 deletions.
14 changes: 9 additions & 5 deletions internal/ignoredrepository/ignoredrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@ import (
)

type FilteredRepository struct {
pattern *regexp.Regexp
patterns []*regexp.Regexp
delegate ooze.Repository
}

func New(pattern *regexp.Regexp, delegate ooze.Repository) *FilteredRepository {
func New(patterns []*regexp.Regexp, delegate ooze.Repository) *FilteredRepository {
return &FilteredRepository{
pattern: pattern,
patterns: patterns,
delegate: delegate,
}
}

func (r *FilteredRepository) ListGoSourceFiles() []*gosourcefile.GoSourceFile {
filtered := []*gosourcefile.GoSourceFile{}

FILE_LOOP:
for _, file := range r.delegate.ListGoSourceFiles() {
if !r.pattern.MatchString(file.String()) {
filtered = append(filtered, file)
for _, pattern := range r.patterns {
if pattern.MatchString(file.String()) {
continue FILE_LOOP
}
}
filtered = append(filtered, file)
}

return filtered
Expand Down
36 changes: 31 additions & 5 deletions internal/ignoredrepository/ignoredrepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestIgnoredRepository(t *testing.T) {
t.Run("empty repository yields empty results", func(t *testing.T) {
repository := ignoredrepository.New(
regexp.MustCompile(".*"),
[]*regexp.Regexp{regexp.MustCompile(".*")},
fakerepository.New(fakerepository.FS{}),
)

Expand All @@ -22,7 +22,7 @@ func TestIgnoredRepository(t *testing.T) {

t.Run("multiple files with match all pattern yields no files", func(t *testing.T) {
repository := ignoredrepository.New(
regexp.MustCompile(".*"),
[]*regexp.Regexp{regexp.MustCompile(".*")},
fakerepository.New(fakerepository.FS{
"source1.go": []byte("source 1"),
"source2.go": []byte("source 2"),
Expand All @@ -36,7 +36,7 @@ func TestIgnoredRepository(t *testing.T) {
t.Run("multiple files with specific pattern yields filtered files", func(t *testing.T) {
{
repository := ignoredrepository.New(
regexp.MustCompile(".*2.*"),
[]*regexp.Regexp{regexp.MustCompile(".*2.*")},
fakerepository.New(fakerepository.FS{
"source1.go": []byte("source 1"),
"source2.go": []byte("source 2"),
Expand All @@ -52,7 +52,7 @@ func TestIgnoredRepository(t *testing.T) {

{
repository := ignoredrepository.New(
regexp.MustCompile("^dir/source.*$"),
[]*regexp.Regexp{regexp.MustCompile("^dir/source.*$")},
fakerepository.New(fakerepository.FS{
"source1.go": []byte("source 1"),
"dir/source2.go": []byte("source 2"),
Expand All @@ -69,13 +69,39 @@ func TestIgnoredRepository(t *testing.T) {
}, repository.ListGoSourceFiles())
}
})

t.Run("multiple files with multiple patterns", func(t *testing.T) {
{
repository := ignoredrepository.New(
[]*regexp.Regexp{
regexp.MustCompile(".*2.*"),
regexp.MustCompile(".*3.*"),
regexp.MustCompile(".*5.*"),
},
fakerepository.New(fakerepository.FS{
"source1.go": []byte("source 1"),
"source2.go": []byte("source 2"),
"source3.go": []byte("source 3"),
"source4.go": []byte("source 4"),
"source5.go": []byte("source 5"),
"source6.go": []byte("source 6"),
}),
)

assert.Equal(t, []*gosourcefile.GoSourceFile{
gosourcefile.New("source1.go", []byte("source 1")),
gosourcefile.New("source4.go", []byte("source 4")),
gosourcefile.New("source6.go", []byte("source 6")),
}, repository.ListGoSourceFiles())
}
})
}

func TestIgnoredRepository_LinkAllToTemporaryRepository(t *testing.T) {
t.Run("delegates to underlying repository", func(t *testing.T) {
expectedTempRepository := fakerepository.NewTemporary()
repository := ignoredrepository.New(
regexp.MustCompile("dummy"),
[]*regexp.Regexp{regexp.MustCompile("dummy")},
fakerepository.New(fakerepository.FS{}, expectedTempRepository),
)

Expand Down
22 changes: 12 additions & 10 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
type Option func(Options) Options

type Options struct {
Repository ooze.Repository
TestRunner laboratory.TestRunner
TemporaryDir laboratory.TemporaryDirectory
MinimumThreshold float32
Parallel bool
IgnoreSourceFilesPattern *regexp.Regexp
Viruses []viruses.Virus
Repository ooze.Repository
TestRunner laboratory.TestRunner
TemporaryDir laboratory.TemporaryDirectory
MinimumThreshold float32
Parallel bool
IgnoreSourceFilesPatterns []*regexp.Regexp
Viruses []viruses.Virus
}

// WithRepositoryRoot configures which directory is the repository root. This is
Expand Down Expand Up @@ -70,11 +70,13 @@ func Parallel() func(Options) Options {
}
}

// IgnoreSourceFiles configures a regular expression representing source files
// IgnoreSourceFiles configures regular expressions representing source files
// to be filtered out and not suffer any mutations.
func IgnoreSourceFiles(pattern string) func(Options) Options {
func IgnoreSourceFiles(patterns ...string) func(Options) Options {
return func(options Options) Options {
options.IgnoreSourceFilesPattern = regexp.MustCompile(pattern)
for _, pattern := range patterns {
options.IgnoreSourceFilesPatterns = append(options.IgnoreSourceFilesPatterns, regexp.MustCompile(pattern))
}

return options
}
Expand Down
4 changes: 2 additions & 2 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func TestOptions(t *testing.T) {
t.Run("can configure source files to ignore", func(t *testing.T) {
{
options := ooze.IgnoreSourceFiles(".*")(ooze.Options{})
assert.Equal(t, regexp.MustCompile(".*"), options.IgnoreSourceFilesPattern)
assert.Equal(t, []*regexp.Regexp{regexp.MustCompile(".*")}, options.IgnoreSourceFilesPatterns)
}

{
options := ooze.IgnoreSourceFiles(`.*\.go`)(ooze.Options{})
assert.Equal(t, regexp.MustCompile(`.*\.go`), options.IgnoreSourceFilesPattern)
assert.Equal(t, []*regexp.Regexp{regexp.MustCompile(`.*\.go`)}, options.IgnoreSourceFilesPatterns)
}
})

Expand Down
16 changes: 8 additions & 8 deletions release.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ func init() { //nolint:gochecknoinits
}

var defaultOptions = Options{ //nolint:gochecknoglobals
Repository: fsrepository.New("."),
TestRunner: cmdtestrunner.New("go", "test", "-count=1", "./..."),
TemporaryDir: fstemporarydir.New("ooze-"),
MinimumThreshold: 1.0,
Parallel: false,
IgnoreSourceFilesPattern: nil,
Repository: fsrepository.New("."),
TestRunner: cmdtestrunner.New("go", "test", "-count=1", "./..."),
TemporaryDir: fstemporarydir.New("ooze-"),
MinimumThreshold: 1.0,
Parallel: false,
IgnoreSourceFilesPatterns: nil,
Viruses: []viruses.Virus{
arithmetic.New(),
arithmeticassignment.New(),
Expand Down Expand Up @@ -108,8 +108,8 @@ func Release(t *testing.T, options ...Option) {
opts.MinimumThreshold,
)

if opts.IgnoreSourceFilesPattern != nil {
opts.Repository = ignoredrepository.New(opts.IgnoreSourceFilesPattern, opts.Repository)
if opts.IgnoreSourceFilesPatterns != nil {
opts.Repository = ignoredrepository.New(opts.IgnoreSourceFilesPatterns, opts.Repository)
}

if verbose() {
Expand Down

0 comments on commit 957d080

Please sign in to comment.