diff --git a/xray/commands/audit/scarunner.go b/xray/commands/audit/scarunner.go index 76293ded5..0490de891 100644 --- a/xray/commands/audit/scarunner.go +++ b/xray/commands/audit/scarunner.go @@ -72,10 +72,10 @@ func runScaScan(params *AuditParams, results *xrayutils.Results) (err error) { // Calculate the scans to preform func getScaScansToPreform(currentWorkingDir string, params *AuditParams) (scansToPreform []*xrayutils.ScaScanResult) { - recursive := len(currentWorkingDir) > 0 - for _, requestedDirectory := range getRequestedDirectoriesToScan(currentWorkingDir, params) { + requestedDirectories, isRecursive := getRequestedDirectoriesToScan(currentWorkingDir, params) + for _, requestedDirectory := range requestedDirectories { // Detect descriptors and technologies in the requested directory. - techToWorkingDirs, err := coreutils.DetectTechnologiesDescriptors(requestedDirectory, recursive, params.Technologies(), getRequestedDescriptors(params), getExcludePattern(params, recursive)) + techToWorkingDirs, err := coreutils.DetectTechnologiesDescriptors(requestedDirectory, isRecursive, params.Technologies(), getRequestedDescriptors(params), getExcludePattern(params, isRecursive)) if err != nil { log.Warn("Couldn't detect technologies in", requestedDirectory, "directory.", err.Error()) continue @@ -116,15 +116,18 @@ func getExcludePattern(params *AuditParams, recursive bool) string { return fspatterns.PrepareExcludePathPattern(exclusions, clientutils.WildCardPattern, recursive) } -func getRequestedDirectoriesToScan(currentWorkingDir string, params *AuditParams) []string { +// Get the directories to scan base on the given parameters. +// If no working directories were specified, the current working directory will be returned with recursive mode. +// If working directories were specified, the recursive mode will be false. +func getRequestedDirectoriesToScan(currentWorkingDir string, params *AuditParams) ([]string, bool) { workingDirs := datastructures.MakeSet[string]() for _, wd := range params.workingDirs { workingDirs.Add(wd) } - if workingDirs.Size() == 0 { - workingDirs.Add(currentWorkingDir) + if len(params.workingDirs) == 0 { + return []string{currentWorkingDir}, true } - return workingDirs.ToSlice() + return workingDirs.ToSlice(), false } // Preform the SCA scan for the given scan information. diff --git a/xray/commands/audit/scarunner_test.go b/xray/commands/audit/scarunner_test.go index 2dc44b144..876df5005 100644 --- a/xray/commands/audit/scarunner_test.go +++ b/xray/commands/audit/scarunner_test.go @@ -166,6 +166,43 @@ func TestGetExcludePattern(t *testing.T) { } } +func TestGetRequestedDirectoriesToScan(t *testing.T) { + tests := []struct { + name string + cwd string + params func() *AuditParams + expectedRecursive bool + expectedDirs []string + }{ + { + name: "Test specific directories", + cwd: "tmp", + params: func() *AuditParams { + param := NewAuditParams() + param.SetWorkingDirs([]string{filepath.Join("tmp", "dir1"), filepath.Join("tmp", "dir2")}) + return param + }, + expectedRecursive: false, + expectedDirs: []string{filepath.Join("tmp", "dir1"), filepath.Join("tmp", "dir2")}, + }, + { + name: "Test recursive", + cwd: "tmp", + params: NewAuditParams, + expectedRecursive: true, + expectedDirs: []string{"tmp"}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + dirs, recursive := getRequestedDirectoriesToScan(test.cwd, test.params()) + assert.ElementsMatch(t, test.expectedDirs, dirs) + assert.Equal(t, test.expectedRecursive, recursive) + }) + } +} + func TestGetScaScansToPreform(t *testing.T) { dir, cleanUp := createTestDir(t)