mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
directory resolver should join path and target directory for retrieving contents
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
parent
429c28720c
commit
2f626bf9fd
@ -74,7 +74,14 @@ func (s DirectoryResolver) FilesByGlob(patterns ...string) ([]file.Reference, er
|
|||||||
func (s DirectoryResolver) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
func (s DirectoryResolver) MultipleFileContentsByRef(f ...file.Reference) (map[file.Reference]string, error) {
|
||||||
refContents := make(map[file.Reference]string)
|
refContents := make(map[file.Reference]string)
|
||||||
for _, fileRef := range f {
|
for _, fileRef := range f {
|
||||||
contents, err := fileContents(fileRef.Path)
|
resolvedPath := path.Join(s.Path, string(fileRef.Path))
|
||||||
|
_, err := os.Stat(resolvedPath)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
continue
|
||||||
|
} else if err != nil {
|
||||||
|
log.Errorf("path (%s) is not valid: %v", resolvedPath, err)
|
||||||
|
}
|
||||||
|
contents, err := fileContents(file.Path(resolvedPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return refContents, fmt.Errorf("could not read contents of file: %s", fileRef.Path)
|
return refContents, fmt.Errorf("could not read contents of file: %s", fileRef.Path)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,43 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/anchore/stereoscope/pkg/file"
|
"github.com/anchore/stereoscope/pkg/file"
|
||||||
|
"github.com/anchore/stereoscope/pkg/image"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestNewScopeFromImageFails(t *testing.T) {
|
||||||
|
t.Run("no image given", func(t *testing.T) {
|
||||||
|
_, err := NewScopeFromImage(nil, AllLayersScope)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error condition but none was given")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewScopeFromImageUnknownOption(t *testing.T) {
|
||||||
|
img := image.Image{}
|
||||||
|
|
||||||
|
t.Run("unknown option is an error", func(t *testing.T) {
|
||||||
|
_, err := NewScopeFromImage(&img, UnknownScope)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error condition but none was given")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewScopeFromImage(t *testing.T) {
|
||||||
|
layer := image.NewLayer(nil)
|
||||||
|
img := image.Image{
|
||||||
|
Layers: []*image.Layer{layer},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("create a new Scope object from image", func(t *testing.T) {
|
||||||
|
_, err := NewScopeFromImage(&img, AllLayersScope)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error when creating a new Scope from img: %w", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestDirectoryScope(t *testing.T) {
|
func TestDirectoryScope(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
@ -35,13 +70,13 @@ func TestDirectoryScope(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
p, err := NewDirScope(test.input, AllLayersScope)
|
p, err := NewScopeFromDir(test.input, AllLayersScope)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not create NewDirScope: %w", err)
|
t.Errorf("could not create NewDirScope: %w", err)
|
||||||
}
|
}
|
||||||
if p.dirSrc.Path != test.input {
|
if p.DirSrc.Path != test.input {
|
||||||
t.Errorf("mismatched stringer: '%s' != '%s'", p.dirSrc.Path, test.input)
|
t.Errorf("mismatched stringer: '%s' != '%s'", p.DirSrc.Path, test.input)
|
||||||
}
|
}
|
||||||
|
|
||||||
refs, err := p.FilesByPath(test.inputPaths...)
|
refs, err := p.FilesByPath(test.inputPaths...)
|
||||||
@ -79,13 +114,13 @@ func TestMultipleFileContentsByRef(t *testing.T) {
|
|||||||
{
|
{
|
||||||
input: "test-fixtures/path-detected",
|
input: "test-fixtures/path-detected",
|
||||||
desc: "file has contents",
|
desc: "file has contents",
|
||||||
path: "test-fixtures/path-detected/.vimrc",
|
path: ".vimrc",
|
||||||
expected: "\" A .vimrc file\n",
|
expected: "\" A .vimrc file\n",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
p, err := NewDirScope(test.input, AllLayersScope)
|
p, err := NewScopeFromDir(test.input, AllLayersScope)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not create NewDirScope: %w", err)
|
t.Errorf("could not create NewDirScope: %w", err)
|
||||||
}
|
}
|
||||||
@ -129,7 +164,7 @@ func TestFilesByGlob(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
p, err := NewDirScope(test.input, AllLayersScope)
|
p, err := NewScopeFromDir(test.input, AllLayersScope)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not create NewDirScope: %w", err)
|
t.Errorf("could not create NewDirScope: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user