executeActionCommand executes the bytebase-action cobra command with given arguments
(ctx context.Context, args ...string)
| 486 | |
| 487 | // executeActionCommand executes the bytebase-action cobra command with given arguments |
| 488 | func executeActionCommand(ctx context.Context, args ...string) (*ActionResult, error) { |
| 489 | // Create new world instance for test isolation |
| 490 | w := world.NewWorld() |
| 491 | w.Platform = world.LocalPlatform |
| 492 | |
| 493 | // Create new command instance using the factory function |
| 494 | cmd := command.NewRootCommand(w) |
| 495 | |
| 496 | // Set up output buffers |
| 497 | stdout := new(bytes.Buffer) |
| 498 | stderr := new(bytes.Buffer) |
| 499 | cmd.SetOut(stdout) |
| 500 | cmd.SetErr(stderr) |
| 501 | |
| 502 | // Set arguments |
| 503 | cmd.SetArgs(args) |
| 504 | |
| 505 | // Execute command |
| 506 | err := cmd.ExecuteContext(ctx) |
| 507 | |
| 508 | // Get output from world.OutputMap |
| 509 | outputJSON := make(map[string]any) |
| 510 | if j, err := json.Marshal(w.OutputMap); err == nil { |
| 511 | if err := json.Unmarshal(j, &outputJSON); err != nil { |
| 512 | return nil, err |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // Also parse output file if specified |
| 517 | if w.Output != "" { |
| 518 | if data, readErr := os.ReadFile(w.Output); readErr == nil { |
| 519 | var fileOutput map[string]any |
| 520 | if json.Unmarshal(data, &fileOutput) == nil { |
| 521 | for k, v := range fileOutput { |
| 522 | outputJSON[k] = v |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | return &ActionResult{ |
| 529 | Stdout: stdout.String(), |
| 530 | Stderr: stderr.String(), |
| 531 | Error: err, |
| 532 | OutputJSON: outputJSON, |
| 533 | }, err |
| 534 | } |
| 535 | |
| 536 | // createTestDatabase creates a test SQLite database instance and database |
| 537 | func (ctl *controller) createTestDatabase(ctx context.Context, t *testing.T) *v1pb.Database { |
no test coverage detected