mirror of
https://github.com/anchore/syft.git
synced 2026-11-03 16:06:36 +01:00
Add "pretty" json configuration and change default behavior to be space-efficient (#2275)
* expose underlying format options Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * remove escape html options and address PR feedback Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * incorporate PR feedback Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli test 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:
@@ -3,11 +3,14 @@ package cyclonedxxml
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anchore/syft/syft/format/internal/cyclonedxutil"
|
||||
"github.com/anchore/syft/syft/format/internal/testutil"
|
||||
"github.com/anchore/syft/syft/sbom"
|
||||
)
|
||||
@@ -16,11 +19,42 @@ var updateSnapshot = flag.Bool("update-cyclonedx-xml", false, "update the *.gold
|
||||
var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing")
|
||||
|
||||
func getEncoder(t testing.TB) sbom.FormatEncoder {
|
||||
enc, err := NewFormatEncoderWithConfig(DefaultEncoderConfig())
|
||||
cfg := DefaultEncoderConfig()
|
||||
cfg.Pretty = true
|
||||
|
||||
enc, err := NewFormatEncoderWithConfig(cfg)
|
||||
require.NoError(t, err)
|
||||
return enc
|
||||
}
|
||||
|
||||
func TestPrettyOutput(t *testing.T) {
|
||||
enc, err := NewFormatEncoderWithConfig(EncoderConfig{
|
||||
Version: cyclonedxutil.DefaultVersion,
|
||||
Pretty: false,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
dir := t.TempDir()
|
||||
s := testutil.DirectoryInput(t, dir)
|
||||
|
||||
var buffer bytes.Buffer
|
||||
err = enc.Encode(&buffer, s)
|
||||
require.NoError(t, err)
|
||||
|
||||
actual := buffer.String()
|
||||
lines := strings.Split(actual, "\n")
|
||||
require.NotEmpty(t, lines)
|
||||
whitespace := regexp.MustCompile(`^\s+`)
|
||||
for _, line := range lines {
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// require a non-whitespace character (tab, space, etc) as the first character of the line
|
||||
require.False(t, whitespace.Match([]byte(line)), "line should not start with whitespace: %q", line)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCycloneDxDirectoryEncoder(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
testutil.AssertEncoderAgainstGoldenSnapshot(t,
|
||||
|
||||
Reference in New Issue
Block a user