Skip to content

Commit 72f7cca

Browse files
committed
refactor: extract SSH key setup method in TestEnvironment
Extract SSH key copying and permission setting logic into a dedicated setup_ssh_key method to improve code organization and maintainability. - Add setup_ssh_key method with proper error handling - Remove inline SSH key setup from new method - Consolidate verbose logging within the extracted method - Maintain exact same functionality while improving structure
1 parent 72f6d4d commit 72f7cca

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/bin/e2e_tests.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,9 @@ impl TestEnvironment {
5050
// Create temporary directory for SSH keys
5151
let temp_dir = TempDir::new().context("Failed to create temporary directory")?;
5252

53-
// Copy SSH private key from fixtures to temp directory
54-
let fixtures_ssh_key = project_root.join("fixtures/testing_rsa");
53+
// Setup SSH key
5554
let temp_ssh_key = temp_dir.path().join("testing_rsa");
56-
57-
std::fs::copy(&fixtures_ssh_key, &temp_ssh_key)
58-
.context("Failed to copy SSH private key to temporary directory")?;
59-
60-
// Set proper permissions on the SSH key (600)
61-
#[cfg(unix)]
62-
{
63-
use std::os::unix::fs::PermissionsExt;
64-
let mut perms = std::fs::metadata(&temp_ssh_key)?.permissions();
65-
perms.set_mode(0o600);
66-
std::fs::set_permissions(&temp_ssh_key, perms)?;
67-
}
55+
Self::setup_ssh_key(&project_root, &temp_dir, verbose, &temp_ssh_key)?;
6856

6957
// Create configuration
7058
let config = Config::new(
@@ -86,10 +74,6 @@ impl TestEnvironment {
8674
Self::clean_and_prepare_templates(&services, verbose)?;
8775

8876
if verbose {
89-
println!(
90-
"🔑 SSH key copied to temporary location: {}",
91-
config.ssh_key_path.display()
92-
);
9377
println!("📁 Temporary directory: {}", temp_dir.path().display());
9478
println!(
9579
"📄 Templates directory: {}",
@@ -104,6 +88,39 @@ impl TestEnvironment {
10488
})
10589
}
10690

91+
/// Setup SSH key by copying from fixtures to temporary directory with proper permissions
92+
fn setup_ssh_key(
93+
project_root: &std::path::Path,
94+
temp_dir: &TempDir,
95+
verbose: bool,
96+
ssh_key_path: &std::path::Path,
97+
) -> Result<()> {
98+
// Copy SSH private key from fixtures to temp directory
99+
let fixtures_ssh_key = project_root.join("fixtures/testing_rsa");
100+
let temp_ssh_key = temp_dir.path().join("testing_rsa");
101+
102+
std::fs::copy(&fixtures_ssh_key, &temp_ssh_key)
103+
.context("Failed to copy SSH private key to temporary directory")?;
104+
105+
// Set proper permissions on the SSH key (600)
106+
#[cfg(unix)]
107+
{
108+
use std::os::unix::fs::PermissionsExt;
109+
let mut perms = std::fs::metadata(&temp_ssh_key)?.permissions();
110+
perms.set_mode(0o600);
111+
std::fs::set_permissions(&temp_ssh_key, perms)?;
112+
}
113+
114+
if verbose {
115+
println!(
116+
"🔑 SSH key copied to temporary location: {}",
117+
ssh_key_path.display()
118+
);
119+
}
120+
121+
Ok(())
122+
}
123+
107124
/// Clean and prepare templates directory to ensure fresh embedded templates
108125
fn clean_and_prepare_templates(services: &Services, verbose: bool) -> Result<()> {
109126
// Clean templates directory to ensure we use fresh templates from embedded resources

0 commit comments

Comments
 (0)