From c301925f8e5e0d437b94862e5a26699411f0ec57 Mon Sep 17 00:00:00 2001 From: David Baggerman Date: Thu, 20 Jun 2019 18:26:33 +1000 Subject: [PATCH] fix ignoring .git on absolute paths --- processor/file.go | 4 ++-- processor/file_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/processor/file.go b/processor/file.go index 4ac7ccca..e2cec2d6 100644 --- a/processor/file.go +++ b/processor/file.go @@ -123,7 +123,7 @@ func walkDirectoryParallel(root string, output chan *FileJob) { // Need to check if the directory is in the blacklist and if so don't bother adding a goroutine to process it shouldSkip := false for _, black := range PathBlacklist { - if strings.HasPrefix(filepath.Join(root, f.Name()), black) { + if strings.HasPrefix(filepath.Join(root, f.Name()), filepath.Join(root, black)) { shouldSkip = true if Verbose { printWarn(fmt.Sprintf("skipping directory due to being in blacklist: %s", filepath.Join(root, f.Name()))) @@ -267,7 +267,7 @@ func walkDirectory(toWalk string, blackList []string, extensionLookup map[string if info.IsDir() { for _, black := range blackList { - if strings.HasPrefix(root, black+"/") || strings.HasPrefix(root, black) { + if strings.HasPrefix(root, filepath.Join(toWalk, black)) { if Verbose { printWarn(fmt.Sprintf("skipping directory due to being in blacklist: %s", root)) } diff --git a/processor/file_test.go b/processor/file_test.go index cd45dc1f..e4a24cad 100644 --- a/processor/file_test.go +++ b/processor/file_test.go @@ -2,7 +2,9 @@ package processor import ( "math/rand" + "path/filepath" "testing" + "strings" ) func TestGetExtension(t *testing.T) { @@ -147,6 +149,44 @@ func TestWalkDirectoryParallelIgnoresRootTrailingSlash(t *testing.T) { } } +// Issue #82 - project .git directory not being filtered when using absolute +// path argument +func TestWalkDirectoryParallelIgnoresAbsoluteGitPath(t *testing.T) { + isLazy = false + ProcessConstants() + + // master is a file extension for ASP.NET, and also a filename (almost) + // certain to appear in the .git directory. + // This test also relies on the behaviour of treating `master` as a file + // with the `master` file extension. + WhiteListExtensions = []string{"master", "go"} + Exclude = []string{"vendor"} + PathBlacklist = []string{".git", "vendor"} + Verbose = true + Trace = true + Debug = true + GcFileCount = 10 + + inputChan := make(chan *FileJob, 10000) + absBaseDir, _ := filepath.Abs("../") + absGitDir := filepath.Join(absBaseDir, ".git") + + walkDirectoryParallel(absBaseDir, inputChan) + close(inputChan) + + sawGit := false + for fileJob := range inputChan { + if strings.HasPrefix(fileJob.Location, absGitDir) { + sawGit = true + break + } + } + + if sawGit { + t.Errorf("Expected .git folder to be ignored") + } +} + func TestWalkDirectory(t *testing.T) { Debug = true Exclude = []string{"test"}