From 1383d7140049d1e2867bbc329fd6822999f7f797 Mon Sep 17 00:00:00 2001 From: MarioCadenas Date: Wed, 18 Mar 2026 15:04:47 +0100 Subject: [PATCH] fix: manifest command stdout --- cmd/apps/init_test.go | 59 +++++++++++++------------------------------ cmd/apps/manifest.go | 9 ++++--- 2 files changed, 23 insertions(+), 45 deletions(-) diff --git a/cmd/apps/init_test.go b/cmd/apps/init_test.go index 66fbc4ea54..5770747310 100644 --- a/cmd/apps/init_test.go +++ b/cmd/apps/init_test.go @@ -1,15 +1,15 @@ package apps import ( - "bytes" + "context" "errors" - "io" "os" "path/filepath" "testing" "github.com/databricks/cli/libs/apps/manifest" "github.com/databricks/cli/libs/apps/prompt" + "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/env" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" @@ -624,44 +624,33 @@ func TestAppendUniqueNoValues(t *testing.T) { assert.Equal(t, []string{"a", "b"}, result) } +func runManifestCommand(t *testing.T, ctx context.Context, args ...string) string { + t.Helper() + + ctx, stdout := cmdio.NewTestContextWithStdout(ctx) + cmd := newManifestCmd() + cmd.SetContext(ctx) + cmd.SetOut(stdout) + cmd.SetArgs(args) + + require.NoError(t, cmd.Execute()) + return stdout.String() +} + func TestRunManifestOnlyFound(t *testing.T) { dir := t.TempDir() manifestPath := filepath.Join(dir, manifest.ManifestFileName) content := `{"$schema":"https://example.com/schema","version":"1.0","plugins":{"analytics":{"name":"analytics","resources":{"required":[],"optional":[]}}}}` require.NoError(t, os.WriteFile(manifestPath, []byte(content), 0o644)) - old := os.Stdout - r, w, err := os.Pipe() - require.NoError(t, err) - os.Stdout = w - - err = runManifestOnly(t.Context(), dir, "", "") - w.Close() - os.Stdout = old - require.NoError(t, err) - - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - out := buf.String() + out := runManifestCommand(t, t.Context(), "--template", dir) assert.Equal(t, content, out) } func TestRunManifestOnlyNotFound(t *testing.T) { dir := t.TempDir() - old := os.Stdout - r, w, err := os.Pipe() - require.NoError(t, err) - os.Stdout = w - - err = runManifestOnly(t.Context(), dir, "", "") - w.Close() - os.Stdout = old - require.NoError(t, err) - - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - out := buf.String() + out := runManifestCommand(t, t.Context(), "--template", dir) assert.Equal(t, "No appkit.plugins.json manifest found in this template.\n", out) } @@ -671,19 +660,7 @@ func TestRunManifestOnlyUsesTemplatePathEnvVar(t *testing.T) { content := `{"version":"1.0","scaffolding":{"command":"databricks apps init"}}` require.NoError(t, os.WriteFile(manifestPath, []byte(content), 0o644)) - old := os.Stdout - r, w, err := os.Pipe() - require.NoError(t, err) - os.Stdout = w - ctx := env.Set(t.Context(), templatePathEnvVar, dir) - err = runManifestOnly(ctx, "", "", "") - w.Close() - os.Stdout = old - require.NoError(t, err) - - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - out := buf.String() + out := runManifestCommand(t, ctx) assert.Equal(t, content, out) } diff --git a/cmd/apps/manifest.go b/cmd/apps/manifest.go index 9b85376964..c27b12577c 100644 --- a/cmd/apps/manifest.go +++ b/cmd/apps/manifest.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io" "os" "path/filepath" @@ -13,7 +14,7 @@ import ( ) // runManifestOnly resolves the template, loads appkit.plugins.json if present, and prints it to stdout (or a message if not found). -func runManifestOnly(ctx context.Context, templatePath, branch, version string) error { +func runManifestOnly(ctx context.Context, out io.Writer, templatePath, branch, version string) error { templateSrc := templatePath if templateSrc == "" { templateSrc = env.Get(ctx, templatePathEnvVar) @@ -59,14 +60,14 @@ func runManifestOnly(ctx context.Context, templatePath, branch, version string) if err != nil { return fmt.Errorf("read manifest: %w", err) } - _, err = os.Stdout.Write(data) + _, err = out.Write(data) if err != nil { return fmt.Errorf("write manifest: %w", err) } return nil } - fmt.Fprintln(os.Stdout, "No appkit.plugins.json manifest found in this template.") + fmt.Fprintln(out, "No appkit.plugins.json manifest found in this template.") return nil } @@ -102,7 +103,7 @@ Examples: if cmd.Flags().Changed("branch") && cmd.Flags().Changed("version") { return errors.New("--branch and --version are mutually exclusive") } - return runManifestOnly(ctx, templatePath, branch, version) + return runManifestOnly(ctx, cmd.OutOrStdout(), templatePath, branch, version) }, }