mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
tests for scope.go
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
parent
3eedb86a1f
commit
3548c7b132
147
imgbom/scope/scope_test.go
Normal file
147
imgbom/scope/scope_test.go
Normal file
@ -0,0 +1,147 @@
|
||||
package scope
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/stereoscope/pkg/file"
|
||||
)
|
||||
|
||||
func TestDirectoryScope(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
input string
|
||||
expString string
|
||||
inputPaths []file.Path
|
||||
expRefs int
|
||||
}{
|
||||
{
|
||||
desc: "no paths exist",
|
||||
input: "foobar/",
|
||||
expString: "dir://foobar/",
|
||||
inputPaths: []file.Path{file.Path("/opt/"), file.Path("/other")},
|
||||
expRefs: 0,
|
||||
},
|
||||
{
|
||||
desc: "path detected",
|
||||
input: "test-fixtures",
|
||||
expString: "dir://test-fixtures",
|
||||
inputPaths: []file.Path{file.Path("path-detected")},
|
||||
expRefs: 1,
|
||||
},
|
||||
{
|
||||
desc: "no files-by-path detected",
|
||||
input: "test-fixtures",
|
||||
expString: "dir://test-fixtures",
|
||||
inputPaths: []file.Path{file.Path("no-path-detected")},
|
||||
expRefs: 0,
|
||||
},
|
||||
}
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
p, err := NewDirScope(test.input, AllLayersScope)
|
||||
if err != nil {
|
||||
t.Errorf("could not create NewDirScope: %w", err)
|
||||
}
|
||||
if p.String() != test.expString {
|
||||
t.Errorf("mismatched stringer: '%s' != '%s'", p.String(), test.expString)
|
||||
}
|
||||
|
||||
refs, err := p.FilesByPath(test.inputPaths...)
|
||||
if err != nil {
|
||||
t.Errorf("FilesByPath call produced an error: %w", err)
|
||||
}
|
||||
if len(refs) != test.expRefs {
|
||||
t.Errorf("unexpected number of refs returned: %d != %d", len(refs), test.expRefs)
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipleFileContentsByRef(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
input string
|
||||
path string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
input: "test-fixtures/path-detected",
|
||||
desc: "empty file",
|
||||
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",
|
||||
path: "test-fixtures/path-detected/.vimrc",
|
||||
expected: "\" A .vimrc file\n",
|
||||
},
|
||||
}
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
p, err := NewDirScope(test.input, AllLayersScope)
|
||||
if err != nil {
|
||||
t.Errorf("could not create NewDirScope: %w", err)
|
||||
}
|
||||
ref := file.NewFileReference(file.Path(test.path))
|
||||
contents, err := p.MultipleFileContentsByRef(ref)
|
||||
content := contents[ref]
|
||||
|
||||
if content != test.expected {
|
||||
t.Errorf("unexpected contents from file: '%s' != '%s'", content, test.expected)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesByGlob(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
input string
|
||||
glob string
|
||||
expected int
|
||||
}{
|
||||
{
|
||||
input: "test-fixtures",
|
||||
desc: "no matches",
|
||||
glob: "bar/foo",
|
||||
expected: 0,
|
||||
},
|
||||
{
|
||||
input: "test-fixtures/path-detected",
|
||||
desc: "a single match",
|
||||
glob: "*vimrc",
|
||||
expected: 1,
|
||||
},
|
||||
{
|
||||
input: "test-fixtures/path-detected",
|
||||
desc: "multiple matches",
|
||||
glob: "*",
|
||||
expected: 2,
|
||||
},
|
||||
}
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
p, err := NewDirScope(test.input, AllLayersScope)
|
||||
if err != nil {
|
||||
t.Errorf("could not create NewDirScope: %w", err)
|
||||
}
|
||||
|
||||
contents, err := p.FilesByGlob(test.glob)
|
||||
|
||||
if len(contents) != test.expected {
|
||||
t.Errorf("unexpected number of files found by glob (%s): %d != %d", test.glob, len(contents), test.expected)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
1
imgbom/scope/test-fixtures/path-detected/.vimrc
Normal file
1
imgbom/scope/test-fixtures/path-detected/.vimrc
Normal file
@ -0,0 +1 @@
|
||||
" A .vimrc file
|
||||
0
imgbom/scope/test-fixtures/path-detected/empty
Normal file
0
imgbom/scope/test-fixtures/path-detected/empty
Normal file
Loading…
x
Reference in New Issue
Block a user