Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 18 additions & 41 deletions cmd/apps/init_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}
9 changes: 5 additions & 4 deletions cmd/apps/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"

Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
},
}

Expand Down
Loading