TestReportError pins reportError's two surfaces after the typed migration: human mode returns the typed error unchanged; JSON mode prints the legacy {ok:false, error:{type, message}} envelope and exits bare with the typed error's exit code (parity with the legacy explicit exit-code argument).
(t *testing.T)
| 382 | // {ok:false, error:{type, message}} envelope and exits bare with the typed |
| 383 | // error's exit code (parity with the legacy explicit exit-code argument). |
| 384 | func TestReportError(t *testing.T) { |
| 385 | t.Run("human mode returns the typed error", func(t *testing.T) { |
| 386 | f, _, _ := newTestFactory(t) |
| 387 | typed := errs.NewAPIError(errs.SubtypeUnknown, "failed to prepare update: disk full") |
| 388 | err := reportError(&UpdateOptions{JSON: false}, f.IOStreams, "update_error", typed) |
| 389 | var apiErr *errs.APIError |
| 390 | if !errors.As(err, &apiErr) { |
| 391 | t.Fatalf("expected *errs.APIError, got %T: %v", err, err) |
| 392 | } |
| 393 | if apiErr != typed { |
| 394 | t.Errorf("reportError must return the typed error unchanged") |
| 395 | } |
| 396 | if got := output.ExitCodeOf(err); got != output.ExitAPI { |
| 397 | t.Errorf("exit code = %d, want %d (ExitAPI, legacy parity)", got, output.ExitAPI) |
| 398 | } |
| 399 | }) |
| 400 | |
| 401 | t.Run("json mode prints envelope and exits bare with typed code", func(t *testing.T) { |
| 402 | f, stdout, _ := newTestFactory(t) |
| 403 | typed := errs.NewNetworkError(errs.SubtypeNetworkTransport, "failed to check latest version: timeout") |
| 404 | err := reportError(&UpdateOptions{JSON: true}, f.IOStreams, "network", typed) |
| 405 | var bareErr *output.BareError |
| 406 | if !errors.As(err, &bareErr) { |
| 407 | t.Fatalf("expected bare *output.BareError, got %T: %v", err, err) |
| 408 | } |
| 409 | if bareErr.Code != output.ExitNetwork { |
| 410 | t.Errorf("bare exit code = %d, want %d", bareErr.Code, output.ExitNetwork) |
| 411 | } |
| 412 | out := stdout.String() |
| 413 | if !strings.Contains(out, `"type": "network"`) && !strings.Contains(out, `"type":"network"`) { |
| 414 | t.Errorf("JSON envelope missing type, got: %s", out) |
| 415 | } |
| 416 | if !strings.Contains(out, "failed to check latest version: timeout") { |
| 417 | t.Errorf("JSON envelope missing message, got: %s", out) |
| 418 | } |
| 419 | }) |
| 420 | } |
| 421 | |
| 422 | func TestUpdateInvalidVersion_JSON(t *testing.T) { |
| 423 | f, stdout, _ := newTestFactory(t) |
nothing calls this directly
no test coverage detected