|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + renderapi "github.com/render-oss/cli/internal/fakes/renderapi" |
| 11 | + "github.com/render-oss/cli/pkg/client" |
| 12 | + "github.com/render-oss/cli/pkg/command" |
| 13 | + "github.com/render-oss/cli/pkg/dependencies" |
| 14 | +) |
| 15 | + |
| 16 | +// executeSandboxCommand runs the CLI with the sandbox tree registered under |
| 17 | +// ea, mirroring executeSandboxGroupsCommand in sandboxgroups_test.go. |
| 18 | +func executeSandboxCommand(t *testing.T, server *renderapi.Server, args ...string) (CommandResult, error) { |
| 19 | + t.Helper() |
| 20 | + t.Setenv("RENDER_CLI_CONFIG_PATH", newTestConfigPath(t)) |
| 21 | + t.Setenv("RENDER_API_KEY", "test-api-key") |
| 22 | + |
| 23 | + c, err := client.NewClientWithResponses(server.URL()) |
| 24 | + require.NoError(t, err) |
| 25 | + deps := dependencies.New(c) |
| 26 | + deps.DetectRuntimeSignals = func() (command.RuntimeSignals, error) { |
| 27 | + return command.RuntimeSignals{StdinTTY: false, StdoutTTY: false, StderrTTY: false}, nil |
| 28 | + } |
| 29 | + |
| 30 | + root := newRootCmd() |
| 31 | + ea := newEarlyAccessCmd() |
| 32 | + root.AddCommand(ea) |
| 33 | + setupSandboxCommands(ea, deps) |
| 34 | + setupRootCmdPersistentRun(root, deps) |
| 35 | + |
| 36 | + var stdout, stderr bytes.Buffer |
| 37 | + root.SetOut(&stdout) |
| 38 | + root.SetErr(&stderr) |
| 39 | + root.SetArgs(args) |
| 40 | + |
| 41 | + execErr := root.Execute() |
| 42 | + return CommandResult{Stdout: stdout.String(), Stderr: stderr.String()}, execErr |
| 43 | +} |
| 44 | + |
| 45 | +// The parent command is plural per the PRD CLI spec and STYLE.md's |
| 46 | +// plural-resource rule. |
| 47 | +func TestSandboxes_PluralCommandResolves(t *testing.T) { |
| 48 | + server := renderapi.NewServer(t) |
| 49 | + |
| 50 | + result, err := executeSandboxCommand(t, server, "ea", "sandboxes", "--help") |
| 51 | + require.NoError(t, err) |
| 52 | + assert.Contains(t, result.Stdout, "Manage sandboxes") |
| 53 | + assert.Contains(t, result.Stdout, "render ea sandboxes") |
| 54 | +} |
| 55 | + |
| 56 | +// The singular form is removed outright (no alias), per the #sandboxes-core |
| 57 | +// decision that pre-alpha backwards compatibility is not needed. This test |
| 58 | +// locks that in: if someone reintroduces "sandbox" as a name or alias, this |
| 59 | +// fails and forces a deliberate decision. |
| 60 | +// |
| 61 | +// No --help here on purpose: the help flag short-circuits before Cobra |
| 62 | +// validates args, so "ea sandbox --help" would print ea's help with no error. |
| 63 | +// The unknown-command error comes from ea's cobra.NoArgs validator. |
| 64 | +func TestSandboxes_SingularFormRemoved(t *testing.T) { |
| 65 | + server := renderapi.NewServer(t) |
| 66 | + |
| 67 | + _, err := executeSandboxCommand(t, server, "ea", "sandbox") |
| 68 | + require.Error(t, err) |
| 69 | + assert.Contains(t, err.Error(), "unknown command") |
| 70 | +} |
| 71 | + |
| 72 | +func TestSandboxes_HelpListsSubcommands(t *testing.T) { |
| 73 | + server := renderapi.NewServer(t) |
| 74 | + |
| 75 | + result, err := executeSandboxCommand(t, server, "ea", "sandboxes", "--help") |
| 76 | + require.NoError(t, err) |
| 77 | + for _, sub := range []string{"create", "exec", "list", "stop"} { |
| 78 | + assert.Contains(t, result.Stdout, sub, "expected subcommand %q in help output", sub) |
| 79 | + } |
| 80 | +} |
0 commit comments