@@ -3,13 +3,6 @@ package scanrepository
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "net/http/httptest"
7
- "os"
8
- "os/exec"
9
- "path/filepath"
10
- "strings"
11
- "testing"
12
-
13
6
"github.com/google/go-github/v45/github"
14
7
biutils "github.com/jfrog/build-info-go/utils"
15
8
"github.com/jfrog/frogbot/v2/utils"
@@ -25,6 +18,12 @@ import (
25
18
"github.com/jfrog/jfrog-client-go/xray/services"
26
19
"github.com/stretchr/testify/assert"
27
20
"github.com/stretchr/testify/require"
21
+ "net/http/httptest"
22
+ "os"
23
+ "os/exec"
24
+ "path/filepath"
25
+ "strings"
26
+ "testing"
28
27
)
29
28
30
29
const rootTestDir = "scanrepository"
@@ -85,6 +84,7 @@ func TestScanRepositoryCmd_Run(t *testing.T) {
85
84
configPath string
86
85
expectedPackagesInBranch map [string ][]string
87
86
expectedVersionUpdatesInBranch map [string ][]string
87
+ expectedMissingFilesInBranch map [string ][]string
88
88
packageDescriptorPaths []string
89
89
aggregateFixes bool
90
90
allowPartialResults bool
@@ -100,6 +100,7 @@ func TestScanRepositoryCmd_Run(t *testing.T) {
100
100
testName : "aggregate-multi-dir" ,
101
101
expectedPackagesInBranch : map [string ][]string {"frogbot-update-npm-dependencies-master" : {"uuid" , "minimatch" , "mpath" , "minimist" }},
102
102
expectedVersionUpdatesInBranch : map [string ][]string {"frogbot-update-npm-dependencies-master" : {"^1.2.6" , "^9.0.0" , "^0.8.4" , "^3.0.5" }},
103
+ expectedMissingFilesInBranch : map [string ][]string {"frogbot-update-npm-dependencies-master" : {"npm1/package-lock.json" , "npm2/package-lock.json" }},
103
104
packageDescriptorPaths : []string {"npm1/package.json" , "npm2/package.json" },
104
105
aggregateFixes : true ,
105
106
configPath : "../testdata/scanrepository/cmd/aggregate-multi-dir/.frogbot/frogbot-config.yml" ,
@@ -108,6 +109,7 @@ func TestScanRepositoryCmd_Run(t *testing.T) {
108
109
testName : "aggregate-multi-project" ,
109
110
expectedPackagesInBranch : map [string ][]string {"frogbot-update-npm-dependencies-master" : {"uuid" , "minimatch" , "mpath" }, "frogbot-update-Pip-dependencies-master" : {"pyjwt" , "pexpect" }},
110
111
expectedVersionUpdatesInBranch : map [string ][]string {"frogbot-update-npm-dependencies-master" : {"^9.0.0" , "^0.8.4" , "^3.0.5" }, "frogbot-update-Pip-dependencies-master" : {"2.4.0" }},
112
+ expectedMissingFilesInBranch : map [string ][]string {"frogbot-update-npm-dependencies-master" : {"npm/package-lock.json" }},
111
113
packageDescriptorPaths : []string {"npm/package.json" , "pip/requirements.txt" },
112
114
aggregateFixes : true ,
113
115
configPath : "../testdata/scanrepository/cmd/aggregate-multi-project/.frogbot/frogbot-config.yml" ,
@@ -221,6 +223,14 @@ func TestScanRepositoryCmd_Run(t *testing.T) {
221
223
assert .Contains (t , string (resultDiff ), updatedVersion )
222
224
}
223
225
}
226
+
227
+ if len (test .expectedMissingFilesInBranch ) > 0 {
228
+ for branch , expectedMissingFiles := range test .expectedMissingFilesInBranch {
229
+ resultDiff , err := verifyLockFileDiff (branch , expectedMissingFiles ... )
230
+ assert .NoError (t , err )
231
+ assert .Empty (t , resultDiff )
232
+ }
233
+ }
224
234
})
225
235
}
226
236
}
@@ -669,6 +679,72 @@ func TestPreparePullRequestDetails(t *testing.T) {
669
679
assert .ElementsMatch (t , expectedExtraComments , extraComments )
670
680
}
671
681
682
+ // This test simulates the cleaning action of cleanNewFilesMissingInRemote.
683
+ // Every file that has been newly CREATED after cloning the repo (here - after creating .git repo) should be removed. Every other file should be kept.
684
+ func TestCleanNewFilesMissingInRemote (t * testing.T ) {
685
+ testCases := []struct {
686
+ name string
687
+ relativeTestDirPath string
688
+ createFileBeforeInit bool
689
+ }{
690
+ {
691
+ name : "new_file_should_remain" ,
692
+ relativeTestDirPath : filepath .Join (rootTestDir , "cmd" , "aggregate" ),
693
+ createFileBeforeInit : true ,
694
+ },
695
+ {
696
+ name : "new_file_should_be_deleted" ,
697
+ relativeTestDirPath : filepath .Join (rootTestDir , "cmd" , "aggregate" ),
698
+ createFileBeforeInit : false ,
699
+ },
700
+ }
701
+
702
+ baseDir , outerErr := os .Getwd ()
703
+ assert .NoError (t , outerErr )
704
+ defer func () {
705
+ assert .NoError (t , os .Chdir (baseDir ))
706
+ }()
707
+
708
+ for _ , test := range testCases {
709
+ t .Run (test .name , func (t * testing.T ) {
710
+ testDir , cleanup := utils .CopyTestdataProjectsToTemp (t , test .relativeTestDirPath )
711
+ defer cleanup ()
712
+
713
+ var file * os.File
714
+ if test .createFileBeforeInit {
715
+ var fileError error
716
+ file , fileError = os .CreateTemp (testDir , test .name )
717
+ assert .NoError (t , fileError )
718
+ }
719
+
720
+ utils .CreateDotGitWithCommit (t , testDir , "1234" , "" )
721
+
722
+ if ! test .createFileBeforeInit {
723
+ var fileError error
724
+ file , fileError = os .CreateTemp (testDir , test .name )
725
+ assert .NoError (t , fileError )
726
+ }
727
+
728
+ // Making a change in the file so it will be modified in the working tree
729
+ _ , err := file .WriteString ("My initial string" )
730
+ assert .NoError (t , err )
731
+ assert .NoError (t , file .Close ())
732
+
733
+ scanRepoCmd := ScanRepositoryCmd {baseWd : testDir }
734
+ assert .NoError (t , scanRepoCmd .cleanNewFilesMissingInRemote ())
735
+
736
+ exists , err := fileutils .IsFileExists (file .Name (), false )
737
+ assert .NoError (t , err )
738
+ if test .createFileBeforeInit {
739
+ assert .True (t , exists )
740
+ } else {
741
+ assert .False (t , exists )
742
+ }
743
+ })
744
+ }
745
+
746
+ }
747
+
672
748
func verifyTechnologyNaming (t * testing.T , scanResponse []services.ScanResponse , expectedType string ) {
673
749
for _ , resp := range scanResponse {
674
750
for _ , vulnerability := range resp .Vulnerabilities {
@@ -698,3 +774,24 @@ func verifyDependencyFileDiff(baseBranch string, fixBranch string, packageDescri
698
774
}
699
775
return
700
776
}
777
+
778
+ func verifyLockFileDiff (branchToInspect string , lockFiles ... string ) (output []byte , err error ) {
779
+ log .Debug (fmt .Sprintf ("Checking lock files differences in %s between branches 'master' and '%s'" , lockFiles , branchToInspect ))
780
+ // Suppress condition always false warning
781
+ //goland:noinspection ALL
782
+ var args []string
783
+ if coreutils .IsWindows () {
784
+ args = []string {"/c" , "git" , "ls-tree" , branchToInspect , "--" }
785
+ args = append (args , lockFiles ... )
786
+ output , err = exec .Command ("cmd" , args ... ).Output ()
787
+ } else {
788
+ args = []string {"ls-tree" , branchToInspect , "--" }
789
+ args = append (args , lockFiles ... )
790
+ output , err = exec .Command ("git" , args ... ).Output ()
791
+ }
792
+ var exitError * exec.ExitError
793
+ if errors .As (err , & exitError ) {
794
+ err = errors .New ("git error: " + string (exitError .Stderr ))
795
+ }
796
+ return
797
+ }
0 commit comments