-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext.rs
More file actions
408 lines (368 loc) · 14.8 KB
/
Copy pathcontext.rs
File metadata and controls
408 lines (368 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Command Context Module
//!
//! This module provides a unified context for command execution that encapsulates
//! shared dependencies used across all command handlers.
//!
//! ## Purpose
//!
//! Previously, each command handler manually created the same dependencies:
//!
//! ```rust,ignore
//! // Duplicate code in every handler:
//! let repository_factory = RepositoryFactory::new(Duration::from_secs(30));
//! let repository = repository_factory.create(working_dir.to_path_buf());
//! let clock = Arc::new(SystemClock);
//! let mut output = UserOutput::new(VerbosityLevel::Normal);
//! ```
//!
//! `CommandContext` eliminates this duplication by providing a single place to:
//! - Initialize shared dependencies with consistent configuration
//! - Access dependencies through a clean interface
//! - Support testing with mock implementations
//!
//! ## Benefits
//!
//! - **Consistency**: All commands use the same dependency initialization
//! - **Maintainability**: Changes to dependency setup only need to be made once
//! - **Testability**: Easy to inject test doubles via `new_for_testing()`
//! - **Simplicity**: Command handlers focus on their logic, not setup
//!
//! ## Usage Example
//!
//! ```rust
//! use std::path::Path;
//! use std::sync::{Arc, Mutex};
//! use torrust_tracker_deployer_lib::presentation::commands::context::CommandContext;
//! use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
//!
//! fn handle_command(working_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
//! let output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
//! let mut ctx = CommandContext::new(working_dir.to_path_buf(), output.clone());
//!
//! output.lock().unwrap().progress("Starting operation...");
//!
//! // Use repository and clock through context
//! let repo = ctx.repository();
//! let clock = ctx.clock();
//!
//! output.lock().unwrap().success("Operation completed");
//! Ok(())
//! }
//! ```
use std::path::PathBuf;
use std::sync::Arc;
use crate::domain::environment::repository::EnvironmentRepository;
use crate::infrastructure::persistence::repository_factory::RepositoryFactory;
use crate::presentation::user_output::UserOutput;
use crate::shared::{Clock, SystemClock};
use super::constants::DEFAULT_LOCK_TIMEOUT;
/// Command execution context containing shared dependencies
///
/// This struct encapsulates all the common dependencies that command handlers need:
/// - Repository for persistent environment state
/// - Clock for timing operations
/// - User output for progress and results
///
/// By centralizing these dependencies, we eliminate duplicate initialization code
/// and ensure consistent configuration across all commands.
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::context::CommandContext;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let working_dir = PathBuf::from(".");
/// let user_output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let ctx = CommandContext::new(working_dir, user_output.clone());
///
/// // Access repository
/// let repo = ctx.repository();
///
/// // Access clock
/// let clock = ctx.clock();
///
/// // Access user output
/// let mut output = ctx.user_output().lock().unwrap();
/// output.progress("Processing...");
/// output.success("Complete!");
/// ```
pub struct CommandContext {
repository: Arc<dyn EnvironmentRepository>,
clock: Arc<dyn Clock>,
user_output: Arc<std::sync::Mutex<UserOutput>>,
}
impl CommandContext {
/// Create a new command context with production dependencies
///
/// This constructor initializes all dependencies using production implementations
/// and default configuration from constants:
/// - Repository with default lock timeout
/// - System clock
/// - Injected user output service
///
/// # Arguments
///
/// * `working_dir` - Root directory for environment data storage
/// * `user_output` - Shared user output service for consistent output formatting
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::context::CommandContext;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let working_dir = PathBuf::from("./data");
/// let user_output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let ctx = CommandContext::new(working_dir, user_output);
/// ```
#[must_use]
pub fn new(working_dir: PathBuf, user_output: Arc<std::sync::Mutex<UserOutput>>) -> Self {
let repository_factory = RepositoryFactory::new(DEFAULT_LOCK_TIMEOUT);
let repository = repository_factory.create(working_dir);
let clock = Arc::new(SystemClock);
Self {
repository,
clock,
user_output,
}
}
/// Create a command context using an existing repository factory
///
/// This constructor allows creating a context with a pre-configured repository factory,
/// useful when consistent repository configuration (like lock timeout) needs to be
/// shared across multiple contexts.
///
/// # Arguments
///
/// * `repository_factory` - Pre-configured repository factory
/// * `working_dir` - Root directory for environment data storage
/// * `user_output` - Shared user output service for consistent output formatting
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::time::Duration;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::context::CommandContext;
/// use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let factory = RepositoryFactory::new(Duration::from_secs(30));
/// let working_dir = PathBuf::from("./data");
/// let user_output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let ctx = CommandContext::new_with_factory(&factory, working_dir, user_output);
/// ```
#[must_use]
pub fn new_with_factory(
repository_factory: &RepositoryFactory,
working_dir: PathBuf,
user_output: Arc<std::sync::Mutex<UserOutput>>,
) -> Self {
let repository = repository_factory.create(working_dir);
let clock = Arc::new(SystemClock);
Self {
repository,
clock,
user_output,
}
}
/// Create a command context for testing with injected dependencies
///
/// This constructor allows tests to inject mock implementations for better isolation
/// and control over behavior.
///
/// # Arguments
///
/// * `repository` - Repository implementation (can be a mock)
/// * `clock` - Clock implementation (can be a mock)
/// * `user_output` - Shared user output service for consistent output formatting
///
/// # Examples
///
/// ```rust
/// use std::sync::{Arc, Mutex};
/// use std::path::PathBuf;
/// use torrust_tracker_deployer_lib::presentation::commands::context::CommandContext;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
/// use torrust_tracker_deployer_lib::infrastructure::persistence::repository_factory::RepositoryFactory;
/// use torrust_tracker_deployer_lib::shared::{Clock, SystemClock};
/// use std::time::Duration;
///
/// // Create test dependencies
/// let factory = RepositoryFactory::new(Duration::from_secs(5));
/// let repository = factory.create(PathBuf::from("/tmp/test"));
/// let clock: Arc<dyn Clock> = Arc::new(SystemClock);
/// let user_output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Quiet)));
///
/// let ctx = CommandContext::new_for_testing(repository, clock, user_output);
/// ```
#[must_use]
pub fn new_for_testing(
repository: Arc<dyn EnvironmentRepository>,
clock: Arc<dyn Clock>,
user_output: Arc<std::sync::Mutex<UserOutput>>,
) -> Self {
Self {
repository,
clock,
user_output,
}
}
/// Get reference to the environment repository
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::context::CommandContext;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let ctx = CommandContext::new(PathBuf::from("."), output);
/// let repo = ctx.repository();
/// ```
#[must_use]
pub fn repository(&self) -> &Arc<dyn EnvironmentRepository> {
&self.repository
}
/// Get reference to the clock
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::context::CommandContext;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let ctx = CommandContext::new(PathBuf::from("."), output);
/// let clock = ctx.clock();
/// ```
#[must_use]
pub fn clock(&self) -> &Arc<dyn Clock> {
&self.clock
}
/// Get reference to the shared user output
///
/// Returns the Arc-wrapped Mutex-protected `UserOutput` instance, allowing
/// multiple components to share access to the same output sink.
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
/// use std::sync::{Arc, Mutex};
/// use torrust_tracker_deployer_lib::presentation::commands::context::CommandContext;
/// use torrust_tracker_deployer_lib::presentation::user_output::{UserOutput, VerbosityLevel};
///
/// let user_output = Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)));
/// let ctx = CommandContext::new(PathBuf::from("."), user_output);
/// let output_ref = ctx.user_output();
/// output_ref.lock().unwrap().progress("Working...");
/// output_ref.lock().unwrap().success("Done!");
/// ```
#[must_use]
pub fn user_output(&self) -> &Arc<std::sync::Mutex<UserOutput>> {
&self.user_output
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
use crate::presentation::user_output::test_support::TestUserOutput;
use crate::presentation::user_output::VerbosityLevel;
/// Test helper to create a test context with temporary directory
///
/// Returns a tuple of (`TempDir`, `PathBuf`, `Arc<Mutex<UserOutput>>`)
/// The `TempDir` must be kept alive for the duration of the test.
fn create_test_setup() -> (TempDir, PathBuf, Arc<std::sync::Mutex<UserOutput>>) {
let temp_dir = TempDir::new().unwrap();
let working_dir = temp_dir.path().to_path_buf();
let user_output = TestUserOutput::wrapped(VerbosityLevel::Normal);
(temp_dir, working_dir, user_output)
}
#[test]
fn it_should_create_context_with_production_dependencies() {
let (_temp_dir, working_dir, user_output) = create_test_setup();
let ctx = CommandContext::new(working_dir, user_output);
// Verify dependencies are present and accessible (we can call methods on them)
let _ = ctx.repository();
let _ = ctx.clock();
}
#[test]
fn it_should_provide_access_to_repository() {
let (_temp_dir, working_dir, user_output) = create_test_setup();
let ctx = CommandContext::new(working_dir, user_output);
// Should be able to access repository
let _repo = ctx.repository();
}
#[test]
fn it_should_provide_access_to_clock() {
let (_temp_dir, working_dir, user_output) = create_test_setup();
let ctx = CommandContext::new(working_dir, user_output);
// Should be able to access clock
let _clock = ctx.clock();
}
#[test]
fn it_should_provide_access_to_user_output() {
let (_temp_dir, working_dir, user_output) = create_test_setup();
let ctx = CommandContext::new(working_dir, user_output);
// Should be able to use output methods through Arc<Mutex<>>
let output_ref = ctx.user_output();
output_ref.lock().unwrap().progress("Test progress");
output_ref.lock().unwrap().success("Test success");
}
#[test]
fn it_should_create_context_with_factory() {
let (_temp_dir, working_dir, user_output) = create_test_setup();
let repository_factory = RepositoryFactory::new(DEFAULT_LOCK_TIMEOUT);
let ctx = CommandContext::new_with_factory(&repository_factory, working_dir, user_output);
// Verify we can access all dependencies
let _repo = ctx.repository();
let _clock = ctx.clock();
}
#[test]
fn it_should_allow_creating_context_for_testing() {
let temp_dir = TempDir::new().unwrap();
let working_dir = temp_dir.path().to_path_buf();
// Create test dependencies
let repository_factory = RepositoryFactory::new(DEFAULT_LOCK_TIMEOUT);
let repository = repository_factory.create(working_dir);
let clock: Arc<dyn Clock> = Arc::new(SystemClock);
let user_output = Arc::new(std::sync::Mutex::new(UserOutput::new(
VerbosityLevel::Normal,
)));
// Create context with test dependencies
let ctx = CommandContext::new_for_testing(repository, clock, user_output);
// Verify we can access all dependencies
let _repo = ctx.repository();
let _clock = ctx.clock();
}
#[test]
fn it_should_allow_accessing_output_multiple_times() {
let (_temp_dir, working_dir, user_output) = create_test_setup();
let ctx = CommandContext::new(working_dir, user_output);
// Should be able to call user_output() multiple times
let output_ref = ctx.user_output();
output_ref.lock().unwrap().progress("First message");
output_ref.lock().unwrap().success("Second message");
output_ref.lock().unwrap().error("Third message");
}
#[test]
fn it_should_use_default_constants() {
let (_temp_dir, working_dir, user_output) = create_test_setup();
// Creating context should use DEFAULT_LOCK_TIMEOUT
let _ctx = CommandContext::new(working_dir, user_output);
// This test verifies that the code compiles with the constants
// The actual values are tested in the constants module
}
}