Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Hideo Hattori committed Feb 13, 2020
1 parent 8e05ec6 commit ed08f46
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
38 changes: 28 additions & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ func isVCSDir(path string) bool {
return false
}

func checkDefaultIgnore(path string, info os.FileInfo, isVCS bool) bool {
if info.IsDir() {
// directory is ignore
return true
}
if !isVCS && isVCSDir(path) {
// vcs file or directory is ignore
return true
}

return false
}

func checkOptionMatch(path string, opts *ClocOptions) bool {
dir := filepath.Dir(path)
if opts.ReNotMatchDir != nil && opts.ReNotMatchDir.MatchString(dir) {
return false
}

if opts.ReMatchDir != nil && !opts.ReMatchDir.MatchString(dir) {
return false
}

return true
}

// getAllFiles return all of the files to be analyzed in paths.
func getAllFiles(paths []string, languages *DefinedLanguages, opts *ClocOptions) (result map[string]*Language, err error) {
result = make(map[string]*Language, 0)
Expand All @@ -79,20 +105,12 @@ func getAllFiles(paths []string, languages *DefinedLanguages, opts *ClocOptions)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !vcsInRoot && isVCSDir(path) {
if ignore := checkDefaultIgnore(path, info, vcsInRoot); ignore {
return nil
}

// check not-match directory
dir := filepath.Dir(path)
if opts.ReNotMatchDir != nil && opts.ReNotMatchDir.MatchString(dir) {
return nil
}

if opts.ReMatchDir != nil && !opts.ReMatchDir.MatchString(dir) {
if match := checkOptionMatch(path, opts); !match {
return nil
}

Expand Down
63 changes: 62 additions & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package gocloc

import "testing"
import (
"os"
"regexp"
"testing"

"github.com/spf13/afero"
)

func TestContainsComment(t *testing.T) {
if !containsComment(`int a; /* A takes care of counts */`, [][]string{{"/*", "*/"}}) {
Expand All @@ -24,3 +30,58 @@ func TestCheckMD5SumIgnore(t *testing.T) {
t.Errorf("invalid sequence")
}
}

func TestCheckDefaultIgnore(t *testing.T) {
appFS := afero.NewMemMapFs()
appFS.Mkdir("/test", os.ModeDir)
_, _ = appFS.Create("/test/one.go")

fileInfo, _ := appFS.Stat("/")
if !checkDefaultIgnore("/", fileInfo, false) {
t.Errorf("invalid logic: this is directory")
}

if !checkDefaultIgnore("/", fileInfo, true) {
t.Errorf("invalid logic: this is vcs file or directory")
}

fileInfo, _ = appFS.Stat("/test/one.go")
if checkDefaultIgnore("/test/one.go", fileInfo, false) {
t.Errorf("invalid logic: should not ignore this file")
}
}

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

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

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

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

opts.ReMatchDir = regexp.MustCompile("thisisdir-not-match")
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")
if !checkOptionMatch("/thisisdir/one.go", opts) {
t.Errorf("invalid logic: renotmatchdir is not ignore")
}
}

0 comments on commit ed08f46

Please sign in to comment.