#[ cfg(test) ]
mod cleanup_functionality_tests
{
use test_tools ::SmokeModuleTest;
#[ test ]
fn test_cleanup_after_successful_test()
{
let mut smoke_test = SmokeModuleTest ::new("success_cleanup_test");
smoke_test.dependency_version("serde", "1.0").expect("Should configure dependency");
smoke_test.code("use serde;".to_string());
smoke_test.form().expect("Should be able to form project");
let project_path = smoke_test.project_path();
assert!(project_path.exists(), "Project directory should exist after form()");
assert!(project_path.join("Cargo.toml").exists(), "Cargo.toml should exist");
assert!(project_path.join("src/main.rs").exists(), "main.rs should exist");
let result = smoke_test.perform();
assert!(!project_path.exists(), "Project directory should be cleaned up after successful test");
assert!(!smoke_test.test_path.exists(), "Test path should be cleaned up after successful test");
assert!(result.is_ok(), "Smoke test should succeed");
}
#[ test ]
fn test_cleanup_after_failed_test()
{
let mut smoke_test = SmokeModuleTest ::new("failure_cleanup_test");
smoke_test.dependency_version("nonexistent_crate_that_will_fail", "999.999.999")
.expect("Should be able to configure dependency");
smoke_test.form().expect("Should be able to form project");
let project_path = smoke_test.project_path();
assert!(project_path.exists(), "Project directory should exist after form()");
let result = smoke_test.perform();
assert!(!project_path.exists(), "Project directory should be cleaned up after failed test");
assert!(!smoke_test.test_path.exists(), "Test path should be cleaned up after failed test");
assert!(result.is_err(), "Smoke test should fail due to invalid dependency");
}
#[ test ]
fn test_complete_file_removal()
{
let mut smoke_test = SmokeModuleTest ::new("complete_removal_test");
smoke_test.form().expect("Should be able to form project");
let project_path = smoke_test.project_path();
let extra_file = project_path.join("extra_test_file.txt");
let extra_dir = project_path.join("extra_directory");
let nested_file = extra_dir.join("nested_file.txt");
std ::fs ::write(&extra_file, "test content").expect("Should be able to create extra file");
std ::fs ::create_dir(&extra_dir).expect("Should be able to create extra directory");
std ::fs ::write(&nested_file, "nested content").expect("Should be able to create nested file");
assert!(project_path.exists(), "Project directory should exist");
assert!(extra_file.exists(), "Extra file should exist");
assert!(extra_dir.exists(), "Extra directory should exist");
assert!(nested_file.exists(), "Nested file should exist");
let result = smoke_test.clean(false);
assert!(result.is_ok(), "Cleanup should succeed");
assert!(!project_path.exists(), "Project directory should be completely removed");
assert!(!extra_file.exists(), "Extra file should be removed");
assert!(!extra_dir.exists(), "Extra directory should be removed");
assert!(!nested_file.exists(), "Nested file should be removed");
assert!(!smoke_test.test_path.exists(), "Root test path should be removed");
}
#[ test ]
fn test_force_cleanup_option()
{
let mut smoke_test = SmokeModuleTest ::new("force_cleanup_test");
smoke_test.form().expect("Should be able to form project");
let project_path = smoke_test.project_path();
let restricted_file = project_path.join("restricted_file.txt");
std ::fs ::write(&restricted_file, "restricted content").expect("Should be able to create file");
#[ cfg(unix) ]
{
use std ::os ::unix ::fs ::PermissionsExt;
let mut perms = std ::fs ::metadata(&project_path).unwrap().permissions();
perms.set_mode(0o444); std ::fs ::set_permissions(&project_path, perms).expect("Should be able to set permissions");
}
let force_result = smoke_test.clean(true);
assert!(force_result.is_ok(), "Force cleanup should succeed even with permission issues");
#[ cfg(unix) ]
{
use std ::os ::unix ::fs ::PermissionsExt;
if project_path.exists()
{
let mut perms = std ::fs ::metadata(&project_path).unwrap().permissions();
perms.set_mode(0o755); std ::fs ::set_permissions(&project_path, perms).ok();
}
}
if smoke_test.test_path.exists()
{
std ::fs ::remove_dir_all(&smoke_test.test_path).ok();
}
}
#[ test ]
fn test_cleanup_error_handling()
{
let mut smoke_test = SmokeModuleTest ::new("error_handling_test");
smoke_test.form().expect("Should be able to form project");
let project_path = smoke_test.project_path();
let problematic_file = project_path.join("problematic_file.txt");
std ::fs ::write(&problematic_file, "problematic content").expect("Should be able to create file");
let mut test_smoke = SmokeModuleTest ::new("error_test2");
test_smoke.test_path = std ::path ::PathBuf ::from("/invalid/path/that/does/not/exist");
let force_result = test_smoke.clean(true);
assert!(force_result.is_ok(), "Force cleanup should succeed even with invalid paths");
let non_force_result = test_smoke.clean(false);
let _ = non_force_result;
#[ cfg(unix) ]
{
use std ::os ::unix ::fs ::PermissionsExt;
if project_path.exists()
{
let mut perms = std ::fs ::metadata(&project_path).unwrap().permissions();
perms.set_mode(0o755); std ::fs ::set_permissions(&project_path, perms).ok();
}
}
if smoke_test.test_path.exists()
{
std ::fs ::remove_dir_all(&smoke_test.test_path).ok();
}
}
#[ test ]
fn test_automatic_cleanup_integration()
{
let mut smoke_test = SmokeModuleTest ::new("integration_cleanup_test");
smoke_test.dependency_version("serde", "1.0").expect("Should configure dependency");
smoke_test.code("use serde;".to_string());
let test_path = smoke_test.test_path.clone();
smoke_test.form().expect("Should be able to form project");
let project_path = smoke_test.project_path();
assert!(project_path.exists(), "Project should exist before execution");
assert!(test_path.exists(), "Test path should exist before execution");
let result = smoke_test.perform();
assert!(!project_path.exists(), "Project should be automatically cleaned up after execution");
assert!(!test_path.exists(), "Test path should be automatically cleaned up after execution");
assert!(result.is_ok(), "Smoke test execution should succeed");
}
#[ test ]
fn test_nested_directory_cleanup()
{
let mut smoke_test = SmokeModuleTest ::new("nested_cleanup_test");
smoke_test.form().expect("Should be able to form project");
let project_path = smoke_test.project_path();
let deep_dir = project_path.join("level1").join("level2").join("level3");
std ::fs ::create_dir_all(&deep_dir).expect("Should be able to create nested directories");
let files_to_create = [
project_path.join("root_file.txt"),
project_path.join("level1").join("level1_file.txt"),
deep_dir.join("deep_file.txt"),
];
for file_path in &files_to_create
{
std ::fs ::write(file_path, "test content").expect("Should be able to create file");
}
assert!(deep_dir.exists(), "Deep directory should exist");
for file_path in &files_to_create
{
assert!(file_path.exists(), "File should exist: {}", file_path.display());
}
let result = smoke_test.clean(false);
assert!(result.is_ok(), "Cleanup should succeed");
assert!(!project_path.exists(), "Project directory should be completely removed");
assert!(!deep_dir.exists(), "Deep directory should be removed");
for file_path in &files_to_create
{
assert!(!file_path.exists(), "File should be removed: {}", file_path.display());
}
assert!(!smoke_test.test_path.exists(), "Root test path should be removed");
}
#[ test ]
fn test_cleanup_timing()
{
let mut smoke_test = SmokeModuleTest ::new("timing_cleanup_test");
let test_path = smoke_test.test_path.clone();
assert!(!test_path.exists(), "Test path should not exist initially");
smoke_test.form().expect("Should be able to form project");
assert!(test_path.exists(), "Test path should exist after form()");
let project_path = smoke_test.project_path();
assert!(project_path.exists(), "Project path should exist after form()");
smoke_test.clean(false).expect("Manual cleanup should succeed");
assert!(!test_path.exists(), "Test path should not exist after manual cleanup");
assert!(!project_path.exists(), "Project path should not exist after manual cleanup");
let second_cleanup = smoke_test.clean(false);
assert!(second_cleanup.is_ok(), "Second cleanup should be safe and succeed");
}
}