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 7a76be3b01a..a94304cca06 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,31 +72,11 @@ 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-") - 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) - t.Cleanup( - assertNoError(t, - func() error { - return os.Remove(destinationArchiveFilePath) - }, - ), - ) - cwd, err := os.Getwd() if err != nil { t.Fatalf("unable to get cwd: %+v", err) diff --git a/internal/file/zip_file_traversal_test.go b/internal/file/zip_file_traversal_test.go index d2bff824438..d5a81d27309 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(tb.TempDir(), "syft-ziputil-prependZipSourceFixtureWithString-") if err != nil { t.Fatalf("unable to create tempfile: %+v", err) } @@ -209,25 +202,14 @@ 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-") - 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" - - t.Cleanup(func() { - assert.NoError(t, os.Remove(archivePath)) - }) + 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/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/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/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/source_test.go b/syft/source/source_test.go index 66f3dc955ef..bfa085d09a6 100644 --- a/syft/source/source_test.go +++ b/syft/source/source_test.go @@ -898,29 +898,13 @@ 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-") + 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) - t.Cleanup( - assertNoError(t, - func() error { - return os.Remove(destinationArchiveFilePath) - }, - ), - ) - cwd, err := os.Getwd() require.NoError(t, err) 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)