Skip to content

Commit

Permalink
Add a few extra test
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgHaj committed May 16, 2023
1 parent a85715b commit 40e59d3
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 2 deletions.
27 changes: 27 additions & 0 deletions pkg/io/finder/finder_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
13 changes: 11 additions & 2 deletions pkg/io/finder/refiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
53 changes: 53 additions & 0 deletions pkg/io/finder/refiner_test.go
Original file line number Diff line number Diff line change
@@ -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/")
}
130 changes: 130 additions & 0 deletions pkg/tui/generation_error_list_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions pkg/wire/cli_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 40e59d3

Please sign in to comment.