diff --git a/asyncgit/src/sync/diff.rs b/asyncgit/src/sync/diff.rs index 54d939e0..f7792cc1 100644 --- a/asyncgit/src/sync/diff.rs +++ b/asyncgit/src/sync/diff.rs @@ -240,7 +240,7 @@ mod tests { use crate::sync::{ stage_add_file, status::{get_status, StatusType}, - tests::{repo_init, repo_init_empty}, + tests::{get_statuses, repo_init, repo_init_empty}, }; use std::{ fs::{self, File}, @@ -280,16 +280,19 @@ mod tests { let root = repo.path().parent().unwrap(); let repo_path = root.as_os_str().to_str().unwrap(); - let res = get_status(repo_path, StatusType::WorkingDir); - assert_eq!(res.len(), 0); + assert_eq!(get_statuses(repo_path), (0, 0)); File::create(&root.join(file_path)) .unwrap() .write_all(b"test\nfoo") .unwrap(); + assert_eq!(get_statuses(repo_path), (1, 0)); + assert_eq!(stage_add_file(repo_path, file_path), true); + assert_eq!(get_statuses(repo_path), (0, 1)); + let diff = get_diff( repo_path, String::from(file_path.to_str().unwrap()), @@ -331,8 +334,7 @@ mod tests { let root = repo.path().parent().unwrap(); let repo_path = root.as_os_str().to_str().unwrap(); - let res = get_status(repo_path, StatusType::WorkingDir); - assert_eq!(res.len(), 0); + assert_eq!(get_statuses(repo_path), (0, 0)); let file_path = root.join("bar.txt"); @@ -349,11 +351,7 @@ mod tests { let res = stage_add_file(repo_path, Path::new("bar.txt")); assert_eq!(res, true); - assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 0 - ); + assert_eq!(get_statuses(repo_path), (0, 1)); // overwrite with next content { @@ -363,11 +361,7 @@ mod tests { .unwrap(); } - assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 1 - ); + assert_eq!(get_statuses(repo_path), (1, 1)); let res = get_diff(repo_path, "bar.txt".to_string(), false); diff --git a/asyncgit/src/sync/mod.rs b/asyncgit/src/sync/mod.rs index ce3f63d3..60223b95 100644 --- a/asyncgit/src/sync/mod.rs +++ b/asyncgit/src/sync/mod.rs @@ -18,6 +18,7 @@ pub use utils::{ #[cfg(test)] mod tests { + use super::status::{get_status, StatusType}; use git2::Repository; use std::process::Command; use tempfile::TempDir; @@ -34,6 +35,7 @@ mod tests { (td, repo) } + /// pub fn repo_init() -> (TempDir, Repository) { let td = TempDir::new().unwrap(); let repo = Repository::init(td.path()).unwrap(); @@ -60,6 +62,14 @@ mod tests { (td, repo) } + /// helper returning amount of files with changes in the (wd,stage) + pub fn get_statuses(repo_path: &str) -> (usize, usize) { + ( + get_status(repo_path, StatusType::WorkingDir).len(), + get_status(repo_path, StatusType::Stage).len(), + ) + } + /// pub fn debug_cmd_print(path: &str, cmd: &str) { eprintln!("\n----\n{}", debug_cmd(path, cmd)) diff --git a/asyncgit/src/sync/reset.rs b/asyncgit/src/sync/reset.rs index 14fed1fa..4c92867b 100644 --- a/asyncgit/src/sync/reset.rs +++ b/asyncgit/src/sync/reset.rs @@ -83,7 +83,9 @@ mod tests { }; use crate::sync::{ status::{get_status, StatusType}, - tests::{debug_cmd_print, repo_init, repo_init_empty}, + tests::{ + debug_cmd_print, get_statuses, repo_init, repo_init_empty, + }, utils::{commit, stage_add_all, stage_add_file}, }; use std::{ @@ -152,22 +154,14 @@ mod tests { debug_cmd_print(repo_path, "git status"); - assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 1 - ); + assert_eq!(get_statuses(repo_path), (1, 1)); let res = reset_workdir_file(repo_path, "bar.txt"); assert_eq!(res, true); debug_cmd_print(repo_path, "git status"); - assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 0 - ); + assert_eq!(get_statuses(repo_path), (0, 1)); } #[test] @@ -186,20 +180,14 @@ mod tests { debug_cmd_print(repo_path, "git status"); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 1 - ); + assert_eq!(get_statuses(repo_path), (1, 0)); let res = reset_workdir_file(repo_path, "foo/bar.txt"); assert_eq!(res, true); debug_cmd_print(repo_path, "git status"); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 0 - ); + assert_eq!(get_statuses(repo_path), (0, 0)); } #[test] @@ -233,21 +221,15 @@ mod tests { .write_all(b"file3\nadded line")?; } + assert_eq!(get_statuses(repo_path), (5, 0)); + stage_add_file(repo_path, Path::new("foo/file5.txt")); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 4 - ); - assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1); + assert_eq!(get_statuses(repo_path), (4, 1)); assert!(reset_workdir_folder(repo_path, "foo")); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 1 - ); - assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1); + assert_eq!(get_statuses(repo_path), (1, 1)); Ok(()) } @@ -282,22 +264,14 @@ mod tests { debug_cmd_print(repo_path, "git status"); - assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 1 - ); + assert_eq!(get_statuses(repo_path), (1, 1)); let res = reset_workdir_file(repo_path, file); assert_eq!(res, true); debug_cmd_print(repo_path, "git status"); - assert_eq!( - get_status(repo_path, StatusType::WorkingDir).len(), - 0 - ); - assert_eq!(get_status(repo_path, StatusType::Stage).len(), 1); + assert_eq!(get_statuses(repo_path), (0, 1)); } #[test] @@ -312,8 +286,14 @@ mod tests { .write_all(b"test\nfoo") .unwrap(); + assert_eq!(get_statuses(repo_path), (1, 0)); + assert_eq!(stage_add_file(repo_path, file_path), true); + assert_eq!(get_statuses(repo_path), (0, 1)); + assert_eq!(reset_stage(repo_path, file_path), true); + + assert_eq!(get_statuses(repo_path), (1, 0)); } } diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index 38373e7d..0190ed74 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -118,7 +118,7 @@ mod tests { use super::*; use crate::sync::{ status::{get_status, StatusType}, - tests::{repo_init, repo_init_empty}, + tests::{get_statuses, repo_init, repo_init_empty}, }; use std::{ fs::{self, remove_file, File}, @@ -133,26 +133,20 @@ mod tests { let root = repo.path().parent().unwrap(); let repo_path = root.as_os_str().to_str().unwrap(); - let status_count = |s: StatusType| -> usize { - get_status(repo_path, s).len() - }; - File::create(&root.join(file_path)) .unwrap() .write_all(b"test\nfoo") .unwrap(); - assert_eq!(status_count(StatusType::WorkingDir), 1); + assert_eq!(get_statuses(repo_path), (1, 0)); assert_eq!(stage_add_file(repo_path, file_path), true); - assert_eq!(status_count(StatusType::WorkingDir), 0); - assert_eq!(status_count(StatusType::Stage), 1); + assert_eq!(get_statuses(repo_path), (0, 1)); commit(repo_path, "commit msg"); - assert_eq!(status_count(StatusType::Stage), 0); - assert_eq!(status_count(StatusType::WorkingDir), 0); + assert_eq!(get_statuses(repo_path), (0, 0)); } #[test] @@ -162,14 +156,22 @@ mod tests { let root = repo.path().parent().unwrap(); let repo_path = root.as_os_str().to_str().unwrap(); + assert_eq!(get_statuses(repo_path), (0, 0)); + File::create(&root.join(file_path)) .unwrap() .write_all(b"test\nfoo") .unwrap(); + assert_eq!(get_statuses(repo_path), (1, 0)); + assert_eq!(stage_add_file(repo_path, file_path), true); + assert_eq!(get_statuses(repo_path), (0, 1)); + commit(repo_path, "commit msg"); + + assert_eq!(get_statuses(repo_path), (0, 0)); } #[test] @@ -189,10 +191,6 @@ mod tests { let root = repo.path().parent().unwrap(); let repo_path = root.as_os_str().to_str().unwrap(); - let status_count = |s: StatusType| -> usize { - get_status(repo_path, s).len() - }; - File::create(&root.join(file_path)) .unwrap() .write_all(b"test file1 content") @@ -203,12 +201,11 @@ mod tests { .write_all(b"test file2 content") .unwrap(); - assert_eq!(status_count(StatusType::WorkingDir), 2); + assert_eq!(get_statuses(repo_path), (2, 0)); assert_eq!(stage_add_file(repo_path, file_path), true); - assert_eq!(status_count(StatusType::WorkingDir), 1); - assert_eq!(status_count(StatusType::Stage), 1); + assert_eq!(get_statuses(repo_path), (1, 1)); } #[test]