(t *testing.T)
| 145 | } |
| 146 | |
| 147 | func TestRunCmd(t *testing.T) { |
| 148 | t.Run("returns stdout json on success", func(t *testing.T) { |
| 149 | fake := newFakeCLI(t) |
| 150 | result, err := RunCmd(context.Background(), Request{ |
| 151 | BinaryPath: fake.BinaryPath, |
| 152 | Args: []string{"--stdout-json", `{"ok":true}`}, |
| 153 | }) |
| 154 | require.NoError(t, err) |
| 155 | result.AssertExitCode(t, 0) |
| 156 | result.AssertStdoutStatus(t, true) |
| 157 | |
| 158 | outMap, ok := result.StdoutJSON(t).(map[string]any) |
| 159 | require.True(t, ok) |
| 160 | assert.Equal(t, true, outMap["ok"]) |
| 161 | }) |
| 162 | |
| 163 | t.Run("captures stderr and exit code on failure", func(t *testing.T) { |
| 164 | fake := newFakeCLI(t) |
| 165 | result, err := RunCmd(context.Background(), Request{ |
| 166 | BinaryPath: fake.BinaryPath, |
| 167 | Args: []string{"--stderr-json", `{"ok":false}`, "--exit", "3"}, |
| 168 | }) |
| 169 | require.NoError(t, err) |
| 170 | result.AssertExitCode(t, 3) |
| 171 | assert.Error(t, result.RunErr) |
| 172 | |
| 173 | errMap, ok := result.StderrJSON(t).(map[string]any) |
| 174 | require.True(t, ok) |
| 175 | assert.Equal(t, false, errMap["ok"]) |
| 176 | }) |
| 177 | |
| 178 | t.Run("passes explicit default-as as flag", func(t *testing.T) { |
| 179 | fake := newFakeCLI(t) |
| 180 | result, err := RunCmd(context.Background(), Request{ |
| 181 | BinaryPath: fake.BinaryPath, |
| 182 | Args: []string{"emit-arg", "--as"}, |
| 183 | DefaultAs: "user", |
| 184 | }) |
| 185 | require.NoError(t, err) |
| 186 | result.AssertExitCode(t, 0) |
| 187 | assert.Equal(t, "user", strings.TrimSpace(result.Stdout)) |
| 188 | }) |
| 189 | |
| 190 | t.Run("asserts stdout code payloads", func(t *testing.T) { |
| 191 | fake := newFakeCLI(t) |
| 192 | result, err := RunCmd(context.Background(), Request{ |
| 193 | BinaryPath: fake.BinaryPath, |
| 194 | Args: []string{"--stdout-json", `{"code":0,"data":{"id":"x"}}`}, |
| 195 | Format: "json", |
| 196 | }) |
| 197 | require.NoError(t, err) |
| 198 | result.AssertExitCode(t, 0) |
| 199 | result.AssertStdoutStatus(t, 0) |
| 200 | }) |
| 201 | |
| 202 | t.Run("passes stdin to process", func(t *testing.T) { |
| 203 | fake := newFakeCLI(t) |
| 204 | result, err := RunCmd(context.Background(), Request{ |
nothing calls this directly
no test coverage detected