|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "gopkg.in/yaml.v3" |
| 11 | + |
| 12 | + sandboxclient "github.com/render-oss/cli/pkg/client/sandboxes" |
| 13 | + "github.com/render-oss/cli/pkg/command" |
| 14 | +) |
| 15 | + |
| 16 | +func TestWriteSandboxExecResultRawOutput(t *testing.T) { |
| 17 | + cmd, stdout, stderr := newSandboxExecTestCommand(command.TEXT) |
| 18 | + result := &sandboxclient.SandboxExecSyncResponse{ |
| 19 | + Stdout: "hello\n", |
| 20 | + Stderr: "warning\n", |
| 21 | + ExitCode: 7, |
| 22 | + } |
| 23 | + |
| 24 | + require.NoError(t, writeSandboxExecResult(cmd, result)) |
| 25 | + require.Equal(t, "hello\n", stdout.String()) |
| 26 | + require.Equal(t, "warning\n", stderr.String()) |
| 27 | +} |
| 28 | + |
| 29 | +func TestWriteSandboxExecResultJSONOutput(t *testing.T) { |
| 30 | + cmd, stdout, stderr := newSandboxExecTestCommand(command.JSON) |
| 31 | + result := &sandboxclient.SandboxExecSyncResponse{ |
| 32 | + Stdout: "hello\n", |
| 33 | + Stderr: "warning\n", |
| 34 | + ExitCode: 7, |
| 35 | + } |
| 36 | + |
| 37 | + require.NoError(t, writeSandboxExecResult(cmd, result)) |
| 38 | + require.JSONEq(t, `{"stdout":"hello\n","stderr":"warning\n","exitCode":7}`, stdout.String()) |
| 39 | + require.Empty(t, stderr.String()) |
| 40 | +} |
| 41 | + |
| 42 | +func TestWriteSandboxExecResultYAMLOutput(t *testing.T) { |
| 43 | + cmd, stdout, stderr := newSandboxExecTestCommand(command.YAML) |
| 44 | + result := &sandboxclient.SandboxExecSyncResponse{ |
| 45 | + Stdout: "hello\n", |
| 46 | + Stderr: "warning\n", |
| 47 | + ExitCode: 7, |
| 48 | + } |
| 49 | + |
| 50 | + require.NoError(t, writeSandboxExecResult(cmd, result)) |
| 51 | + |
| 52 | + var got map[string]any |
| 53 | + require.NoError(t, yaml.Unmarshal(stdout.Bytes(), &got)) |
| 54 | + require.Equal(t, "hello\n", got["stdout"]) |
| 55 | + require.Equal(t, "warning\n", got["stderr"]) |
| 56 | + require.Equal(t, 7, got["exitCode"]) |
| 57 | + require.Empty(t, stderr.String()) |
| 58 | +} |
| 59 | + |
| 60 | +func TestExitSandboxExecUsesRemoteExitCode(t *testing.T) { |
| 61 | + oldExit := sandboxExecExit |
| 62 | + defer func() { sandboxExecExit = oldExit }() |
| 63 | + |
| 64 | + var gotExitCode *int |
| 65 | + sandboxExecExit = func(code int) { |
| 66 | + gotExitCode = &code |
| 67 | + } |
| 68 | + |
| 69 | + exitSandboxExec(7) |
| 70 | + require.NotNil(t, gotExitCode) |
| 71 | + require.Equal(t, 7, *gotExitCode) |
| 72 | +} |
| 73 | + |
| 74 | +func TestExitSandboxExecSkipsZeroExitCode(t *testing.T) { |
| 75 | + oldExit := sandboxExecExit |
| 76 | + defer func() { sandboxExecExit = oldExit }() |
| 77 | + |
| 78 | + called := false |
| 79 | + sandboxExecExit = func(int) { |
| 80 | + called = true |
| 81 | + } |
| 82 | + |
| 83 | + exitSandboxExec(0) |
| 84 | + require.False(t, called) |
| 85 | +} |
| 86 | + |
| 87 | +func TestJoinShellCommand(t *testing.T) { |
| 88 | + tests := []struct { |
| 89 | + name string |
| 90 | + args []string |
| 91 | + want string |
| 92 | + }{ |
| 93 | + {name: "simple command", args: []string{"echo", "hello"}, want: "echo hello"}, |
| 94 | + {name: "preserves quoted spaces", args: []string{"echo", "a b"}, want: "echo 'a b'"}, |
| 95 | + {name: "safe punctuation unquoted", args: []string{"ls", "-la", "./path/to-file_1"}, want: "ls -la ./path/to-file_1"}, |
| 96 | + {name: "quotes shell metacharacters", args: []string{"echo", "$HOME", "&&", "rm"}, want: "echo '$HOME' '&&' rm"}, |
| 97 | + {name: "escapes embedded single quote", args: []string{"echo", "it's"}, want: `echo 'it'\''s'`}, |
| 98 | + {name: "empty token", args: []string{"echo", ""}, want: "echo ''"}, |
| 99 | + } |
| 100 | + |
| 101 | + for _, tt := range tests { |
| 102 | + t.Run(tt.name, func(t *testing.T) { |
| 103 | + require.Equal(t, tt.want, joinShellCommand(tt.args)) |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func newSandboxExecTestCommand(output command.Output) (*cobra.Command, *bytes.Buffer, *bytes.Buffer) { |
| 109 | + cmd := &cobra.Command{Use: "exec"} |
| 110 | + stdout := new(bytes.Buffer) |
| 111 | + stderr := new(bytes.Buffer) |
| 112 | + cmd.SetOut(stdout) |
| 113 | + cmd.SetErr(stderr) |
| 114 | + cmd.SetContext(command.SetFormatInContext(context.Background(), &output)) |
| 115 | + return cmd, stdout, stderr |
| 116 | +} |
0 commit comments