diff --git a/go.mod b/go.mod index 40c52a3..21a454f 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,6 @@ require ( github.com/google/go-cmp v0.6.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/objx v0.5.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a1955bf..cd885de 100644 --- a/go.sum +++ b/go.sum @@ -78,7 +78,6 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= diff --git a/pkg/engine/score.go b/pkg/engine/score.go index d7e9c95..8ebe74d 100644 --- a/pkg/engine/score.go +++ b/pkg/engine/score.go @@ -100,36 +100,26 @@ func ProcessScore(ctx context.Context, ep *Params) ([]sbom.Document, []string, [ func HandlePaths(ctx context.Context, paths []string) []string { log := logger.FromContext(ctx) - log.Debugf("Vandling path :%s\n", paths) + log.Debugf("Handling paths: %v\n", paths) + var allFilesPath []string for _, path := range paths { - log.Debugf("Handling each path :%s\n", path) - - pathInfo, _ := os.Stat(path) - - if pathInfo.IsDir() { - log.Debugf("Provided path is directory :%s\n", path) - - files, err := os.ReadDir(path) + log.Debugf("Handling path: %s\n", path) + err := filepath.Walk(path, func(filePath string, fileInfo os.FileInfo, err error) error { if err != nil { - log.Debugf("os.ReadDir failed for path:%s\n", path) - log.Debugf("%s\n", err) - continue + log.Debugf("filepath.Walk error for path %s: %v\n", filePath, err) + return nil } - - for _, file := range files { - log.Debugf("Processing file :%s\n", file.Name()) - if file.IsDir() { - continue - } - path := filepath.Join(path, file.Name()) - allFilesPath = append(allFilesPath, path) + if !fileInfo.IsDir() { + allFilesPath = append(allFilesPath, filePath) } + return nil + }) + if err != nil { + log.Debugf("filepath.Walk encountered an error for path %s: %v\n", path, err) continue } - allFilesPath = append(allFilesPath, paths[0]) - } return allFilesPath } diff --git a/pkg/engine/score_test.go b/pkg/engine/score_test.go index 451ac39..6d81166 100644 --- a/pkg/engine/score_test.go +++ b/pkg/engine/score_test.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -63,3 +64,52 @@ func TestValidateFile(t *testing.T) { // Restore file permissions to delete the temp file os.Chmod(filePath, 0o644) } + +// TestHandlePaths tests the HandlePaths function +func TestHandlePaths(t *testing.T) { + ctx := context.Background() + + baseDir, err := os.MkdirTemp("", "testdir") + if err != nil { + t.Fatalf("Failed to create temporary directory: %v", err) + } + defer os.RemoveAll(baseDir) + + file1 := filepath.Join(baseDir, "file1.txt") + err = os.WriteFile(file1, []byte("content1"), 0o644) + if err != nil { + t.Fatalf("Failed to create temporary file: %v", err) + } + + subDir := filepath.Join(baseDir, "subdir") + err = os.Mkdir(subDir, 0o755) + if err != nil { + t.Fatalf("Failed to create temporary subdirectory: %v", err) + } + + file2 := filepath.Join(subDir, "file2.txt") + err = os.WriteFile(file2, []byte("content2"), 0o644) + if err != nil { + t.Fatalf("Failed to create temporary file: %v", err) + } + + // Test case: directory containing sub-dir and files + paths := []string{baseDir} + expectedPaths := []string{file1, file2} + allFilesPath := HandlePaths(ctx, paths) + assert.NotNil(t, allFilesPath) + assert.ElementsMatch(t, expectedPaths, allFilesPath) + + // Test case: non-existent path + nonExistentPath := "/nonexistent" + paths = []string{nonExistentPath} + allFilesPath = HandlePaths(ctx, paths) + assert.Empty(t, allFilesPath) + + // Test case: single file path + singleFilePath := file1 + paths = []string{singleFilePath} + expectedPaths = []string{singleFilePath} + allFilesPath = HandlePaths(ctx, paths) + assert.ElementsMatch(t, expectedPaths, allFilesPath) +}