Skip to content

Commit

Permalink
Improved unit tests. Fixing integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diogo-fjrocha committed Apr 2, 2024
1 parent 50a5cc6 commit 18f8f08
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 46 deletions.
61 changes: 15 additions & 46 deletions internal/commands/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
package commands

import (
"bytes"
"fmt"
"io"
"os"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -369,24 +368,14 @@ func Test_addPackageInformation(t *testing.T) {
expectedFixLink := "https://devhub.checkmarx.com/cve-details/CVE-2021-23-424"
actualFixLink := resultsModel.Results[0].ScanResultData.ScaPackageCollection.FixLink
assert.Equal(t, expectedFixLink, actualFixLink, "FixLink should match the result ID")
}

func TestRunGetResultsByScanIdSummaryConsoleFormatWithScsNotScanned(t *testing.T) {
// Writing stdout to file
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

err := executeTestCommand(createASTTestCommandWithScs(false, false, false),
buffer, err := executeRedirectedOsStdoutTestCommand(createASTTestCommandWithScs(false, false, false),
"results", "show", "--scan-id", "MOCK", "--report-format", "summaryConsole")
assert.NilError(t, err)

w.Close()
os.Stdout = old

// Writing output to buffer
var buf bytes.Buffer
io.Copy(&buf, r)
stdoutString := buf.String()
stdoutString := buffer.String()
fmt.Print(stdoutString)

scsSummary := "| SCS - - - - - |"
Expand All @@ -401,58 +390,38 @@ func TestRunGetResultsByScanIdSummaryConsoleFormatWithScsNotScanned(t *testing.T
}

func TestRunGetResultsByScanIdSummaryConsoleFormatWithScsPartial(t *testing.T) {
// Writing stdout to file
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

err := executeTestCommand(createASTTestCommandWithScs(true, true, true),
buffer, err := executeRedirectedOsStdoutTestCommand(createASTTestCommandWithScs(true, true, true),
"results", "show", "--scan-id", "MOCK", "--report-format", "summaryConsole")
assert.NilError(t, err)

w.Close()
os.Stdout = old

// Writing output to buffer
var buf bytes.Buffer
io.Copy(&buf, r)
stdoutString := buf.String()
stdoutString := buffer.String()
ansiRegexp := regexp.MustCompile("\x1b\\[[0-9;]*[mK]")
cleanString := ansiRegexp.ReplaceAllString(stdoutString, "")
fmt.Print(stdoutString)

TotalResults := "Total Results: 17"
assert.Equal(t, strings.Contains(stdoutString, TotalResults), true,
assert.Equal(t, strings.Contains(cleanString, TotalResults), true,
"Expected: "+TotalResults)
TotalSummary := "| TOTAL 10 4 3 0 Completed |"
assert.Equal(t, strings.Contains(stdoutString, TotalSummary), true,
assert.Equal(t, strings.Contains(cleanString, TotalSummary), true,
"Expected TOTAL summary: "+TotalSummary)
scsSummary := "| SCS 5 3 2 0 Partial |"
assert.Equal(t, strings.Contains(stdoutString, scsSummary), true,
assert.Equal(t, strings.Contains(cleanString, scsSummary), true,
"Expected SCS summary:"+scsSummary)
secretDetectionSummary := "| Secret Detection 5 3 2 0 Completed |"

Check failure on line 411 in internal/commands/result_test.go

View workflow job for this annotation

GitHub Actions / lint

string `| Secret Detection 5 3 2 0 Completed |` has 2 occurrences, make it a constant (goconst)
assert.Equal(t, strings.Contains(stdoutString, secretDetectionSummary), true,
assert.Equal(t, strings.Contains(cleanString, secretDetectionSummary), true,
"Expected Secret Detection summary:"+secretDetectionSummary)
scorecardSummary := "| Scorecard 0 0 0 0 Failed |"
assert.Equal(t, strings.Contains(stdoutString, scorecardSummary), true,
assert.Equal(t, strings.Contains(cleanString, scorecardSummary), true,
"Expected Scorecard summary:"+scorecardSummary)
}

func TestRunGetResultsByScanIdSummaryConsoleFormatWithScsScorecardNotScanned(t *testing.T) {
// Writing stdout to file
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

err := executeTestCommand(createASTTestCommandWithScs(true, false, false),
buffer, err := executeRedirectedOsStdoutTestCommand(createASTTestCommandWithScs(true, false, false),
"results", "show", "--scan-id", "MOCK", "--report-format", "summaryConsole")
assert.NilError(t, err)

w.Close()
os.Stdout = old

// Writing output to buffer
var buf bytes.Buffer
io.Copy(&buf, r)
stdoutString := buf.String()
stdoutString := buffer.String()
fmt.Print(stdoutString)

scsSummary := "| SCS 5 3 2 0 Completed |"
Expand Down
20 changes: 20 additions & 0 deletions internal/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"bytes"
"fmt"
"io"
"log"
"os"
"strings"
Expand Down Expand Up @@ -190,6 +191,25 @@ func executeRedirectedTestCommand(args ...string) (*bytes.Buffer, error) {
return buffer, cmd.Execute()
}

func executeRedirectedOsStdoutTestCommand(cmd *cobra.Command, args ...string) (bytes.Buffer, error) {
// Writing os stdout to file
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

cmd.SetArgs(args)
cmd.SilenceUsage = true
err := cmd.Execute()

// Writing output to buffer
w.Close()
os.Stdout = old
var buffer bytes.Buffer
io.Copy(&buffer, r)

Check failure on line 208 in internal/commands/root_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `io.Copy` is not checked (errcheck)

return buffer, err
}

func execCmdNilAssertion(t *testing.T, args ...string) {
err := executeTestCommand(createASTTestCommand(), args...)
assert.NilError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions test/integration/util_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func createASTIntegrationTestCommand(t *testing.T) *cobra.Command {
scanSummmaryPath := viper.GetString(params.ScanSummaryPathKey)
scaPackage := viper.GetString(params.ScaPackagePathKey)
risksOverview := viper.GetString(params.RisksOverviewPathKey)
scsScanOverviewPath := viper.GetString(params.ScsScanOverviewPathKey)
uploads := viper.GetString(params.UploadsPathKey)
logs := viper.GetString(params.LogsPathKey)
codebashing := viper.GetString(params.CodeBashingPathKey)
Expand Down Expand Up @@ -94,6 +95,7 @@ func createASTIntegrationTestCommand(t *testing.T) *cobra.Command {
projectsWrapper := wrappers.NewHTTPProjectsWrapper(projects)
resultsWrapper := wrappers.NewHTTPResultsWrapper(results, scaPackage, scanSummmaryPath)
risksOverviewWrapper := wrappers.NewHTTPRisksOverviewWrapper(risksOverview)
scsScanOverviewWrapper := wrappers.NewHTTPScanOverviewWrapper(scsScanOverviewPath)
authWrapper := wrappers.NewAuthHTTPWrapper()
logsWrapper := wrappers.NewLogsWrapper(logs)
codeBashingWrapper := wrappers.NewCodeBashingHTTPWrapper(codebashing)
Expand Down Expand Up @@ -124,6 +126,7 @@ func createASTIntegrationTestCommand(t *testing.T) *cobra.Command {
projectsWrapper,
resultsWrapper,
risksOverviewWrapper,
scsScanOverviewWrapper,
authWrapper,
logsWrapper,
groupsWrapper,
Expand Down

0 comments on commit 18f8f08

Please sign in to comment.