diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-05-16 22:05:55 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-05-16 22:05:55 +0200 |
| commit | 0065feab2673862d168a6271e680c6115a7e9a73 (patch) | |
| tree | 103085472e213d71850ebd9c730ba9e08f929c0e /src | |
| parent | 56d3e03f4812b872eccc9f0e1800a4fd978d7367 (diff) | |
Add tests for blog_statusrbdr-add-tests-and-gema-texto
Diffstat (limited to 'src')
| -rw-r--r-- | src/command/status/blog_status.rs | 84 | ||||
| -rw-r--r-- | src/command/status/mod.rs | 48 |
2 files changed, 132 insertions, 0 deletions
diff --git a/src/command/status/blog_status.rs b/src/command/status/blog_status.rs index 3f42f32..32881d0 100644 --- a/src/command/status/blog_status.rs +++ b/src/command/status/blog_status.rs @@ -29,3 +29,87 @@ fn count_entries(path: &PathBuf) -> String { Err(_) => "0".to_string(), } } + +#[cfg(test)] +mod tests { + use std::fs::create_dir_all; + use std::path::PathBuf; + + use crate::configuration::Configuration; + + use test_utilities::*; + + use super::*; + + #[test] + fn count_entries_counts_the_number_of_entries() { + let test_dir = setup_test_dir(); + + assert_eq!(count_entries(&test_dir), "0"); + + let first_post_dir = test_dir.join("1736035200055"); + create_dir_all(&first_post_dir).expect("Could not create first post dir"); + assert_eq!(count_entries(&test_dir), "1"); + + let second_post_dir = test_dir.join("1736035200058"); + create_dir_all(&second_post_dir).expect("Could not create second post dir"); + assert_eq!(count_entries(&test_dir), "2"); + + cleanup_test_dir(&test_dir); + } + + #[test] + fn count_entries_defaults_to_zero_in_case_of_error() { + let test_dir = PathBuf::from("/ohnokrillin"); + assert_eq!(count_entries(&test_dir), "0"); + } + + #[test] + fn blog_status_counts_the_number_of_entries_in_blog_and_archive() { + let test_dir = setup_test_dir(); + let posts_dir = test_dir.join("blog"); + let archive_dir = test_dir.join("archive"); + + create_dir_all(&posts_dir).expect("Could not create blog dir"); + create_dir_all(posts_dir.join("0")).expect("Could not create blog post 0 dir"); + create_dir_all(posts_dir.join("1")).expect("Could not create blog post 1 dir"); + create_dir_all(posts_dir.join("2")).expect("Could not create blog post 2 dir"); + + create_dir_all(&archive_dir).expect("Could not create blog dir"); + create_dir_all(archive_dir.join("0")).expect("Could not create blog post 0 dir"); + create_dir_all(archive_dir.join("1")).expect("Could not create blog post 1 dir"); + create_dir_all(archive_dir.join("2")).expect("Could not create blog post 2 dir"); + create_dir_all(archive_dir.join("3")).expect("Could not create blog post 3 dir"); + create_dir_all(archive_dir.join("4")).expect("Could not create blog post 4 dir"); + create_dir_all(archive_dir.join("5")).expect("Could not create blog post 5 dir"); + + let mut configuration = Configuration::new().unwrap(); + configuration.archive_directory = archive_dir.clone(); + configuration.posts_directory = posts_dir.clone(); + + if let Ok(output) = status(&configuration) { + assert!(output.contains("blog: 3")); + assert!(output.contains("archive: 6")); + } else { + panic!("Could not generate status output."); + } + } + + #[test] + fn blog_status_prints_zero_if_directories_do_not_exist() { + let test_dir = setup_test_dir(); + let posts_dir = test_dir.join("blog"); + let archive_dir = test_dir.join("archive"); + + let mut configuration = Configuration::new().unwrap(); + configuration.archive_directory = archive_dir.clone(); + configuration.posts_directory = posts_dir.clone(); + + if let Ok(output) = status(&configuration) { + assert!(output.contains("blog: 0")); + assert!(output.contains("archive: 0")); + } else { + panic!("Could not generate status output."); + } + } +} diff --git a/src/command/status/mod.rs b/src/command/status/mod.rs index 3bad923..5b547bb 100644 --- a/src/command/status/mod.rs +++ b/src/command/status/mod.rs @@ -41,3 +41,51 @@ impl super::Command for Status { fn available_status_providers() -> Vec<fn(&Configuration) -> Result<String>> { vec![configuration_status::status, blog_status::status] } + +#[cfg(test)] +mod tests { + + use super::*; + use crate::command::Command; + use crate::configuration::Configuration; + + #[test] + fn test_status_command() { + let status = Status::new(); + + let configuration = Configuration::new().unwrap(); + status + .execute(None, &configuration, "") + .expect("Could not call status"); + } + + #[test] + fn status_before_dependencies() { + let status = Status::new(); + let dependencies = status.before_dependencies(); + + assert_eq!(dependencies.len(), 0); + } + + #[test] + fn status_after_dependencies() { + let status = Status::new(); + let dependencies = status.after_dependencies(); + + assert_eq!(dependencies.len(), 0); + } + + // These two tests feel pointless but I'm doing it for the coverage :p + + #[test] + fn status_command_output() { + let status = Status::new(); + status.command(); + } + + #[test] + fn status_help_output() { + let status = Status::new(); + status.help(); + } +} |