Skip to content

Commit d4ea008

Browse files
Merge pull request #982 from Checkmarx/other/MiryanFoifer/cleanTmpFile
Clean temp folder upon any CLI failure(AST-77958)
2 parents 0014279 + 4eb4d3f commit d4ea008

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
PR_GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
6666
PR_GITHUB_NAMESPACE: "checkmarx"
6767
PR_GITHUB_REPO_NAME: "ast-cli"
68-
PR_GITHUB_NUMBER: 43
68+
PR_GITHUB_NUMBER: 983
6969
PR_GITLAB_TOKEN : ${{ secrets.PR_GITLAB_TOKEN }}
7070
PR_GITLAB_NAMESPACE: ${{ secrets.PR_GITLAB_NAMESPACE }}
7171
PR_GITLAB_REPO_NAME: ${{ secrets.PR_GITLAB_REPO_NAME }}

internal/commands/scan.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,7 @@ func compressFolder(sourceDir, filter, userIncludeFilter, scaResolver string) (s
10861086
if err != nil {
10871087
return "", errors.Wrapf(err, "Cannot source code temp file.")
10881088
}
1089+
defer outputFile.Close()
10891090
zipWriter := zip.NewWriter(outputFile)
10901091
err = addDirFiles(zipWriter, "", sourceDir, getExcludeFilters(filter), getIncludeFilters(userIncludeFilter))
10911092
if err != nil {
@@ -1462,6 +1463,9 @@ func uploadZip(uploadsWrapper wrappers.UploadsWrapper, zipFilePath string, unzip
14621463
var preSignedURL *string
14631464
preSignedURL, zipFilePathErr = uploadsWrapper.UploadFile(zipFilePath, featureFlagsWrapper)
14641465
if zipFilePathErr != nil {
1466+
if unzip || !userProvidedZip {
1467+
return "", zipFilePath, errors.Wrapf(zipFilePathErr, "%s: Failed to upload sources file\n", failedCreating)
1468+
}
14651469
return "", "", errors.Wrapf(zipFilePathErr, "%s: Failed to upload sources file\n", failedCreating)
14661470
}
14671471
if unzip || !userProvidedZip {
@@ -1616,6 +1620,7 @@ func runCreateScanCommand(
16161620
featureFlagsWrapper,
16171621
jwtWrapper,
16181622
)
1623+
defer cleanUpTempZip(zipFilePath)
16191624
if err != nil {
16201625
return errors.Errorf("%s", err)
16211626
}
@@ -1681,7 +1686,6 @@ func runCreateScanCommand(
16811686
}
16821687
}
16831688

1684-
defer cleanUpTempZip(zipFilePath)
16851689
// verify break build from policy
16861690
if policyResponseModel != nil && len(policyResponseModel.Policies) > 0 && policyResponseModel.BreakBuild {
16871691
logger.PrintIfVerbose("Breaking the build due to policy violation")

internal/commands/scan_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,3 +1353,37 @@ func TestResubmitConfig_ProjectDoesNotExist_ReturnedEmptyConfig(t *testing.T) {
13531353
assert.NilError(t, err)
13541354
assert.Equal(t, len(config), 0)
13551355
}
1356+
1357+
func TestUploadZip_whenUserProvideZip_shouldReturnEmptyZipFilePathInSuccessCase(t *testing.T) {
1358+
uploadWrapper := mock.UploadsMockWrapper{}
1359+
featureFlagsWrapper := &mock.FeatureFlagsMockWrapper{}
1360+
_, zipPath, err := uploadZip(&uploadWrapper, "test.zip", false, true, featureFlagsWrapper)
1361+
assert.NilError(t, err)
1362+
assert.Equal(t, zipPath, "")
1363+
}
1364+
1365+
func TestUploadZip_whenUserProvideZip_shouldReturnEmptyZipFilePathInFailureCase(t *testing.T) {
1366+
uploadWrapper := mock.UploadsMockWrapper{}
1367+
featureFlagsWrapper := &mock.FeatureFlagsMockWrapper{}
1368+
_, zipPath, err := uploadZip(&uploadWrapper, "failureCase.zip", false, true, featureFlagsWrapper)
1369+
assert.Assert(t, err != nil)
1370+
assert.Assert(t, strings.Contains(err.Error(), "error from UploadFile"), err.Error())
1371+
assert.Equal(t, zipPath, "")
1372+
}
1373+
1374+
func TestUploadZip_whenUserNotProvideZip_shouldReturnZipFilePathInSuccessCase(t *testing.T) {
1375+
uploadWrapper := mock.UploadsMockWrapper{}
1376+
featureFlagsWrapper := &mock.FeatureFlagsMockWrapper{}
1377+
_, zipPath, err := uploadZip(&uploadWrapper, "test.zip", false, false, featureFlagsWrapper)
1378+
assert.NilError(t, err)
1379+
assert.Equal(t, zipPath, "test.zip")
1380+
}
1381+
1382+
func TestUploadZip_whenUserNotProvideZip_shouldReturnZipFilePathInFailureCase(t *testing.T) {
1383+
uploadWrapper := mock.UploadsMockWrapper{}
1384+
featureFlagsWrapper := &mock.FeatureFlagsMockWrapper{}
1385+
_, zipPath, err := uploadZip(&uploadWrapper, "failureCase.zip", false, false, featureFlagsWrapper)
1386+
assert.Assert(t, err != nil)
1387+
assert.Assert(t, strings.Contains(err.Error(), "error from UploadFile"), err.Error())
1388+
assert.Equal(t, zipPath, "failureCase.zip")
1389+
}

internal/wrappers/mock/uploads-mock.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ package mock
22

33
import (
44
"fmt"
5+
"github.com/pkg/errors"
56

67
"github.com/checkmarx/ast-cli/internal/wrappers"
78
)
89

910
type UploadsMockWrapper struct {
1011
}
1112

12-
func (u *UploadsMockWrapper) UploadFile(_ string, featureFlagsWrapper wrappers.FeatureFlagsWrapper) (*string, error) {
13+
func (u *UploadsMockWrapper) UploadFile(filePath string, featureFlagsWrapper wrappers.FeatureFlagsWrapper) (*string, error) {
1314
fmt.Println("Called Create in UploadsMockWrapper")
15+
if filePath == "failureCase.zip" {
16+
return nil, errors.New("error from UploadFile")
17+
}
1418
url := "/path/to/nowhere"
1519
return &url, nil
1620
}

0 commit comments

Comments
 (0)