mirror of
https://github.com/anchore/syft.git
synced 2026-02-12 10:36:45 +01:00
Pr 1825 (#1865)
chore: code cleanup Signed-off-by: guoguangwu <guoguangwu@magic-shield.com> --------- Signed-off-by: guoguangwu <guoguangwu@magic-shield.com> Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com> Co-authored-by: guoguangwu <guoguangwu@magic-shield.com>
This commit is contained in:
parent
d676e5e781
commit
f07581f504
@ -32,12 +32,12 @@ func ExtractGlobsFromTarToUniqueTempFile(archivePath, dir string, globs ...strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we have a file we want to extract....
|
// we have a file we want to extract....
|
||||||
tempfilePrefix := filepath.Base(filepath.Clean(file.Name())) + "-"
|
tempFilePrefix := filepath.Base(filepath.Clean(file.Name())) + "-"
|
||||||
tempFile, err := os.CreateTemp(dir, tempfilePrefix)
|
tempFile, err := os.CreateTemp(dir, tempFilePrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create temp file: %w", err)
|
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
|
// 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.
|
// provides a ReadCloser. It is up to the caller to handle closing the file explicitly.
|
||||||
defer tempFile.Close()
|
defer tempFile.Close()
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
@ -73,31 +72,11 @@ func assertNoError(t testing.TB, fn func() error) func() {
|
|||||||
func setupZipFileTest(t testing.TB, sourceDirPath string, zip64 bool) string {
|
func setupZipFileTest(t testing.TB, sourceDirPath string, zip64 bool) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
archivePrefix, err := ioutil.TempFile("", "syft-ziputil-archive-TEST-")
|
archivePrefix := path.Join(t.TempDir(), "syft-ziputil-archive-TEST-")
|
||||||
if err != nil {
|
destinationArchiveFilePath := archivePrefix + ".zip"
|
||||||
t.Fatalf("unable to create tempfile: %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Cleanup(
|
|
||||||
assertNoError(t,
|
|
||||||
func() error {
|
|
||||||
return os.Remove(archivePrefix.Name())
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
destinationArchiveFilePath := archivePrefix.Name() + ".zip"
|
|
||||||
t.Logf("archive path: %s", destinationArchiveFilePath)
|
t.Logf("archive path: %s", destinationArchiveFilePath)
|
||||||
createZipArchive(t, sourceDirPath, destinationArchiveFilePath, zip64)
|
createZipArchive(t, sourceDirPath, destinationArchiveFilePath, zip64)
|
||||||
|
|
||||||
t.Cleanup(
|
|
||||||
assertNoError(t,
|
|
||||||
func() error {
|
|
||||||
return os.Remove(destinationArchiveFilePath)
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to get cwd: %+v", err)
|
t.Fatalf("unable to get cwd: %+v", err)
|
||||||
|
|||||||
@ -9,7 +9,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -49,13 +48,7 @@ func TestUnzipToDir(t *testing.T) {
|
|||||||
sourceDirPath := path.Join(goldenRootDir, "zip-source")
|
sourceDirPath := path.Join(goldenRootDir, "zip-source")
|
||||||
archiveFilePath := setupZipFileTest(t, sourceDirPath, false)
|
archiveFilePath := setupZipFileTest(t, sourceDirPath, false)
|
||||||
|
|
||||||
unzipDestinationDir, err := ioutil.TempDir("", "syft-ziputil-contents-TEST-")
|
unzipDestinationDir := t.TempDir()
|
||||||
t.Cleanup(assertNoError(t, func() error {
|
|
||||||
return os.RemoveAll(unzipDestinationDir)
|
|
||||||
}))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create tempdir: %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("content path: %s", unzipDestinationDir)
|
t.Logf("content path: %s", unzipDestinationDir)
|
||||||
|
|
||||||
@ -170,7 +163,7 @@ func prependZipSourceFixtureWithString(tb testing.TB, value string) func(tb test
|
|||||||
archivePath := prepZipSourceFixture(t)
|
archivePath := prepZipSourceFixture(t)
|
||||||
|
|
||||||
// create a temp file
|
// create a temp file
|
||||||
tmpFile, err := ioutil.TempFile("", "syft-ziputil-prependZipSourceFixtureWithString-")
|
tmpFile, err := os.CreateTemp(tb.TempDir(), "syft-ziputil-prependZipSourceFixtureWithString-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create tempfile: %+v", err)
|
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 {
|
func prepZipSourceFixture(t testing.TB) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
archivePrefix, err := ioutil.TempFile("", "syft-ziputil-prepZipSourceFixture-")
|
archivePrefix := path.Join(t.TempDir(), "syft-ziputil-prepZipSourceFixture-")
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create tempfile: %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Cleanup(func() {
|
|
||||||
assert.NoError(t, os.Remove(archivePrefix.Name()))
|
|
||||||
})
|
|
||||||
|
|
||||||
// the zip utility will add ".zip" to the end of the given name
|
// the zip utility will add ".zip" to the end of the given name
|
||||||
archivePath := archivePrefix.Name() + ".zip"
|
archivePath := archivePrefix + ".zip"
|
||||||
|
|
||||||
t.Cleanup(func() {
|
|
||||||
assert.NoError(t, os.Remove(archivePath))
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Logf("archive path: %s", archivePath)
|
t.Logf("archive path: %s", archivePath)
|
||||||
|
|
||||||
createZipArchive(t, "zip-source", archivePrefix.Name(), false)
|
createZipArchive(t, "zip-source", archivePrefix, false)
|
||||||
|
|
||||||
return archivePath
|
return archivePath
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package cpe
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -80,12 +80,12 @@ func Test_normalizeCpeField(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_CPEParser(t *testing.T) {
|
func Test_CPEParser(t *testing.T) {
|
||||||
testCases := []struct {
|
var testCases []struct {
|
||||||
CPEString string `json:"cpe-string"`
|
CPEString string `json:"cpe-string"`
|
||||||
CPEUrl string `json:"cpe-url"`
|
CPEUrl string `json:"cpe-url"`
|
||||||
WFN CPE `json:"wfn"`
|
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, err)
|
||||||
require.NoError(t, json.Unmarshal(out, &testCases))
|
require.NoError(t, json.Unmarshal(out, &testCases))
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package linux
|
package linux
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -539,7 +539,7 @@ func retrieveFixtureContentsAsString(fixturePath string, t *testing.T) string {
|
|||||||
}
|
}
|
||||||
defer fixture.Close()
|
defer fixture.Close()
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(fixture)
|
b, err := io.ReadAll(fixture)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to read fixture file: %+v", err)
|
t.Fatalf("unable to read fixture file: %+v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,6 @@ import (
|
|||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
@ -36,7 +35,7 @@ func TestParseNativeImage(t *testing.T) {
|
|||||||
t.Run(test.fixture, func(t *testing.T) {
|
t.Run(test.fixture, func(t *testing.T) {
|
||||||
f, err := os.Open("test-fixtures/java-builds/packages/" + test.fixture)
|
f, err := os.Open("test-fixtures/java-builds/packages/" + test.fixture)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
readerCloser := io.ReadCloser(ioutil.NopCloser(f))
|
readerCloser := io.NopCloser(f)
|
||||||
reader, err := unionreader.GetUnionReader(readerCloser)
|
reader, err := unionreader.GetUnionReader(readerCloser)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
parsed := false
|
parsed := false
|
||||||
@ -107,7 +106,7 @@ func TestParseNativeImageSbom(t *testing.T) {
|
|||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(path.Base(test.fixture), func(t *testing.T) {
|
t.Run(path.Base(test.fixture), func(t *testing.T) {
|
||||||
// Create a buffer to resemble a compressed SBOM in a native image.
|
// 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)
|
assert.NoError(t, err)
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
writebytes := bufio.NewWriter(&b)
|
writebytes := bufio.NewWriter(&b)
|
||||||
|
|||||||
@ -898,29 +898,13 @@ func createArchive(t testing.TB, sourceDirPath, destinationArchivePath string, l
|
|||||||
func setupArchiveTest(t testing.TB, sourceDirPath string, layer2 bool) string {
|
func setupArchiveTest(t testing.TB, sourceDirPath string, layer2 bool) string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
archivePrefix, err := os.CreateTemp("", "syft-archive-TEST-")
|
archivePrefix, err := os.CreateTemp(t.TempDir(), "syft-archive-TEST-")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
t.Cleanup(
|
|
||||||
assertNoError(t,
|
|
||||||
func() error {
|
|
||||||
return os.Remove(archivePrefix.Name())
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
destinationArchiveFilePath := archivePrefix.Name() + ".tar"
|
destinationArchiveFilePath := archivePrefix.Name() + ".tar"
|
||||||
t.Logf("archive path: %s", destinationArchiveFilePath)
|
t.Logf("archive path: %s", destinationArchiveFilePath)
|
||||||
createArchive(t, sourceDirPath, destinationArchiveFilePath, layer2)
|
createArchive(t, sourceDirPath, destinationArchiveFilePath, layer2)
|
||||||
|
|
||||||
t.Cleanup(
|
|
||||||
assertNoError(t,
|
|
||||||
func() error {
|
|
||||||
return os.Remove(destinationArchiveFilePath)
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
|||||||
@ -44,9 +44,7 @@ func TestValidCycloneDX(t *testing.T) {
|
|||||||
args := []string{
|
args := []string{
|
||||||
test.subcommand, fixtureRef, "-q",
|
test.subcommand, fixtureRef, "-q",
|
||||||
}
|
}
|
||||||
for _, a := range test.args {
|
args = append(args, test.args...)
|
||||||
args = append(args, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd, stdout, stderr := runSyft(t, nil, args...)
|
cmd, stdout, stderr := runSyft(t, nil, args...)
|
||||||
for _, traitFn := range test.assertions {
|
for _, traitFn := range test.assertions {
|
||||||
|
|||||||
@ -57,9 +57,7 @@ func TestJSONSchema(t *testing.T) {
|
|||||||
args := []string{
|
args := []string{
|
||||||
test.subcommand, fixtureRef, "-q",
|
test.subcommand, fixtureRef, "-q",
|
||||||
}
|
}
|
||||||
for _, a := range test.args {
|
args = append(args, test.args...)
|
||||||
args = append(args, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, stdout, stderr := runSyft(t, nil, args...)
|
_, stdout, stderr := runSyft(t, nil, args...)
|
||||||
|
|
||||||
|
|||||||
@ -50,9 +50,7 @@ func TestSPDXJSONSchema(t *testing.T) {
|
|||||||
args := []string{
|
args := []string{
|
||||||
test.subcommand, fixtureRef, "-q",
|
test.subcommand, fixtureRef, "-q",
|
||||||
}
|
}
|
||||||
for _, a := range test.args {
|
args = append(args, test.args...)
|
||||||
args = append(args, a)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, stdout, _ := runSyft(t, nil, args...)
|
_, stdout, _ := runSyft(t, nil, args...)
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package integration
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -64,7 +63,7 @@ func TestEncodeDecodeEncodeCycleComparison(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
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 {
|
for _, image := range images {
|
||||||
originalSBOM, _ := catalogFixtureImage(t, image, source.SquashedScope, nil)
|
originalSBOM, _ := catalogFixtureImage(t, image, source.SquashedScope, nil)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user