diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-18 13:27:03 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-01-18 13:27:03 +0100 |
| commit | 64296ca9c83011e3de4d9f1be02335d8eb1bfe95 (patch) | |
| tree | 21d13b4689a2f52fc9d0b9077151cab0159d3cc6 /src/remote | |
| parent | 760b30e71e565d4db2a3d8f25af0d05fa174e871 (diff) | |
Add tests for
Diffstat (limited to 'src/remote')
| -rw-r--r-- | src/remote/git.rs | 120 | ||||
| -rw-r--r-- | src/remote/mod.rs | 64 |
2 files changed, 90 insertions, 94 deletions
diff --git a/src/remote/git.rs b/src/remote/git.rs index 75e6292..912d8c1 100644 --- a/src/remote/git.rs +++ b/src/remote/git.rs @@ -13,10 +13,8 @@ impl Git { impl super::Remote for Git { fn can_handle(&self, remote: &str) -> bool { - let command = format!("git ls-remote {remote}"); - match Command::new("sh") - .arg("-c") - .arg(&command) + match Command::new("git") + .args(["ls-remote", remote]) .stdout(Stdio::null()) .stderr(Stdio::null()) .status() @@ -32,25 +30,18 @@ impl super::Remote for Git { .map_err(|_| Error::new(Other, "Invalid time"))? .as_millis(); - let commands = vec![ - format!("cd {} && git init -b main", directory.display()), - format!("cd {} && git add .", directory.display()), - format!( - "cd {} && git commit --allow-empty -m blog-sync-up-{}", - directory.display(), - timestamp - ), - format!( - "cd {} && git push {} main --force", - directory.display(), - remote - ), + let commit_name = format!("blog-sync-up-{}", timestamp); + let command_arguments = vec![ + vec!["init", "-b", "main"], + vec!["add", "."], + vec!["commit", "--allow-empty", "-m", &commit_name], + vec!["push", &remote, "main", "--force"], ]; - for command in commands { - Command::new("sh") - .arg("-c") - .arg(&command) + for arguments in command_arguments { + Command::new("git") + .current_dir(&directory) + .args(&arguments) .stdout(Stdio::null()) .stderr(Stdio::null()) .status() @@ -61,17 +52,17 @@ impl super::Remote for Git { } fn sync_down(&self, remote: &str, directory: &Path) -> Result<()> { - let commands = vec![ - format!("cd {} && git init -b main", directory.display()), - format!("cd {} && git checkout .", directory.display()), - format!("cd {} && git clean . -f", directory.display()), - format!("cd {} && git pull {} main", directory.display(), remote), + let command_arguments = vec![ + vec!["init", "-b", "main"], + vec!["checkout", "."], + vec!["clean", ".", "-f"], + vec!["pull", &remote, "main"], ]; - for command in commands { - Command::new("sh") - .arg("-c") - .arg(&command) + for arguments in command_arguments { + Command::new("git") + .current_dir(&directory) + .args(&arguments) .stdout(Stdio::null()) .stderr(Stdio::null()) .status() @@ -84,71 +75,12 @@ impl super::Remote for Git { #[cfg(test)] mod tests { use std::fs::create_dir_all; - use std::path::Path; - use std::process::{Command, Stdio}; use super::*; use crate::remote::Remote; use test_utilities::*; - fn create_remote(path: &Path) { - let remote_path = path.display(); - Command::new("sh") - .arg("-c") - .arg(format!("cd {remote_path} && git init --bare")) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .expect("Failed to initialize bare repository"); - } - - fn assert_file_does_not_exist(repo_path: &str, file_path: &str) { - let output = Command::new("sh") - .arg("-c") - .arg(format!("cd {repo_path} && git show main:{file_path}")) - .stderr(Stdio::null()) - .output() - .expect("Failed to read from repo"); - - assert!(output.status.code().unwrap() != 0); - } - - fn assert_file_in_repo_with_contents(repo_path: &str, file_path: &str, contents: &str) { - let output = Command::new("sh") - .arg("-c") - .arg(format!("cd {repo_path} && git show main:{file_path}")) - .stderr(Stdio::null()) - .output() - .expect("Failed to read from repo"); - - assert!(String::from_utf8(output.stdout) - .map(|file| file.trim() == contents) - .unwrap_or(false)); - } - - fn commit_file_to_remote(repo_path: &str, file_path: &str, contents: &str) { - let test_dir = setup_test_dir(); - let test_dir_display = test_dir.display(); - - Command::new("sh") - .arg("-c") - .arg(format!( - "cd {test_dir_display} && \ - git clone {repo_path} . && \ - echo '{contents}' > {file_path} && \ - git add {file_path} && \ - git -c user.name='test' -c user.email='test@example.com' commit -m 'test commit' && \ - git push origin main", - )) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .expect("Failed to commit file"); - - cleanup_test_dir(&test_dir); - } - #[test] fn test_can_handle_git_remotes() { let git = Git::new(); @@ -201,7 +133,7 @@ mod tests { git.sync_up(&remote_dir_as_string, &local_dir) .expect("Could not sync up to remote"); assert_file_in_repo_with_contents(&remote_dir_as_string, "file1.txt", "I exist"); - assert_file_does_not_exist(&remote_dir_as_string, "file2.txt"); + assert_file_in_repo_does_not_exist(&remote_dir_as_string, "file2.txt"); create_test_file(&local_dir.join("file2.txt"), "I also exist now, btw"); git.sync_up(&remote_dir_as_string, &local_dir) @@ -219,8 +151,8 @@ mod tests { fn test_remote_up_force_pushes() { let git = Git::new(); let test_dir = setup_test_dir(); - let local_dir = test_dir.join("local"); - let remote_dir = test_dir.join("remote"); + let local_dir = test_dir.join("gitlocal"); + let remote_dir = test_dir.join("gitremote"); create_dir_all(&local_dir).expect("Could not create local test directory"); create_dir_all(&remote_dir).expect("Could not create remote test directory"); @@ -229,7 +161,7 @@ mod tests { let remote_dir_as_string = remote_dir.display().to_string(); commit_file_to_remote(&remote_dir_as_string, "boo.txt", "A ghost"); - assert_file_does_not_exist(&remote_dir_as_string, "file1.txt"); + assert_file_in_repo_does_not_exist(&remote_dir_as_string, "file1.txt"); assert_file_in_repo_with_contents(&remote_dir_as_string, "boo.txt", "A ghost"); create_test_file(&local_dir.join("file1.txt"), "I exist"); @@ -237,7 +169,7 @@ mod tests { .expect("Could not sync up to remote"); assert_file_in_repo_with_contents(&remote_dir_as_string, "file1.txt", "I exist"); - assert_file_does_not_exist(&remote_dir_as_string, "boo.txt"); + assert_file_in_repo_does_not_exist(&remote_dir_as_string, "boo.txt"); cleanup_test_dir(&test_dir); } diff --git a/src/remote/mod.rs b/src/remote/mod.rs index 9782b0d..1a2421d 100644 --- a/src/remote/mod.rs +++ b/src/remote/mod.rs @@ -70,6 +70,7 @@ fn read_remote(file_path: &Path) -> Option<String> { #[cfg(test)] mod tests { + use std::fs::create_dir_all; use super::*; @@ -102,4 +103,67 @@ mod tests { cleanup_test_dir(&test_dir); } + + #[test] + fn test_syncs_remote_up() { + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("modlocal"); + let remote_dir = test_dir.join("modremote"); + 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); + + create_test_file(&local_dir.join("file1.txt"), "I exist"); + sync_up(&local_dir, &test_dir.join("remoteconfig")).expect("Could not sync up to remote"); + assert_file_in_repo_with_contents(&remote_dir_as_string, "file1.txt", "I exist"); + assert_file_in_repo_does_not_exist(&remote_dir_as_string, "file2.txt"); + + create_test_file(&local_dir.join("file2.txt"), "I also exist now, btw"); + sync_up(&local_dir, &test_dir.join("remoteconfig")).expect("Could not sync up to remote"); + assert_file_in_repo_with_contents(&remote_dir_as_string, "file1.txt", "I exist"); + assert_file_in_repo_with_contents( + &remote_dir_as_string, + "file2.txt", + "I also exist now, btw", + ); + cleanup_test_dir(&test_dir); + } + + #[test] + fn test_syncs_remote_down() { + let test_dir = setup_test_dir(); + let local_dir = test_dir.join("modownlocal"); + let remote_dir = test_dir.join("modownremote"); + 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, "file 1.txt", "I exist, but remotely"); + assert!(!&local_dir.join("file 1.txt").exists()); + assert!(!&local_dir.join("file2.txt").exists()); + sync_down(&local_dir, &test_dir.join("remoteconfig")) + .expect("Could not sync down from remote"); + assert_file_contents(&local_dir.join("file 1.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(&local_dir, &test_dir.join("remoteconfig")) + .expect("Could not sync down from remote"); + assert_file_contents(&local_dir.join("file 1.txt"), "I exist, but remotely"); + assert_file_contents(&local_dir.join("file2.txt"), "I also exist, but remotely"); + cleanup_test_dir(&test_dir); + } } |