github.com/anchore/syft@v1.38.2/test/cli/archive_test.go (about) 1 package cli 2 3 import ( 4 "archive/tar" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestArchiveScan(t *testing.T) { 13 tests := []struct { 14 name string 15 args []string 16 archiveFixture string 17 env map[string]string 18 assertions []traitAssertion 19 }{ 20 { 21 name: "scan an archive within the temp dir", 22 args: []string{ 23 "scan", 24 "-o", 25 "json", 26 "file:" + createArchive(t, "test-fixtures/archive", t.TempDir()), 27 }, 28 assertions: []traitAssertion{ 29 assertSuccessfulReturnCode, 30 assertJsonReport, 31 assertPackageCount(1), 32 }, 33 }, 34 } 35 36 for _, test := range tests { 37 t.Run(test.name, func(t *testing.T) { 38 cmd, stdout, stderr := runSyft(t, test.env, test.args...) 39 for _, traitAssertionFn := range test.assertions { 40 traitAssertionFn(t, stdout, stderr, cmd.ProcessState.ExitCode()) 41 } 42 logOutputOnFailure(t, cmd, stdout, stderr) 43 }) 44 } 45 } 46 47 func createArchive(t *testing.T, path string, destDir string) string { 48 // create a tarball of the test fixtures (not by shelling out) 49 archivePath := filepath.Join(destDir, "test.tar") 50 51 fh, err := os.Create(archivePath) 52 require.NoError(t, err) 53 defer fh.Close() 54 55 writer := tar.NewWriter(fh) 56 require.NoError(t, writer.AddFS(os.DirFS(path))) 57 require.NoError(t, writer.Close()) 58 59 return archivePath 60 }