Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check ReNotMatch and ReMatch against filepath not info.Name() #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ func checkDefaultIgnore(path string, info os.FileInfo, isVCS bool) bool {
return false
}

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

Expand Down Expand Up @@ -118,7 +118,7 @@ func getAllFiles(paths []string, languages *DefinedLanguages, opts *ClocOptions)
}

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

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

"github.com/spf13/afero"
)
Expand Down Expand Up @@ -54,55 +53,37 @@ 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{}
fi := MockFileInfo{FileName: "/", IsDirectory: true}
if !checkOptionMatch("/", fi, opts) {
if !checkOptionMatch("/", opts) {
t.Errorf("invalid logic: renotmatchdir is nil")
}

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

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

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

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

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