Skip to content

Commit

Permalink
test(libmake): ✅ Add new tests for test_directory.rs and refactorin…
Browse files Browse the repository at this point in the history
…g others
  • Loading branch information
sebastienrousseau committed May 15, 2024
1 parent 9d0375a commit b0a5901
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/macros/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions tests/models/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions tests/utilities/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// The `test_directory.rs` module contains tests for the directory module.
pub mod test_directory;

90 changes: 90 additions & 0 deletions tests/utilities/test_directory.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit b0a5901

Please sign in to comment.