From 50e0bda26e0f164f9fe74da0a5f91b9969a01ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Owen=20Rumney?= Date: Wed, 10 Feb 2021 08:55:35 +0000 Subject: [PATCH] update the should ignore file --- internal/app/squealer/scan/scanner.go | 8 +++++--- internal/app/squealer/scan/scanner_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/internal/app/squealer/scan/scanner.go b/internal/app/squealer/scan/scanner.go index 2746f66..ef1e5e0 100644 --- a/internal/app/squealer/scan/scanner.go +++ b/internal/app/squealer/scan/scanner.go @@ -2,10 +2,12 @@ package scan import ( "fmt" - "github.com/owenrumney/squealer/internal/app/squealer/config" - "github.com/owenrumney/squealer/internal/app/squealer/mertics" "os" + "regexp" "strings" + + "github.com/owenrumney/squealer/internal/app/squealer/config" + "github.com/owenrumney/squealer/internal/app/squealer/mertics" ) type ScannerType string @@ -48,7 +50,7 @@ func notGit(basepath string) bool { func shouldIgnore(filename string, ignorePaths []string, ignoreExtensions []string) bool { for _, ignorePath := range ignorePaths { - if strings.HasPrefix(filename, ignorePath) { + if match, err := regexp.MatchString(fmt.Sprintf(`\b%s\b`, ignorePath), filename); err == nil && match { return true } } diff --git a/internal/app/squealer/scan/scanner_test.go b/internal/app/squealer/scan/scanner_test.go index 8608177..16258b3 100644 --- a/internal/app/squealer/scan/scanner_test.go +++ b/internal/app/squealer/scan/scanner_test.go @@ -30,3 +30,17 @@ func TestNewScannerIsDirectoryScanner(t *testing.T) { assert.NoError(t, err) assert.IsType(t, &directoryScanner{}, scanner) } + +func TestShouldIgnore(t *testing.T) { + ignorePaths := []string{"vendor", "npm_modules"} + ignoreExtensions := []string{"zip"} + assert.True(t, shouldIgnore("/src/scan/vendor/github.com", ignorePaths, ignoreExtensions)) + assert.True(t, shouldIgnore("/src/scan/npm_modules/github.com", ignorePaths, ignoreExtensions)) + assert.True(t, shouldIgnore("vendor/github.com", ignorePaths, ignoreExtensions)) + assert.True(t, shouldIgnore("npm_modules/github.com", ignorePaths, ignoreExtensions)) + assert.True(t, shouldIgnore("pingu.zip", ignorePaths, ignoreExtensions)) + assert.False(t, shouldIgnore("src/scan", ignorePaths, ignoreExtensions)) + assert.False(t, shouldIgnore("test", ignorePaths, ignoreExtensions)) + assert.False(t, shouldIgnore("govendor", ignorePaths, ignoreExtensions)) + assert.False(t, shouldIgnore("pingu.honk", ignorePaths, ignoreExtensions)) +}