Fix file creation for output options (#875)

This commit is contained in:
Alex Goodman
2022-03-08 15:37:28 +00:00
committed by GitHub
parent 5123f073c7
commit 07d3c9af52
12 changed files with 190 additions and 20 deletions
+21
View File
@@ -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 {
+16
View File
@@ -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{}