use super::*;
use std::fs;
use std::path::PathBuf;
#[test]
fn test_ensure_dir_exists() {
let test_dir = PathBuf::from("test_dir/subdir");
if test_dir.exists() {
fs::remove_dir_all(&test_dir).unwrap();
}
ensure_dir_exists(&test_dir).unwrap();
assert!(test_dir.exists());
fs::remove_dir_all(&test_dir).unwrap();
}
#[test]
fn test_ensure_parent_dir_exists() {
let test_file = PathBuf::from("test_dir/subdir/file.txt");
if test_file.parent().unwrap().exists() {
fs::remove_dir_all(test_file.parent().unwrap()).unwrap();
}
ensure_parent_dir_exists(&test_file).unwrap();
assert!(test_file.parent().unwrap().exists());
fs::remove_dir_all(test_file.parent().unwrap()).unwrap();
}
#[test]
fn test_remove_file_if_exists() {
let test_dir = PathBuf::from("test_dir/remove_file_if_exists");
let test_file = test_dir.join("file.txt");
if test_dir.exists() {
fs::remove_dir_all(&test_dir).unwrap();
}
fs::create_dir_all(&test_dir).unwrap();
fs::write(&test_file, "test").unwrap();
remove_file_if_exists(&test_file).unwrap();
assert!(!test_file.exists());
remove_file_if_exists(&test_file).unwrap();
fs::remove_dir_all(&test_dir).unwrap();
}
#[test]
fn test_remove_dir_all_if_exists() {
let test_dir = PathBuf::from("test_dir/remove_dir_all_if_exists");
let nested_dir = test_dir.join("subdir");
let test_file = nested_dir.join("file.txt");
if test_dir.exists() {
fs::remove_dir_all(&test_dir).unwrap();
}
fs::create_dir_all(&nested_dir).unwrap();
fs::write(&test_file, "test").unwrap();
remove_dir_all_if_exists(&test_dir).unwrap();
assert!(!test_dir.exists());
remove_dir_all_if_exists(&test_dir).unwrap();
}