aboutsummaryrefslogtreecommitdiff
path: root/src/command
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-01-18 13:27:03 +0100
committerRuben Beltran del Rio <git@r.bdr.sh>2025-01-18 13:27:03 +0100
commit64296ca9c83011e3de4d9f1be02335d8eb1bfe95 (patch)
tree21d13b4689a2f52fc9d0b9077151cab0159d3cc6 /src/command
parent760b30e71e565d4db2a3d8f25af0d05fa174e871 (diff)
Add tests for
Diffstat (limited to 'src/command')
-rw-r--r--src/command/add.rs95
-rw-r--r--src/command/help.rs47
-rw-r--r--src/command/update.rs223
3 files changed, 365 insertions, 0 deletions
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 {
"<path_to_post>\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();
+ }
+}
diff --git a/src/command/help.rs b/src/command/help.rs
index b682fc0..da5831c 100644
--- a/src/command/help.rs
+++ b/src/command/help.rs
@@ -38,3 +38,50 @@ impl super::Command for Help {
"\t\t\t\tPrints this help"
}
}
+
+#[cfg(test)]
+mod tests {
+
+ use super::*;
+ use crate::command::Command;
+ use crate::configuration::Configuration;
+
+ #[test]
+ fn test_add_command() {
+ let help = Help::new();
+
+ let configuration = Configuration::new();
+ help.execute(None, &configuration, "")
+ .expect("Could not call help");
+ }
+
+ #[test]
+ fn help_before_dependencies() {
+ let help = Help::new();
+ let dependencies = help.before_dependencies();
+
+ assert_eq!(dependencies.len(), 0);
+ }
+
+ #[test]
+ fn help_after_dependencies() {
+ let help = Help::new();
+ let dependencies = help.after_dependencies();
+
+ assert_eq!(dependencies.len(), 0);
+ }
+
+ // These two tests feel pointless but I'm doing it for the coverage :p
+
+ #[test]
+ fn help_command_output() {
+ let help = Help::new();
+ help.command();
+ }
+
+ #[test]
+ fn help_help_output() {
+ let help = Help::new();
+ help.help();
+ }
+}
diff --git a/src/command/update.rs b/src/command/update.rs
index 54005f8..fedec13 100644
--- a/src/command/update.rs
+++ b/src/command/update.rs
@@ -40,6 +40,7 @@ impl Update {
let entry_target = target.join(entry_name);
if entry_type.is_dir() {
+ create_dir_all(&entry_target)?;
Update::archive(&entry_source, &entry_target)?;
} else {
copy(&entry_source, &entry_target)?;
@@ -110,3 +111,225 @@ impl super::Command for Update {
"<path_to_post>\t\tUpdates latest blog post"
}
}
+
+#[cfg(test)]
+mod tests {
+ use std::fs::{create_dir_all, read_dir};
+ use std::path::PathBuf;
+
+ use super::*;
+ use crate::command::Command;
+ use crate::configuration::Configuration;
+
+ use test_utilities::*;
+
+ #[test]
+ fn test_update_command() {
+ let update = Update::new();
+
+ // Create all directories
+ let test_dir = setup_test_dir();
+
+ // Reference but don't create the directories
+ let posts_dir = test_dir.join("posts");
+ let archive_dir = test_dir.join("archive");
+
+ // Create configuration
+ let mut configuration = Configuration::new();
+ configuration.posts_directory = posts_dir.clone();
+ configuration.archive_directory = archive_dir.clone();
+
+ // Create Test File
+ let test_file = test_dir.join("cool-test-file-😎.gmi");
+ let test_file_string = test_file.display().to_string();
+ create_test_file(&test_file, "# I try.");
+
+ // Update 1 of 2
+ update.execute(Some(&test_file_string), &configuration, "update")
+ .expect("Could not update a .gmi");
+ assert!(&posts_dir.join("0").exists());
+ assert_file_contents(&posts_dir.join("0/cool-test-file-😎.gmi"), "# I try.");
+
+ let entries = read_dir(&archive_dir)
+ .expect("Could not read archive");
+ let mut found_path = PathBuf::new();
+ for entry in entries {
+ let entry = entry
+ .expect("Could not read entry");
+ found_path = entry.path();
+ assert_file_contents(&found_path.join("cool-test-file-😎.gmi"), "# I try.");
+ assert!(&found_path.join("metadata.json").exists());
+ }
+
+ // Create Second Test File
+ let second_test_file = test_dir.join("cooler-test-file-😎.gmi");
+ let second_test_file_string = second_test_file.display().to_string();
+ create_test_file(&second_test_file, "# I REALLY try.");
+
+ // Update 2 of 2
+ update.execute(Some(&second_test_file_string), &configuration, "update")
+ .expect("Could not update a .gmi");
+ assert_file_contents(&posts_dir.join("0/cooler-test-file-😎.gmi"), "# I REALLY try.");
+ assert_file_contents(&found_path.join("cooler-test-file-😎.gmi"), "# I REALLY try.");
+
+ cleanup_test_dir(&test_dir);
+ }
+
+ #[test]
+ fn test_write_metadata_fail() {
+ // Create all directories
+ let test_dir = setup_test_dir();
+
+ // Reference but don't create the directories
+ let metadata_dir = test_dir.join("metadata/nested/cannot/write");
+
+ // Create metadata
+ let metadata = Metadata {
+ id: "1234".to_string(),
+ created_on: 1234
+ };
+
+ // Update 1 of 2
+ let result = Update::write_metadata(&metadata, &metadata_dir);
+ assert!(result.is_err());
+
+ cleanup_test_dir(&test_dir);
+ }
+
+ #[test]
+ fn test_recursive_archive() {
+ // Create all directories
+ let test_dir = setup_test_dir();
+
+ // Create the directories
+ let source_dir = test_dir.join("source");
+ let target_dir = test_dir.join("target");
+ let nested_dir = source_dir.join("nested");
+ create_dir_all(&nested_dir).expect("Could not create source dir.");
+ create_dir_all(&target_dir).expect("Could not create target dir.");
+
+ // Create the two test files.
+ let test_file = source_dir.join("not_nested.gmi");
+ let nested_test_file = nested_dir.join("very_much_nested.gmi");
+ create_test_file(&test_file, "# Unlike a Bird");
+ create_test_file(&nested_test_file, "# Very much like a Bird");
+
+ Update::archive(&source_dir, &target_dir)
+ .expect("Could not archive the test directories");
+
+ assert_file_contents(&target_dir.join("not_nested.gmi"), "# Unlike a Bird");
+ assert_file_contents(&target_dir.join("nested/very_much_nested.gmi"), "# Very much like a Bird");
+
+ cleanup_test_dir(&test_dir);
+ }
+
+ #[test]
+ fn test_update_fails_on_directory() {
+ let update = Update::new();
+
+ // Create all directories
+ let test_dir = setup_test_dir();
+
+ // Reference but don't create the directories
+ let nested_dir = test_dir.join("nested");
+ let posts_dir = test_dir.join("posts");
+ let archive_dir = test_dir.join("archive");
+ create_dir_all(&nested_dir).expect("Could not create nested dir.");
+
+ // Create configuration
+ let mut configuration = Configuration::new();
+ configuration.posts_directory = posts_dir.clone();
+ configuration.archive_directory = archive_dir.clone();
+
+ // Create Test File
+ let test_file = nested_dir.join("cool-test-file-😎.gmi");
+ create_test_file(&test_file, "# I try.");
+ let nested_string = nested_dir.display().to_string();
+
+ let result = update.execute(Some(&nested_string), &configuration, "update");
+ assert!(result.is_err());
+ cleanup_test_dir(&test_dir);
+ }
+
+ #[test]
+ fn test_update_fails_on_missing_input() {
+ let update = Update::new();
+
+ // Create all directories
+ let test_dir = setup_test_dir();
+
+ // Reference but don't create the directories
+ let posts_dir = test_dir.join("posts");
+ let archive_dir = test_dir.join("archive");
+
+ // Create configuration
+ let mut configuration = Configuration::new();
+ configuration.posts_directory = posts_dir.clone();
+ configuration.archive_directory = archive_dir.clone();
+
+ // Add 1 of 3
+ let result = update.execute(None, &configuration, "update");
+ assert!(result.is_err());
+ cleanup_test_dir(&test_dir);
+ }
+
+ #[test]
+ fn test_update_fails_if_file_does_not_exist() {
+ let update = Update::new();
+
+ // Create all directories
+ let test_dir = setup_test_dir();
+
+ // Reference but don't create the directories
+ let posts_dir = test_dir.join("posts");
+ let archive_dir = test_dir.join("archive");
+
+ // Create configuration
+ let mut configuration = Configuration::new();
+ configuration.posts_directory = posts_dir.clone();
+ configuration.archive_directory = archive_dir.clone();
+
+ // Create Test File
+ let test_file = test_dir.join("cool-secret-test-file-😎.gmi");
+ let test_file_string = test_file.display().to_string();
+
+ // Add 1 of 3
+ let result = update.execute(Some(&test_file_string), &configuration, "update");
+ assert!(result.is_err());
+
+ cleanup_test_dir(&test_dir);
+ }
+
+ #[test]
+ fn update_before_dependencies() {
+ let update = Update::new();
+ let dependencies = update.before_dependencies();
+
+ assert_eq!(dependencies.len(), 1);
+ assert_eq!(dependencies[0].command(), "sync-down");
+ }
+
+ #[test]
+ fn update_after_dependencies() {
+ let update = Update::new();
+ let dependencies = update.after_dependencies();
+
+ assert_eq!(dependencies.len(), 2);
+ assert_eq!(dependencies[0].command(), "generate");
+ assert_eq!(dependencies[1].command(), "sync-up");
+ }
+
+ // These two tests feel pointless but I'm doing it for the coverage :p
+
+ #[test]
+ fn update_command_output() {
+ let update = Update::new();
+ update.command();
+ }
+
+ #[test]
+ fn update_help_output() {
+ let update = Update::new();
+ update.help();
+ }
+}