Backport of Make `validation -json` command produce JSON output when argument parsing raises an error into v1.15 (#38419)

pull/38421/head
github-actions[bot] 4 weeks ago committed by GitHub
parent 91fc323672
commit 48b918b0b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -38,15 +38,18 @@ func (c *ValidateCommand) Run(rawArgs []string) int {
// Parse and validate flags
args, diags := arguments.ParseValidate(rawArgs)
if diags.HasErrors() {
c.View.Diagnostics(diags)
c.View.HelpPrompt("validate")
return 1
}
c.ParsedArgs = args
view := views.NewValidate(args.ViewType, c.View)
// Now the view is ready, process any diagnostics from argument parsing
if diags.HasErrors() {
if args.ViewType == arguments.ViewHuman {
defer c.View.HelpPrompt("validate")
}
return view.Results(diags)
}
// If the query flag is set, include query files in the validation.
c.includeQueryFiles = c.ParsedArgs.Query

@ -578,19 +578,43 @@ func TestValidate_json(t *testing.T) {
}
func TestValidate_ensure_json_diags(t *testing.T) {
output, code := setupTest(t, "validate-invalid", "-json", "-var", "foo")
t.Run("-var error diags are rendered using the correct view ", func(t *testing.T) {
output, code := setupTest(t, "validate-invalid", "-json", "-var", "foo")
if code != 1 {
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
}
if code != 1 {
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
}
if output.Stderr() != "" {
t.Fatalf("Expected output, all json output should go to stdout, got stderr: %s", output.Stderr())
}
gotString := output.Stdout()
gotString := output.Stdout()
var got map[string]any
err := json.Unmarshal([]byte(gotString), &got)
if err != nil {
t.Fatalf("Test did not produce valid JSON: %s", err)
}
var got map[string]any
err := json.Unmarshal([]byte(gotString), &got)
if err != nil {
t.Fatalf("Test did not produce valid JSON: %s", err)
}
})
t.Run("Flag/argument parsing error diags are rendered using the correct view ", func(t *testing.T) {
output, code := setupTest(t, "validate-invalid", "-json", "-foobar")
if code != 1 {
t.Fatalf("Should have failed: %d\n\n%s", code, output.Stderr())
}
if output.Stderr() != "" {
t.Fatalf("Expected output, all json output should go to stdout, got stderr: %s", output.Stderr())
}
gotString := output.Stdout()
var got map[string]any
err := json.Unmarshal([]byte(gotString), &got)
if err != nil {
t.Fatalf("Test did not produce valid JSON: %s", err)
}
})
}
func TestValidateWithInvalidListResource(t *testing.T) {

Loading…
Cancel
Save