Skip to content

refactor: inject io.Writer into cmd/test.go to decouple output from test orchestration logic #387

@abhaygoudannavar

Description

@abhaygoudannavar

Reason/Context

Why we need this improvement?
The upcoming LFX Term 2 2026 mentorship project — "Microcks-CLI v2 - IDE (VS Code) Integration and Local Dev Loop Enhancement" — will require the CLI to emit output to different destinations: a VS Code extension's output channel, GitHub Action log groups, and structured file output. Currently, cmd/test.go uses 14 hardcoded fmt.Printf/fmt.Println calls that write directly to os.Stdout, making it impossible to redirect or capture output programmatically.

How will this change help?
By threading an io.Writer through the test command, any consumer (VS Code extension, GitHub Action, unit test) can inject their own writer to capture output without parsing raw stdout text. The LFX mentee will be able to simply swap in a bytes.Buffer or a structured JSON writer on day one, instead of first having to refactor all 14 fmt.Printf call sites.

What is the motivation?
This is a pure preparatory refactor zero behavioral change that unblocks the LFX mentee from having to mix "output plumbing" work with their actual VS Code integration logic. It keeps the mentorship focused on the real deliverables.

Description

This refactoring replaces all fmt.Printf / fmt.Println calls in cmd/test.go with fmt.Fprintf(out, ...), where out is an injectable io.Writer that defaults to os.Stdout.

Before (hardcoded):

fmt.Printf("MicrocksClient got status for test \"%s\" - success: %s, inProgress: %s \n", ...)
fmt.Println("MicrocksTester waiting for 2 seconds before checking again or exiting.")

After (injectable):

fmt.Fprintf(out, "MicrocksClient got status for test \"%s\" - success: %s, inProgress: %s \n", ...)
fmt.Fprintln(out, "MicrocksTester waiting for 2 seconds before checking again or exiting.")

This is NOT a breaking change. The default writer is os.Stdout, so existing users see identical output. The only difference is that the writer is now swappable.

Scope clarification — this PR will NOT:

  • ❌ Build the VS Code extension
  • ❌ Add --output=json or --output=junit support
  • ❌ Build the GitHub Action
  • ✅ Only thread io.Writer through the test command and replace 14 print calls

Implementation ideas

  1. Add an out io.Writer variable at the top of NewTestCommand's Run closure, defaulting to os.Stdout:
    out := cmd.OutOrStdout() // Cobra's built-in writer support
  2. Replace all 14 fmt.Printf(...) and fmt.Println(...) calls with fmt.Fprintf(out, ...) and fmt.Fprintln(out, ...).
  3. Add a unit test that creates the command with a bytes.Buffer as output and verifies the test status messages appear in the buffer.
  4. Apply the same pattern to os.Exit(1) calls by wrapping them in a testable exit function (optional, can be a follow-up).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions