diff options
| -rw-r--r-- | src/command/publish.rs | 162 | ||||
| -rw-r--r-- | src/command/publish_archive.rs | 183 | ||||
| -rw-r--r-- | src/remote/git.rs | 48 |
3 files changed, 386 insertions, 7 deletions
diff --git a/src/command/publish.rs b/src/command/publish.rs index e203f62..620604b 100644 --- a/src/command/publish.rs +++ b/src/command/publish.rs @@ -33,7 +33,7 @@ impl super::Command for Publish { .status() .map_err(|_| Error::other("Publishing requires rsync"))?; - Command::new(COMMAND) + let status = Command::new(COMMAND) .arg("-r") .arg(format!( "{}/", @@ -44,6 +44,10 @@ impl super::Command for Publish { .stderr(Stdio::null()) .status() .map_err(|_| Error::other("Rsync failed to publish."))?; + + if !status.success() { + return Err(Error::other("Rsync failed to publish.")); + } Ok(()) } @@ -59,3 +63,159 @@ impl super::Command for Publish { "<destination>\t\tPublishes the blog to a remote host" } } + +#[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_publish_command() { + let publish = Publish::new(); + + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("publishlocal"); + let remote_dir = test_dir.join("publishremote"); + create_dir_all(&local_dir).expect("Could not create local test directory"); + create_dir_all(&remote_dir).expect("Could not create remote test directory"); + + let remote_dir_as_string = remote_dir.display().to_string(); + + let mut configuration = Configuration::new().unwrap(); + configuration.blog_output_directory = local_dir.clone(); + + create_test_file( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + assert!(!&remote_dir.join("very_local.txt").exists()); + assert!(!&remote_dir.join("second_file.txt").exists()); + publish + .execute(Some(&remote_dir_as_string), &configuration, "publish") + .expect("Could not publish"); + assert_file_contents( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + assert!(!&remote_dir.join("second_file.txt").exists()); + + create_test_file( + &local_dir.join("second_file.txt"), + "Me, I don't care at all.", + ); + publish + .execute(Some(&remote_dir_as_string), &configuration, "publish") + .expect("Could not publish second file."); + assert_file_contents( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + assert_file_contents( + &local_dir.join("second_file.txt"), + "Me, I don't care at all.", + ); + + cleanup_test_dir(&test_dir); + } + + #[test] + fn test_publish_command_should_fail_if_it_has_invalid_string() { + let publish = Publish::new(); + + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("publishlocal"); + create_dir_all(&local_dir).expect("Could not create local test directory"); + + let remote_dir_as_string = "/zzzzz/\0".to_string(); + + let mut configuration = Configuration::new().unwrap(); + configuration.blog_output_directory = local_dir.clone(); + + create_test_file( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + let result = publish.execute(Some(&remote_dir_as_string), &configuration, "publish"); + + assert!(result.is_err()); + cleanup_test_dir(&test_dir); + } + + #[test] + fn test_publish_command_should_fail_if_cannot_reach_destination() { + let publish = Publish::new(); + + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("publishlocal"); + create_dir_all(&local_dir).expect("Could not create local test directory"); + + let remote_dir_as_string = "/zzzzz/ifthissucceeds/honestly/noidea/whattomakeofyour/directorystructure/but/welldone".to_string(); + + let mut configuration = Configuration::new().unwrap(); + configuration.blog_output_directory = local_dir.clone(); + + create_test_file( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + let result = publish.execute(Some(&remote_dir_as_string), &configuration, "publish"); + + assert!(result.is_err()); + cleanup_test_dir(&test_dir); + } + + #[test] + fn test_publish_command_should_fail_if_cannot_reach_source() { + let publish = Publish::new(); + + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("absolutelynot_we_cannot"); + let remote_dir = test_dir.join("publishremote"); + create_dir_all(&remote_dir).expect("Could not create remote test directory"); + + let remote_dir_as_string = remote_dir.display().to_string(); + + let mut configuration = Configuration::new().unwrap(); + configuration.blog_output_directory = local_dir.clone(); + + let result = publish.execute(Some(&remote_dir_as_string), &configuration, "publish"); + + assert!(result.is_err()); + cleanup_test_dir(&test_dir); + } + + #[test] + fn publish_before_dependencies() { + let publish = Publish::new(); + let dependencies = publish.before_dependencies(); + + assert_eq!(dependencies.len(), 0); + } + + #[test] + fn publish_after_dependencies() { + let publish = Publish::new(); + let dependencies = publish.after_dependencies(); + + assert_eq!(dependencies.len(), 0); + } + + // These two tests feel pointless but I'm doing it for the coverage :p + + #[test] + fn publish_command_output() { + let publish = Publish::new(); + publish.command(); + } + + #[test] + fn publish_help_output() { + let publish = Publish::new(); + publish.help(); + } +} diff --git a/src/command/publish_archive.rs b/src/command/publish_archive.rs index 0d1bca5..2f122c5 100644 --- a/src/command/publish_archive.rs +++ b/src/command/publish_archive.rs @@ -33,7 +33,7 @@ impl super::Command for PublishArchive { .status() .map_err(|_| Error::other("Publishing requires rsync"))?; - Command::new(COMMAND) + let status = Command::new(COMMAND) .arg("-r") .arg(format!( "{}/", @@ -44,6 +44,11 @@ impl super::Command for PublishArchive { .stderr(Stdio::null()) .status() .map_err(|_| Error::other("Rsync failed to publish."))?; + + if !status.success() { + return Err(Error::other("Rsync failed to publish.")); + } + Ok(()) } @@ -59,3 +64,179 @@ impl super::Command for PublishArchive { "<destination>\tPublishes the archive to a remote host" } } + +#[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_publish_archive_command() { + let publish_archive = PublishArchive::new(); + + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("publishlocal"); + let remote_dir = test_dir.join("publishremote"); + create_dir_all(&local_dir).expect("Could not create local test directory"); + create_dir_all(&remote_dir).expect("Could not create remote test directory"); + + let remote_dir_as_string = remote_dir.display().to_string(); + + let mut configuration = Configuration::new().unwrap(); + configuration.archive_output_directory = local_dir.clone(); + + create_test_file( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + assert!(!&remote_dir.join("very_local.txt").exists()); + assert!(!&remote_dir.join("second_file.txt").exists()); + publish_archive + .execute( + Some(&remote_dir_as_string), + &configuration, + "publish_archive", + ) + .expect("Could not publish"); + assert_file_contents( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + assert!(!&remote_dir.join("second_file.txt").exists()); + + create_test_file( + &local_dir.join("second_file.txt"), + "Me, I don't care at all.", + ); + publish_archive + .execute( + Some(&remote_dir_as_string), + &configuration, + "publish_archive", + ) + .expect("Could not publish second file."); + assert_file_contents( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + assert_file_contents( + &local_dir.join("second_file.txt"), + "Me, I don't care at all.", + ); + + cleanup_test_dir(&test_dir); + } + + #[test] + fn test_publish_archive_command_should_fail_if_it_has_invalid_string() { + let publish_archive = PublishArchive::new(); + + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("publishlocal"); + create_dir_all(&local_dir).expect("Could not create local test directory"); + + let remote_dir_as_string = "/zzzzz/\0".to_string(); + + let mut configuration = Configuration::new().unwrap(); + configuration.archive_output_directory = local_dir.clone(); + + create_test_file( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + let result = publish_archive.execute( + Some(&remote_dir_as_string), + &configuration, + "publish_archive", + ); + + assert!(result.is_err()); + cleanup_test_dir(&test_dir); + } + + #[test] + fn test_publish_archive_command_should_fail_if_cannot_reach_destination() { + let publish_archive = PublishArchive::new(); + + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("publishlocal"); + create_dir_all(&local_dir).expect("Could not create local test directory"); + + let remote_dir_as_string = "/zzzzz/ifthissucceeds/honestly/noidea/whattomakeofyour/directorystructure/but/welldone".to_string(); + + let mut configuration = Configuration::new().unwrap(); + configuration.archive_output_directory = local_dir.clone(); + + create_test_file( + &local_dir.join("very_local_file.txt"), + "I like my cornershop.", + ); + let result = publish_archive.execute( + Some(&remote_dir_as_string), + &configuration, + "publish_archive", + ); + + assert!(result.is_err()); + cleanup_test_dir(&test_dir); + } + + #[test] + fn test_publish_archive_command_should_fail_if_cannot_reach_source() { + let publish_archive = PublishArchive::new(); + + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("absolutelynot_we_cannot"); + let remote_dir = test_dir.join("publishremote"); + create_dir_all(&remote_dir).expect("Could not create remote test directory"); + + let remote_dir_as_string = remote_dir.display().to_string(); + + let mut configuration = Configuration::new().unwrap(); + configuration.archive_output_directory = local_dir.clone(); + + let result = publish_archive.execute( + Some(&remote_dir_as_string), + &configuration, + "publish_archive", + ); + + assert!(result.is_err()); + cleanup_test_dir(&test_dir); + } + + #[test] + fn publish_archive_before_dependencies() { + let publish_archive = PublishArchive::new(); + let dependencies = publish_archive.before_dependencies(); + + assert_eq!(dependencies.len(), 0); + } + + #[test] + fn publish_archive_after_dependencies() { + let publish_archive = PublishArchive::new(); + let dependencies = publish_archive.after_dependencies(); + + assert_eq!(dependencies.len(), 0); + } + + // These two tests feel pointless but I'm doing it for the coverage :p + + #[test] + fn publish_archive_command_output() { + let publish_archive = PublishArchive::new(); + publish_archive.command(); + } + + #[test] + fn publish_archive_help_output() { + let publish_archive = PublishArchive::new(); + publish_archive.help(); + } +} diff --git a/src/remote/git.rs b/src/remote/git.rs index c8aaf46..2960448 100644 --- a/src/remote/git.rs +++ b/src/remote/git.rs @@ -39,13 +39,17 @@ impl super::Remote for Git { ]; for arguments in command_arguments { - Command::new("git") + let status = Command::new("git") .current_dir(directory) .args(&arguments) .stdout(Stdio::null()) .stderr(Stdio::null()) .status() .map_err(|_| Error::other("Failed while performing sync up with git"))?; + + if !status.success() { + return Err(Error::other("Failed while performing sync up with git")); + } } Ok(()) @@ -54,19 +58,23 @@ impl super::Remote for Git { fn sync_down(&self, remote: &str, directory: &Path) -> Result<()> { let command_arguments = vec![ vec!["init", "-b", "main"], - vec!["checkout", "."], + vec!["reset", "--hard"], vec!["clean", ".", "-f"], vec!["pull", &remote, "main"], ]; for arguments in command_arguments { - Command::new("git") + let status = Command::new("git") .current_dir(directory) .args(&arguments) .stdout(Stdio::null()) .stderr(Stdio::null()) .status() .map_err(|_| Error::other("Failed while performing sync down with git"))?; + + if !status.success() { + return Err(Error::other("Failed while performing sync down with git")); + } } Ok(()) } @@ -174,7 +182,7 @@ mod tests { } #[test] - fn test_sync_up_fails_with_an_invalid_remote() { + fn test_sync_up_fails_with_an_invalid_string() { let git = Git::new(); let test_dir = setup_test_dir(); let local_dir = test_dir.join("local"); @@ -189,6 +197,21 @@ mod tests { } #[test] + fn test_sync_up_fails_with_an_invalid_remote() { + let git = Git::new(); + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("local"); + create_dir_all(&local_dir).expect("Could not create local test directory"); + + let invalid_remote = String::from("my guy does not exist"); + + let result = git.sync_up(&invalid_remote, &local_dir); + + assert!(result.is_err()); + cleanup_test_dir(&test_dir); + } + + #[test] fn test_syncs_remote_down() { let git = Git::new(); let test_dir = setup_test_dir(); @@ -247,7 +270,7 @@ mod tests { } #[test] - fn test_sync_down_fails_with_an_invalid_remote() { + fn test_sync_down_fails_with_an_invalid_string() { let git = Git::new(); let test_dir = setup_test_dir(); let local_dir = test_dir.join("local"); @@ -260,4 +283,19 @@ mod tests { assert!(result.is_err()); cleanup_test_dir(&test_dir); } + + #[test] + fn test_sync_down_fails_with_an_invalid_remote() { + let git = Git::new(); + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("local"); + create_dir_all(&local_dir).expect("Could not create local test directory"); + + let invalid_remote = String::from("thisremotedoesnotexist"); + + let result = git.sync_down(&invalid_remote, &local_dir); + + assert!(result.is_err()); + cleanup_test_dir(&test_dir); + } } |