Skip to content

Commit

Permalink
add test for HandlePaths fn
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Kumar Sahu <[email protected]>
  • Loading branch information
viveksahu26 committed Jul 20, 2024
1 parent 2bdd7c7 commit ce85e3f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
34 changes: 12 additions & 22 deletions pkg/engine/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
50 changes: 50 additions & 0 deletions pkg/engine/score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
}

0 comments on commit ce85e3f

Please sign in to comment.