Skip to content

Commit

Permalink
Log warnings to stderr for "bundle validate -o json" (#2109)
Browse files Browse the repository at this point in the history
## Changes
Previously diagnostics were not seen in JSON output mode. This change
prints them to stderr.

This also fixes acceptance tests to preprocess all output with
s/execPath/$CLI/ not just output.txt.

## Tests
Existing acceptance tests. In one case I've added non-json command to
check that they match in output.
  • Loading branch information
denik authored Jan 10, 2025
1 parent b0c1c23 commit 6d3b415
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
13 changes: 9 additions & 4 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ func TestAccept(t *testing.T) {
// Do not read user's ~/.databrickscfg
t.Setenv(env.HomeEnvVar(), homeDir)

repls := testdiff.ReplacementsContext{}
repls.Set(execPath, "$CLI")

testDirs := getTests(t)
require.NotEmpty(t, testDirs)
for _, dir := range testDirs {
t.Run(dir, func(t *testing.T) {
t.Parallel()
runTest(t, dir)
runTest(t, dir, repls)
})
}
}
Expand All @@ -85,7 +88,7 @@ func getTests(t *testing.T) []string {
return testDirs
}

func runTest(t *testing.T, dir string) {
func runTest(t *testing.T, dir string, repls testdiff.ReplacementsContext) {
var tmpDir string
var err error
if KeepTmp {
Expand All @@ -112,7 +115,7 @@ func runTest(t *testing.T, dir string) {
outB, err := cmd.CombinedOutput()

out := formatOutput(string(outB), err)
out = strings.ReplaceAll(out, os.Getenv("CLI"), "$CLI")
out = repls.Replace(out)
doComparison(t, filepath.Join(dir, "output.txt"), "script output", out)

for key := range outputs {
Expand All @@ -131,7 +134,8 @@ func runTest(t *testing.T, dir string) {
continue
}
pathExpected := filepath.Join(dir, key)
doComparison(t, pathExpected, pathNew, string(newValBytes))
newVal := repls.Replace(string(newValBytes))
doComparison(t, pathExpected, pathNew, newVal)
}

// Make sure there are not unaccounted for new files
Expand All @@ -152,6 +156,7 @@ func runTest(t *testing.T, dir string) {
// Show the contents & support overwrite mode for it:
pathNew := filepath.Join(tmpDir, name)
newVal := testutil.ReadFile(t, pathNew)
newVal = repls.Replace(newVal)
doComparison(t, filepath.Join(dir, name), filepath.Join(tmpDir, name), newVal)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

>>> errcode $CLI bundle validate -o json -t development
Error: file ./test1.py not found


Exit code: 1
19 changes: 14 additions & 5 deletions acceptance/bundle/override/job_tasks/output.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@

>>> errcode $CLI bundle validate -o json -t development
Error: file ./test1.py not found

Exit code: 1
{
"name": "job",
"queue": {
Expand Down Expand Up @@ -36,6 +31,7 @@ Exit code: 1
>>> errcode $CLI bundle validate -o json -t staging
Error: file ./test1.py not found


Exit code: 1
{
"name": "job",
Expand Down Expand Up @@ -66,3 +62,16 @@ Exit code: 1
}
]
}

>>> errcode $CLI bundle validate -t staging
Error: file ./test1.py not found

Name: override_job_tasks
Target: staging
Workspace:
User: [email protected]
Path: /Workspace/Users/[email protected]/.bundle/override_job_tasks/staging

Found 1 error

Exit code: 1
3 changes: 2 additions & 1 deletion acceptance/bundle/override/job_tasks/script
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
trace errcode $CLI bundle validate -o json -t development | jq .resources.jobs.foo
trace errcode $CLI bundle validate -o json -t development 2> out.development.stderr.txt | jq .resources.jobs.foo
trace errcode $CLI bundle validate -o json -t staging | jq .resources.jobs.foo
trace errcode $CLI bundle validate -t staging
4 changes: 4 additions & 0 deletions acceptance/bundle/override/merge-string-map/output.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

>>> $CLI bundle validate -o json -t dev
Warning: expected map, found string
at resources.clusters.my_cluster
in databricks.yml:6:17

{
"clusters": {
"my_cluster": {
Expand Down
23 changes: 19 additions & 4 deletions cmd/bundle/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ import (
"github.com/databricks/cli/bundle/render"
"github.com/databricks/cli/cmd/bundle/utils"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/flags"
"github.com/spf13/cobra"
)

func renderJsonOutput(cmd *cobra.Command, b *bundle.Bundle, diags diag.Diagnostics) error {
func renderJsonOutput(cmd *cobra.Command, b *bundle.Bundle) error {
buf, err := json.MarshalIndent(b.Config.Value().AsAny(), "", " ")
if err != nil {
return err
}
_, _ = cmd.OutOrStdout().Write(buf)
return diags.Error()
return nil
}

func newValidateCommand() *cobra.Command {
Expand Down Expand Up @@ -66,7 +65,23 @@ func newValidateCommand() *cobra.Command {

return nil
case flags.OutputJSON:
return renderJsonOutput(cmd, b, diags)
renderOpts := render.RenderOptions{RenderSummaryTable: false}
err1 := render.RenderDiagnostics(cmd.ErrOrStderr(), b, diags, renderOpts)
err2 := renderJsonOutput(cmd, b)

if err2 != nil {
return err2
}

if err1 != nil {
return err1
}

if diags.HasError() {
return root.ErrAlreadyPrinted
}

return nil
default:
return fmt.Errorf("unknown output type %s", root.OutputType(cmd))
}
Expand Down

0 comments on commit 6d3b415

Please sign in to comment.