diff --git a/pkg/io/finder/finder_test.go b/pkg/io/finder/finder_test.go new file mode 100644 index 00000000..be7ca765 --- /dev/null +++ b/pkg/io/finder/finder_test.go @@ -0,0 +1,27 @@ +package finder + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFindMavenRoots(t *testing.T) { + + files := []string{"test/asd/pom.xml", "test2/pom.xml", "test2/test/asd/pom.xml", "test3/tes"} + f := Finder{} + roots, err := f.FindMavenRoots(files) + + assert.Nil(t, err) + assert.Len(t, roots, 0) +} + +func TestFindJavaClassDirs(t *testing.T) { + files := []string{"test/asd/pom.xml", "test2/basd/qwe/asd.class", "test2/test/asd", "test3/tes"} + f := Finder{} + files, err := f.FindJavaClassDirs(files) + + assert.Nil(t, err) + assert.Len(t, files, 1) + assert.Equal(t, files[0], "test2/basd/qwe") +} diff --git a/pkg/io/finder/refiner.go b/pkg/io/finder/refiner.go index 172fc541..eae5e0e6 100644 --- a/pkg/io/finder/refiner.go +++ b/pkg/io/finder/refiner.go @@ -82,19 +82,28 @@ func MapFilesToDir(dirs []string, files []string) map[string][]string { for _, file := range files { longestMatchLength := 0 var matchingDir string + matched := false for _, dir := range dirs { matchLength := 0 + longestSeperatorMatch := 0 for i := 0; i < len(file) && i < len(dir); i++ { if file[i] != dir[i] { break } matchLength++ + if filepath.Separator == file[i] { + longestSeperatorMatch = matchLength + } } - if matchLength > longestMatchLength { - longestMatchLength = matchLength + if longestSeperatorMatch > longestMatchLength { + longestMatchLength = longestSeperatorMatch matchingDir = dir + matched = true } } + if !matched { + continue + } if _, ok := dirToFilesMap[matchingDir]; !ok { dirToFilesMap[matchingDir] = []string{} diff --git a/pkg/io/finder/refiner_test.go b/pkg/io/finder/refiner_test.go new file mode 100644 index 00000000..94feedbb --- /dev/null +++ b/pkg/io/finder/refiner_test.go @@ -0,0 +1,53 @@ +package finder + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFindFiles(t *testing.T) { + files, err := FindFiles([]string{"."}, nil) + assert.Nil(t, err) + assert.NotEmpty(t, files) +} + +func TestFindFilesErr(t *testing.T) { + files, err := FindFiles([]string{"totaly-not-a-valid-path-123123123"}, nil) + assert.NotNil(t, err) + assert.Empty(t, files) +} + +func TestConvertPathsToAbsPaths(t *testing.T) { + before := "321" + files, err := ConvertPathsToAbsPaths([]string{before}) + after := files[0] + assert.Nil(t, err) + assert.True(t, len(before) < len(after)) +} + +func TestMapFilesToDir(t *testing.T) { + + dirs := []string{"test/", "test2/"} + files := []string{"test/asd", "test2/basd/qwe", "test2/test/asd", "test3/tes"} + mapFiles := MapFilesToDir(dirs, files) + + assert.Len(t, mapFiles["test/"], 1) + assert.Len(t, mapFiles["test2/"], 2) +} + +func TestMapFilesToEmptyDir(t *testing.T) { + + dirs := []string{} + files := []string{"test/asd", "test2/basd/qwe", "test2/test/asd", "test3/tes"} + mapFiles := MapFilesToDir(dirs, files) + + assert.Empty(t, mapFiles) +} + +func TestGCDPath(t *testing.T) { + files := []string{"test2/bas", "test2/basd/qwe", "test2/test/asd"} + res := GCDPath(files) + + assert.Equal(t, res, "test2/") +} diff --git a/pkg/tui/generation_error_list_test.go b/pkg/tui/generation_error_list_test.go new file mode 100644 index 00000000..38954f7b --- /dev/null +++ b/pkg/tui/generation_error_list_test.go @@ -0,0 +1,130 @@ +package tui + +import ( + "bytes" + "errors" + "os" + "testing" + + "github.com/debricked/cli/pkg/callgraph/job" + "github.com/debricked/cli/pkg/callgraph/job/testdata" + "github.com/stretchr/testify/assert" +) + +func TestNewCallgraphJobsErrorList(t *testing.T) { + mirror := os.Stdout + errList := NewCallgraphJobsErrorList(mirror, []job.IJob{}) + assert.NotNil(t, errList) +} + +func TestRenderNoCallgraphJobs(t *testing.T) { + var listBuffer bytes.Buffer + errList := NewCallgraphJobsErrorList(&listBuffer, []job.IJob{}) + + err := errList.Render() + + assert.NoError(t, err) + output := listBuffer.String() + assertOutput(t, output, nil) +} + +func TestRenderWarningCallgraphJob(t *testing.T) { + var listBuffer bytes.Buffer + + warningErr := errors.New("warning-message") + jobMock := testdata.NewJobMock("file", nil) + jobMock.Errors().Warning(warningErr) + errList := NewCallgraphJobsErrorList(&listBuffer, []job.IJob{jobMock}) + + err := errList.Render() + + assert.NoError(t, err) + output := listBuffer.String() + contains := []string{ + "file", + "\n* ", + "Warning", + "|", + "warning-message\n", + } + assertOutput(t, output, contains) +} + +func TestRenderCriticalCallgraphJob(t *testing.T) { + var listBuffer bytes.Buffer + + warningErr := errors.New("critical-message") + jobMock := testdata.NewJobMock("file", nil) + jobMock.Errors().Critical(warningErr) + errList := NewCallgraphJobsErrorList(&listBuffer, []job.IJob{jobMock}) + + err := errList.Render() + + assert.NoError(t, err) + output := listBuffer.String() + contains := []string{ + "file", + "\n* ", + "Critical", + "|", + "critical-message\n", + } + assertOutput(t, output, contains) +} + +func TestRenderCriticalAndWarningCallgraphJob(t *testing.T) { + var listBuffer bytes.Buffer + + jobMock := testdata.NewJobMock("manifest-file", nil) + + warningErr := errors.New("warning-message") + jobMock.Errors().Warning(warningErr) + + criticalErr := errors.New("critical-message") + jobMock.Errors().Critical(criticalErr) + + errList := NewCallgraphJobsErrorList(&listBuffer, []job.IJob{jobMock}) + + err := errList.Render() + + assert.NoError(t, err) + output := listBuffer.String() + contains := []string{ + "manifest-file", + "\n* ", + "Critical", + "critical-message\n", + "Warning", + "|", + "warning-message\n", + } + assertOutput(t, output, contains) +} + +func TestRenderCriticalAndWorkingCallgraphJob(t *testing.T) { + var listBuffer bytes.Buffer + + jobWithErrMock := testdata.NewJobMock("manifest-file", nil) + + criticalErr := errors.New("critical-message") + jobWithErrMock.Errors().Critical(criticalErr) + + jobWorkingMock := testdata.NewJobMock("working-manifest-file", nil) + + errList := NewCallgraphJobsErrorList(&listBuffer, []job.IJob{jobWithErrMock, jobWorkingMock}) + + err := errList.Render() + + assert.NoError(t, err) + output := listBuffer.String() + contains := []string{ + "manifest-file", + "\n* ", + "Critical", + "|", + "critical-message\n", + } + assertOutput(t, output, contains) + + assert.NotContains(t, output, jobWorkingMock) +} diff --git a/pkg/wire/cli_container_test.go b/pkg/wire/cli_container_test.go index 536716fa..18a7a544 100644 --- a/pkg/wire/cli_container_test.go +++ b/pkg/wire/cli_container_test.go @@ -36,6 +36,7 @@ func assertCliContainer(t *testing.T, cc *CliContainer) { assert.NotNil(t, cc.Finder()) assert.NotNil(t, cc.Scanner()) assert.NotNil(t, cc.Resolver()) + assert.NotNil(t, cc.CallgraphGenerator()) assert.NotNil(t, cc.LicenseReporter()) assert.NotNil(t, cc.VulnerabilityReporter()) }