Commit 0d56c69
committed
Merge #84: Create shared test utilities module for command handlers
045f131 style: [#75] Fix clippy warnings and formatting (copilot-swe-agent[bot])
c2cc568 feat: [#75] Create shared test utilities module (copilot-swe-agent[bot])
0ad3a26 Initial plan (copilot-swe-agent[bot])
Pull request description:
Test utilities were duplicated across `create/tests/fixtures.rs` and inconsistently implemented in `destroy/tests/`. This created maintenance burden and inconsistent test patterns.
## Changes
**New shared test utilities** (`src/presentation/commands/tests/mod.rs`):
- `TestContext` - Manages temporary directories with automatic cleanup
- `create_valid_config()` - Generates valid environment configurations
- `create_invalid_json_config()` - Creates malformed JSON for error testing
- `create_config_with_invalid_name()` - Invalid environment name for validation testing
- `create_config_with_missing_keys()` - Non-existent SSH keys for file validation testing
**Test file updates**:
- `create/tests/integration.rs` - 8 tests migrated to shared utilities
- `create/tests/template.rs` - 4 tests migrated to shared utilities
- `destroy/tests/integration.rs` - 5 tests migrated to shared utilities
- Removed duplicate `create/tests/fixtures.rs` (162 lines)
## Usage
```rust
use crate::presentation::commands::tests::{TestContext, create_valid_config};
#[test]
fn it_should_create_environment_from_valid_config() {
let context = TestContext::new();
let config_path = create_valid_config(context.working_dir(), "test-env");
let result = handle_environment_creation(&config_path, context.working_dir());
assert!(result.is_ok());
// Temp directory cleaned up automatically when context drops
}
```
Net reduction of ~190 lines with improved consistency across all command tests.
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Create Shared Test Utilities Module</issue_title>
> <issue_description>**Parent Issue**: #63
> **Type**: 🔨 Structural Improvement
> **Impact**: 🟢🟢 Medium
> **Effort**: 🔵🔵 Medium
> **Priority**: P1
>
> ## Problem
>
> Test utilities are duplicated and inconsistent:
>
> - `create/tests/` has comprehensive fixtures module
> - `destroy/tests/` lacks test helpers
> - Common patterns like creating temp directories, generating test configs are repeated
>
> ## Proposed Solution
>
> Create shared test utilities at `src/presentation/commands/tests/mod.rs`:
>
> ```rust
> //! Shared test utilities for command handlers
>
> use std::path::{Path, PathBuf};
> use std::sync::Arc;
> use tempfile::TempDir;
>
> /// Test context with temp directory and common test dependencies
> pub struct TestContext {
> _temp_dir: TempDir,
> pub working_dir: PathBuf,
> pub repository: Arc<MockEnvironmentRepository>,
> pub clock: Arc<MockClock>,
> }
>
> impl TestContext {
> pub fn new() -> Self {
> let temp_dir = TempDir::new().expect("Failed to create temp directory");
> let working_dir = temp_dir.path().to_path_buf();
>
> let repository_factory = RepositoryFactory::new(std::time::Duration::from_secs(30));
> let repository = Arc::new(repository_factory.create(working_dir.clone()));
>
> let clock = Arc::new(MockClock::new(chrono::Utc::now()));
>
> Self { _temp_dir: temp_dir, working_dir, repository, clock }
> }
>
> pub fn working_dir(&self) -> &Path {
> &self.working_dir
> }
> }
>
> /// Create a valid test configuration file
> pub fn create_test_config(path: &Path, env_name: &str) -> PathBuf {
> // Implementation
> }
>
> /// Create an invalid JSON configuration file for error testing
> pub fn create_invalid_json_config(path: &Path) -> PathBuf {
> // Implementation
> }
>
> /// Create a configuration with invalid environment name
> pub fn create_config_with_invalid_name(path: &Path) -> PathBuf {
> // Implementation
> }
> ```
>
> Then use in tests:
> ```rust
> use crate::presentation::commands::tests::{TestContext, create_test_config};
>
> #[test]
> fn it_should_create_environment_from_valid_config() {
> let context = TestContext::new();
> let config_path = create_test_config(context.working_dir(), "test-env");
>
> let result = handle_environment_creation(&config_path, context.working_dir());
> assert!(result.is_ok());
> }
> ```
>
> ## Benefits
>
> - ✅ Reduces test code duplication
> - ✅ Consistent test patterns across commands
> - ✅ Easier to write new tests
> - ✅ Shared improvements benefit all tests
>
> ## Implementation Checklist
>
> - [ ] Create `src/presentation/commands/tests/mod.rs`
> - [ ] Implement `TestContext` struct with temp dir management
> - [ ] Migrate `create_test_config()` from fixtures.rs
> - [ ] Migrate `create_invalid_json_config()` from fixtures.rs
> - [ ] Migrate `create_config_with_invalid_name()` from fixtures.rs
> - [ ] Add comprehensive documentation
> - [ ] Update `create/tests/` to use shared utilities
> - [ ] Update `destroy/tests/` to use shared utilities
> - [ ] Remove duplicate code from old test files
> - [ ] Verify all tests pass
> - [ ] Run linter
>
> ## Acceptance Criteria
>
> - [ ] Shared test utilities module exists
> - [ ] `TestContext` provides consistent test setup
> - [ ] Common test config generators are shared
> - [ ] Both create and destroy tests use shared utilities
> - [ ] No duplicate test helper code remains
> - [ ] All tests pass: `cargo test presentation::commands`
> - [ ] Pre-commit checks pass: `./scripts/pre-commit.sh`
> - [ ] Code follows project conventions
>
> ## Related Documentation
>
> - [Testing Guide](https://github.com/torrust/torrust-tracker-deployer/blob/main/docs/contributing/testing.md)
> - [Refactor Plan](https://github.com/torrust/torrust-tracker-deployer/blob/main/docs/refactors/plans/presentation-commands-cleanup.md)</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
- Fixes #70
<!-- 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 045f131
Tree-SHA512: fee2de626b1010bcde7e06dfbfa7f0e145caf0c4efbe2d9b294ad8b80eabb9c1ea800f2c830a6db5d539a762d278270444c70fbb3db6d92db917066a23de6604File tree
7 files changed
+435
-234
lines changed- src/presentation/commands
- create/tests
- destroy/tests
- tests
7 files changed
+435
-234
lines changedThis file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | 6 | | |
9 | 7 | | |
10 | | - | |
11 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | | - | |
| 26 | + | |
| 27 | + | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
40 | | - | |
| 39 | + | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
| 51 | + | |
| 52 | + | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | | - | |
| 67 | + | |
| 68 | + | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
| 83 | + | |
| 84 | + | |
85 | 85 | | |
86 | | - | |
| 86 | + | |
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
100 | | - | |
| 99 | + | |
| 100 | + | |
101 | 101 | | |
102 | | - | |
| 102 | + | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
116 | | - | |
| 115 | + | |
| 116 | + | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
| 119 | + | |
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
136 | | - | |
137 | | - | |
| 136 | + | |
| 137 | + | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
| 140 | + | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
158 | | - | |
| 158 | + | |
159 | 159 | | |
160 | 160 | | |
161 | | - | |
162 | | - | |
| 161 | + | |
| 162 | + | |
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
170 | | - | |
| 169 | + | |
| 170 | + | |
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
177 | | - | |
178 | | - | |
| 177 | + | |
| 178 | + | |
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
0 commit comments