Skip to content

Commit

Permalink
formatters:chore - remove unused methods from IService (#912)
Browse files Browse the repository at this point in the history
Since the `GetCommitAuthor` was just being used internally by
IService implementation there's no reason to keep this method, and as
pointed by @iancardosozup, on `GetProjectPathWithWorkDir` docs, their
usage can be simple changed by a `filepath.Join` call, so this method was
removed too.

Signed-off-by: Matheus Alcantara <matheus.alcantara@zup.com.br>
  • Loading branch information
matheusalcantarazup committed Jan 6, 2022
1 parent d3018a8 commit 802f0c4
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 69 deletions.
4 changes: 3 additions & 1 deletion internal/services/formatters/default_engine_formatter.go
Expand Up @@ -15,6 +15,8 @@
package formatters

import (
"path/filepath"

"github.com/ZupIT/horusec-devkit/pkg/enums/languages"
"github.com/ZupIT/horusec-devkit/pkg/enums/tools"
"github.com/ZupIT/horusec-devkit/pkg/utils/logger"
Expand Down Expand Up @@ -65,7 +67,7 @@ func (f *DefaultFormatter) execEngineAndParseResults(src string) error {
}

func (f *DefaultFormatter) execEngineAnalysis(src string) ([]engine.Finding, error) {
textUnit, err := f.manager.GetTextUnitByRulesExt(f.svc.GetProjectPathWithWorkdir(src))
textUnit, err := f.manager.GetTextUnitByRulesExt(filepath.Join(f.svc.GetConfigProjectPath(), src))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/services/formatters/default_engine_formatter_test.go
Expand Up @@ -87,7 +87,7 @@ func TestStartAnalysis(t *testing.T) {
service.On("SetToolFinishedAnalysis")
service.On("SetAnalysisError")
service.On("ToolIsToIgnore").Return(false)
service.On("GetProjectPathWithWorkdir").Return(".")
service.On("GetConfigProjectPath").Return(".")
service.On("ParseFindingsToVulnerabilities").Return(nil)
service.On("GetCustomRulesByLanguage").Return([]engine.Rule{})

Expand All @@ -105,7 +105,7 @@ func TestStartAnalysis(t *testing.T) {
service.On("SetToolFinishedAnalysis")
service.On("SetAnalysisError")
service.On("ToolIsToIgnore").Return(false)
service.On("GetProjectPathWithWorkdir").Return("!!!")
service.On("GetConfigProjectPath").Return(".")
service.On("ParseFindingsToVulnerabilities").Return(nil)
service.On("GetCustomRulesByLanguage").Return([]engine.Rule{})

Expand Down
7 changes: 0 additions & 7 deletions internal/services/formatters/interface.go
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/ZupIT/horusec-devkit/pkg/enums/tools"
engine "github.com/ZupIT/horusec-engine"

commitauthor "github.com/ZupIT/horusec/internal/entities/commit_author"
"github.com/ZupIT/horusec/internal/entities/docker"
)

Expand Down Expand Up @@ -48,9 +47,6 @@ type IService interface {
// generate the error and the error itself.
GetAnalysisIDErrorMessage(tool tools.Tool, output string) string

// GetCommitAuthor return commit author info to a given line and filepath.
GetCommitAuthor(line, filePath string) commitauthor.CommitAuthor

// AddWorkDirInCmd replace {{WORK_DIR}} from cmd with a `cd` into projectSubPath.
AddWorkDirInCmd(cmd string, projectSubPath string, tool tools.Tool) string

Expand All @@ -75,9 +71,6 @@ type IService interface {
// to a given filename
GetFilepathFromFilename(filename, projectSubPath string) string

// GetProjectPathWithWorkdir return the project path inside working directory.
GetProjectPathWithWorkdir(projectSubPath string) string

// SetCommitAuthor set commit author info on vulnerability.
SetCommitAuthor(vulnerability *vulnerability.Vulnerability) *vulnerability.Vulnerability

Expand Down
12 changes: 1 addition & 11 deletions internal/services/formatters/service.go
Expand Up @@ -86,10 +86,6 @@ func (s *Service) GetAnalysisIDErrorMessage(tool tools.Tool, output string) stri
return msg
}

func (s *Service) GetCommitAuthor(line, filePath string) commitauthor.CommitAuthor {
return s.git.CommitAuthor(line, filePath)
}

func (s *Service) GetConfigProjectPath() string {
return filepath.Join(s.config.ProjectPath, ".horusec", s.analysis.ID.String())
}
Expand Down Expand Up @@ -196,7 +192,7 @@ func (s *Service) GetFilepathFromFilename(filename, projectSubPath string) strin
}

func (s *Service) SetCommitAuthor(vuln *vulnerability.Vulnerability) *vulnerability.Vulnerability {
author := s.GetCommitAuthor(vuln.Line, vuln.File)
author := s.git.CommitAuthor(vuln.Line, vuln.File)

vuln.CommitAuthor = author.Author
vuln.CommitEmail = author.Email
Expand Down Expand Up @@ -267,12 +263,6 @@ func (s *Service) GetConfigCMDByFileExtension(projectSubPath, imageCmd, ext stri
return s.AddWorkDirInCmd(imageCmd, projectSubPath, tool)
}

// NOTE(iancardosozup): i think this functions below could be erased from service interface
// since they only get a value that could be accessed directly
func (s *Service) GetProjectPathWithWorkdir(projectSubPath string) string {
return filepath.Join(s.GetConfigProjectPath(), projectSubPath)
}

func (s *Service) IsDockerDisabled() bool {
return s.config.DisableDocker
}
Expand Down
37 changes: 0 additions & 37 deletions internal/services/formatters/service_test.go
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/ZupIT/horusec/config"
commitauthor "github.com/ZupIT/horusec/internal/entities/commit_author"
dockerentities "github.com/ZupIT/horusec/internal/entities/docker"
"github.com/ZupIT/horusec/internal/entities/toolsconfig"
"github.com/ZupIT/horusec/internal/entities/workdir"
Expand Down Expand Up @@ -107,7 +106,6 @@ func TestMock_AddWorkDirInCmd(t *testing.T) {
mock.On("SetAnalysisError").Return()
mock.On("ExecuteContainer").Return("", nil)
mock.On("GetAnalysisIDErrorMessage").Return("")
mock.On("GetCommitAuthor").Return(commitauthor.CommitAuthor{})
mock.On("AddWorkDirInCmd").Return("")
mock.On("GetConfigProjectPath").Return("")
mock.On("GetAnalysis").Return(&analysis.Analysis{})
Expand All @@ -120,7 +118,6 @@ func TestMock_AddWorkDirInCmd(t *testing.T) {
_ = mock.GetAnalysisID()
_, _ = mock.ExecuteContainer(&dockerentities.AnalysisData{})
_ = mock.GetAnalysisIDErrorMessage("", "")
_ = mock.GetCommitAuthor("", "")
_ = mock.AddWorkDirInCmd("", "", "")
_ = mock.GetConfigProjectPath()
mock.SetAnalysisError(errors.New(""), "", "", "")
Expand Down Expand Up @@ -171,40 +168,6 @@ func TestGetAnalysisIDErrorMessage(t *testing.T) {
})
}

func TestGetCommitAuthor(t *testing.T) {
t.Run("should get commit author default values when .git folder is not found", func(t *testing.T) {
monitorController := NewFormatterService(&analysis.Analysis{}, testutil.NewDockerMock(), &config.Config{})

result := monitorController.GetCommitAuthor("", "")
assert.Equal(t, "-", result.Author)
assert.Equal(t, "-", result.CommitHash)
assert.Equal(t, "-", result.Date)
assert.Equal(t, "-", result.Email)
assert.Equal(t, "-", result.Message)
assert.NotEmpty(t, result)
})
t.Run("should get commit author values when .git folder is found", func(t *testing.T) {
cfg := &config.Config{
StartOptions: config.StartOptions{
ProjectPath: testutil.ExamplesPath,
EnableCommitAuthor: true,
},
}
monitorController := NewFormatterService(&analysis.Analysis{}, testutil.NewDockerMock(), cfg)

result := monitorController.GetCommitAuthor("15", filepath.Join(testutil.GoExample1, "api", "server.go"))
notExpected := commitauthor.CommitAuthor{
Author: "-",
Email: "-",
CommitHash: "-",
Message: "-",
Date: "-",
}
assert.NotEmpty(t, result)
assert.NotEqual(t, notExpected, result)
})
}

func TestGetConfigProjectPath(t *testing.T) {
t.Run("should success get project path", func(t *testing.T) {
cliConfig := &config.Config{
Expand Down
11 changes: 0 additions & 11 deletions internal/utils/testutil/formatter_mock.go
Expand Up @@ -22,7 +22,6 @@ import (
engine "github.com/ZupIT/horusec-engine"
"github.com/stretchr/testify/mock"

commitauthor "github.com/ZupIT/horusec/internal/entities/commit_author"
dockerentities "github.com/ZupIT/horusec/internal/entities/docker"
)

Expand Down Expand Up @@ -53,11 +52,6 @@ func (m *FormatterMock) GetAnalysisIDErrorMessage(_ tools.Tool, _ string) string
return args.Get(0).(string)
}

func (m *FormatterMock) GetCommitAuthor(_, _ string) (author commitauthor.CommitAuthor) {
args := m.MethodCalled("GetCommitAuthor")
return args.Get(0).(commitauthor.CommitAuthor)
}

func (m *FormatterMock) AddWorkDirInCmd(_, _ string, _ tools.Tool) string {
args := m.MethodCalled("AddWorkDirInCmd")
return args.Get(0).(string)
Expand Down Expand Up @@ -92,11 +86,6 @@ func (m *FormatterMock) GetFilepathFromFilename(_, _ string) string {
return args.Get(0).(string)
}

func (m *FormatterMock) GetProjectPathWithWorkdir(_ string) string {
args := m.MethodCalled("GetProjectPathWithWorkdir")
return args.Get(0).(string)
}

func (m *FormatterMock) SetCommitAuthor(_ *vulnerability.Vulnerability) *vulnerability.Vulnerability {
args := m.MethodCalled("SetCommitAuthor")
return args.Get(0).(*vulnerability.Vulnerability)
Expand Down

0 comments on commit 802f0c4

Please sign in to comment.