Commit 3f6e2b3
committed
Merge #125: Centralize test infrastructure for UserOutput to eliminate duplication
f5dfeb8 fix: add clippy annotations and panics documentation to TestUserOutput (copilot-swe-agent[bot])
eb5088a refactor: update all remaining test files to use TestUserOutput (copilot-swe-agent[bot])
a3a9cb8 refactor: update factory, context, and destroy/handler tests to use TestUserOutput (copilot-swe-agent[bot])
160c403 refactor: update progress.rs to use TestUserOutput helper (copilot-swe-agent[bot])
895be5c feat: add TestUserOutput helper for simplified test infrastructure (copilot-swe-agent[bot])
807a682 Initial plan (copilot-swe-agent[bot])
Pull request description:
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:**
```rust
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:**
```rust
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>>`:
```rust
let output = TestUserOutput::wrapped(VerbosityLevel::Normal);
```
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *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](https://github.com/torrust/torrust-tracker-deployer/blob/main/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](https://github.com/torrust/torrust-tracker-deployer/blob/main/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
>
> - Parent: #102 (Epic: User Output Architecture Improvements)
> - Roadmap: N/A (Code quality improvement)
> - Specification: docs/issues/TBD-simplify-test-infrastructure.md</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
- Fixes #123
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 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](https://gh.io/copilot-coding-agent-tips) in the docs.
ACKs for top commit:
josecelano:
ACK f5dfeb8
Tree-SHA512: e21725010f45d282e65e365dde82235748270f161cc0d59a88bcd86c64d69e20bc799a6426d6166c71982f4014920570584be929ae7cab298a68572b78439dd4File tree
9 files changed
+326
-242
lines changed- src/presentation
- commands
- create
- subcommands
- tests
- destroy
- tests
9 files changed
+326
-242
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
| 295 | + | |
295 | 296 | | |
296 | 297 | | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | 298 | | |
305 | 299 | | |
306 | 300 | | |
307 | 301 | | |
308 | 302 | | |
309 | 303 | | |
310 | 304 | | |
311 | | - | |
| 305 | + | |
312 | 306 | | |
313 | 307 | | |
314 | 308 | | |
| |||
Lines changed: 12 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
| 237 | + | |
237 | 238 | | |
238 | 239 | | |
239 | 240 | | |
240 | 241 | | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | 242 | | |
247 | 243 | | |
248 | 244 | | |
| |||
268 | 264 | | |
269 | 265 | | |
270 | 266 | | |
271 | | - | |
| 267 | + | |
272 | 268 | | |
273 | 269 | | |
274 | 270 | | |
| |||
292 | 288 | | |
293 | 289 | | |
294 | 290 | | |
295 | | - | |
| 291 | + | |
296 | 292 | | |
297 | 293 | | |
298 | 294 | | |
| |||
314 | 310 | | |
315 | 311 | | |
316 | 312 | | |
317 | | - | |
| 313 | + | |
318 | 314 | | |
319 | 315 | | |
320 | 316 | | |
| |||
350 | 346 | | |
351 | 347 | | |
352 | 348 | | |
353 | | - | |
| 349 | + | |
354 | 350 | | |
355 | 351 | | |
356 | 352 | | |
| |||
394 | 390 | | |
395 | 391 | | |
396 | 392 | | |
397 | | - | |
| 393 | + | |
398 | 394 | | |
399 | 395 | | |
400 | 396 | | |
| |||
438 | 434 | | |
439 | 435 | | |
440 | 436 | | |
441 | | - | |
| 437 | + | |
442 | 438 | | |
443 | 439 | | |
444 | 440 | | |
| |||
451 | 447 | | |
452 | 448 | | |
453 | 449 | | |
454 | | - | |
| 450 | + | |
455 | 451 | | |
456 | 452 | | |
457 | 453 | | |
| |||
469 | 465 | | |
470 | 466 | | |
471 | 467 | | |
472 | | - | |
| 468 | + | |
473 | 469 | | |
474 | 470 | | |
475 | 471 | | |
| |||
507 | 503 | | |
508 | 504 | | |
509 | 505 | | |
510 | | - | |
| 506 | + | |
511 | 507 | | |
512 | 508 | | |
513 | 509 | | |
| |||
543 | 539 | | |
544 | 540 | | |
545 | 541 | | |
546 | | - | |
| 542 | + | |
547 | 543 | | |
548 | 544 | | |
549 | 545 | | |
| |||
597 | 593 | | |
598 | 594 | | |
599 | 595 | | |
600 | | - | |
| 596 | + | |
601 | 597 | | |
602 | 598 | | |
603 | 599 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 12 | + | |
| 13 | + | |
20 | 14 | | |
21 | 15 | | |
22 | 16 | | |
| |||
26 | 20 | | |
27 | 21 | | |
28 | 22 | | |
29 | | - | |
| 23 | + | |
30 | 24 | | |
31 | 25 | | |
32 | 26 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 6 | + | |
| 7 | + | |
14 | 8 | | |
15 | 9 | | |
16 | 10 | | |
| |||
21 | 15 | | |
22 | 16 | | |
23 | 17 | | |
24 | | - | |
| 18 | + | |
25 | 19 | | |
26 | 20 | | |
27 | 21 | | |
| |||
65 | 59 | | |
66 | 60 | | |
67 | 61 | | |
68 | | - | |
| 62 | + | |
69 | 63 | | |
70 | 64 | | |
71 | 65 | | |
| |||
90 | 84 | | |
91 | 85 | | |
92 | 86 | | |
93 | | - | |
| 87 | + | |
94 | 88 | | |
95 | 89 | | |
96 | 90 | | |
| |||
135 | 129 | | |
136 | 130 | | |
137 | 131 | | |
138 | | - | |
| 132 | + | |
139 | 133 | | |
140 | 134 | | |
141 | 135 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
| 236 | + | |
236 | 237 | | |
237 | 238 | | |
238 | 239 | | |
239 | 240 | | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | 241 | | |
246 | 242 | | |
247 | 243 | | |
248 | 244 | | |
249 | | - | |
| 245 | + | |
250 | 246 | | |
251 | 247 | | |
252 | 248 | | |
| |||
264 | 260 | | |
265 | 261 | | |
266 | 262 | | |
267 | | - | |
| 263 | + | |
268 | 264 | | |
269 | 265 | | |
270 | 266 | | |
| |||
281 | 277 | | |
282 | 278 | | |
283 | 279 | | |
284 | | - | |
| 280 | + | |
285 | 281 | | |
286 | 282 | | |
287 | 283 | | |
| |||
300 | 296 | | |
301 | 297 | | |
302 | 298 | | |
303 | | - | |
| 299 | + | |
304 | 300 | | |
305 | 301 | | |
306 | 302 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 10 | + | |
| 11 | + | |
17 | 12 | | |
18 | 13 | | |
19 | 14 | | |
| |||
27 | 22 | | |
28 | 23 | | |
29 | 24 | | |
30 | | - | |
| 25 | + | |
31 | 26 | | |
32 | 27 | | |
33 | 28 | | |
| |||
44 | 39 | | |
45 | 40 | | |
46 | 41 | | |
47 | | - | |
| 42 | + | |
48 | 43 | | |
49 | 44 | | |
50 | 45 | | |
| |||
64 | 59 | | |
65 | 60 | | |
66 | 61 | | |
67 | | - | |
| 62 | + | |
68 | 63 | | |
69 | 64 | | |
70 | 65 | | |
| |||
77 | 72 | | |
78 | 73 | | |
79 | 74 | | |
80 | | - | |
| 75 | + | |
81 | 76 | | |
82 | 77 | | |
83 | 78 | | |
| |||
88 | 83 | | |
89 | 84 | | |
90 | 85 | | |
91 | | - | |
| 86 | + | |
92 | 87 | | |
93 | 88 | | |
94 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
242 | 242 | | |
243 | 243 | | |
244 | 244 | | |
| 245 | + | |
245 | 246 | | |
246 | 247 | | |
247 | 248 | | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | 249 | | |
254 | 250 | | |
255 | 251 | | |
| |||
263 | 259 | | |
264 | 260 | | |
265 | 261 | | |
266 | | - | |
| 262 | + | |
267 | 263 | | |
268 | 264 | | |
269 | 265 | | |
| |||
321 | 317 | | |
322 | 318 | | |
323 | 319 | | |
324 | | - | |
325 | | - | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
326 | 326 | | |
327 | 327 | | |
328 | 328 | | |
| |||
0 commit comments