Skip to content

Commit

Permalink
migrate resolvers to syft/internal
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
  • Loading branch information
wagoodman committed May 24, 2023
1 parent 6ba8922 commit 837ec75
Show file tree
Hide file tree
Showing 98 changed files with 49 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package resolver
package fileresolver

import (
"io"
"sort"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/scylladb/go-set/strset"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -514,26 +513,6 @@ func Test_imageSquashResolver_resolvesLinks(t *testing.T) {

}

func compareLocations(t *testing.T, expected, actual []file.Location) {
t.Helper()
ignoreUnexported := cmpopts.IgnoreFields(file.LocationData{}, "ref")
ignoreMetadata := cmpopts.IgnoreFields(file.LocationMetadata{}, "Annotations")
ignoreFS := cmpopts.IgnoreFields(file.Coordinates{}, "FileSystemID")

sort.Sort(file.Locations(expected))
sort.Sort(file.Locations(actual))

if d := cmp.Diff(expected, actual,
ignoreUnexported,
ignoreFS,
ignoreMetadata,
); d != "" {

t.Errorf("unexpected locations (-want +got):\n%s", d)
}

}

func TestSquashResolver_AllLocations(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-files-deleted")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"io/fs"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build !windows
// +build !windows

package resolver
package fileresolver

import (
"io"
Expand Down Expand Up @@ -49,9 +49,9 @@ func TestDirectoryResolver_FilesByPath_relativeRoot(t *testing.T) {
name: "should find a file from a relative path (root above cwd)",
// TODO: refactor me! this test depends on the structure of the source dir not changing, which isn't great
relativeRoot: "../",
input: "resolver.go",
input: "fileresolver/directory.go",
expected: []string{
"resolver.go",
"fileresolver/directory.go",
},
},
}
Expand Down Expand Up @@ -99,9 +99,9 @@ func TestDirectoryResolver_FilesByPath_absoluteRoot(t *testing.T) {
name: "should find a file from a relative path (root above cwd)",
// TODO: refactor me! this test depends on the structure of the source dir not changing, which isn't great
relativeRoot: "../",
input: "resolver.go",
input: "fileresolver/directory.go",
expected: []string{
"resolver.go",
"fileresolver/directory.go",
},
},
}
Expand Down Expand Up @@ -664,20 +664,6 @@ func Test_SymlinkLoopWithGlobsShouldResolve(t *testing.T) {
testWithTimeout(t, 5*time.Second, test)
}

func testWithTimeout(t *testing.T, timeout time.Duration, test func(*testing.T)) {
done := make(chan bool)
go func() {
test(t)
done <- true
}()

select {
case <-time.After(timeout):
t.Fatal("test timed out")
case <-done:
}
}

func TestDirectoryResolver_FilesByPath_baseRoot(t *testing.T) {
cases := []struct {
name string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import "testing"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"github.com/anchore/stereoscope/pkg/image"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resolver
package fileresolver

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build !windows
// +build !windows

package resolver
package fileresolver

import (
"io"
Expand Down Expand Up @@ -60,9 +60,9 @@ func Test_UnindexedDirectoryResolver_FilesByPath_relativeRoot(t *testing.T) {
name: "should find a file from a relative path (root above cwd)",
// TODO: refactor me! this test depends on the structure of the source dir not changing, which isn't great
relativeRoot: "../",
input: "resolver.go",
input: "fileresolver/deferred.go",
expected: []string{
"resolver.go",
"fileresolver/deferred.go",
},
},
}
Expand Down Expand Up @@ -109,9 +109,9 @@ func Test_UnindexedDirectoryResolver_FilesByPath_absoluteRoot(t *testing.T) {
name: "should find a file from a relative path (root above cwd)",
// TODO: refactor me! this test depends on the structure of the source dir not changing, which isn't great
relativeRoot: "../",
input: "resolver.go",
input: "fileresolver/directory.go",
expected: []string{
"resolver.go",
"fileresolver/directory.go",
},
},
}
Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions syft/pkg/cataloger/golang/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/event"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/internal/resolver"
"github.com/anchore/syft/syft/internal/fileresolver"
"github.com/anchore/syft/syft/pkg"
)

Expand Down Expand Up @@ -61,15 +61,15 @@ func modCacheResolver(modCacheDir string) file.WritableResolver {

if modCacheDir == "" {
log.Trace("unable to determine mod cache directory, skipping mod cache resolver")
r = resolver.Empty{}
r = fileresolver.Empty{}
} else {
stat, err := os.Stat(modCacheDir)

if os.IsNotExist(err) || stat == nil || !stat.IsDir() {
log.Tracef("unable to open mod cache directory: %s, skipping mod cache resolver", modCacheDir)
r = resolver.Empty{}
r = fileresolver.Empty{}
} else {
r = resolver.NewFromUnindexedDirectory(modCacheDir)
r = fileresolver.NewFromUnindexedDirectory(modCacheDir)
}
}

Expand Down
6 changes: 3 additions & 3 deletions syft/pkg/cataloger/golang/licenses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

"github.com/anchore/syft/internal"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/internal/resolver"
"github.com/anchore/syft/syft/internal/fileresolver"
"github.com/anchore/syft/syft/license"
"github.com/anchore/syft/syft/pkg"
)
Expand Down Expand Up @@ -64,7 +64,7 @@ func Test_LocalLicenseSearch(t *testing.T) {
localModCacheDir: path.Join(wd, "test-fixtures", "licenses", "pkg", "mod"),
},
)
licenses, err := l.getLicenses(resolver.Empty{}, test.name, test.version)
licenses, err := l.getLicenses(fileresolver.Empty{}, test.name, test.version)
require.NoError(t, err)

require.Len(t, licenses, 1)
Expand Down Expand Up @@ -154,7 +154,7 @@ func Test_RemoteProxyLicenseSearch(t *testing.T) {
localModCacheDir: modDir,
})

licenses, err := l.getLicenses(resolver.Empty{}, test.name, test.version)
licenses, err := l.getLicenses(fileresolver.Empty{}, test.name, test.version)
require.NoError(t, err)

require.Len(t, licenses, 1)
Expand Down
4 changes: 2 additions & 2 deletions syft/pkg/cataloger/golang/parse_go_binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/internal/resolver"
"github.com/anchore/syft/syft/internal/fileresolver"
"github.com/anchore/syft/syft/pkg"
)

Expand Down Expand Up @@ -508,7 +508,7 @@ func TestBuildGoPkgInfo(t *testing.T) {
)

c := goBinaryCataloger{}
pkgs := c.buildGoPkgInfo(resolver.Empty{}, location, test.mod, test.arch)
pkgs := c.buildGoPkgInfo(fileresolver.Empty{}, location, test.mod, test.arch)
assert.Equal(t, test.expected, pkgs)
})
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

19 changes: 9 additions & 10 deletions syft/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
resolver2 "github.com/anchore/syft/syft/internal/resolver"
"github.com/anchore/syft/syft/source/internal/resolver"
"github.com/anchore/syft/syft/internal/fileresolver"
)

// Source is an object that captures the data source to be cataloged, configuration, and a specific resolver used
Expand All @@ -33,7 +32,7 @@ type Source struct {
id artifact.ID `hash:"ignore"`
Image *image.Image `hash:"ignore"` // the image object to be cataloged (image only)
Metadata Metadata
directoryResolver *resolver.Directory `hash:"ignore"`
directoryResolver *fileresolver.Directory `hash:"ignore"`
path string
base string
mutex *sync.Mutex
Expand Down Expand Up @@ -465,7 +464,7 @@ func (s *Source) FileResolver(scope Scope) (file.Resolver, error) {
if err != nil {
return nil, err
}
res, err := resolver.NewFromDirectory(s.path, s.base, exclusionFunctions...)
res, err := fileresolver.NewFromDirectory(s.path, s.base, exclusionFunctions...)
if err != nil {
return nil, fmt.Errorf("unable to create directory resolver: %w", err)
}
Expand All @@ -477,9 +476,9 @@ func (s *Source) FileResolver(scope Scope) (file.Resolver, error) {
var err error
switch scope {
case SquashedScope:
res, err = resolver.NewFromContainerImageSquash(s.Image)
res, err = fileresolver.NewFromContainerImageSquash(s.Image)
case AllLayersScope:
res, err = resolver.NewFromContainerImageAllLayers(s.Image)
res, err = fileresolver.NewFromContainerImageAllLayers(s.Image)
default:
return nil, fmt.Errorf("bad image scope provided: %+v", scope)
}
Expand All @@ -488,7 +487,7 @@ func (s *Source) FileResolver(scope Scope) (file.Resolver, error) {
}
// image tree contains all paths, so we filter out the excluded entries afterwards
if len(s.Exclusions) > 0 {
res = resolver2.NewExcluding(res, getImageExclusionFunction(s.Exclusions))
res = fileresolver.NewExcluding(res, getImageExclusionFunction(s.Exclusions))
}
return res, nil
}
Expand Down Expand Up @@ -532,7 +531,7 @@ func getImageExclusionFunction(exclusions []string) func(string) bool {
}
}

func getDirectoryExclusionFunctions(root string, exclusions []string) ([]resolver.PathIndexVisitor, error) {
func getDirectoryExclusionFunctions(root string, exclusions []string) ([]fileresolver.PathIndexVisitor, error) {
if len(exclusions) == 0 {
return nil, nil
}
Expand Down Expand Up @@ -565,7 +564,7 @@ func getDirectoryExclusionFunctions(root string, exclusions []string) ([]resolve
return nil, fmt.Errorf("invalid exclusion pattern(s): '%s' (must start with one of: './', '*/', or '**/')", strings.Join(errors, "', '"))
}

return []resolver.PathIndexVisitor{
return []fileresolver.PathIndexVisitor{
func(path string, info os.FileInfo, _ error) error {
for _, exclusion := range exclusions {
// this is required to handle Windows filepaths
Expand All @@ -578,7 +577,7 @@ func getDirectoryExclusionFunctions(root string, exclusions []string) ([]resolve
if info != nil && info.IsDir() {
return filepath.SkipDir
}
return resolver.ErrSkipPath
return fileresolver.ErrSkipPath
}
}
return nil
Expand Down
Loading

0 comments on commit 837ec75

Please sign in to comment.