From 48b918b0b3746dbc8a62bc15fe69ea2a454cb5ef Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 21 Apr 2026 18:22:30 +0100 Subject: [PATCH] Backport of Make `validation -json` command produce JSON output when argument parsing raises an error into v1.15 (#38419) --- internal/command/validate.go | 13 +++++---- internal/command/validate_test.go | 44 ++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/internal/command/validate.go b/internal/command/validate.go index 7a69dbeb28..d37e819945 100644 --- a/internal/command/validate.go +++ b/internal/command/validate.go @@ -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 diff --git a/internal/command/validate_test.go b/internal/command/validate_test.go index 5827ac26a1..ed7df2e738 100644 --- a/internal/command/validate_test.go +++ b/internal/command/validate_test.go @@ -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) {