Files
syft/test/cli/archive_test.go
T
Keith Zantow 50100f6531 Merge PR #4044: recursive nested-archive scanning at the resolver layer
Adds recursive archive indexing/traversal in the fileresolver and source
provider layers.

Original work by @Rupikz in anchore/syft#4044.

# Conflicts:
#	cmd/syft/internal/options/source.go
#	syft/internal/fileresolver/directory.go
#	syft/internal/fileresolver/filetree_resolver.go
#	syft/internal/fileresolver/filetree_resolver_test.go
#	syft/pkg/cataloger/java/internal/maven/resolver_test.go
#	syft/source/directorysource/directory_source.go
#	test/cli/archive_test.go
2026-06-30 16:50:23 +00:00

77 lines
1.7 KiB
Go

package cli
import (
"archive/tar"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestArchiveScan(t *testing.T) {
archive := createArchive(t, "testdata/archive", t.TempDir())
nestedArchive := createArchive(t, filepath.Dir(archive), t.TempDir())
tests := []struct {
name string
args []string
archiveFixture string
env map[string]string
assertions []traitAssertion
}{
{
name: "scan an archive within the temp dir",
args: []string{
"scan",
"-o",
"json",
"file:" + archive,
},
assertions: []traitAssertion{
assertSuccessfulReturnCode,
assertJsonReport,
assertPackageCount(1),
},
},
{
name: "scan an nested archive within the temp dir",
args: []string{
"scan",
"-o",
"json",
"file:" + nestedArchive,
},
assertions: []traitAssertion{
assertSuccessfulReturnCode,
assertJsonReport,
assertPackageCount(1),
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cmd, stdout, stderr := runSyft(t, test.env, test.args...)
for _, traitAssertionFn := range test.assertions {
traitAssertionFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
}
logOutputOnFailure(t, cmd, stdout, stderr)
})
}
}
func createArchive(t *testing.T, path string, destDir string) string {
// create a tarball of the test fixtures (not by shelling out)
archivePath := filepath.Join(destDir, "test.tar")
fh, err := os.Create(archivePath)
require.NoError(t, err)
defer fh.Close()
writer := tar.NewWriter(fh)
require.NoError(t, writer.AddFS(os.DirFS(path)))
require.NoError(t, writer.Close())
return archivePath
}