Skip to content

Commit

Permalink
language_detect: fix ignore files on Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Alcantara <matheus.alcantara@zup.com.br>
  • Loading branch information
matheusalcantarazup committed Dec 7, 2021
1 parent 3cde903 commit d0b00e3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/language_detect/language_detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (ld *LanguageDetect) checkDefaultPathsToIgnore(path string) bool {

func (ld *LanguageDetect) checkAdditionalPathsToIgnore(path string) bool {
for _, value := range ld.config.FilesOrPathsToIgnore {
matched, _ := doublestar.Match(strings.TrimSpace(value), path)
matched, _ := doublestar.Match(filepath.ToSlash(strings.TrimSpace(value)), filepath.ToSlash(path))
if matched {
return true
}
Expand Down
86 changes: 74 additions & 12 deletions internal/controllers/language_detect/language_detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package languagedetect
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

Expand All @@ -45,7 +47,52 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

func TestNewLanguageDetect(t *testing.T) {
func TestLanguageDetectCopyFilesWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Only execute on Windows")
}

logger.LogSetOutput(io.Discard)

cfg := config.New()
cfg.FilesOrPathsToIgnore = []string{"**\\routes\\**", "**\\*.mod", "**\\*.sum"}
cfg.ProjectPath = testutil.GoExample1

assertTestLanguageDetectCopyFiles(t, cfg)
}

func TestLanguageDetectCopyFiles(t *testing.T) {
logger.LogSetOutput(io.Discard)

cfg := config.New()
cfg.FilesOrPathsToIgnore = []string{"**/routes/**", "**/*.mod", "**/*.sum"}
cfg.ProjectPath = testutil.GoExample1

assertTestLanguageDetectCopyFiles(t, cfg)
}

func assertTestLanguageDetectCopyFiles(t *testing.T, cfg *config.Config) {
analysisID := uuid.New()

ld := NewLanguageDetect(cfg, analysisID)

langs, err := ld.Detect(cfg.ProjectPath)

assert.NoError(t, err, "Expected no error to detect languages: %v", err)

assert.Equal(t, []languages.Language{languages.Leaks, languages.Generic, languages.Go}, langs)

analysisPath := filepath.Join(cfg.ProjectPath, ".horusec", analysisID.String())
assert.NoDirExists(t, filepath.Join(analysisPath, "api", "routes"))
assert.NoFileExists(t, filepath.Join(analysisPath, "go.mod"))
assert.NoFileExists(t, filepath.Join(analysisPath, "go.sum"))

assert.FileExists(t, filepath.Join(analysisPath, "api", "server.go"))
assert.FileExists(t, filepath.Join(analysisPath, "api", "util", "util.go"))
}

func TestLanguageDetect(t *testing.T) {
logger.LogSetOutput(io.Discard)
t.Cleanup(func() {
err := os.RemoveAll(filepath.Join(testutil.ExamplesPath, ".horusec"))
assert.NoError(t, err)
Expand All @@ -54,20 +101,26 @@ func TestNewLanguageDetect(t *testing.T) {
assert.NoError(t, err)
}
})

t.Run("Should return error when the folder not exists", func(t *testing.T) {
controller := NewLanguageDetect(config.New(), uuid.New())

monitor, err := controller.Detect("./NOT-EXIST-PATH")
langs, err := controller.Detect("./NOT-EXIST-PATH")

assert.Error(t, err)
assert.Nil(t, monitor)
assert.Nil(t, langs)
})

t.Run("Should ignore files of the type images", func(t *testing.T) {
dstPath := filepath.Join(tmpPath, uuid.New().String())
srcPath := filepath.Join(testutil.RootPath, "assets")
t.Cleanup(func() {
assert.NoError(t, os.RemoveAll(filepath.Join(srcPath, ".horusec")))
})

err := copy.Copy(srcPath, dstPath, func(src string) bool { return false })
assert.NoError(t, err)

controller := NewLanguageDetect(config.New(), uuid.New())

langs, err := controller.Detect(srcPath)
Expand All @@ -76,7 +129,6 @@ func TestNewLanguageDetect(t *testing.T) {
assert.Contains(t, langs, languages.Leaks)
assert.Contains(t, langs, languages.Generic)
assert.Len(t, langs, 2)
assert.NoError(t, os.RemoveAll(filepath.Join(srcPath, ".horusec")))
})

t.Run("Should ignore additional folder setup in configs", func(t *testing.T) {
Expand Down Expand Up @@ -253,17 +305,30 @@ func TestNewLanguageDetect(t *testing.T) {
assert.Len(t, langs, len(languages.Values())-2)
})
t.Run("Should ignore folders present in toignore.GetDefaultFoldersToIgnore()", func(t *testing.T) {
controller := NewLanguageDetect(config.New(), uuid.New())
wd, err := os.Getwd()
assert.NoError(t, err)

t.Cleanup(func() {
logger.SetLogLevel("info")
logger.LogSetOutput(os.Stdout)
for _, folderName := range toignore.GetDefaultFoldersToIgnore() {
err = os.RemoveAll(filepath.Join(wd, folderName))
}
})

controller := NewLanguageDetect(config.New(), uuid.New())

logger.SetLogLevel("debug")
stdOutMock := bytes.NewBufferString("")
logger.LogSetOutput(stdOutMock)

for _, folderName := range toignore.GetDefaultFoldersToIgnore() {
err = os.MkdirAll(filepath.Join(wd, folderName), 0o700)
assert.NoError(t, err)
}

langs, err := controller.Detect(wd)

assert.NoError(t, err)
assert.Len(t, langs, 3)
assert.Contains(t, langs, languages.Go)
Expand All @@ -274,24 +339,21 @@ func TestNewLanguageDetect(t *testing.T) {
log := fmt.Sprint(messages.MsgDebugFolderOrFileIgnored, "[", filepath.Join(wd, folderName), "]")
assert.Contains(t, stdOutMock.String(), strings.ReplaceAll(log, `\`, `\\`))
}
t.Cleanup(func() {
logger.SetLogLevel("info")
logger.LogSetOutput(os.Stdout)
for _, folderName := range toignore.GetDefaultFoldersToIgnore() {
err = os.RemoveAll(filepath.Join(wd, folderName))
}
})
})
t.Run("Should read git submodule path and copy to .horusec folder git submodule correctly", func(t *testing.T) {
analysisID := uuid.New()
configs := config.New()
configs.EnableGitHistoryAnalysis = true

controller := NewLanguageDetect(configs, analysisID)
_, err := controller.Detect(testutil.ExamplesPath)

assert.NoError(t, err)

projectClonedPath := filepath.Join(testutil.ExamplesPath, ".horusec", analysisID.String())
fileInfo, err := os.Stat(filepath.Join(projectClonedPath, ".git"))
assert.NoError(t, err)

assert.True(t, fileInfo.IsDir())
})
}

0 comments on commit d0b00e3

Please sign in to comment.