aboutsummaryrefslogtreecommitdiff
path: root/src/command/sync_down.rs
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-03-06 22:17:22 +0100
committerRuben Beltran del Rio <git@r.bdr.sh>2025-03-06 22:17:22 +0100
commita9e0a8829a0b83cc44e1b24a8ae5df762adf0f76 (patch)
treea6765a8f5088ce97056c5a624582e7ab568e63b3 /src/command/sync_down.rs
parentda7a4a2f12fbf09c30e92806637d41561789fea3 (diff)
Add tests for sync down
Diffstat (limited to 'src/command/sync_down.rs')
-rw-r--r--src/command/sync_down.rs118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/command/sync_down.rs b/src/command/sync_down.rs
index f9122d4..bc9d983 100644
--- a/src/command/sync_down.rs
+++ b/src/command/sync_down.rs
@@ -44,3 +44,121 @@ impl super::Command for SyncDown {
"\t\t\t\tPulls from the git remote if configured"
}
}
+
+#[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_sync_down_command() {
+ let sync_down = SyncDown::new();
+
+ let test_dir = setup_test_dir();
+ let local_dir = test_dir.join("commandlocalup");
+ let remote_dir = test_dir.join("commandremoteup");
+ create_dir_all(&local_dir).expect("Could not create local test directory");
+ create_dir_all(&remote_dir).expect("Could not create remote test directory");
+
+ create_remote(&remote_dir);
+
+ let remote_dir_as_string = remote_dir.display().to_string();
+ create_test_file(&test_dir.join("remoteconfig"), &remote_dir_as_string);
+
+ commit_file_to_remote(&remote_dir_as_string, "file1.txt", "I exist, but remotely");
+ assert!(!&local_dir.join("file1.txt").exists());
+ assert!(!&local_dir.join("file2.txt").exists());
+
+ let mut configuration = Configuration::new();
+ configuration.data_directory = local_dir.clone();
+ configuration.remote_config = test_dir.join("remoteconfig");
+
+ create_test_file(&local_dir.join("file1.txt"), "I exist");
+ sync_down
+ .execute(None, &configuration, "sync-down")
+ .expect("Could not sync down from remote");
+ assert_file_contents(&local_dir.join("file1.txt"), "I exist, but remotely");
+ assert!(!&local_dir.join("file2.txt").exists());
+
+ commit_file_to_remote(
+ &remote_dir_as_string,
+ "file2.txt",
+ "I also exist, but remotely",
+ );
+ assert!(!&local_dir.join("file2.txt").exists());
+
+ sync_down
+ .execute(None, &configuration, "sync-down")
+ .expect("Could not sync down from remote");
+ assert_file_contents(&local_dir.join("file1.txt"), "I exist, but remotely");
+ assert_file_contents(&local_dir.join("file2.txt"), "I also exist, but remotely");
+ cleanup_test_dir(&test_dir);
+ }
+
+ #[test]
+ fn test_sync_down_fails_with_incorrect_remote() {
+ let sync_down = SyncDown::new();
+
+ let test_dir = setup_test_dir();
+ create_test_file(&test_dir.join("remoteconfigdown"), "DO NOT WAAANT");
+
+ let mut configuration = Configuration::new();
+ configuration.data_directory = test_dir.clone();
+ configuration.remote_config = test_dir.join("remoteconfig");
+
+ let result = sync_down.execute(None, &configuration, "sync-down");
+ assert!(result.is_err());
+ cleanup_test_dir(&test_dir);
+ }
+
+ #[test]
+ fn test_sync_down_does_not_fail_if_running_as_dependency() {
+ let sync_down = SyncDown::new();
+
+ let test_dir = setup_test_dir();
+ create_test_file(&test_dir.join("remoteconfigdown"), "DO NOT WAAANT");
+
+ let mut configuration = Configuration::new();
+ configuration.data_directory = test_dir.clone();
+ configuration.remote_config = test_dir.join("remoteconfig");
+
+ let result = sync_down.execute(None, &configuration, "NOPE");
+ assert!(result.is_ok());
+ cleanup_test_dir(&test_dir);
+ }
+
+ #[test]
+ fn sync_down_before_dependencies() {
+ let sync_down = SyncDown::new();
+ let dependencies = sync_down.before_dependencies();
+
+ assert_eq!(dependencies.len(), 0);
+ }
+
+ #[test]
+ fn sync_down_after_dependencies() {
+ let sync_down = SyncDown::new();
+ let dependencies = sync_down.after_dependencies();
+
+ assert_eq!(dependencies.len(), 0);
+ }
+
+ // These two tests feel pointless but I'm doing it for the coverage :p
+
+ #[test]
+ fn sync_down_command_output() {
+ let sync_down = SyncDown::new();
+ sync_down.command();
+ }
+
+ #[test]
+ fn sync_down_help_output() {
+ let sync_down = SyncDown::new();
+ sync_down.help();
+ }
+}