use super::{generate::Generate, sync_down::SyncDown, sync_up::SyncUp, update::Update}; use crate::configuration::Configuration; use std::fs::{create_dir_all, remove_dir_all, rename}; use std::io::Result; pub struct Add; impl Add { pub fn new() -> Self { Add } } impl super::Command for Add { fn before_dependencies(&self) -> Vec> { vec![Box::new(SyncDown::new())] } fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &str) -> Result<()> { create_dir_all(&configuration.posts_directory)?; for i in (0..configuration.max_posts - 1).rev() { let source = configuration.posts_directory.join(i.to_string()); let target = configuration.posts_directory.join((i + 1).to_string()); if target.exists() { remove_dir_all(&target)?; } if source.exists() { rename(&source, &target)?; } } Ok(()) } fn after_dependencies(&self) -> Vec> { vec![ Box::new(Update::new()), Box::new(Generate::new()), Box::new(SyncUp::new()), ] } fn command(&self) -> &'static str { "add" } fn help(&self) -> &'static str { "\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().unwrap(); 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.first().unwrap().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.first().unwrap().command(), "update"); assert_eq!(dependencies.get(1).unwrap().command(), "generate"); assert_eq!(dependencies.get(2).unwrap().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(); } }