Skip to content

Commit

Permalink
prepareSkips: to resolve regexps in excludes
Browse files Browse the repository at this point in the history
  • Loading branch information
mfederowicz committed Oct 10, 2024
1 parent d76ffb9 commit f970547
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
)

require (
github.com/bmatcuk/doublestar/v4 v4.6.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I=
github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
github.com/chavacava/garif v0.1.0/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
32 changes: 31 additions & 1 deletion revivelib/core.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/bmatcuk/doublestar/v4"
"github.com/mgechev/dots"
"github.com/mgechev/revive/config"
"github.com/mgechev/revive/lint"
Expand Down Expand Up @@ -169,14 +171,42 @@ func getPackages(includePatterns []string, excludePatterns ArrayFlags) ([][]stri
globs = append(globs, ".")
}

packages, err := dots.ResolvePackages(globs, normalizeSplit(excludePatterns))
globs, skips, err := prepareSkips(globs, normalizeSplit(excludePatterns))
if err != nil {
return nil, fmt.Errorf("prepare skips - resolving excludes before dots: %w", err)
}

packages, err := dots.ResolvePackages(globs, skips)
if err != nil {
return nil, fmt.Errorf("getting packages - resolving packages in dots: %w", err)
}

return packages, nil
}

func prepareSkips(globs, excludes []string) ([]string, []string, error) {
var skips []string
for _, path := range globs {
var basepath string
basepath, _ = doublestar.SplitPattern(path)
fsys := os.DirFS(basepath)
for _, skip := range excludes {
matches, err := doublestar.Glob(fsys, skip)
if err != nil {
return nil, nil, fmt.Errorf("Skips Error: %v", err)
}
for _, match := range matches {
path = basepath+"/"+match
// create skip only for .go files
if filepath.Ext(path) == ".go" {
skips = append(skips, path)
}
}
}
}
return globs, skips, nil
}

func normalizeSplit(strs []string) []string {
res := []string{}

Expand Down
37 changes: 37 additions & 0 deletions revivelib/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,43 @@ func TestReviveLint(t *testing.T) {
}
}

func TestReviveLintExcludeWithRegexp(t *testing.T) {
// ARRANGE
revive := getMockRevive(t)

// ACT
files := []string{"../testdata/if-return.go"}
excludePatterns := []string{"*return*"}
packages := []*revivelib.LintPattern{}
for _, file := range files {
packages = append(packages, revivelib.Include(file))
}

for _, file := range excludePatterns {
packages = append(packages, revivelib.Exclude(file))
}

failures, err := revive.Lint(packages...)

if err != nil {
t.Fatal(err)
}

// ASSERT
failureList := []lint.Failure{}

for failure := range failures {
failureList = append(failureList, failure)
}

const expected = 0

got := len(failureList)
if got != expected {
t.Fatalf("Expected failures to have %d failures, but it has %d.", expected, got)
}
}

func TestReviveFormat(t *testing.T) {
// ARRANGE
revive := getMockRevive(t)
Expand Down

0 comments on commit f970547

Please sign in to comment.