mirror of
https://github.com/anchore/syft.git
synced 2026-10-31 06:26:30 +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:
@@ -23,6 +23,7 @@ func TestAllFormatsExpressible(t *testing.T) {
|
||||
}
|
||||
|
||||
opts := options.DefaultOutput()
|
||||
require.NoError(t, opts.PostLoad())
|
||||
encoders, err := opts.Encoders()
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
@@ -39,8 +39,8 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
args: []string{"packages", "-o", "json", coverageImage},
|
||||
assertions: []traitAssertion{
|
||||
assertJsonReport,
|
||||
assertInOutput(`"metadataType": "apk-db-entry"`),
|
||||
assertNotInOutput(`"metadataType": "ApkMetadata"`),
|
||||
assertInOutput(`"metadataType":"apk-db-entry"`),
|
||||
assertNotInOutput(`"metadataType":"ApkMetadata"`),
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
@@ -126,8 +126,8 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
},
|
||||
assertions: []traitAssertion{
|
||||
assertJsonReport,
|
||||
assertNotInOutput(`"metadataType": "apk-db-entry"`),
|
||||
assertInOutput(`"metadataType": "ApkMetadata"`),
|
||||
assertNotInOutput(`"metadataType":"apk-db-entry"`),
|
||||
assertInOutput(`"metadataType":"ApkMetadata"`),
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -25,10 +25,10 @@ func TestPowerUserCmdFlags(t *testing.T) {
|
||||
name: "default-results-w-pkg-coverage",
|
||||
args: []string{"power-user", "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")},
|
||||
assertions: []traitAssertion{
|
||||
assertNotInOutput(" command is deprecated"), // only the root command should be deprecated
|
||||
assertInOutput(`"type": "RegularFile"`), // proof of file-metadata data
|
||||
assertInOutput(`"algorithm": "sha256"`), // proof of file-metadata default digest algorithm of sha256
|
||||
assertInOutput(`"metadataType": "apk-db-entry"`), // proof of package artifacts data
|
||||
assertNotInOutput(" command is deprecated"), // only the root command should be deprecated
|
||||
assertInOutput(`"type":"RegularFile"`), // proof of file-metadata data
|
||||
assertInOutput(`"algorithm":"sha256"`), // proof of file-metadata default digest algorithm of sha256
|
||||
assertInOutput(`"metadataType":"apk-db-entry"`), // proof of package artifacts data
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
@@ -39,7 +39,7 @@ func TestPowerUserCmdFlags(t *testing.T) {
|
||||
"SYFT_FILE_CONTENTS_GLOBS": "/api-key.txt",
|
||||
},
|
||||
assertions: []traitAssertion{
|
||||
assertInOutput(`"contents": "c29tZV9BcEkta0V5ID0gIjEyMzQ1QTdhOTAxYjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MCIK"`), // proof of the content cataloger
|
||||
assertInOutput(`"contents":"c29tZV9BcEkta0V5ID0gIjEyMzQ1QTdhOTAxYjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MCIK"`), // proof of the content cataloger
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
@@ -47,10 +47,10 @@ func TestPowerUserCmdFlags(t *testing.T) {
|
||||
name: "default-dir-results-w-pkg-coverage",
|
||||
args: []string{"power-user", "dir:test-fixtures/image-pkg-coverage"},
|
||||
assertions: []traitAssertion{
|
||||
assertNotInOutput(" command is deprecated"), // only the root command should be deprecated
|
||||
assertInOutput(`"type": "RegularFile"`), // proof of file-metadata data
|
||||
assertInOutput(`"algorithm": "sha256"`), // proof of file-metadata default digest algorithm of sha256
|
||||
assertInOutput(`"metadataType": "apk-db-entry"`), // proof of package artifacts data
|
||||
assertNotInOutput(" command is deprecated"), // only the root command should be deprecated
|
||||
assertInOutput(`"type":"RegularFile"`), // proof of file-metadata data
|
||||
assertInOutput(`"algorithm":"sha256"`), // proof of file-metadata default digest algorithm of sha256
|
||||
assertInOutput(`"metadataType":"apk-db-entry"`), // proof of package artifacts data
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
@@ -61,7 +61,7 @@ func TestPowerUserCmdFlags(t *testing.T) {
|
||||
},
|
||||
args: []string{"power-user", "docker-archive:" + secretsFixture},
|
||||
assertions: []traitAssertion{
|
||||
assertInOutput(`"classification": "generic-api-key"`), // proof of the secrets cataloger finding something
|
||||
assertInOutput(`"classification":"generic-api-key"`), // proof of the secrets cataloger finding something
|
||||
assertInOutput(`"12345A7a901b345678901234567890123456789012345678901234567890"`), // proof of the secrets cataloger finding the api key
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
@@ -70,7 +70,7 @@ func TestPowerUserCmdFlags(t *testing.T) {
|
||||
name: "default-secret-results-dont-reveal-values",
|
||||
args: []string{"power-user", "docker-archive:" + secretsFixture},
|
||||
assertions: []traitAssertion{
|
||||
assertInOutput(`"classification": "generic-api-key"`), // proof of the secrets cataloger finding something
|
||||
assertInOutput(`"classification":"generic-api-key"`), // proof of the secrets cataloger finding something
|
||||
assertNotInOutput(`"12345A7a901b345678901234567890123456789012345678901234567890"`), // proof of the secrets cataloger finding the api key
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
@@ -82,7 +82,7 @@ func TestPowerUserCmdFlags(t *testing.T) {
|
||||
},
|
||||
args: []string{"power-user", "dir:test-fixtures/image-secrets-dir"},
|
||||
assertions: []traitAssertion{
|
||||
assertInOutput(`"classification": "generic-api-key"`), // proof of the secrets cataloger finding something
|
||||
assertInOutput(`"classification":"generic-api-key"`), // proof of the secrets cataloger finding something
|
||||
assertInOutput(`"12345A7a901b345678901234567890123456789012345678901234567890"`), // proof of the secrets cataloger finding the api key
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
|
||||
@@ -86,7 +86,10 @@ func assertNotInOutput(data string) traitAssertion {
|
||||
func assertNoStderr(tb testing.TB, _, stderr string, _ int) {
|
||||
tb.Helper()
|
||||
if len(stderr) > 0 {
|
||||
tb.Errorf("expected stderr to be empty, but got %q", stderr)
|
||||
tb.Errorf("expected stderr to be empty, but wasn't")
|
||||
if showOutput != nil && *showOutput {
|
||||
tb.Errorf("STDERR:%s", stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +99,10 @@ func assertInOutput(data string) traitAssertion {
|
||||
stdout = stripansi.Strip(stdout)
|
||||
stderr = stripansi.Strip(stderr)
|
||||
if !strings.Contains(stdout, data) && !strings.Contains(stderr, data) {
|
||||
tb.Errorf("data=%q was NOT found in any output, but should have been there\nSTDOUT:%s\nSTDERR:%s", data, stdout, stderr)
|
||||
tb.Errorf("data=%q was NOT found in any output, but should have been there", data)
|
||||
if showOutput != nil && *showOutput {
|
||||
tb.Errorf("STDOUT:%s\nSTDERR:%s", stdout, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user