Skip to content

Commit 72f6d4d

Browse files
committed
refactor: extract reset_templates_dir method in TemplateManager
- Add reset_templates_dir() method that combines clean_templates_dir() and ensure_templates_dir() - Extract clean_and_prepare_templates() method in e2e_tests.rs to improve code organization - Add comprehensive unit tests for the new reset functionality - Improve code reusability and maintainability by centralizing template directory reset logic
1 parent 1ea6bda commit 72f6d4d

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

src/bin/e2e_tests.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,8 @@ impl TestEnvironment {
8282
// Create services using the configuration
8383
let services = Services::new(&config);
8484

85-
// Clean templates directory to ensure we use fresh templates from embedded resources
86-
if verbose {
87-
println!("🧹 Cleaning templates directory to ensure fresh embedded templates...");
88-
}
89-
services.template_manager.clean_templates_dir()?;
90-
services.template_manager.ensure_templates_dir()?;
85+
// Clean and prepare templates directory
86+
Self::clean_and_prepare_templates(&services, verbose)?;
9187

9288
if verbose {
9389
println!(
@@ -108,6 +104,19 @@ impl TestEnvironment {
108104
})
109105
}
110106

107+
/// Clean and prepare templates directory to ensure fresh embedded templates
108+
fn clean_and_prepare_templates(services: &Services, verbose: bool) -> Result<()> {
109+
// Clean templates directory to ensure we use fresh templates from embedded resources
110+
if verbose {
111+
println!("🧹 Cleaning templates directory to ensure fresh embedded templates...");
112+
}
113+
services
114+
.template_manager
115+
.reset_templates_dir()
116+
.map_err(|e| anyhow::anyhow!(e))?;
117+
Ok(())
118+
}
119+
111120
/// Stage 1: Render provision templates (`OpenTofu`) to build/tofu/ directory
112121
async fn render_provision_templates(&self) -> Result<()> {
113122
self.services

src/template/embedded.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ impl TemplateManager {
7676
Ok(())
7777
}
7878

79+
/// Clean and prepare the templates directory to ensure fresh embedded templates
80+
///
81+
/// This method combines `clean_templates_dir()` and `ensure_templates_dir()` to provide
82+
/// a clean slate for template operations. It's particularly useful in testing and
83+
/// development environments where you want to ensure fresh templates from embedded resources.
84+
///
85+
/// # Errors
86+
///
87+
/// Returns an error if either directory cleaning or creation fails due to permissions
88+
/// or filesystem issues.
89+
pub fn reset_templates_dir(&self) -> Result<(), TemplateManagerError> {
90+
self.clean_templates_dir()?;
91+
self.ensure_templates_dir()?;
92+
Ok(())
93+
}
94+
7995
/// Get the path to a template file, creating it from embedded resources if it doesn't exist
8096
///
8197
/// # Errors
@@ -473,4 +489,49 @@ mod tests {
473489
let result = manager.clean_templates_dir();
474490
assert!(result.is_ok());
475491
}
492+
493+
#[test]
494+
fn it_should_reset_templates_directory() {
495+
let temp_dir = TempDir::new().unwrap();
496+
let templates_path = temp_dir.path().join("test_templates");
497+
498+
let manager = TemplateManager::new(&templates_path);
499+
500+
// Initially the directory should not exist
501+
assert!(!templates_path.exists());
502+
503+
// First, create the directory and some templates
504+
manager.ensure_templates_dir().unwrap();
505+
let template_path = manager.get_template_path("ansible/ansible.cfg").unwrap();
506+
assert!(template_path.exists());
507+
assert!(templates_path.exists());
508+
509+
// Now use the combined method
510+
manager.reset_templates_dir().unwrap();
511+
512+
// Directory should exist but templates should be gone
513+
assert!(templates_path.exists());
514+
assert!(templates_path.is_dir());
515+
// Old template file should be gone (directory was cleaned)
516+
assert!(!template_path.exists());
517+
}
518+
519+
#[test]
520+
fn it_should_reset_templates_directory_on_nonexistent_directory() {
521+
let temp_dir = TempDir::new().unwrap();
522+
let templates_path = temp_dir.path().join("nonexistent_templates");
523+
524+
let manager = TemplateManager::new(&templates_path);
525+
526+
// Directory should not exist initially
527+
assert!(!templates_path.exists());
528+
529+
// Combined method should work on non-existent directory
530+
let result = manager.reset_templates_dir();
531+
assert!(result.is_ok());
532+
533+
// Directory should now exist
534+
assert!(templates_path.exists());
535+
assert!(templates_path.is_dir());
536+
}
476537
}

0 commit comments

Comments
 (0)