Skip to content

Commit

Permalink
Merge pull request #9 from antfie/feature/test-coverage
Browse files Browse the repository at this point in the history
Feature/test coverage
  • Loading branch information
antfie authored Oct 30, 2023
2 parents b1f705f + 3ce906c commit d9e7509
Show file tree
Hide file tree
Showing 35 changed files with 1,728 additions and 106 deletions.
4 changes: 2 additions & 2 deletions checks/dependencies_selected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestDependenciesSelected(t *testing.T) {

dependenciesSelected(mockReport)

assert.Equal(t, len(mockReport.Issues), 0, "Issues were reported which should not have been")
assert.Empty(t, mockReport.Issues, "Issues were reported which should not have been")
})

t.Run("Selected dependencies should be reported", func(t *testing.T) {
Expand All @@ -37,6 +37,6 @@ func TestDependenciesSelected(t *testing.T) {

dependenciesSelected(mockReport)

assert.Equal(t, len(mockReport.Issues), 1, "Issues were not reported which should have been")
assert.Equal(t, 1, len(mockReport.Issues), "Issues were not reported which should have been")
})
}
26 changes: 25 additions & 1 deletion checks/duplicates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,31 @@ func TestIdenticalModulesParallel(t *testing.T) {
}

duplicateModules(&testReport)
assert.Equal(t, 1, len(testReport.Issues))
if !assert.Equal(t, 1, len(testReport.Issues)) {
t.FailNow()
}

assert.Contains(t, testReport.Issues[0].Description, "A duplicate file name")
assert.Equal(t, report.IssueSeverityHigh, testReport.Issues[0].Severity)
})

t.Run("Different Files with same name different hashes", func(t *testing.T) {
t.Parallel()
testReport := report.Report{
UploadedFiles: []report.UploadedFile{
{Id: 111111, Name: "file1", MD5: "hash1", IsIgnored: false, IsThirdParty: false},
{Id: 222222, Name: "file1", MD5: "hash2", IsIgnored: false, IsThirdParty: false},
{Id: 333333, Name: "file2", MD5: "hash1", IsIgnored: false, IsThirdParty: false},
{Id: 444444, Name: "file2", MD5: "hash2", IsIgnored: false, IsThirdParty: false},
},
}

duplicateModules(&testReport)
if !assert.Equal(t, 1, len(testReport.Issues)) {
t.FailNow()
}

assert.Contains(t, testReport.Issues[0].Description, "2 duplicate file names")
assert.Equal(t, report.IssueSeverityHigh, testReport.Issues[0].Severity)
})
}
9 changes: 4 additions & 5 deletions checks/fatal_errors_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checks

import (
"strings"
"testing"

"github.com/antfie/scan_health/v2/report"
Expand Down Expand Up @@ -79,7 +78,7 @@ func TestModulesAreFine(t *testing.T) {
t.FailNow()
}

if !assert.True(t, strings.Contains(testReport.Issues[0].Description, "2 modules")) {
if !assert.Contains(t, testReport.Issues[0].Description, "2 modules") {
t.FailNow()
}

Expand All @@ -89,7 +88,7 @@ func TestModulesAreFine(t *testing.T) {
t.FailNow()
}

assert.True(t, strings.Contains(testReport.Recommendations[0], "PDB"))
assert.Contains(t, testReport.Recommendations[0], "PDB")
})

t.Run("Two Java Modules With No Scannable Binaries", func(t *testing.T) {
Expand Down Expand Up @@ -132,7 +131,7 @@ func TestModulesAreFine(t *testing.T) {
t.FailNow()
}

if !assert.True(t, strings.Contains(testReport.Issues[0].Description, "3 Java modules")) {
if !assert.Contains(t, testReport.Issues[0].Description, "3 Java modules") {
t.FailNow()
}

Expand Down Expand Up @@ -165,7 +164,7 @@ func TestModulesAreFine(t *testing.T) {
t.FailNow()
}

if !assert.True(t, strings.Contains(testReport.Issues[0].Description, "nested/shaded")) {
if !assert.Contains(t, testReport.Issues[0].Description, "nested/shaded") {
t.FailNow()
}

Expand Down
5 changes: 2 additions & 3 deletions checks/files_to_ignore_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checks

import (
"strings"
"testing"

"github.com/antfie/scan_health/v2/report"
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestFilesToIgnore(t *testing.T) {
t.FailNow()
}

assert.True(t, strings.Contains(testReport.Issues[0].Description, "unnecessary"))
assert.Contains(t, testReport.Issues[0].Description, "unnecessary")

if !assert.Equal(t, 1, len(testReport.Recommendations)) {
t.FailNow()
Expand All @@ -79,7 +78,7 @@ func TestFilesToIgnore(t *testing.T) {
t.FailNow()
}

assert.True(t, strings.Contains(testReport.Issues[0].Description, "2 unnecessary"))
assert.Contains(t, testReport.Issues[0].Description, "2 unnecessary")

if !assert.Equal(t, 1, len(testReport.Recommendations)) {
t.FailNow()
Expand Down
9 changes: 4 additions & 5 deletions checks/flaw_count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package checks

import (
"github.com/antfie/scan_health/v2/utils"
"strings"
"testing"

"github.com/antfie/scan_health/v2/report"
Expand Down Expand Up @@ -42,13 +41,13 @@ func TestTooManyFlaws(t *testing.T) {
t.FailNow()
}

assert.True(t, strings.Contains(testReport.Issues[0].Description, "No flaws"))
assert.Contains(t, testReport.Issues[0].Description, "No flaws")

if !assert.Equal(t, 1, len(testReport.Recommendations)) {
t.FailNow()
}

assert.True(t, strings.Contains(testReport.Recommendations[0], "When no flaws have been found"))
assert.Contains(t, testReport.Recommendations[0], "When no flaws have been found")
})

t.Run("Too Many Flaws", func(t *testing.T) {
Expand All @@ -66,12 +65,12 @@ func TestTooManyFlaws(t *testing.T) {
t.FailNow()
}

assert.True(t, strings.Contains(testReport.Issues[0].Description, "A large number"))
assert.Contains(t, testReport.Issues[0].Description, "A large number")

if !assert.Equal(t, 1, len(testReport.Recommendations)) {
t.FailNow()
}

assert.True(t, strings.Contains(testReport.Recommendations[0], "scan could be misconfigured"))
assert.Contains(t, testReport.Recommendations[0], "scan could be misconfigured")
})
}
131 changes: 131 additions & 0 deletions checks/general_recommendations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package checks

import (
"testing"

"github.com/antfie/scan_health/v2/report"
"github.com/stretchr/testify/assert"
)

// Test Cases
func TestGeneralRecommendations(t *testing.T) {

t.Run("No Recommendations Exists", func(t *testing.T) {
t.Parallel()
testReport := report.Report{
Modules: []report.Module{
{Name: "test.jar",
IsThirdParty: false,
IsIgnored: false,
Instances: []report.ModuleInstance{{
IsSelected: true,
HasFatalErrors: false,
}},
},
},

Issues: []report.Issue{},
Recommendations: []string{},
}

generalRecommendations(&testReport)
assert.Empty(t, testReport.Issues)
assert.Empty(t, testReport.Recommendations)
})

t.Run("No Module Recommendation Exists", func(t *testing.T) {
t.Parallel()
testReport := report.Report{
Modules: []report.Module{
{Name: "test.jar",
IsThirdParty: false,
IsIgnored: false,
Instances: []report.ModuleInstance{{
IsSelected: true,
HasFatalErrors: false,
}},
},
},

Issues: []report.Issue{},
Recommendations: []string{
"Test Recommendation",
},
}

generalRecommendations(&testReport)
assert.Empty(t, testReport.Issues)

// No additional recommendations made
assert.Equal(t, 1, len(testReport.Recommendations))
})

t.Run("Module Recommendation Exists", func(t *testing.T) {
t.Parallel()
testReport := report.Report{
Modules: []report.Module{
{Name: "test.jar",
IsThirdParty: false,
IsIgnored: false,
Instances: []report.ModuleInstance{{
IsSelected: true,
HasFatalErrors: false,
}},
},
},

Issues: []report.Issue{},
Recommendations: []string{
"module",
},
}

generalRecommendations(&testReport)
assert.Empty(t, testReport.Issues)

// Additional recommendations are made on the presence of 'module' in the recommendations list
assert.Equal(t, 6, len(testReport.Recommendations))
})

t.Run("Gradle Wrapper is the only selected one", func(t *testing.T) {
t.Parallel()
testReport := report.Report{
Modules: []report.Module{
{Name: "test.jar",
IsThirdParty: false,
IsIgnored: false,
Instances: []report.ModuleInstance{{
IsSelected: false,
HasFatalErrors: false,
}},
},
{Name: "gradle-wrapper.jar",
IsThirdParty: false,
IsIgnored: false,
Instances: []report.ModuleInstance{{
IsSelected: true,
HasFatalErrors: false,
}},
},
{Name: "test.jar",
IsThirdParty: false,
IsIgnored: false,
Instances: []report.ModuleInstance{{
IsSelected: false,
HasFatalErrors: false,
}},
},
},
Issues: []report.Issue{},
}

gradleWrapper(&testReport)
if !assert.Equal(t, 1, len(testReport.Issues)) {
t.FailNow()
}

assert.Contains(t, testReport.Issues[0].Description, "The only module selected ")

assert.Equal(t, 1, len(testReport.Recommendations))
})
}
Loading

0 comments on commit d9e7509

Please sign in to comment.