#[ cfg( feature = "enabled" ) ]
use std::path::PathBuf;
#[ cfg( feature = "enabled" ) ]
use std::fs;
#[ cfg( feature = "enabled" ) ]
use fs_tools as the_module;
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn full_path_joins_components()
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.base_path = PathBuf::from( "/tmp" );
temp_dir.prefix_path = PathBuf::from( "test" );
temp_dir.postfix_path = PathBuf::from( "run_1" );
let full = temp_dir.full_path();
assert_eq!( full, PathBuf::from( "/tmp/test/run_1" ) );
}
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn full_path_handles_empty_components()
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.base_path = PathBuf::from( "/tmp" );
let full = temp_dir.full_path();
assert_eq!( full, PathBuf::from( "/tmp" ) );
}
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn full_path_postfix_only()
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.postfix_path = PathBuf::from( "suffix" );
let full = temp_dir.full_path();
assert_eq!( full, PathBuf::from( "suffix" ) );
}
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn create_creates_directory()
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.base_path = std::env::temp_dir();
temp_dir.postfix_path = PathBuf::from( format!( "fs_tools_create_test_{}", std::process::id() ) );
let path = temp_dir.create().expect( "create should succeed" );
assert!( path.exists(), "Directory should exist after create()" );
assert!( path.is_dir(), "Path should be a directory" );
assert_eq!( path, temp_dir.full_path() );
}
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn create_fails_without_parent()
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.base_path = PathBuf::from( "/nonexistent_parent_12345" );
temp_dir.prefix_path = PathBuf::from( "child" );
let result = temp_dir.create();
assert!( result.is_err(), "create() should fail without parent" );
}
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn create_all_creates_nested_directories()
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.base_path = std::env::temp_dir();
temp_dir.prefix_path = PathBuf::from( format!( "fs_tools_nested_{}", std::process::id() ) );
temp_dir.postfix_path = PathBuf::from( "deep/nested/path" );
let path = temp_dir.create_all().expect( "create_all should succeed" );
assert!( path.exists(), "Directory should exist after create_all()" );
assert!( path.is_dir(), "Path should be a directory" );
}
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn create_all_idempotent()
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.base_path = std::env::temp_dir();
temp_dir.prefix_path = PathBuf::from( format!( "fs_tools_idem_{}", std::process::id() ) );
let path1 = temp_dir.create_all().expect( "first create_all should succeed" );
let path2 = temp_dir.create_all().expect( "second create_all should succeed" );
assert_eq!( path1, path2, "Both calls should return same path" );
}
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn drop_cleans_up_created_directory()
{
let path_to_check;
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.base_path = std::env::temp_dir();
temp_dir.prefix_path = PathBuf::from( format!( "fs_tools_drop_{}", std::process::id() ) );
let path = temp_dir.create_all().expect( "create should succeed" );
path_to_check = path.clone();
assert!( path_to_check.exists(), "Directory should exist before drop" );
}
assert!( !path_to_check.exists(), "Directory should be removed after drop" );
}
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn drop_does_not_cleanup_manual_paths()
{
let manual_path = std::env::temp_dir().join( format!( "fs_tools_manual_{}", std::process::id() ) );
fs::create_dir_all( &manual_path ).expect( "manual create should succeed" );
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.base_path = manual_path.clone();
}
assert!( manual_path.exists(), "Manual directory should NOT be removed by drop" );
fs::remove_dir_all( &manual_path ).ok();
}
#[ test ]
#[ cfg( feature = "enabled" ) ]
fn drop_handles_already_deleted()
{
let mut temp_dir = the_module::TempDir::new();
temp_dir.base_path = std::env::temp_dir();
temp_dir.prefix_path = PathBuf::from( format!( "fs_tools_predelete_{}", std::process::id() ) );
let path = temp_dir.create_all().expect( "create should succeed" );
fs::remove_dir_all( &path ).expect( "manual delete should succeed" );
drop( temp_dir );
}