mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 14:02:26 +01:00
Fix file creation for output options (#875)
This commit is contained in:
@@ -2,6 +2,7 @@ package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -192,6 +193,26 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "json-file-flag",
|
||||
args: []string{"packages", "-o", "json", "--file", filepath.Join(tmp, "output-1.json"), coverageImage},
|
||||
assertions: []traitAssertion{
|
||||
assertSuccessfulReturnCode,
|
||||
assertFileOutput(t, filepath.Join(tmp, "output-1.json"),
|
||||
assertJsonReport,
|
||||
),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "json-output-flag-to-file",
|
||||
args: []string{"packages", "-o", fmt.Sprintf("json=%s", filepath.Join(tmp, "output-2.json")), coverageImage},
|
||||
assertions: []traitAssertion{
|
||||
assertSuccessfulReturnCode,
|
||||
assertFileOutput(t, filepath.Join(tmp, "output-2.json"),
|
||||
assertJsonReport,
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
@@ -10,10 +10,26 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/acarl005/stripansi"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type traitAssertion func(tb testing.TB, stdout, stderr string, rc int)
|
||||
|
||||
func assertFileOutput(tb testing.TB, path string, assertions ...traitAssertion) traitAssertion {
|
||||
tb.Helper()
|
||||
|
||||
return func(tb testing.TB, _, stderr string, rc int) {
|
||||
content, err := os.ReadFile(path)
|
||||
require.NoError(tb, err)
|
||||
contentStr := string(content)
|
||||
|
||||
for _, assertion := range assertions {
|
||||
// treat the file content as stdout
|
||||
assertion(tb, contentStr, stderr, rc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertJsonReport(tb testing.TB, stdout, _ string, _ int) {
|
||||
tb.Helper()
|
||||
var data interface{}
|
||||
|
||||
Reference in New Issue
Block a user