mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
Merge pull request #83 from anchore/issue-82
Do not join paths when returning multiple file contents by ref
This commit is contained in:
commit
aa0693bdc0
@ -74,14 +74,8 @@ 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 {
|
||||||
resolvedPath := path.Join(s.Path, string(fileRef.Path))
|
contents, err := fileContents(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)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -113,7 +113,13 @@ func TestDirectoryResolver_MultipleFileContentsByRef(t *testing.T) {
|
|||||||
resolver := DirectoryResolver{"test-fixtures"}
|
resolver := DirectoryResolver{"test-fixtures"}
|
||||||
|
|
||||||
for _, p := range c.input {
|
for _, p := range c.input {
|
||||||
refs = append(refs, file.NewFileReference(p))
|
newRefs, err := resolver.FilesByPath(p)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not generate refs: %+v", err)
|
||||||
|
}
|
||||||
|
for _, ref := range newRefs {
|
||||||
|
refs = append(refs, ref)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contents, err := resolver.MultipleFileContentsByRef(refs...)
|
contents, err := resolver.MultipleFileContentsByRef(refs...)
|
||||||
|
|||||||
@ -92,7 +92,7 @@ func TestDirectoryScope(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMultipleFileContentsByRef(t *testing.T) {
|
func TestMultipleFileContentsByRefContents(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
input string
|
input string
|
||||||
@ -105,12 +105,6 @@ func TestMultipleFileContentsByRef(t *testing.T) {
|
|||||||
path: "empty",
|
path: "empty",
|
||||||
expected: "",
|
expected: "",
|
||||||
},
|
},
|
||||||
{
|
|
||||||
input: "test-fixtures/path-detected",
|
|
||||||
desc: "path does not exist",
|
|
||||||
path: "foo",
|
|
||||||
expected: "",
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
input: "test-fixtures/path-detected",
|
input: "test-fixtures/path-detected",
|
||||||
desc: "file has contents",
|
desc: "file has contents",
|
||||||
@ -124,7 +118,16 @@ func TestMultipleFileContentsByRef(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("could not create NewDirScope: %w", err)
|
t.Errorf("could not create NewDirScope: %w", err)
|
||||||
}
|
}
|
||||||
ref := file.NewFileReference(file.Path(test.path))
|
refs, err := p.FilesByPath(file.Path(test.path))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not get file references from path: %s, %v", test.path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(refs) != 1 {
|
||||||
|
t.Errorf("expected a single ref to be generated but got: %d", len(refs))
|
||||||
|
}
|
||||||
|
ref := refs[0]
|
||||||
|
|
||||||
contents, err := p.MultipleFileContentsByRef(ref)
|
contents, err := p.MultipleFileContentsByRef(ref)
|
||||||
content := contents[ref]
|
content := contents[ref]
|
||||||
|
|
||||||
@ -136,6 +139,38 @@ func TestMultipleFileContentsByRef(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMultipleFileContentsByRefNoContents(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
input string
|
||||||
|
path string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: "test-fixtures/path-detected",
|
||||||
|
desc: "path does not exist",
|
||||||
|
path: "foo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range testCases {
|
||||||
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
|
p, err := NewScopeFromDir(test.input, AllLayersScope)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not create NewDirScope: %w", err)
|
||||||
|
}
|
||||||
|
refs, err := p.FilesByPath(file.Path(test.path))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not get file references from path: %s, %v", test.path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(refs) != 0 {
|
||||||
|
t.Errorf("didnt' expect a ref, but got: %d", len(refs))
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFilesByGlob(t *testing.T) {
|
func TestFilesByGlob(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user