util-easy 0.1.0

Small utility helpers for common tasks.
Documentation
use super::*;
use std::fs;
use std::path::PathBuf;

#[test]
fn test_ensure_dir_exists() {
    let test_dir = PathBuf::from("test_dir/subdir");
    // Ensure the directory does not exist before the test
    if test_dir.exists() {
        fs::remove_dir_all(&test_dir).unwrap();
    }

    // Call the function to ensure the directory exists
    ensure_dir_exists(&test_dir).unwrap();

    // Check that the directory now exists
    assert!(test_dir.exists());

    // Clean up after the test
    fs::remove_dir_all(&test_dir).unwrap();
}

#[test]
fn test_ensure_parent_dir_exists() {
    let test_file = PathBuf::from("test_dir/subdir/file.txt");
    // Ensure the parent directory does not exist before the test
    if test_file.parent().unwrap().exists() {
        fs::remove_dir_all(test_file.parent().unwrap()).unwrap();
    }

    // Call the function to ensure the parent directory exists
    ensure_parent_dir_exists(&test_file).unwrap();

    // Check that the parent directory now exists
    assert!(test_file.parent().unwrap().exists());

    // Clean up after the test
    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();
}