Skip to content

Commit

Permalink
tests:chore - move Mock implementations to testutil pkg (ZupIT#753)
Browse files Browse the repository at this point in the history
Previously each mock implementation of structs that is used by tests was
implemented on your own package, which add some unnecessary testing
dependencies on main packages.

This commit move all mocks implementation to testutil package and change
their references.

Note that the `internal/utils/mock` package was also removed and their
code was moved to testutil package to centralize all testing stuffs
only on testutil package.

Signed-off-by: Matheus Alcantara <[email protected]>
  • Loading branch information
matheusalcantarazup authored Nov 22, 2021
1 parent 4c4d2de commit e70e404
Show file tree
Hide file tree
Showing 46 changed files with 538 additions and 603 deletions.
142 changes: 69 additions & 73 deletions cmd/app/start/start_test.go

Large diffs are not rendered by default.

108 changes: 0 additions & 108 deletions config/mocks/system_calls_mock.go

This file was deleted.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/go-enry/go-enry/v2 v2.7.2
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/iancoleman/strcase v0.2.0
github.com/manifoldco/promptui v0.9.0
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
2 changes: 1 addition & 1 deletion horusec-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"**/examples/**",
"**/*.toml",
"**/*_test.go",
"**/*_mock.go",
"**/testutil/**",
"**/*README.md",
"**/cmd/app/start/analysis/**",
"**/tmp/**",
Expand Down
31 changes: 13 additions & 18 deletions internal/controllers/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/entities/vulnerability"
vulnerabilityenum "github.com/ZupIT/horusec-devkit/pkg/enums/vulnerability"
"github.com/ZupIT/horusec-devkit/pkg/utils/logger"
horusecAPI "github.com/ZupIT/horusec/internal/services/horusec_api"
"github.com/ZupIT/horusec/internal/utils/mock"
vulnhash "github.com/ZupIT/horusec/internal/utils/vuln_hash"

"github.com/ZupIT/horusec/internal/entities/workdir"
Expand All @@ -48,10 +46,7 @@ import (

"github.com/ZupIT/horusec-devkit/pkg/enums/languages"
"github.com/ZupIT/horusec/config"
languageDetect "github.com/ZupIT/horusec/internal/controllers/language_detect"
"github.com/ZupIT/horusec/internal/controllers/printresults"
"github.com/ZupIT/horusec/internal/services/docker"
dockerClient "github.com/ZupIT/horusec/internal/services/docker/client"
"github.com/ZupIT/horusec/internal/services/formatters"
)

Expand Down Expand Up @@ -205,7 +200,7 @@ func TestAnalyzer_AnalysisDirectory(t *testing.T) {
configs.EnableGitHistoryAnalysis = true
configs.FalsePositiveHashes = []string{"test"}

languageDetectMock := &languageDetect.Mock{}
languageDetectMock := testutil.NewLanguageDetectMock()
languageDetectMock.On("LanguageDetect").Return([]languages.Language{
languages.Go,
languages.CSharp,
Expand All @@ -222,15 +217,15 @@ func TestAnalyzer_AnalysisDirectory(t *testing.T) {
languages.Yaml,
}, nil)

printResultMock := &printresults.Mock{}
printResultMock := testutil.NewPrintResultsMock()
printResultMock.On("StartPrintResults").Return(0, nil)
printResultMock.On("SetAnalysis")

horusecAPIMock := &horusecAPI.Mock{}
horusecAPIMock := testutil.NewHorusecAPIMock()
horusecAPIMock.On("SendAnalysis").Return(nil)
horusecAPIMock.On("GetAnalysis").Return(&entitiesAnalysis.Analysis{}, nil)

dockerMocker := &dockerClient.Mock{}
dockerMocker := testutil.NewDockerClientMock()
dockerMocker.On("CreateLanguageAnalysisContainer").Return("", nil)
dockerMocker.On("ImageList").Return([]types.ImageSummary{{}}, nil)
dockerMocker.On("ImagePull").Return(io.NopCloser(bytes.NewReader([]byte(""))), nil)
Expand Down Expand Up @@ -263,7 +258,7 @@ func TestAnalyzer_AnalysisDirectory(t *testing.T) {
configs.WorkDir = &workdir.WorkDir{Go: []string{"test"}}
configs.FalsePositiveHashes = []string{"test"}

languageDetectMock := &languageDetect.Mock{}
languageDetectMock := testutil.NewLanguageDetectMock()
languageDetectMock.On("LanguageDetect").Return([]languages.Language{
languages.Go,
languages.CSharp,
Expand All @@ -280,15 +275,15 @@ func TestAnalyzer_AnalysisDirectory(t *testing.T) {
languages.Yaml,
}, nil)

printResultMock := &printresults.Mock{}
printResultMock := testutil.NewPrintResultsMock()
printResultMock.On("StartPrintResults").Return(0, nil)
printResultMock.On("SetAnalysis")

horusecAPIMock := &horusecAPI.Mock{}
horusecAPIMock := testutil.NewHorusecAPIMock()
horusecAPIMock.On("SendAnalysis").Return(nil)
horusecAPIMock.On("GetAnalysis").Return(mock.CreateAnalysisMock(), nil)
horusecAPIMock.On("GetAnalysis").Return(testutil.CreateAnalysisMock(), nil)

dockerMocker := &dockerClient.Mock{}
dockerMocker := testutil.NewDockerClientMock()
dockerMocker.On("CreateLanguageAnalysisContainer").Return("", nil)
dockerMocker.On("ImageList").Return([]types.ImageSummary{{}}, nil)
dockerMocker.On("ImagePull").Return(io.NopCloser(bytes.NewReader([]byte(""))), nil)
Expand Down Expand Up @@ -320,18 +315,18 @@ func TestAnalyzer_AnalysisDirectory(t *testing.T) {
configs := config.New()
configs.WorkDir = &workdir.WorkDir{}

languageDetectMock := &languageDetect.Mock{}
languageDetectMock := testutil.NewLanguageDetectMock()
languageDetectMock.On("LanguageDetect").Return([]languages.Language{}, errors.New("test"))

printResultMock := &printresults.Mock{}
printResultMock := testutil.NewPrintResultsMock()
printResultMock.On("StartPrintResults").Return(0, nil)
printResultMock.On("SetAnalysis")

horusecAPIMock := &horusecAPI.Mock{}
horusecAPIMock := testutil.NewHorusecAPIMock()
horusecAPIMock.On("SendAnalysis").Return(nil)
horusecAPIMock.On("GetAnalysis").Return(&entitiesAnalysis.Analysis{}, nil)

dockerMocker := &dockerClient.Mock{}
dockerMocker := testutil.NewDockerClientMock()
dockerMocker.On("CreateLanguageAnalysisContainer").Return("", nil)
dockerMocker.On("ImageList").Return([]types.ImageSummary{{}}, nil)
dockerMocker.On("ImagePull").Return(io.NopCloser(bytes.NewReader([]byte(""))), nil)
Expand Down
19 changes: 9 additions & 10 deletions internal/controllers/printresults/print_results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/utils/logger"
"github.com/ZupIT/horusec/internal/enums/outputtype"
"github.com/ZupIT/horusec/internal/helpers/messages"
"github.com/ZupIT/horusec/internal/utils/mock"
"github.com/ZupIT/horusec/internal/utils/testutil"

"github.com/stretchr/testify/assert"
Expand All @@ -58,7 +57,7 @@ type testcase struct {

func TestStartPrintResultsMock(t *testing.T) {
t.Run("Should return correctly mock", func(t *testing.T) {
m := &Mock{}
m := testutil.NewPrintResultsMock()
m.On("StartPrintResults").Return(0, nil)

totalVulns, err := m.Print()
Expand Down Expand Up @@ -122,7 +121,7 @@ func TestPrintResultsStartPrintResults(t *testing.T) {
JSONOutputFilePath: filepath.Join(t.TempDir(), "sonar-output.json"),
},
},
analysis: *mock.CreateAnalysisMock(),
analysis: *testutil.CreateAnalysisMock(),
outputs: []string{messages.MsgInfoStartGenerateSonarQubeFile},
validateFn: func(t *testing.T, tt testcase) {
assert.FileExists(t, tt.cfg.JSONOutputFilePath)
Expand All @@ -147,7 +146,7 @@ func TestPrintResultsStartPrintResults(t *testing.T) {
PrintOutputType: outputtype.JSON,
},
},
analysis: *mock.CreateAnalysisMock(),
analysis: *testutil.CreateAnalysisMock(),
err: true,
outputs: []string{messages.MsgErrorGenerateJSONFile},
},
Expand All @@ -158,14 +157,14 @@ func TestPrintResultsStartPrintResults(t *testing.T) {
IsTimeout: true,
},
},
analysis: *mock.CreateAnalysisMock(),
analysis: *testutil.CreateAnalysisMock(),
vulnerabilities: 11,
outputs: []string{messages.MsgWarnTimeoutOccurs},
},
{
name: "Should print 11 vulnerabilities",
cfg: config.Config{},
analysis: *mock.CreateAnalysisMock(),
analysis: *testutil.CreateAnalysisMock(),
vulnerabilities: 11,
},
{
Expand All @@ -175,7 +174,7 @@ func TestPrintResultsStartPrintResults(t *testing.T) {
EnableCommitAuthor: true,
},
},
analysis: *mock.CreateAnalysisMock(),
analysis: *testutil.CreateAnalysisMock(),
vulnerabilities: 11,
outputs: []string{
"Commit Author", "Commit Date", "Commit Email", "Commit CommitHash", "Commit Message",
Expand All @@ -188,7 +187,7 @@ func TestPrintResultsStartPrintResults(t *testing.T) {
SeveritiesToIgnore: []string{"MEDIUM", "LOW"},
},
},
analysis: *mock.CreateAnalysisMock(),
analysis: *testutil.CreateAnalysisMock(),
vulnerabilities: 3,
},
{
Expand All @@ -199,7 +198,7 @@ func TestPrintResultsStartPrintResults(t *testing.T) {
JSONOutputFilePath: filepath.Join(t.TempDir(), "output"),
},
},
analysis: *mock.CreateAnalysisMock(),
analysis: *testutil.CreateAnalysisMock(),
vulnerabilities: 11,
validateFn: func(t *testing.T, tt testcase) {
assert.FileExists(t, tt.cfg.JSONOutputFilePath, "output")
Expand All @@ -219,7 +218,7 @@ func TestPrintResultsStartPrintResults(t *testing.T) {
ProjectPath: testutil.JavaScriptExample1,
},
},
analysis: *mock.CreateAnalysisMock(),
analysis: *testutil.CreateAnalysisMock(),
vulnerabilities: 11,
outputs: []string{
fmt.Sprintf("File: %s", filepath.Join(os.TempDir(), "cert.pem")),
Expand Down
Loading

0 comments on commit e70e404

Please sign in to comment.