diff --git a/cmd/detectExecuteScan.go b/cmd/detectExecuteScan.go index 027dedafa..b244e5981 100644 --- a/cmd/detectExecuteScan.go +++ b/cmd/detectExecuteScan.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "os" "strings" piperhttp "github.com/SAP/jenkins-library/pkg/http" @@ -16,7 +17,19 @@ import ( "github.com/SAP/jenkins-library/pkg/versioning" ) -func detectExecuteScan(config detectExecuteScanOptions, telemetryData *telemetry.CustomData) { +type detectFileUtils interface { + Abs(path string) (string, error) + FileExists(filename string) (bool, error) + FileRemove(filename string) error + Copy(src, dest string) (int64, error) + FileRead(path string) ([]byte, error) + FileWrite(path string, content []byte, perm os.FileMode) error + MkdirAll(path string, perm os.FileMode) error + Chmod(path string, mode os.FileMode) error + Glob(pattern string) (matches []string, err error) +} + +func detectExecuteScan(config detectExecuteScanOptions, _ *telemetry.CustomData) { c := command.Command{ ErrorCategoryMapping: map[string][]string{ log.ErrorCompliance.String(): { @@ -43,10 +56,19 @@ func detectExecuteScan(config detectExecuteScanOptions, telemetryData *telemetry } } -func runDetect(config detectExecuteScanOptions, command command.ShellRunner, fileUtils piperutils.FileUtils, httpClient piperhttp.Downloader) error { +func runDetect(config detectExecuteScanOptions, command command.ShellRunner, fileUtils detectFileUtils, httpClient piperhttp.Downloader) error { // detect execution details, see https://synopsys.atlassian.net/wiki/spaces/INTDOCS/pages/88440888/Sample+Synopsys+Detect+Scan+Configuration+Scenarios+for+Black+Duck - httpClient.DownloadFile("https://detect.synopsys.com/detect.sh", "detect.sh", nil, nil) - err := fileUtils.Chmod("detect.sh", 0700) + err := httpClient.DownloadFile("https://detect.synopsys.com/detect.sh", "detect.sh", nil, nil) + if err != nil { + return fmt.Errorf("failed to download 'detect.sh' script: %w", err) + } + defer func() { + err := fileUtils.FileRemove("detect.sh") + if err != nil { + log.Entry().Warnf("failed to delete 'detect.sh' script: %v", err) + } + }() + err = fileUtils.Chmod("detect.sh", 0700) if err != nil { return err } diff --git a/cmd/detectExecuteScan_test.go b/cmd/detectExecuteScan_test.go index 87ffc121f..44f2fbc99 100644 --- a/cmd/detectExecuteScan_test.go +++ b/cmd/detectExecuteScan_test.go @@ -45,9 +45,7 @@ func TestRunDetect(t *testing.T) { err := runDetect(detectExecuteScanOptions{}, &s, &fileUtilsMock, &httpClient) assert.Equal(t, httpClient.downloadedFiles["https://detect.synopsys.com/detect.sh"], "detect.sh") - fileStatus, err := fileUtilsMock.Stat("detect.sh") - assert.NoError(t, err) - assert.Equal(t, fileStatus.Mode(), os.FileMode(0700)) + assert.True(t, fileUtilsMock.HasRemovedFile("detect.sh")) assert.NoError(t, err) assert.Equal(t, ".", s.Dir, "Wrong execution directory used") assert.Equal(t, "/bin/bash", s.Shell[0], "Bash shell expected") @@ -58,9 +56,11 @@ func TestRunDetect(t *testing.T) { t.Run("failure case", func(t *testing.T) { s := mock.ShellMockRunner{ShouldFailOnCommand: map[string]error{"./detect.sh --blackduck.url= --blackduck.api.token= --detect.project.name=\\\"\\\" --detect.project.version.name=\\\"\\\" --detect.code.location.name=\\\"\\\"": fmt.Errorf("Test Error")}} fileUtilsMock := mock.FilesMock{} + fileUtilsMock.AddFile("detect.sh", []byte("")) httpClient := httpClientMock{} err := runDetect(detectExecuteScanOptions{}, &s, &fileUtilsMock, &httpClient) - assert.NotNil(t, err) + assert.EqualError(t, err, "Test Error") + assert.True(t, fileUtilsMock.HasRemovedFile("detect.sh")) }) t.Run("maven parameters", func(t *testing.T) { diff --git a/cmd/mtaBuild_test.go b/cmd/mtaBuild_test.go index da989dd62..5c0c0e2a7 100644 --- a/cmd/mtaBuild_test.go +++ b/cmd/mtaBuild_test.go @@ -9,7 +9,6 @@ import ( piperhttp "github.com/SAP/jenkins-library/pkg/http" "github.com/SAP/jenkins-library/pkg/maven" "github.com/SAP/jenkins-library/pkg/mock" - "github.com/SAP/jenkins-library/pkg/piperutils" "github.com/ghodss/yaml" "github.com/stretchr/testify/assert" ) @@ -267,7 +266,7 @@ func TestMarBuild(t *testing.T) { downloadAndCopySettingsFiles = func( globalSettings string, projectSettings string, - fileUtils piperutils.FileUtils, + fileUtils maven.FileUtils, httpClient maven.SettingsDownloadUtils) error { projectSettingsFile = projectSettings globalSettingsFile = globalSettings diff --git a/pkg/maven/maven.go b/pkg/maven/maven.go index dca7fdb92..53bc120d3 100644 --- a/pkg/maven/maven.go +++ b/pkg/maven/maven.go @@ -43,7 +43,7 @@ type mavenExecRunner interface { } type mavenUtils interface { - piperutils.FileUtils + FileUtils DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error } diff --git a/pkg/maven/settings.go b/pkg/maven/settings.go index 74c1a5ea9..53d62ce2b 100644 --- a/pkg/maven/settings.go +++ b/pkg/maven/settings.go @@ -3,7 +3,6 @@ package maven import ( "fmt" "github.com/SAP/jenkins-library/pkg/log" - "github.com/SAP/jenkins-library/pkg/piperutils" "net/http" "os" "path/filepath" @@ -12,11 +11,22 @@ import ( var getenv = os.Getenv +// SettingsDownloadUtils defines an interface for downloading files. type SettingsDownloadUtils interface { DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error } -func DownloadAndGetMavenParameters(globalSettingsFile string, projectSettingsFile string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils) ([]string, error) { +// FileUtils defines the external file-related functionality needed by this package. +type FileUtils interface { + FileExists(filename string) (bool, error) + Copy(src, dest string) (int64, error) + MkdirAll(path string, perm os.FileMode) error + Glob(pattern string) (matches []string, err error) +} + +// DownloadAndGetMavenParameters downloads the global or project settings file if the strings contain URLs. +// It then constructs the arguments that need to be passed to maven in order to point to use these settings files. +func DownloadAndGetMavenParameters(globalSettingsFile string, projectSettingsFile string, fileUtils FileUtils, httpClient SettingsDownloadUtils) ([]string, error) { mavenArgs := []string{} if len(globalSettingsFile) > 0 { globalSettingsFileName, err := downloadSettingsIfURL(globalSettingsFile, ".pipeline/mavenGlobalSettings.xml", fileUtils, httpClient, false) @@ -42,7 +52,10 @@ func DownloadAndGetMavenParameters(globalSettingsFile string, projectSettingsFil return mavenArgs, nil } -func DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils) error { +// DownloadAndCopySettingsFiles downloads the global or project settings file if the strings contain URLs. +// It copies the given files to either the locations specified in the environment variables M2_HOME and HOME +// or the default locations where maven expects them. +func DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile string, fileUtils FileUtils, httpClient SettingsDownloadUtils) error { if len(projectSettingsFile) > 0 { destination, err := getProjectSettingsFileDest() if err != nil { @@ -73,7 +86,7 @@ func DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile return nil } -func downloadAndCopySettingsFile(src string, dest string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils) error { +func downloadAndCopySettingsFile(src string, dest string, fileUtils FileUtils, httpClient SettingsDownloadUtils) error { if len(src) == 0 { return fmt.Errorf("Settings file source location not provided") } @@ -115,7 +128,7 @@ func downloadAndCopySettingsFile(src string, dest string, fileUtils piperutils.F return nil } -func downloadSettingsIfURL(settingsFileOption, settingsFile string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils, overwrite bool) (string, error) { +func downloadSettingsIfURL(settingsFileOption, settingsFile string, fileUtils FileUtils, httpClient SettingsDownloadUtils, overwrite bool) (string, error) { result := settingsFileOption if strings.HasPrefix(settingsFileOption, "http:") || strings.HasPrefix(settingsFileOption, "https:") { err := downloadSettingsFromURL(settingsFileOption, settingsFile, fileUtils, httpClient, overwrite) @@ -127,7 +140,7 @@ func downloadSettingsIfURL(settingsFileOption, settingsFile string, fileUtils pi return result, nil } -func downloadSettingsFromURL(url, filename string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils, overwrite bool) error { +func downloadSettingsFromURL(url, filename string, fileUtils FileUtils, httpClient SettingsDownloadUtils, overwrite bool) error { exists, _ := fileUtils.FileExists(filename) if exists && !overwrite { log.Entry().Infof("Not downloading maven settings file, because it already exists at '%s'", filename)