From b3ad966c05f890f6f04d3ccb998356e6bd119a36 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Thu, 18 May 2023 13:14:21 +0800 Subject: [PATCH 1/7] code optimization Signed-off-by: guoguangwu --- internal/file/zip_file_helpers_test.go | 3 +-- internal/file/zip_file_traversal_test.go | 13 +++------- syft/cpe/cpe_test.go | 8 +++--- syft/file/digest_cataloger_test.go | 4 +-- syft/linux/identify_release_test.go | 4 +-- syft/pkg/cataloger/generic/cataloger_test.go | 4 +-- .../cataloger/golang/internal/xcoff/file.go | 26 +++++++++---------- .../internal/unionreader/union_reader_test.go | 3 --- .../graalvm_native_image_cataloger_test.go | 5 ++-- syft/source/directory_resolver_test.go | 3 +-- syft/source/source_test.go | 3 +-- test/cli/cyclonedx_valid_test.go | 4 +-- test/cli/json_schema_test.go | 4 +-- test/cli/spdx_json_schema_test.go | 4 +-- test/integration/encode_decode_cycle_test.go | 3 +-- 15 files changed, 35 insertions(+), 56 deletions(-) diff --git a/internal/file/zip_file_helpers_test.go b/internal/file/zip_file_helpers_test.go index 7a76be3b01a..69c4acf81d8 100644 --- a/internal/file/zip_file_helpers_test.go +++ b/internal/file/zip_file_helpers_test.go @@ -1,7 +1,6 @@ package file import ( - "io/ioutil" "os" "os/exec" "path" @@ -73,7 +72,7 @@ func assertNoError(t testing.TB, fn func() error) func() { func setupZipFileTest(t testing.TB, sourceDirPath string, zip64 bool) string { t.Helper() - archivePrefix, err := ioutil.TempFile("", "syft-ziputil-archive-TEST-") + archivePrefix, err := os.CreateTemp("", "syft-ziputil-archive-TEST-") if err != nil { t.Fatalf("unable to create tempfile: %+v", err) } diff --git a/internal/file/zip_file_traversal_test.go b/internal/file/zip_file_traversal_test.go index d2bff824438..7bbc32a09b0 100644 --- a/internal/file/zip_file_traversal_test.go +++ b/internal/file/zip_file_traversal_test.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path" "path/filepath" @@ -49,13 +48,7 @@ func TestUnzipToDir(t *testing.T) { sourceDirPath := path.Join(goldenRootDir, "zip-source") archiveFilePath := setupZipFileTest(t, sourceDirPath, false) - unzipDestinationDir, err := ioutil.TempDir("", "syft-ziputil-contents-TEST-") - t.Cleanup(assertNoError(t, func() error { - return os.RemoveAll(unzipDestinationDir) - })) - if err != nil { - t.Fatalf("unable to create tempdir: %+v", err) - } + unzipDestinationDir := t.TempDir() t.Logf("content path: %s", unzipDestinationDir) @@ -170,7 +163,7 @@ func prependZipSourceFixtureWithString(tb testing.TB, value string) func(tb test archivePath := prepZipSourceFixture(t) // create a temp file - tmpFile, err := ioutil.TempFile("", "syft-ziputil-prependZipSourceFixtureWithString-") + tmpFile, err := os.CreateTemp("", "syft-ziputil-prependZipSourceFixtureWithString-") if err != nil { t.Fatalf("unable to create tempfile: %+v", err) } @@ -209,7 +202,7 @@ func prependZipSourceFixtureWithString(tb testing.TB, value string) func(tb test func prepZipSourceFixture(t testing.TB) string { t.Helper() - archivePrefix, err := ioutil.TempFile("", "syft-ziputil-prepZipSourceFixture-") + archivePrefix, err := os.CreateTemp("", "syft-ziputil-prepZipSourceFixture-") if err != nil { t.Fatalf("unable to create tempfile: %+v", err) } diff --git a/syft/cpe/cpe_test.go b/syft/cpe/cpe_test.go index 38d6112ae68..9af8f210542 100644 --- a/syft/cpe/cpe_test.go +++ b/syft/cpe/cpe_test.go @@ -3,7 +3,7 @@ package cpe import ( "encoding/json" "fmt" - "io/ioutil" + "os" "strings" "testing" @@ -80,12 +80,12 @@ func Test_normalizeCpeField(t *testing.T) { } func Test_CPEParser(t *testing.T) { - testCases := []struct { + var testCases []struct { CPEString string `json:"cpe-string"` CPEUrl string `json:"cpe-url"` WFN CPE `json:"wfn"` - }{} - out, err := ioutil.ReadFile("test-fixtures/cpe-data.json") + } + out, err := os.ReadFile("test-fixtures/cpe-data.json") require.NoError(t, err) require.NoError(t, json.Unmarshal(out, &testCases)) diff --git a/syft/file/digest_cataloger_test.go b/syft/file/digest_cataloger_test.go index 57a1a8071ee..bf548ccc70e 100644 --- a/syft/file/digest_cataloger_test.go +++ b/syft/file/digest_cataloger_test.go @@ -3,7 +3,7 @@ package file import ( "crypto" "fmt" - "io/ioutil" + "io" "os" "path/filepath" "testing" @@ -24,7 +24,7 @@ func testDigests(t testing.TB, root string, files []string, hashes ...crypto.Has if err != nil { t.Fatalf("could not open %q : %+v", f, err) } - b, err := ioutil.ReadAll(fh) + b, err := io.ReadAll(fh) if err != nil { t.Fatalf("could not read %q : %+v", f, err) } diff --git a/syft/linux/identify_release_test.go b/syft/linux/identify_release_test.go index 3d6577b8c39..00d04ee71fc 100644 --- a/syft/linux/identify_release_test.go +++ b/syft/linux/identify_release_test.go @@ -1,7 +1,7 @@ package linux import ( - "io/ioutil" + "io" "os" "testing" @@ -539,7 +539,7 @@ func retrieveFixtureContentsAsString(fixturePath string, t *testing.T) string { } defer fixture.Close() - b, err := ioutil.ReadAll(fixture) + b, err := io.ReadAll(fixture) if err != nil { t.Fatalf("unable to read fixture file: %+v", err) } diff --git a/syft/pkg/cataloger/generic/cataloger_test.go b/syft/pkg/cataloger/generic/cataloger_test.go index fd864787a42..5476888d23a 100644 --- a/syft/pkg/cataloger/generic/cataloger_test.go +++ b/syft/pkg/cataloger/generic/cataloger_test.go @@ -2,7 +2,7 @@ package generic import ( "fmt" - "io/ioutil" + "io" "testing" "github.com/stretchr/testify/assert" @@ -17,7 +17,7 @@ func Test_Cataloger(t *testing.T) { allParsedPaths := make(map[string]bool) parser := func(resolver source.FileResolver, env *Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) { allParsedPaths[reader.AccessPath()] = true - contents, err := ioutil.ReadAll(reader) + contents, err := io.ReadAll(reader) require.NoError(t, err) if len(contents) == 0 { diff --git a/syft/pkg/cataloger/golang/internal/xcoff/file.go b/syft/pkg/cataloger/golang/internal/xcoff/file.go index e4eb8876a74..7d801e8cf93 100644 --- a/syft/pkg/cataloger/golang/internal/xcoff/file.go +++ b/syft/pkg/cataloger/golang/internal/xcoff/file.go @@ -168,7 +168,7 @@ func NewFile(r io.ReaderAt) (*File, error) { f.TargetMachine = magic // Read XCOFF file header - if _, err := sr.Seek(0, os.SEEK_SET); err != nil { + if _, err := sr.Seek(0, io.SeekStart); err != nil { return nil, err } var nscns uint16 @@ -205,7 +205,7 @@ func NewFile(r io.ReaderAt) (*File, error) { // Read string table (located right after symbol table). offset := symptr + uint64(nsyms)*SYMESZ - if _, err := sr.Seek(int64(offset), os.SEEK_SET); err != nil { + if _, err := sr.Seek(int64(offset), io.SeekStart); err != nil { return nil, err } // The first 4 bytes contain the length (in bytes). @@ -214,7 +214,7 @@ func NewFile(r io.ReaderAt) (*File, error) { return nil, err } if l > 4 { - if _, err := sr.Seek(int64(offset), os.SEEK_SET); err != nil { + if _, err := sr.Seek(int64(offset), io.SeekStart); err != nil { return nil, err } f.StringTable = make([]byte, l) @@ -224,7 +224,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } // Read section headers - if _, err := sr.Seek(int64(hdrsz)+int64(opthdr), os.SEEK_SET); err != nil { + if _, err := sr.Seek(int64(hdrsz)+int64(opthdr), io.SeekStart); err != nil { return nil, err } f.Sections = make([]*Section, nscns) @@ -270,7 +270,7 @@ func NewFile(r io.ReaderAt) (*File, error) { var idxToSym = make(map[int]*Symbol) // Read symbol table - if _, err := sr.Seek(int64(symptr), os.SEEK_SET); err != nil { + if _, err := sr.Seek(int64(symptr), io.SeekStart); err != nil { return nil, err } f.Symbols = make([]*Symbol, 0) @@ -356,7 +356,7 @@ func NewFile(r io.ReaderAt) (*File, error) { // Read csect auxiliary entry (by convention, it is the last). if !needAuxFcn { - if _, err := sr.Seek(int64(numaux-1)*SYMESZ, os.SEEK_CUR); err != nil { + if _, err := sr.Seek(int64(numaux-1)*SYMESZ, io.SeekStart); err != nil { return nil, err } } @@ -383,7 +383,7 @@ func NewFile(r io.ReaderAt) (*File, error) { f.Symbols = append(f.Symbols, sym) skip: i += numaux // Skip auxiliary entries - if _, err := sr.Seek(int64(numaux)*SYMESZ, os.SEEK_CUR); err != nil { + if _, err := sr.Seek(int64(numaux)*SYMESZ, io.SeekStart); err != nil { return nil, err } } @@ -398,7 +398,7 @@ func NewFile(r io.ReaderAt) (*File, error) { if sect.Relptr == 0 { continue } - if _, err := sr.Seek(int64(sect.Relptr), os.SEEK_SET); err != nil { + if _, err := sr.Seek(int64(sect.Relptr), io.SeekStart); err != nil { return nil, err } for i := uint32(0); i < sect.Nreloc; i++ { @@ -509,7 +509,7 @@ func (s *Section) Data() ([]byte, error) { // Library name pattern is either path/base/member or base/member func (f *File) readImportIDs(s *Section) ([]string, error) { // Read loader header - if _, err := s.sr.Seek(0, os.SEEK_SET); err != nil { + if _, err := s.sr.Seek(0, io.SeekStart); err != nil { return nil, err } var istlen uint32 @@ -535,7 +535,7 @@ func (f *File) readImportIDs(s *Section) ([]string, error) { } // Read loader import file ID table - if _, err := s.sr.Seek(int64(impoff), os.SEEK_SET); err != nil { + if _, err := s.sr.Seek(int64(impoff), io.SeekStart); err != nil { return nil, err } table := make([]byte, istlen) @@ -578,7 +578,7 @@ func (f *File) ImportedSymbols() ([]ImportedSymbol, error) { return nil, nil } // Read loader header - if _, err := s.sr.Seek(0, os.SEEK_SET); err != nil { + if _, err := s.sr.Seek(0, io.SeekStart); err != nil { return nil, err } var stlen uint32 @@ -607,7 +607,7 @@ func (f *File) ImportedSymbols() ([]ImportedSymbol, error) { } // Read loader section string table - if _, err := s.sr.Seek(int64(stoff), os.SEEK_SET); err != nil { + if _, err := s.sr.Seek(int64(stoff), io.SeekStart); err != nil { return nil, err } st := make([]byte, stlen) @@ -622,7 +622,7 @@ func (f *File) ImportedSymbols() ([]ImportedSymbol, error) { } // Read loader symbol table - if _, err := s.sr.Seek(int64(symoff), os.SEEK_SET); err != nil { + if _, err := s.sr.Seek(int64(symoff), io.SeekStart); err != nil { return nil, err } all := make([]ImportedSymbol, 0) diff --git a/syft/pkg/cataloger/internal/unionreader/union_reader_test.go b/syft/pkg/cataloger/internal/unionreader/union_reader_test.go index 15c67459aa1..e2a3c5fa270 100644 --- a/syft/pkg/cataloger/internal/unionreader/union_reader_test.go +++ b/syft/pkg/cataloger/internal/unionreader/union_reader_test.go @@ -20,9 +20,6 @@ func Test_getUnionReader_notUnionReader(t *testing.T) { actual, err := GetUnionReader(reader) require.NoError(t, err) - _, ok = actual.(UnionReader) - require.True(t, ok) - b, err := io.ReadAll(actual) require.NoError(t, err) diff --git a/syft/pkg/cataloger/java/graalvm_native_image_cataloger_test.go b/syft/pkg/cataloger/java/graalvm_native_image_cataloger_test.go index e320dc292fe..dcb2d6dbf13 100644 --- a/syft/pkg/cataloger/java/graalvm_native_image_cataloger_test.go +++ b/syft/pkg/cataloger/java/graalvm_native_image_cataloger_test.go @@ -6,7 +6,6 @@ import ( "compress/gzip" "encoding/binary" "io" - "io/ioutil" "os" "path" "testing" @@ -36,7 +35,7 @@ func TestParseNativeImage(t *testing.T) { t.Run(test.fixture, func(t *testing.T) { f, err := os.Open("test-fixtures/java-builds/packages/" + test.fixture) assert.NoError(t, err) - readerCloser := io.ReadCloser(ioutil.NopCloser(f)) + readerCloser := io.NopCloser(f) reader, err := unionreader.GetUnionReader(readerCloser) assert.NoError(t, err) parsed := false @@ -107,7 +106,7 @@ func TestParseNativeImageSbom(t *testing.T) { for _, test := range tests { t.Run(path.Base(test.fixture), func(t *testing.T) { // Create a buffer to resemble a compressed SBOM in a native image. - sbom, err := ioutil.ReadFile(test.fixture) + sbom, err := os.ReadFile(test.fixture) assert.NoError(t, err) var b bytes.Buffer writebytes := bufio.NewWriter(&b) diff --git a/syft/source/directory_resolver_test.go b/syft/source/directory_resolver_test.go index 0a752ba41a7..0f38f3e017d 100644 --- a/syft/source/directory_resolver_test.go +++ b/syft/source/directory_resolver_test.go @@ -6,7 +6,6 @@ package source import ( "io" "io/fs" - "io/ioutil" "os" "path/filepath" "sort" @@ -598,7 +597,7 @@ func Test_directoryResolver_FileContentsByLocation(t *testing.T) { require.NoError(t, err) if test.expects != "" { - b, err := ioutil.ReadAll(actual) + b, err := io.ReadAll(actual) require.NoError(t, err) assert.Equal(t, test.expects, string(b)) } diff --git a/syft/source/source_test.go b/syft/source/source_test.go index cfc6b8f2923..1ef16f6c138 100644 --- a/syft/source/source_test.go +++ b/syft/source/source_test.go @@ -6,7 +6,6 @@ package source import ( "io" "io/fs" - "io/ioutil" "os" "os/exec" "path" @@ -898,7 +897,7 @@ func createArchive(t testing.TB, sourceDirPath, destinationArchivePath string, l func setupArchiveTest(t testing.TB, sourceDirPath string, layer2 bool) string { t.Helper() - archivePrefix, err := ioutil.TempFile("", "syft-archive-TEST-") + archivePrefix, err := os.CreateTemp("", "syft-archive-TEST-") require.NoError(t, err) t.Cleanup( diff --git a/test/cli/cyclonedx_valid_test.go b/test/cli/cyclonedx_valid_test.go index bbd4b0b3af1..5a5b240ab88 100644 --- a/test/cli/cyclonedx_valid_test.go +++ b/test/cli/cyclonedx_valid_test.go @@ -44,9 +44,7 @@ func TestValidCycloneDX(t *testing.T) { args := []string{ test.subcommand, fixtureRef, "-q", } - for _, a := range test.args { - args = append(args, a) - } + args = append(args, test.args...) cmd, stdout, stderr := runSyft(t, nil, args...) for _, traitFn := range test.assertions { diff --git a/test/cli/json_schema_test.go b/test/cli/json_schema_test.go index 6757490a3e0..dd4d19e690e 100644 --- a/test/cli/json_schema_test.go +++ b/test/cli/json_schema_test.go @@ -57,9 +57,7 @@ func TestJSONSchema(t *testing.T) { args := []string{ test.subcommand, fixtureRef, "-q", } - for _, a := range test.args { - args = append(args, a) - } + args = append(args, test.args...) _, stdout, stderr := runSyft(t, nil, args...) diff --git a/test/cli/spdx_json_schema_test.go b/test/cli/spdx_json_schema_test.go index 4dd934b255f..c1e78f06cb0 100644 --- a/test/cli/spdx_json_schema_test.go +++ b/test/cli/spdx_json_schema_test.go @@ -50,9 +50,7 @@ func TestSPDXJSONSchema(t *testing.T) { args := []string{ test.subcommand, fixtureRef, "-q", } - for _, a := range test.args { - args = append(args, a) - } + args = append(args, test.args...) _, stdout, _ := runSyft(t, nil, args...) diff --git a/test/integration/encode_decode_cycle_test.go b/test/integration/encode_decode_cycle_test.go index 67940212bb1..458d33cdda9 100644 --- a/test/integration/encode_decode_cycle_test.go +++ b/test/integration/encode_decode_cycle_test.go @@ -2,7 +2,6 @@ package integration import ( "bytes" - "fmt" "regexp" "testing" @@ -64,7 +63,7 @@ func TestEncodeDecodeEncodeCycleComparison(t *testing.T) { } for _, test := range tests { - t.Run(fmt.Sprintf("%s", test.formatOption), func(t *testing.T) { + t.Run(string(test.formatOption), func(t *testing.T) { for _, image := range images { originalSBOM, _ := catalogFixtureImage(t, image, source.SquashedScope, nil) From 0500e57d5a2e724c045236c2351bc889d530410b Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Fri, 19 May 2023 10:28:42 +0800 Subject: [PATCH 2/7] code optimization Signed-off-by: guoguangwu --- internal/file/tar_file_traversal.go | 6 +++--- internal/file/zip_file_helpers_test.go | 16 ++-------------- internal/file/zip_file_traversal_test.go | 9 +-------- syft/source/source_test.go | 14 ++------------ 4 files changed, 8 insertions(+), 37 deletions(-) diff --git a/internal/file/tar_file_traversal.go b/internal/file/tar_file_traversal.go index dbe2a60f6b4..9e7adfc8ddc 100644 --- a/internal/file/tar_file_traversal.go +++ b/internal/file/tar_file_traversal.go @@ -32,12 +32,12 @@ func ExtractGlobsFromTarToUniqueTempFile(archivePath, dir string, globs ...strin } // we have a file we want to extract.... - tempfilePrefix := filepath.Base(filepath.Clean(file.Name())) + "-" - tempFile, err := os.CreateTemp(dir, tempfilePrefix) + tempFilePrefix := filepath.Base(filepath.Clean(file.Name())) + "-" + tempFile, err := os.CreateTemp(dir, tempFilePrefix) if err != nil { return fmt.Errorf("unable to create temp file: %w", err) } - // we shouldn't try and keep the tempfile open as the returned result may have several files, which takes up + // we shouldn't try and keep the tempFile open as the returned result may have several files, which takes up // resources (leading to "too many open files"). Instead we'll return a file opener to the caller which // provides a ReadCloser. It is up to the caller to handle closing the file explicitly. defer tempFile.Close() diff --git a/internal/file/zip_file_helpers_test.go b/internal/file/zip_file_helpers_test.go index 69c4acf81d8..3bb9b2c0681 100644 --- a/internal/file/zip_file_helpers_test.go +++ b/internal/file/zip_file_helpers_test.go @@ -72,20 +72,8 @@ func assertNoError(t testing.TB, fn func() error) func() { func setupZipFileTest(t testing.TB, sourceDirPath string, zip64 bool) string { t.Helper() - archivePrefix, err := os.CreateTemp("", "syft-ziputil-archive-TEST-") - if err != nil { - t.Fatalf("unable to create tempfile: %+v", err) - } - - t.Cleanup( - assertNoError(t, - func() error { - return os.Remove(archivePrefix.Name()) - }, - ), - ) - - destinationArchiveFilePath := archivePrefix.Name() + ".zip" + archivePrefix := path.Join(t.TempDir(), "syft-ziputil-archive-TEST-") + destinationArchiveFilePath := archivePrefix + ".zip" t.Logf("archive path: %s", destinationArchiveFilePath) createZipArchive(t, sourceDirPath, destinationArchiveFilePath, zip64) diff --git a/internal/file/zip_file_traversal_test.go b/internal/file/zip_file_traversal_test.go index 7bbc32a09b0..ff6f22c67de 100644 --- a/internal/file/zip_file_traversal_test.go +++ b/internal/file/zip_file_traversal_test.go @@ -202,14 +202,7 @@ func prependZipSourceFixtureWithString(tb testing.TB, value string) func(tb test func prepZipSourceFixture(t testing.TB) string { t.Helper() - archivePrefix, err := os.CreateTemp("", "syft-ziputil-prepZipSourceFixture-") - if err != nil { - t.Fatalf("unable to create tempfile: %+v", err) - } - - t.Cleanup(func() { - assert.NoError(t, os.Remove(archivePrefix.Name())) - }) + archivePrefix := path.Join(t.TempDir(), "syft-ziputil-prepZipSourceFixture-") // the zip utility will add ".zip" to the end of the given name archivePath := archivePrefix.Name() + ".zip" diff --git a/syft/source/source_test.go b/syft/source/source_test.go index 1ef16f6c138..36880eb40c6 100644 --- a/syft/source/source_test.go +++ b/syft/source/source_test.go @@ -897,18 +897,8 @@ func createArchive(t testing.TB, sourceDirPath, destinationArchivePath string, l func setupArchiveTest(t testing.TB, sourceDirPath string, layer2 bool) string { t.Helper() - archivePrefix, err := os.CreateTemp("", "syft-archive-TEST-") - require.NoError(t, err) - - t.Cleanup( - assertNoError(t, - func() error { - return os.Remove(archivePrefix.Name()) - }, - ), - ) - - destinationArchiveFilePath := archivePrefix.Name() + ".tar" + archivePrefix := path.Join(t.TempDir(),"syft-archive-TEST-") + destinationArchiveFilePath := archivePrefix + ".tar" t.Logf("archive path: %s", destinationArchiveFilePath) createArchive(t, sourceDirPath, destinationArchiveFilePath, layer2) From de4541be6ed70b5860088ce5644c93fdd3e84e3a Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Tue, 23 May 2023 09:57:04 +0800 Subject: [PATCH 3/7] code optimization Signed-off-by: guoguangwu --- internal/file/zip_file_helpers_test.go | 7 ------- internal/file/zip_file_traversal_test.go | 4 ---- syft/source/source_test.go | 8 -------- 3 files changed, 19 deletions(-) diff --git a/internal/file/zip_file_helpers_test.go b/internal/file/zip_file_helpers_test.go index 3bb9b2c0681..e398449dd13 100644 --- a/internal/file/zip_file_helpers_test.go +++ b/internal/file/zip_file_helpers_test.go @@ -77,13 +77,6 @@ func setupZipFileTest(t testing.TB, sourceDirPath string, zip64 bool) string { t.Logf("archive path: %s", destinationArchiveFilePath) createZipArchive(t, sourceDirPath, destinationArchiveFilePath, zip64) - t.Cleanup( - assertNoError(t, - func() error { - return os.Remove(destinationArchiveFilePath) - }, - ), - ) cwd, err := os.Getwd() if err != nil { diff --git a/internal/file/zip_file_traversal_test.go b/internal/file/zip_file_traversal_test.go index ff6f22c67de..626b7f0a2a8 100644 --- a/internal/file/zip_file_traversal_test.go +++ b/internal/file/zip_file_traversal_test.go @@ -207,10 +207,6 @@ func prepZipSourceFixture(t testing.TB) string { // the zip utility will add ".zip" to the end of the given name archivePath := archivePrefix.Name() + ".zip" - t.Cleanup(func() { - assert.NoError(t, os.Remove(archivePath)) - }) - t.Logf("archive path: %s", archivePath) createZipArchive(t, "zip-source", archivePrefix.Name(), false) diff --git a/syft/source/source_test.go b/syft/source/source_test.go index 36880eb40c6..df550c595c4 100644 --- a/syft/source/source_test.go +++ b/syft/source/source_test.go @@ -902,14 +902,6 @@ func setupArchiveTest(t testing.TB, sourceDirPath string, layer2 bool) string { t.Logf("archive path: %s", destinationArchiveFilePath) createArchive(t, sourceDirPath, destinationArchiveFilePath, layer2) - t.Cleanup( - assertNoError(t, - func() error { - return os.Remove(destinationArchiveFilePath) - }, - ), - ) - cwd, err := os.Getwd() require.NoError(t, err) From ba38b97874dbcc96d3ce2d60d84d54401b105e57 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Tue, 23 May 2023 10:03:15 +0800 Subject: [PATCH 4/7] This check remain Signed-off-by: guoguangwu --- syft/pkg/cataloger/internal/unionreader/union_reader_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/syft/pkg/cataloger/internal/unionreader/union_reader_test.go b/syft/pkg/cataloger/internal/unionreader/union_reader_test.go index e2a3c5fa270..15c67459aa1 100644 --- a/syft/pkg/cataloger/internal/unionreader/union_reader_test.go +++ b/syft/pkg/cataloger/internal/unionreader/union_reader_test.go @@ -20,6 +20,9 @@ func Test_getUnionReader_notUnionReader(t *testing.T) { actual, err := GetUnionReader(reader) require.NoError(t, err) + _, ok = actual.(UnionReader) + require.True(t, ok) + b, err := io.ReadAll(actual) require.NoError(t, err) From 4a2b9cc88a5e3380f03eee4b386841d786fae2ab Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 5 Jun 2023 12:00:25 -0400 Subject: [PATCH 5/7] chore: revert XCOFF changes Signed-off-by: Christopher Phillips --- internal/file/zip_file_traversal_test.go | 6 ++--- .../cataloger/golang/internal/xcoff/file.go | 26 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/internal/file/zip_file_traversal_test.go b/internal/file/zip_file_traversal_test.go index 626b7f0a2a8..d5a81d27309 100644 --- a/internal/file/zip_file_traversal_test.go +++ b/internal/file/zip_file_traversal_test.go @@ -163,7 +163,7 @@ func prependZipSourceFixtureWithString(tb testing.TB, value string) func(tb test archivePath := prepZipSourceFixture(t) // create a temp file - tmpFile, err := os.CreateTemp("", "syft-ziputil-prependZipSourceFixtureWithString-") + tmpFile, err := os.CreateTemp(tb.TempDir(), "syft-ziputil-prependZipSourceFixtureWithString-") if err != nil { t.Fatalf("unable to create tempfile: %+v", err) } @@ -205,11 +205,11 @@ func prepZipSourceFixture(t testing.TB) string { archivePrefix := path.Join(t.TempDir(), "syft-ziputil-prepZipSourceFixture-") // the zip utility will add ".zip" to the end of the given name - archivePath := archivePrefix.Name() + ".zip" + archivePath := archivePrefix + ".zip" t.Logf("archive path: %s", archivePath) - createZipArchive(t, "zip-source", archivePrefix.Name(), false) + createZipArchive(t, "zip-source", archivePrefix, false) return archivePath } diff --git a/syft/pkg/cataloger/golang/internal/xcoff/file.go b/syft/pkg/cataloger/golang/internal/xcoff/file.go index 7d801e8cf93..e4eb8876a74 100644 --- a/syft/pkg/cataloger/golang/internal/xcoff/file.go +++ b/syft/pkg/cataloger/golang/internal/xcoff/file.go @@ -168,7 +168,7 @@ func NewFile(r io.ReaderAt) (*File, error) { f.TargetMachine = magic // Read XCOFF file header - if _, err := sr.Seek(0, io.SeekStart); err != nil { + if _, err := sr.Seek(0, os.SEEK_SET); err != nil { return nil, err } var nscns uint16 @@ -205,7 +205,7 @@ func NewFile(r io.ReaderAt) (*File, error) { // Read string table (located right after symbol table). offset := symptr + uint64(nsyms)*SYMESZ - if _, err := sr.Seek(int64(offset), io.SeekStart); err != nil { + if _, err := sr.Seek(int64(offset), os.SEEK_SET); err != nil { return nil, err } // The first 4 bytes contain the length (in bytes). @@ -214,7 +214,7 @@ func NewFile(r io.ReaderAt) (*File, error) { return nil, err } if l > 4 { - if _, err := sr.Seek(int64(offset), io.SeekStart); err != nil { + if _, err := sr.Seek(int64(offset), os.SEEK_SET); err != nil { return nil, err } f.StringTable = make([]byte, l) @@ -224,7 +224,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } // Read section headers - if _, err := sr.Seek(int64(hdrsz)+int64(opthdr), io.SeekStart); err != nil { + if _, err := sr.Seek(int64(hdrsz)+int64(opthdr), os.SEEK_SET); err != nil { return nil, err } f.Sections = make([]*Section, nscns) @@ -270,7 +270,7 @@ func NewFile(r io.ReaderAt) (*File, error) { var idxToSym = make(map[int]*Symbol) // Read symbol table - if _, err := sr.Seek(int64(symptr), io.SeekStart); err != nil { + if _, err := sr.Seek(int64(symptr), os.SEEK_SET); err != nil { return nil, err } f.Symbols = make([]*Symbol, 0) @@ -356,7 +356,7 @@ func NewFile(r io.ReaderAt) (*File, error) { // Read csect auxiliary entry (by convention, it is the last). if !needAuxFcn { - if _, err := sr.Seek(int64(numaux-1)*SYMESZ, io.SeekStart); err != nil { + if _, err := sr.Seek(int64(numaux-1)*SYMESZ, os.SEEK_CUR); err != nil { return nil, err } } @@ -383,7 +383,7 @@ func NewFile(r io.ReaderAt) (*File, error) { f.Symbols = append(f.Symbols, sym) skip: i += numaux // Skip auxiliary entries - if _, err := sr.Seek(int64(numaux)*SYMESZ, io.SeekStart); err != nil { + if _, err := sr.Seek(int64(numaux)*SYMESZ, os.SEEK_CUR); err != nil { return nil, err } } @@ -398,7 +398,7 @@ func NewFile(r io.ReaderAt) (*File, error) { if sect.Relptr == 0 { continue } - if _, err := sr.Seek(int64(sect.Relptr), io.SeekStart); err != nil { + if _, err := sr.Seek(int64(sect.Relptr), os.SEEK_SET); err != nil { return nil, err } for i := uint32(0); i < sect.Nreloc; i++ { @@ -509,7 +509,7 @@ func (s *Section) Data() ([]byte, error) { // Library name pattern is either path/base/member or base/member func (f *File) readImportIDs(s *Section) ([]string, error) { // Read loader header - if _, err := s.sr.Seek(0, io.SeekStart); err != nil { + if _, err := s.sr.Seek(0, os.SEEK_SET); err != nil { return nil, err } var istlen uint32 @@ -535,7 +535,7 @@ func (f *File) readImportIDs(s *Section) ([]string, error) { } // Read loader import file ID table - if _, err := s.sr.Seek(int64(impoff), io.SeekStart); err != nil { + if _, err := s.sr.Seek(int64(impoff), os.SEEK_SET); err != nil { return nil, err } table := make([]byte, istlen) @@ -578,7 +578,7 @@ func (f *File) ImportedSymbols() ([]ImportedSymbol, error) { return nil, nil } // Read loader header - if _, err := s.sr.Seek(0, io.SeekStart); err != nil { + if _, err := s.sr.Seek(0, os.SEEK_SET); err != nil { return nil, err } var stlen uint32 @@ -607,7 +607,7 @@ func (f *File) ImportedSymbols() ([]ImportedSymbol, error) { } // Read loader section string table - if _, err := s.sr.Seek(int64(stoff), io.SeekStart); err != nil { + if _, err := s.sr.Seek(int64(stoff), os.SEEK_SET); err != nil { return nil, err } st := make([]byte, stlen) @@ -622,7 +622,7 @@ func (f *File) ImportedSymbols() ([]ImportedSymbol, error) { } // Read loader symbol table - if _, err := s.sr.Seek(int64(symoff), io.SeekStart); err != nil { + if _, err := s.sr.Seek(int64(symoff), os.SEEK_SET); err != nil { return nil, err } all := make([]ImportedSymbol, 0) From b44bbbc04d5a55bb1f9f681f649559708b20bab6 Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 5 Jun 2023 12:34:24 -0400 Subject: [PATCH 6/7] chore: lint-fix Signed-off-by: Christopher Phillips --- internal/file/zip_file_helpers_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/file/zip_file_helpers_test.go b/internal/file/zip_file_helpers_test.go index e398449dd13..a94304cca06 100644 --- a/internal/file/zip_file_helpers_test.go +++ b/internal/file/zip_file_helpers_test.go @@ -77,7 +77,6 @@ func setupZipFileTest(t testing.TB, sourceDirPath string, zip64 bool) string { t.Logf("archive path: %s", destinationArchiveFilePath) createZipArchive(t, sourceDirPath, destinationArchiveFilePath, zip64) - cwd, err := os.Getwd() if err != nil { t.Fatalf("unable to get cwd: %+v", err) From e9d3fb140a5252c14ecffb83c916640e021be4f4 Mon Sep 17 00:00:00 2001 From: Christopher Phillips Date: Mon, 5 Jun 2023 12:52:52 -0400 Subject: [PATCH 7/7] chore: remove cleanup Signed-off-by: Christopher Phillips --- syft/source/source_test.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/syft/source/source_test.go b/syft/source/source_test.go index 4b8e2369320..bfa085d09a6 100644 --- a/syft/source/source_test.go +++ b/syft/source/source_test.go @@ -901,14 +901,6 @@ func setupArchiveTest(t testing.TB, sourceDirPath string, layer2 bool) string { archivePrefix, err := os.CreateTemp(t.TempDir(), "syft-archive-TEST-") require.NoError(t, err) - t.Cleanup( - assertNoError(t, - func() error { - return os.Remove(archivePrefix.Name()) - }, - ), - ) - destinationArchiveFilePath := archivePrefix.Name() + ".tar" t.Logf("archive path: %s", destinationArchiveFilePath) createArchive(t, sourceDirPath, destinationArchiveFilePath, layer2)