mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 14:02:26 +01:00
Internalize majority of cmd package (#2533)
* internalize majority of cmd package and migrate integration tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add internal api encoder Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * create internal representation of all formats Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * export capability to get default encoders Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * restore test fixtures Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anchore/syft/cmd/syft/cli/options"
|
||||
"github.com/anchore/syft/syft/format"
|
||||
"github.com/anchore/syft/syft/format/template"
|
||||
)
|
||||
@@ -22,12 +21,7 @@ func TestAllFormatsExpressible(t *testing.T) {
|
||||
assertSuccessfulReturnCode,
|
||||
}
|
||||
|
||||
opts := options.DefaultOutput()
|
||||
require.NoError(t, opts.PostLoad())
|
||||
encoders, err := opts.Encoders()
|
||||
require.NoError(t, err)
|
||||
|
||||
encs := format.NewEncoderCollection(encoders...)
|
||||
encs := format.NewEncoderCollection(format.Encoders()...)
|
||||
formatIDs := encs.IDs()
|
||||
require.NotEmpty(t, formatIDs)
|
||||
for _, o := range formatIDs {
|
||||
|
||||
@@ -4,19 +4,32 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anchore/syft/syft/format"
|
||||
"github.com/anchore/syft/syft/format/cyclonedxjson"
|
||||
"github.com/anchore/syft/syft/format/cyclonedxxml"
|
||||
"github.com/anchore/syft/syft/format/spdxjson"
|
||||
"github.com/anchore/syft/syft/format/spdxtagvalue"
|
||||
"github.com/anchore/syft/syft/sbom"
|
||||
)
|
||||
|
||||
func TestConvertCmd(t *testing.T) {
|
||||
assertions := []traitAssertion{
|
||||
assertInOutput("PackageName: musl-utils"),
|
||||
assertInOutput("musl-utils"),
|
||||
assertSuccessfulReturnCode,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
from string
|
||||
to string
|
||||
from string
|
||||
to string
|
||||
expect sbom.FormatEncoder
|
||||
}{
|
||||
{from: "syft-json", to: "spdx-tag-value"},
|
||||
{from: "syft-json", to: "spdx-tag-value", expect: mustEncoder(spdxtagvalue.NewFormatEncoderWithConfig(spdxtagvalue.DefaultEncoderConfig()))},
|
||||
{from: "syft-json", to: "spdx-json", expect: mustEncoder(spdxjson.NewFormatEncoderWithConfig(spdxjson.DefaultEncoderConfig()))},
|
||||
{from: "syft-json", to: "cyclonedx-json", expect: mustEncoder(cyclonedxjson.NewFormatEncoderWithConfig(cyclonedxjson.DefaultEncoderConfig()))},
|
||||
{from: "syft-json", to: "cyclonedx-xml", expect: mustEncoder(cyclonedxxml.NewFormatEncoderWithConfig(cyclonedxxml.DefaultEncoderConfig()))},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@@ -41,6 +54,18 @@ func TestConvertCmd(t *testing.T) {
|
||||
traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
|
||||
}
|
||||
logOutputOnFailure(t, cmd, stdout, stderr)
|
||||
|
||||
// let's make sure the output is valid relative to the expected format
|
||||
foundID, _ := format.Identify(strings.NewReader(stdout))
|
||||
require.Equal(t, test.expect.ID(), foundID)
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func mustEncoder(enc sbom.FormatEncoder, err error) sbom.FormatEncoder {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return enc
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
../../integration/test-fixtures/image-pkg-coverage
|
||||
../../../cmd/syft/internal/test/integration/test-fixtures/image-pkg-coverage
|
||||
Reference in New Issue
Block a user