Skip to content

Branching unittests #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 99 additions & 19 deletions asyncgit/src/sync/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -174,3 +179,78 @@ 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<_>>(),
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<_>>(),
vec!["master", "test"]
);
}
}

#[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());
}
}