From d82a0872c953e4e5ee02addeb64ec15ee3cde7d5 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Thu, 16 Jul 2020 13:12:05 -0400 Subject: [PATCH 1/2] tests: verify MultipleFileContentsByRef gets contents from the right path Signed-off-by: Alfredo Deza --- .../resolvers/directory_resolver_test.go | 8 ++- imgbom/scope/scope_test.go | 51 ++++++++++++++++--- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/imgbom/scope/resolvers/directory_resolver_test.go b/imgbom/scope/resolvers/directory_resolver_test.go index 44d67f90f..1825e87c6 100644 --- a/imgbom/scope/resolvers/directory_resolver_test.go +++ b/imgbom/scope/resolvers/directory_resolver_test.go @@ -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...) diff --git a/imgbom/scope/scope_test.go b/imgbom/scope/scope_test.go index 96eb16e72..c0b0783dc 100644 --- a/imgbom/scope/scope_test.go +++ b/imgbom/scope/scope_test.go @@ -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 From a05d58686e3473f1dfca990bb6a5e280a666204c Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Thu, 16 Jul 2020 13:12:42 -0400 Subject: [PATCH 2/2] resolvers: do not join paths, it is assumed they are already joined Signed-off-by: Alfredo Deza --- imgbom/scope/resolvers/directory_resolver.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/imgbom/scope/resolvers/directory_resolver.go b/imgbom/scope/resolvers/directory_resolver.go index eb61d55b3..32ddfd0bb 100644 --- a/imgbom/scope/resolvers/directory_resolver.go +++ b/imgbom/scope/resolvers/directory_resolver.go @@ -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) }