Merge pull request #83 from anchore/issue-82

Do not join paths when returning multiple file contents by ref
This commit is contained in:
Alfredo Deza 2020-07-16 14:36:30 -04:00 committed by GitHub
commit aa0693bdc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 17 deletions

View File

@ -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) {
refContents := make(map[file.Reference]string)
for _, fileRef := range f {
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))
contents, err := fileContents(fileRef.Path)
if err != nil {
return refContents, fmt.Errorf("could not read contents of file: %s", fileRef.Path)
}

View File

@ -113,7 +113,13 @@ func TestDirectoryResolver_MultipleFileContentsByRef(t *testing.T) {
resolver := DirectoryResolver{"test-fixtures"}
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...)

View File

@ -92,7 +92,7 @@ func TestDirectoryScope(t *testing.T) {
}
}
func TestMultipleFileContentsByRef(t *testing.T) {
func TestMultipleFileContentsByRefContents(t *testing.T) {
testCases := []struct {
desc string
input string
@ -105,12 +105,6 @@ func TestMultipleFileContentsByRef(t *testing.T) {
path: "empty",
expected: "",
},
{
input: "test-fixtures/path-detected",
desc: "path does not exist",
path: "foo",
expected: "",
},
{
input: "test-fixtures/path-detected",
desc: "file has contents",
@ -124,7 +118,16 @@ func TestMultipleFileContentsByRef(t *testing.T) {
if err != nil {
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)
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) {
testCases := []struct {
desc string