use optionstratlib::utils::file::prepare_file_path;
use std::fs;
use tempfile::tempdir;
#[test]
fn test_prepare_file_path_new_file() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let file_path = temp_dir.path().join("test_file.txt");
let result = prepare_file_path(&file_path);
assert!(
result.is_ok(),
"prepare_file_path should succeed for a new file"
);
assert!(
file_path.parent().unwrap().exists(),
"Parent directory should exist"
);
assert!(!file_path.exists(), "File should not exist yet");
}
#[test]
fn test_prepare_file_path_existing_file() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let file_path = temp_dir.path().join("existing_file.txt");
fs::write(&file_path, "test content").expect("Failed to write test file");
assert!(file_path.exists(), "File should exist before test");
let result = prepare_file_path(&file_path);
assert!(
result.is_ok(),
"prepare_file_path should succeed for an existing file"
);
assert!(!file_path.exists(), "File should have been removed");
}
#[test]
fn test_prepare_file_path_nested_directories() {
let temp_dir = tempdir().expect("Failed to create temp directory");
let nested_path = temp_dir.path().join("nested/dirs/for/test_file.txt");
assert!(
!nested_path.parent().unwrap().exists(),
"Nested directories should not exist yet"
);
let result = prepare_file_path(&nested_path);
assert!(
result.is_ok(),
"prepare_file_path should succeed for nested directories"
);
assert!(
nested_path.parent().unwrap().exists(),
"Nested directories should have been created"
);
}
#[test]
fn test_prepare_file_path_permission_error() {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let temp_dir = tempdir().expect("Failed to create temp directory");
let readonly_dir = temp_dir.path().join("readonly");
fs::create_dir(&readonly_dir).expect("Failed to create readonly directory");
let file_path = readonly_dir.join("test_file.txt");
fs::write(&file_path, "test content").expect("Failed to write test file");
let metadata = fs::metadata(&readonly_dir).expect("Failed to get metadata");
let mut perms = metadata.permissions();
perms.set_mode(0o555); fs::set_permissions(&readonly_dir, perms).expect("Failed to set permissions");
let result = prepare_file_path(&file_path);
assert!(
result.is_err(),
"prepare_file_path should fail when removing file in readonly directory"
);
}
}