use std::fs;
use tempfile::TempDir;
use tree_type::GenericDir;
use tree_type::GenericFile;
#[test]
fn test_generic_file_rename_success() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path();
let original_path = temp_path.join("original.txt");
fs::write(&original_path, "test content").unwrap();
let file = GenericFile::new(&original_path).unwrap();
let new_path = temp_path.join("renamed.txt");
let renamed_file = file.rename(&new_path).unwrap();
assert!(!original_path.exists());
assert!(new_path.exists());
assert_eq!(fs::read_to_string(&new_path).unwrap(), "test content");
assert_eq!(renamed_file.as_path(), new_path);
}
#[test]
fn test_generic_file_rename_with_parent_creation() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path();
let original_path = temp_path.join("original.txt");
fs::write(&original_path, "test content").unwrap();
let file = GenericFile::new(&original_path).unwrap();
let new_path = temp_path.join("nested").join("deep").join("renamed.txt");
let renamed_file = file.rename(&new_path).unwrap();
assert!(!original_path.exists());
assert!(new_path.exists());
assert!(new_path.parent().unwrap().exists());
assert_eq!(fs::read_to_string(&new_path).unwrap(), "test content");
assert_eq!(renamed_file.as_path(), new_path);
}
#[test]
fn test_generic_file_rename_error_recovery() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path();
let original_path = temp_path.join("original.txt");
fs::write(&original_path, "test content").unwrap();
let file = GenericFile::new(&original_path).unwrap();
let blocking_file = temp_path.join("blocking");
fs::write(&blocking_file, "blocking").unwrap();
let invalid_path = blocking_file.join("cannot_create_here.txt");
match file.rename(&invalid_path) {
Ok(_) => panic!("Expected rename to fail"),
Err((error, recovered_file)) => {
assert!(
error.kind() == std::io::ErrorKind::NotADirectory
|| error.kind() == std::io::ErrorKind::PermissionDenied
);
assert_eq!(recovered_file.as_path(), original_path);
assert!(original_path.exists());
assert_eq!(recovered_file.read_to_string().unwrap(), "test content");
}
}
}
#[test]
fn test_generic_dir_rename_success() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path();
let original_path = temp_path.join("original_dir");
fs::create_dir(&original_path).unwrap();
fs::write(original_path.join("file.txt"), "content").unwrap();
let dir = GenericDir::new(&original_path).unwrap();
let new_path = temp_path.join("renamed_dir");
let renamed_dir = dir.rename(&new_path).unwrap();
assert!(!original_path.exists());
assert!(new_path.exists());
assert!(new_path.join("file.txt").exists());
assert_eq!(
fs::read_to_string(new_path.join("file.txt")).unwrap(),
"content"
);
assert_eq!(renamed_dir.as_path(), new_path);
}
#[test]
fn test_generic_dir_rename_with_parent_creation() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path();
let original_path = temp_path.join("original_dir");
fs::create_dir(&original_path).unwrap();
fs::write(original_path.join("file.txt"), "content").unwrap();
let dir = GenericDir::new(&original_path).unwrap();
let new_path = temp_path.join("nested").join("deep").join("renamed_dir");
let renamed_dir = dir.rename(&new_path).unwrap();
assert!(!original_path.exists());
assert!(new_path.exists());
assert!(new_path.parent().unwrap().exists());
assert!(new_path.join("file.txt").exists());
assert_eq!(renamed_dir.as_path(), new_path);
}
#[test]
fn test_generic_dir_rename_error_recovery() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path();
let original_path = temp_path.join("original_dir");
fs::create_dir(&original_path).unwrap();
fs::write(original_path.join("file.txt"), "content").unwrap();
let dir = GenericDir::new(&original_path).unwrap();
let existing_file = temp_path.join("existing_file");
fs::write(&existing_file, "blocking").unwrap();
match dir.rename(&existing_file) {
Ok(_) => panic!("Expected rename to fail"),
Err((error, recovered_dir)) => {
assert!(
error.kind() == std::io::ErrorKind::NotADirectory
|| error.kind() == std::io::ErrorKind::AlreadyExists
|| error.kind() == std::io::ErrorKind::PermissionDenied
);
assert_eq!(recovered_dir.as_path(), original_path);
assert!(original_path.exists());
assert!(recovered_dir.read_dir().is_ok());
}
}
}
#[test]
fn test_rename_nonexistent_file() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path();
let nonexistent_path = temp_path.join("nonexistent.txt");
let file = GenericFile::new(&nonexistent_path).unwrap();
let new_path = temp_path.join("renamed.txt");
match file.rename(&new_path) {
Ok(_) => panic!("Expected rename to fail"),
Err((error, recovered_file)) => {
assert_eq!(error.kind(), std::io::ErrorKind::NotFound);
assert_eq!(recovered_file.as_path(), nonexistent_path);
}
}
}
#[test]
fn test_rename_to_same_path() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path();
let file_path = temp_path.join("test.txt");
fs::write(&file_path, "content").unwrap();
let file = GenericFile::new(&file_path).unwrap();
let renamed_file = file.rename(&file_path).unwrap();
assert!(file_path.exists());
assert_eq!(fs::read_to_string(&file_path).unwrap(), "content");
assert_eq!(renamed_file.as_path(), file_path);
}