-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuilders.rs
More file actions
78 lines (66 loc) · 2.75 KB
/
Copy pathbuilders.rs
File metadata and controls
78 lines (66 loc) · 2.75 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
//! Test builders for Provision Command
//!
//! This module provides test builders that simplify test setup by managing
//! dependencies and lifecycle for `ProvisionCommandHandler` tests.
use std::sync::Arc;
use tempfile::TempDir;
use crate::adapters::ssh::SshCredentials;
use crate::application::command_handlers::provision::ProvisionCommandHandler;
use crate::infrastructure::persistence::repository_factory::RepositoryFactory;
use crate::shared::Username;
/// Test builder for `ProvisionCommandHandler` that manages dependencies and lifecycle
///
/// This builder simplifies test setup by:
/// - Managing `TempDir` lifecycle
/// - Providing sensible defaults for all dependencies
/// - Allowing selective customization of dependencies
/// - Returning only the command handler and necessary test artifacts
pub struct ProvisionCommandHandlerTestBuilder {
#[allow(dead_code)]
temp_dir: TempDir,
ssh_credentials: Option<SshCredentials>,
}
impl ProvisionCommandHandlerTestBuilder {
/// Create a new test builder with default configuration
#[must_use]
pub fn new() -> Self {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
Self {
temp_dir,
ssh_credentials: None,
}
}
/// Customize SSH credentials (optional - uses defaults if not called)
#[allow(dead_code)]
pub fn with_ssh_credentials(mut self, credentials: SshCredentials) -> Self {
self.ssh_credentials = Some(credentials);
self
}
/// Build the `ProvisionCommandHandler` with all dependencies
///
/// Returns: (`command_handler`, `temp_dir`, `ssh_credentials`)
/// The `temp_dir` must be kept alive for the duration of the test.
#[allow(dead_code)]
pub fn build(self) -> (ProvisionCommandHandler, TempDir, SshCredentials) {
// Use provided SSH credentials or create defaults
let ssh_credentials = self.ssh_credentials.unwrap_or_else(|| {
let ssh_key_path = self.temp_dir.path().join("test_key");
let ssh_pub_key_path = self.temp_dir.path().join("test_key.pub");
SshCredentials::new(
ssh_key_path,
ssh_pub_key_path,
Username::new("test_user").unwrap(),
)
});
let clock: Arc<dyn crate::shared::Clock> = Arc::new(crate::shared::SystemClock);
let repository_factory = RepositoryFactory::new(std::time::Duration::from_secs(30));
let repository = repository_factory.create(self.temp_dir.path().to_path_buf());
let command_handler = ProvisionCommandHandler::new(clock, repository);
(command_handler, self.temp_dir, ssh_credentials)
}
}
impl Default for ProvisionCommandHandlerTestBuilder {
fn default() -> Self {
Self::new()
}
}