From 64296ca9c83011e3de4d9f1be02335d8eb1bfe95 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sat, 18 Jan 2025 13:27:03 +0100 Subject: Add tests for --- src/command/add.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) (limited to 'src/command/add.rs') diff --git a/src/command/add.rs b/src/command/add.rs index d38d077..9622f81 100644 --- a/src/command/add.rs +++ b/src/command/add.rs @@ -48,3 +48,98 @@ impl super::Command for Add { "\t\t\tCreates new blog post" } } + +#[cfg(test)] +mod tests { + use std::fs::create_dir_all; + + use super::*; + use crate::command::Command; + use crate::configuration::Configuration; + + use test_utilities::*; + + #[test] + fn test_add_command() { + let add = Add::new(); + + // Create all directories + let test_dir = setup_test_dir(); + + let posts_dir = test_dir.join("posts"); + create_dir_all(&posts_dir).expect("Could not create posts test directory"); + create_dir_all(&posts_dir.join("0")).expect("Could not create post 0 test directory"); + + // Create Test Files + + // Create configuration + let mut configuration = Configuration::new(); + configuration.posts_directory = posts_dir.clone(); + + // Let's ensure the initial state is OK + assert!(&posts_dir.join("0").exists()); + assert!(!&posts_dir.join("1").exists()); + assert!(!&posts_dir.join("2").exists()); + assert!(!&posts_dir.join("3").exists()); + + // Add 1 of 3 + add.execute(None, &configuration, "add") + .expect("Could not add a.gmi"); + assert!(!&posts_dir.join("0").exists()); + assert!(&posts_dir.join("1").exists()); + assert!(!&posts_dir.join("2").exists()); + assert!(!&posts_dir.join("3").exists()); + + // Add 2 of 3 + add.execute(None, &configuration, "add") + .expect("Could not add a.gmi"); + assert!(!&posts_dir.join("0").exists()); + assert!(!&posts_dir.join("1").exists()); + assert!(&posts_dir.join("2").exists()); + assert!(!&posts_dir.join("3").exists()); + + // Add 3 of 3 + add.execute(None, &configuration, "add") + .expect("Could not add a.gmi"); + assert!(!&posts_dir.join("0").exists()); + assert!(!&posts_dir.join("1").exists()); + assert!(!&posts_dir.join("2").exists()); + assert!(!&posts_dir.join("3").exists()); + + cleanup_test_dir(&test_dir); + } + + #[test] + fn add_before_dependencies() { + let add = Add::new(); + let dependencies = add.before_dependencies(); + + assert_eq!(dependencies.len(), 1); + assert_eq!(dependencies[0].command(), "sync-down"); + } + + #[test] + fn add_after_dependencies() { + let add = Add::new(); + let dependencies = add.after_dependencies(); + + assert_eq!(dependencies.len(), 3); + assert_eq!(dependencies[0].command(), "update"); + assert_eq!(dependencies[1].command(), "generate"); + assert_eq!(dependencies[2].command(), "sync-up"); + } + + // These two tests feel pointless but I'm doing it for the coverage :p + + #[test] + fn add_command_output() { + let add = Add::new(); + add.command(); + } + + #[test] + fn add_help_output() { + let add = Add::new(); + add.help(); + } +} -- cgit