From dfed4101d2bb1a94dfd14f65f9f502847d3dcb04 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 11 Oct 2020 12:47:57 +0200 Subject: [PATCH 1/3] add unittests for `get_branches_to_display` --- asyncgit/src/sync/branch.rs | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/asyncgit/src/sync/branch.rs b/asyncgit/src/sync/branch.rs index 9fe530fb5e..24faf30905 100644 --- a/asyncgit/src/sync/branch.rs +++ b/asyncgit/src/sync/branch.rs @@ -174,3 +174,43 @@ mod tests_create_branch { ); } } + +#[cfg(test)] +mod tests_branches { + use super::*; + use crate::sync::tests::repo_init; + + #[test] + fn test_smoke() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + assert_eq!( + get_branches_to_display(repo_path) + .unwrap() + .iter() + .map(|b| b.name.clone()) + .collect::>(), + vec!["master"] + ); + } + + #[test] + fn test_multiple() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + create_branch(repo_path, "test").unwrap(); + + assert_eq!( + get_branches_to_display(repo_path) + .unwrap() + .iter() + .map(|b| b.name.clone()) + .collect::>(), + vec!["master", "test"] + ); + } +} From 111860f08b234cec9c4944bee28fae0211d3f5da Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 11 Oct 2020 13:08:44 +0200 Subject: [PATCH 2/3] more idiomatic way --- asyncgit/src/sync/branch.rs | 43 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/asyncgit/src/sync/branch.rs b/asyncgit/src/sync/branch.rs index 24faf30905..64b2f8b2c7 100644 --- a/asyncgit/src/sync/branch.rs +++ b/asyncgit/src/sync/branch.rs @@ -51,26 +51,31 @@ pub fn get_branches_to_display( scope_time!("get_branches_to_display"); let cur_repo = utils::repo(repo_path)?; - let mut branches_for_display = vec![]; - - for b in cur_repo.branches(Some(BranchType::Local))? { - let branch = &b?.0; - let top_commit = branch.get().peel_to_commit()?; - let mut commit_id = top_commit.id().to_string(); - commit_id.truncate(7); - - branches_for_display.push(BranchForDisplay { - name: String::from_utf8(Vec::from(branch.name_bytes()?))?, - reference: String::from_utf8(Vec::from( - branch.get().name_bytes(), - ))?, - top_commit_message: String::from_utf8(Vec::from( - top_commit.summary_bytes().unwrap_or_default(), - ))?, - top_commit_reference: commit_id, - is_head: branch.is_head(), + let branches_for_display = cur_repo + .branches(Some(BranchType::Local))? + .map(|b| { + let branch = b?.0; + let top_commit = branch.get().peel_to_commit()?; + let mut commit_id = top_commit.id().to_string(); + commit_id.truncate(7); + + Ok(BranchForDisplay { + name: String::from_utf8(Vec::from( + branch.name_bytes()?, + ))?, + reference: String::from_utf8(Vec::from( + branch.get().name_bytes(), + ))?, + top_commit_message: String::from_utf8(Vec::from( + top_commit.summary_bytes().unwrap_or_default(), + ))?, + top_commit_reference: commit_id, + is_head: branch.is_head(), + }) }) - } + .filter_map(Result::ok) + .collect(); + Ok(branches_for_display) } From d976e60fb5d5fc075d2b3a5e6bd48baec9ebb161 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 11 Oct 2020 13:20:50 +0200 Subject: [PATCH 3/3] add unittests for checkout --- asyncgit/src/sync/branch.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/asyncgit/src/sync/branch.rs b/asyncgit/src/sync/branch.rs index 64b2f8b2c7..5a99344b11 100644 --- a/asyncgit/src/sync/branch.rs +++ b/asyncgit/src/sync/branch.rs @@ -219,3 +219,38 @@ mod tests_branches { ); } } + +#[cfg(test)] +mod tests_checkout { + use super::*; + use crate::sync::tests::repo_init; + + #[test] + fn test_smoke() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + assert!( + checkout_branch(repo_path, "refs/heads/master").is_ok() + ); + assert!( + checkout_branch(repo_path, "refs/heads/foobar").is_err() + ); + } + + #[test] + fn test_multiple() { + let (_td, repo) = repo_init().unwrap(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + create_branch(repo_path, "test").unwrap(); + + assert!(checkout_branch(repo_path, "refs/heads/test").is_ok()); + assert!( + checkout_branch(repo_path, "refs/heads/master").is_ok() + ); + assert!(checkout_branch(repo_path, "refs/heads/test").is_ok()); + } +}