Skip to content

Commit

Permalink
File matching options.
Browse files Browse the repository at this point in the history
  • Loading branch information
pascaldekloe authored and hhatto committed Nov 26, 2021
1 parent 3ddbb04 commit 0815d00
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
14 changes: 11 additions & 3 deletions cmd/gocloc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type CmdOptions struct {
OutputType string `long:"output-type" default:"default" description:"output type [values: default,cloc-xml,sloccount,json]"`
ExcludeExt string `long:"exclude-ext" description:"exclude file name extensions (separated commas)"`
IncludeLang string `long:"include-lang" description:"include language name (separated commas)"`
Match string `long:"match" description:"include file name (regex)"`
NotMatch string `long:"not-match" description:"exclude file name (regex)"`
MatchDir string `long:"match-d" description:"include dir name (regex)"`
NotMatchDir string `long:"not-match-d" description:"exclude dir name (regex)"`
Debug bool `long:"debug" description:"dump debug log for developer"`
Expand Down Expand Up @@ -239,13 +241,19 @@ func main() {
}
}

// setup option for not match directory
if opts.NotMatchDir != "" {
clocOpts.ReNotMatchDir = regexp.MustCompile(opts.NotMatchDir)
// directory and file matching options
if opts.Match != "" {
clocOpts.ReMatch = regexp.MustCompile(opts.Match)
}
if opts.NotMatch != "" {
clocOpts.ReNotMatch = regexp.MustCompile(opts.NotMatch)
}
if opts.MatchDir != "" {
clocOpts.ReMatchDir = regexp.MustCompile(opts.MatchDir)
}
if opts.NotMatchDir != "" {
clocOpts.ReNotMatchDir = regexp.MustCompile(opts.NotMatchDir)
}

// setup option for include languages
for _, lang := range strings.Split(opts.IncludeLang, ",") {
Expand Down
2 changes: 2 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type ClocOptions struct {
SkipDuplicated bool
ExcludeExts map[string]struct{}
IncludeLangs map[string]struct{}
ReNotMatch *regexp.Regexp
ReMatch *regexp.Regexp
ReNotMatchDir *regexp.Regexp
ReMatchDir *regexp.Regexp

Expand Down
14 changes: 11 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ func checkDefaultIgnore(path string, info os.FileInfo, isVCS bool) bool {
return false
}

func checkOptionMatch(path string, opts *ClocOptions) bool {
func checkOptionMatch(path string, info os.FileInfo, opts *ClocOptions) bool {
// check match directory & file options
if opts.ReNotMatch != nil && opts.ReNotMatch.MatchString(info.Name()) {
return false
}
if opts.ReMatch != nil && !opts.ReMatch.MatchString(info.Name()) {
return false
}

dir := filepath.Dir(path)
if opts.ReNotMatchDir != nil && opts.ReNotMatchDir.MatchString(dir) {
return false
Expand Down Expand Up @@ -109,8 +117,8 @@ func getAllFiles(paths []string, languages *DefinedLanguages, opts *ClocOptions)
return nil
}

// check not-match directory
if match := checkOptionMatch(path, opts); !match {
// check match & not-match directory
if match := checkOptionMatch(path, info, opts); !match {
return nil
}

Expand Down
31 changes: 25 additions & 6 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"regexp"
"testing"
"time"

"github.com/spf13/afero"
)
Expand Down Expand Up @@ -51,37 +52,55 @@ func TestCheckDefaultIgnore(t *testing.T) {
}
}

type MockFileInfo struct {
FileName string
IsDirectory bool
}

func (mfi MockFileInfo) Name() string { return mfi.FileName }
func (mfi MockFileInfo) Size() int64 { return int64(8) }
func (mfi MockFileInfo) Mode() os.FileMode { return os.ModePerm }
func (mfi MockFileInfo) ModTime() time.Time { return time.Now() }
func (mfi MockFileInfo) IsDir() bool { return mfi.IsDirectory }
func (mfi MockFileInfo) Sys() interface{} { return nil }

func TestCheckOptionMatch(t *testing.T) {
opts := &ClocOptions{}
if !checkOptionMatch("/", opts) {
fi := MockFileInfo{FileName: "/", IsDirectory: true}
if !checkOptionMatch("/", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is nil")
}

opts.ReNotMatchDir = regexp.MustCompile("thisisdir-not-match")
if !checkOptionMatch("/thisisdir/one.go", opts) {
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is nil")
}

opts.ReNotMatchDir = regexp.MustCompile("thisisdir")
if checkOptionMatch("/thisisdir/one.go", opts) {
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is ignore")
}

opts = &ClocOptions{}
opts.ReMatchDir = regexp.MustCompile("thisisdir")
if !checkOptionMatch("/thisisdir/one.go", opts) {
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is not ignore")
}

opts.ReMatchDir = regexp.MustCompile("thisisdir-not-match")
if checkOptionMatch("/thisisdir/one.go", opts) {
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is ignore")
}

opts = &ClocOptions{}
opts.ReNotMatchDir = regexp.MustCompile("thisisdir-not-match")
opts.ReMatchDir = regexp.MustCompile("thisisdir")
if !checkOptionMatch("/thisisdir/one.go", opts) {
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is not ignore")
}
}

0 comments on commit 0815d00

Please sign in to comment.