Skip to content

Commit

Permalink
Parse include options (#50)
Browse files Browse the repository at this point in the history
* Parse include options

* Fail with invalid regex

* Check only included paths

* Bump version
  • Loading branch information
raviqqe authored Dec 2, 2020
1 parent 73aa1ba commit b4baadb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 15 deletions.
1 change: 1 addition & 0 deletions .snapshots/TestHelp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Application Options:
-c, --cases= Comma-separated names of enabled case styles (options:
camel, upper-camel, kebab, upper-kebab, snake, upper-snake,
space, upper-space)
-i, --include= Include paths matched with the given regular expression
-e, --exclude= Exclude paths matched with the given regular expression
--ignore-git Ignore Git repository information
-v, --verbose Be verbose
Expand Down
9 changes: 9 additions & 0 deletions argument_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type arguments struct {
Bare bool `short:"b" long:"bare" description:"Use patterns as they are"`
Regexp bool `short:"r" long:"regexp" description:"Use patterns as regular expressions"`
RawCaseNames string `short:"c" long:"cases" description:"Comma-separated names of enabled case styles (options: camel, upper-camel, kebab, upper-kebab, snake, upper-snake, space, upper-space)"`
RawInclude string `short:"i" long:"include" description:"Include paths matched with the given regular expression"`
RawExclude string `short:"e" long:"exclude" description:"Exclude paths matched with the given regular expression"`
IgnoreGit bool `long:"ignore-git" description:"Ignore Git repository information"`
Verbose bool `short:"v" long:"verbose" description:"Be verbose"`
Expand All @@ -26,6 +27,7 @@ type arguments struct {
To string
Path string
CaseNames map[caseName]struct{}
Include *regexp.Regexp
Exclude *regexp.Regexp
}

Expand Down Expand Up @@ -73,6 +75,13 @@ func (p *argumentParser) Parse(ss []string) (*arguments, error) {
}
}

if args.RawInclude != "" {
args.Include, err = regexp.Compile(args.RawInclude)
if err != nil {
return nil, err
}
}

if args.RawExclude != "" {
args.Exclude, err = regexp.Compile(args.RawExclude)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions argument_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestParseArguments(t *testing.T) {
{"-c", "camel", "foo", "bar"},
{"--cases", "camel", "foo", "bar"},
{"-c", "camel,kebab", "foo", "bar"},
{"-i", "foo", "foo", "bar"},
{"--include", "foo", "foo", "bar"},
{"-e", "foo", "foo", "bar"},
{"--exclude", "foo", "foo", "bar"},
{"--ignore-git", "foo", "bar"},
Expand All @@ -38,6 +40,7 @@ func TestParseArgumentsError(t *testing.T) {
{"foo"},
{"foo", "bar", "baz", "blah"},
{"-c", "caml", "foo", "bar"},
{"--include", "(", "foo", "bar"},
{"--exclude", "(", "foo", "bar"},
} {
_, err := newArgumentParser(".").Parse(ss)
Expand Down
7 changes: 6 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ func (c *command) Run(ss []string) error {
)
}

ss, err = c.fileFinder.Find(args.Path, args.Exclude, args.IgnoreGit)
ss, err = c.fileFinder.Find(
args.Path,
args.Include,
args.Exclude,
args.IgnoreGit,
)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion configuration.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

const (
version = "0.13.1"
version = "0.14.0"
errorChannelCapacity = 512
maxOpenFiles = 200 // lower than 256 on macOS by default
fileTypeDetectionBufferSize = 512
Expand Down
14 changes: 9 additions & 5 deletions file_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@ func newFileFinder(f *repositoryFileFinder, fs billy.Filesystem) *fileFinder {
return &fileFinder{f, fs}
}

func (f *fileFinder) Find(d string, excludedPattern *regexp.Regexp, ignoreGit bool) ([]string, error) {
func (f *fileFinder) Find(d string, includedPattern, excludedPattern *regexp.Regexp, ignoreGit bool) ([]string, error) {
fs, err := f.findFiles(d, ignoreGit)
if err != nil {
return nil, err
} else if excludedPattern == nil {
return fs, nil
}

ffs := make([]string, 0, len(fs))

for _, f := range fs {
if !excludedPattern.MatchString(f) {
ffs = append(ffs, f)
if includedPattern != nil &&
!includedPattern.MatchString(f) {
continue
} else if excludedPattern != nil &&
excludedPattern.MatchString(f) {
continue
}

ffs = append(ffs, f)
}

return ffs, nil
Expand Down
30 changes: 22 additions & 8 deletions file_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestFileFinderFindFile(t *testing.T) {
_, err := fs.Create("foo")
assert.Nil(t, err)

ss, err := newTestFileFinder(fs).Find(".", nil, false)
ss, err := newTestFileFinder(fs).Find(".", nil, nil, false)
assert.Nil(t, err)
assert.Equal(t, []string{"foo"}, ss)
}
Expand All @@ -39,7 +39,7 @@ func TestFileFinderDoNotFindDirectory(t *testing.T) {
err := fs.MkdirAll("foo", 0o755)
assert.Nil(t, err)

ss, err := newTestFileFinder(fs).Find(".", nil, false)
ss, err := newTestFileFinder(fs).Find(".", nil, nil, false)
assert.Nil(t, err)
assert.Equal(t, []string{}, ss)
}
Expand All @@ -49,7 +49,7 @@ func TestFileFinderFindRecursively(t *testing.T) {
_, err := fs.Create("foo/foo")
assert.Nil(t, err)

ss, err := newTestFileFinder(fs).Find(".", nil, false)
ss, err := newTestFileFinder(fs).Find(".", nil, nil, false)
assert.Nil(t, err)
assert.Equal(t, []string{"foo/foo"}, normalizePaths(ss))
}
Expand All @@ -61,7 +61,7 @@ func TestFileFinderDoNotIncludePathsNotIncludedInRepository(t *testing.T) {

commitFiles(t, fs, []string{"bar"})

ss, err := newTestFileFinder(fs).Find(".", nil, false)
ss, err := newTestFileFinder(fs).Find(".", nil, nil, false)
assert.Nil(t, err)
assert.Equal(t, []string{"bar"}, normalizePaths(ss))
}
Expand All @@ -71,7 +71,7 @@ func TestFileFinderIgnoreGitRepositoryInformation(t *testing.T) {

commitFiles(t, fs, []string{".foo"})

ss, err := newTestFileFinder(fs).Find(".", nil, true)
ss, err := newTestFileFinder(fs).Find(".", nil, nil, true)
assert.Nil(t, err)
assert.Equal(t, []string{}, normalizePaths(ss))
}
Expand All @@ -81,7 +81,7 @@ func TestFileFinderDoNotFindHiddenFile(t *testing.T) {
_, err := fs.Create(".foo")
assert.Nil(t, err)

ss, err := newTestFileFinder(fs).Find(".", nil, false)
ss, err := newTestFileFinder(fs).Find(".", nil, nil, false)
assert.Nil(t, err)
assert.Equal(t, []string{}, ss)
}
Expand All @@ -91,11 +91,25 @@ func TestFileFinderFindFileInHiddenDirectory(t *testing.T) {
_, err := fs.Create(".foo/foo")
assert.Nil(t, err)

ss, err := newTestFileFinder(fs).Find(".foo", nil, false)
ss, err := newTestFileFinder(fs).Find(".foo", nil, nil, false)
assert.Nil(t, err)
assert.Equal(t, []string{".foo/foo"}, normalizePaths(ss))
}

func TestFileFinderDoNotFindIncludedFile(t *testing.T) {
fs := memfs.New()

_, err := fs.Create("foo")
assert.Nil(t, err)

_, err = fs.Create("bar")
assert.Nil(t, err)

ss, err := newTestFileFinder(fs).Find(".", regexp.MustCompile("foo"), nil, false)
assert.Nil(t, err)
assert.Equal(t, []string{"foo"}, normalizePaths(ss))
}

func TestFileFinderDoNotFindExcludedFile(t *testing.T) {
fs := memfs.New()

Expand All @@ -105,7 +119,7 @@ func TestFileFinderDoNotFindExcludedFile(t *testing.T) {
_, err = fs.Create("bar")
assert.Nil(t, err)

ss, err := newTestFileFinder(fs).Find(".", regexp.MustCompile("foo"), false)
ss, err := newTestFileFinder(fs).Find(".", nil, regexp.MustCompile("foo"), false)
assert.Nil(t, err)
assert.Equal(t, []string{"bar"}, normalizePaths(ss))
}

0 comments on commit b4baadb

Please sign in to comment.