Skip to content

Centralize test infrastructure for UserOutput to eliminate duplication - #125

Merged
josecelano merged 6 commits into
mainfrom
copilot/simplify-test-infrastructure-useroutput
Nov 3, 2025
Merged

josecelano merged 6 commits into
mainfrom
copilot/simplify-test-infrastructure-useroutput

Conversation

Copilot AI commented Nov 3, 2025 •

Copy link
Copy Markdown
Contributor

Eight test files each maintained duplicate create_test_user_output() helpers and SharedWriter implementations for capturing UserOutput in tests. This created maintenance burden and verbose test code requiring manual buffer lock management.

Changes

  • Created centralized test_support module in src/presentation/user_output.rs

    • TestUserOutput: Wrapper providing clean API for test output capture
    • TestWriter: Write trait implementation using Arc<Mutex<Vec<u8>>>
    • Helper methods: wrapped(), into_wrapped(), stdout(), stderr(), clear()
  • Updated 9 test files to use centralized infrastructure

    • user_output.rs, progress.rs, factory.rs, context.rs
    • destroy/handler.rs, destroy/tests/integration.rs
    • create/subcommands/environment.rs, create/tests/{template,integration}.rs
  • Removed duplicate code

    • 8 create_test_user_output() functions
    • 2 SharedWriter implementations

Example

Before:

let (mut output, stdout_buf, stderr_buf) = create_test_user_output(VerbosityLevel::Normal);
output.progress("Processing...");
let stderr_content = String::from_utf8(stderr_buf.lock().unwrap().clone()).unwrap();
assert_eq!(stderr_content, "⏳ Processing...\n");

After:

let mut test_output = TestUserOutput::new(VerbosityLevel::Normal);
test_output.output.progress("Processing...");
assert_eq!(test_output.stderr(), "⏳ Processing...\n");

For tests requiring Arc<Mutex<UserOutput>>:

let output = TestUserOutput::wrapped(VerbosityLevel::Normal);
Original prompt

This section details on the original issue you should resolve

<issue_title>Simplify Test Infrastructure for UserOutput</issue_title>
<issue_description>## Overview

Simplify the test infrastructure for UserOutput by replacing complex Arc<Mutex<Vec<u8>>> and custom SharedWriter with a simpler, more maintainable TestUserOutput wrapper. This makes test code easier to understand and reduces cognitive load for contributors.

Specification

See detailed specification: docs/issues/TBD-simplify-test-infrastructure.md

(Link will be updated after file rename)

🏗️ Architecture Requirements

DDD Layer: Presentation (Test Support Module)
Module Path: src/presentation/user_output.rs (test module)
Pattern: Test Helper Struct

Module Structure Requirements

  • Keep test infrastructure in #[cfg(test)] module within user_output.rs
  • Create TestUserOutput helper struct for test scenarios
  • Maintain separation between production code and test helpers
  • Follow module organization conventions (see docs/contributing/module-organization.md)

Architectural Constraints

  • Test-only code: All changes confined to #[cfg(test)] modules
  • No breaking changes: Production UserOutput API unchanged
  • Standard library preference: Use Vec<u8> and standard types over complex wrappers
  • Simplicity over cleverness: Favor readable code over minimal boilerplate

Anti-Patterns to Avoid

  • ❌ Over-engineering test infrastructure - Don't add unnecessary abstractions
  • ❌ Leaking test code to production - Keep test helpers in test modules only
  • ❌ Complex synchronization primitives - Avoid Arc<Mutex<>> unless truly needed

Implementation Plan

Phase 1: Create New Test Infrastructure (1-2 hours)

  • Create test_support module in #[cfg(test)] section
  • Implement TestUserOutput struct with accessor methods
  • Implement TestWriter with Write trait
  • Add documentation for test infrastructure

Phase 2: Update Existing Tests (2-3 hours)

  • Identify all tests using create_test_user_output()
  • Update tests to use TestUserOutput::new()
  • Simplify test code with new cleaner API
  • Verify all tests pass

Phase 3: Remove Old Infrastructure (30 minutes)

  • Remove SharedWriter struct
  • Remove create_test_user_output() function
  • Run linter and fix warnings

Phase 4: Documentation and Verification (30 minutes)

  • Add module documentation
  • Run full test suite
  • Run pre-commit checks: ./scripts/pre-commit.sh

Acceptance Criteria

Note for Contributors: These criteria define what the PR reviewer will check. Use this as your pre-review checklist before submitting the PR to minimize back-and-forth iterations.

Quality Checks:

  • Pre-commit checks pass: ./scripts/pre-commit.sh

Infrastructure Checks:

  • TestUserOutput struct exists in test module
  • All accessor methods implemented: stdout(), stderr(), output(), clear()
  • Old SharedWriter and create_test_user_output() removed

Test Migration Checks:

  • All tests updated to new infrastructure
  • No remaining references to old helpers
  • All tests pass
  • Test code is simpler and more readable

Code Quality Checks:

  • Test infrastructure well-documented
  • Usage examples provided
  • Standard library types used (Rc<RefCell<>> instead of Arc<Mutex<>>)

Related

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Simplify test infrastructure for UserOutput Centralize test infrastructure for UserOutput to eliminate duplication Nov 3, 2025
Copilot AI requested a review from josecelano November 3, 2025 21:13

@josecelano josecelano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f5dfeb8

@josecelano
josecelano marked this pull request as ready for review November 3, 2025 21:48
@josecelano
josecelano merged commit 3f6e2b3 into main Nov 3, 2025
@josecelano
josecelano deleted the copilot/simplify-test-infrastructure-useroutput branch April 15, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplify Test Infrastructure for UserOutput

2 participants