From b0a5901513c5f9bc32311929acbf68a804a201c6 Mon Sep 17 00:00:00 2001 From: Sebastien Rousseau Date: Thu, 16 May 2024 00:05:42 +0100 Subject: [PATCH] test(libmake): :white_check_mark: Add new tests for `test_directory.rs` and refactoring others --- tests/macros/mod.rs | 17 ++++ tests/{ => macros}/test_ascii_macros.rs | 0 .../test_directory_macros.rs} | 0 tests/{ => macros}/test_file_macros.rs | 0 .../test_generator_macros.rs} | 0 tests/{ => macros}/test_log_macros.rs | 0 tests/{ => macros}/test_utility_macros.rs | 0 tests/mod.rs | 5 ++ tests/models/mod.rs | 5 ++ tests/{ => models}/test_error_ascii_art.rs | 0 tests/{ => models}/test_model_params.rs | 0 tests/utilities/mod.rs | 3 + tests/utilities/test_directory.rs | 90 +++++++++++++++++++ 13 files changed, 120 insertions(+) create mode 100644 tests/macros/mod.rs rename tests/{ => macros}/test_ascii_macros.rs (100%) rename tests/{test_macro_directory.rs => macros/test_directory_macros.rs} (100%) rename tests/{ => macros}/test_file_macros.rs (100%) rename tests/{test_macros.rs => macros/test_generator_macros.rs} (100%) rename tests/{ => macros}/test_log_macros.rs (100%) rename tests/{ => macros}/test_utility_macros.rs (100%) create mode 100644 tests/mod.rs create mode 100644 tests/models/mod.rs rename tests/{ => models}/test_error_ascii_art.rs (100%) rename tests/{ => models}/test_model_params.rs (100%) create mode 100644 tests/utilities/mod.rs create mode 100644 tests/utilities/test_directory.rs diff --git a/tests/macros/mod.rs b/tests/macros/mod.rs new file mode 100644 index 0000000..f6071d6 --- /dev/null +++ b/tests/macros/mod.rs @@ -0,0 +1,17 @@ +/// The `test_ascii_macros` module contains tests for the ascii_macros module. +pub mod test_ascii_macros; + +/// The `test_directory_macros` module contains tests for the directory_macros module. +pub mod test_directory_macros; + +/// The `test_file_macros` module contains tests for the file_macros module. +pub mod test_file_macros; + +/// The `test_generator_macros` module contains tests for the generator_macros module. +pub mod test_generator_macros; + +/// The `test_log_macros` module contains tests for the log_macros module. +pub mod test_log_macros; + +/// The `test_utility_macros` module contains tests for the utility_macros module. +pub mod test_utility_macros; diff --git a/tests/test_ascii_macros.rs b/tests/macros/test_ascii_macros.rs similarity index 100% rename from tests/test_ascii_macros.rs rename to tests/macros/test_ascii_macros.rs diff --git a/tests/test_macro_directory.rs b/tests/macros/test_directory_macros.rs similarity index 100% rename from tests/test_macro_directory.rs rename to tests/macros/test_directory_macros.rs diff --git a/tests/test_file_macros.rs b/tests/macros/test_file_macros.rs similarity index 100% rename from tests/test_file_macros.rs rename to tests/macros/test_file_macros.rs diff --git a/tests/test_macros.rs b/tests/macros/test_generator_macros.rs similarity index 100% rename from tests/test_macros.rs rename to tests/macros/test_generator_macros.rs diff --git a/tests/test_log_macros.rs b/tests/macros/test_log_macros.rs similarity index 100% rename from tests/test_log_macros.rs rename to tests/macros/test_log_macros.rs diff --git a/tests/test_utility_macros.rs b/tests/macros/test_utility_macros.rs similarity index 100% rename from tests/test_utility_macros.rs rename to tests/macros/test_utility_macros.rs diff --git a/tests/mod.rs b/tests/mod.rs new file mode 100644 index 0000000..a4f7626 --- /dev/null +++ b/tests/mod.rs @@ -0,0 +1,5 @@ +/// This module contains the tests for the `macros` module. +pub mod macros; + +/// This module contains the tests for the `models` module. +pub mod models; diff --git a/tests/models/mod.rs b/tests/models/mod.rs new file mode 100644 index 0000000..28849db --- /dev/null +++ b/tests/models/mod.rs @@ -0,0 +1,5 @@ +/// The `test_error_ascii_art` module contains tests for the error_ascii_art module. +pub mod test_error_ascii_art; + +/// The `test_model_params` module contains tests for the model_params module. +pub mod test_model_params; diff --git a/tests/test_error_ascii_art.rs b/tests/models/test_error_ascii_art.rs similarity index 100% rename from tests/test_error_ascii_art.rs rename to tests/models/test_error_ascii_art.rs diff --git a/tests/test_model_params.rs b/tests/models/test_model_params.rs similarity index 100% rename from tests/test_model_params.rs rename to tests/models/test_model_params.rs diff --git a/tests/utilities/mod.rs b/tests/utilities/mod.rs new file mode 100644 index 0000000..ddda348 --- /dev/null +++ b/tests/utilities/mod.rs @@ -0,0 +1,3 @@ +/// The `test_directory.rs` module contains tests for the directory module. +pub mod test_directory; + diff --git a/tests/utilities/test_directory.rs b/tests/utilities/test_directory.rs new file mode 100644 index 0000000..6c376f7 --- /dev/null +++ b/tests/utilities/test_directory.rs @@ -0,0 +1,90 @@ +#[cfg(test)] +mod tests { + use libmake::utilities::directory::directory; + use std::fs; + use std::io::Error; + use std::path::Path; + use tempfile::tempdir; + + #[test] + fn test_directory_exists() { + let dir = tempdir().unwrap(); + let path = dir.path().join("logs"); + + fs::create_dir(&path).unwrap(); + let result = directory(&path, "logs"); + assert!(result.is_ok()); + assert_eq!(result.unwrap(), ""); + } + + #[test] + fn test_directory_create() { + let dir = tempdir().unwrap(); + let path = dir.path().join("logs"); + + let result = directory(&path, "logs"); + assert!(result.is_ok()); + assert_eq!(result.unwrap(), ""); + assert!(path.exists()); + } + + #[test] + fn test_directory_not_a_directory() { + let dir = tempdir().unwrap(); + let file_path = dir.path().join("not_a_directory"); + + fs::File::create(&file_path).unwrap(); + let result = directory(&file_path, "not_a_directory"); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), "❌ Error: not_a_directory is not a directory."); + } + + #[test] + fn test_move_output_directory() { + let dir = tempdir().unwrap(); + let out_dir = dir.path().join("out"); + + fs::create_dir(&out_dir).unwrap(); + let result = move_output_directory("site_name", &out_dir); + assert!(result.is_ok()); + + let public_dir = dir.path().join("public"); + let new_project_dir = public_dir.join("site_name"); + assert!(new_project_dir.exists()); + } + + #[test] + fn test_cleanup_directory() { + let dir = tempdir().unwrap(); + let cleanup_dir = dir.path().join("cleanup"); + + fs::create_dir(&cleanup_dir).unwrap(); + let result = cleanup_directory(&[cleanup_dir.as_path()]); + assert!(result.is_ok()); + assert!(!cleanup_dir.exists()); + } + + #[test] + fn test_create_directory() { + let dir = tempdir().unwrap(); + let create_dir = dir.path().join("create"); + + let result = create_directory(&[create_dir.as_path()]); + assert!(result.is_ok()); + assert!(create_dir.exists()); + } + + #[test] + fn test_truncate_path() { + let path = Path::new("/a/b/c/d/e"); + + let result = truncate(&path, 3); + assert_eq!(result, Some("c/d/e".to_string())); + + let result = truncate(&path, 0); + assert_eq!(result, None); + + let result = truncate(&path, 10); + assert_eq!(result, None); + } +}