Update testing framework

This commit is contained in:
Caleb Owens 2024-09-29 20:23:23 +02:00
parent be1fc2cf7e
commit 47a4fc03c7
4 changed files with 41 additions and 35 deletions

View File

@ -5,6 +5,9 @@ use gitbutler_repo::RepositoryExt as _;
use crate::VirtualBranchesExt as _;
/// Checks out the combined trees of all branches in the workspace.
///
/// This function will fail if the applied branches conflict with each other.
pub(crate) fn checkout_branch_trees<'a>(
ctx: &'a CommandContext,
_perm: &mut WorktreeWritePermission,

View File

@ -8,6 +8,10 @@ use gitbutler_repo::{rebase::cherry_rebase_group, LogUntil, RepositoryExt as _};
use crate::{branch_trees::checkout_branch_trees, VirtualBranchesExt as _};
/// Moves a commit up or down a stack by a certain offset.
///
/// After the commit is moved, the combined branch trees are checked out.
/// A stack must have at least two commits in it. A 0 offset is a no-op.
///
/// Presume we had the stack:
///
@ -23,25 +27,12 @@ use crate::{branch_trees::checkout_branch_trees, VirtualBranchesExt as _};
/// B
/// D
///
/// Presume we had the stack:
///
/// A
/// B
/// C
/// D
///
/// If B was the subject, and the offset was 1, we would expect:
/// Or, if B was the subject, and the offset was 1, we would expect:
///
/// A
/// C
/// B
/// D
///
// move a given commit in a branch up one or down one
// if the offset is positive, move the commit down one
// if the offset is negative, move the commit up one
// rewrites the branch head to the new head commit
pub(crate) fn reorder_commit(
ctx: &CommandContext,
branch_id: BranchId,
@ -246,7 +237,9 @@ mod test {
use gitbutler_commit::commit_ext::CommitExt as _;
use gitbutler_repo::LogUntil;
use gitbutler_repo::RepositoryExt as _;
use gitbutler_testsupport::testing_repository::{assert_tree_matches, TestingRepository};
use gitbutler_testsupport::testing_repository::{
assert_commit_tree_matches, TestingRepository,
};
#[test]
fn less_than_two_commits_is_an_error() {
@ -299,7 +292,7 @@ mod test {
test_repository.repository.find_commit(result.head).unwrap();
assert!(!a_prime.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&a_prime,
&[("foo.txt", b"a"), ("bar.txt", b"a"), ("baz.txt", b"a")],
@ -308,7 +301,7 @@ mod test {
let b_prime: git2::Commit = a_prime.parent(0).unwrap();
assert!(!b_prime.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&b_prime,
&[("foo.txt", b"a"), ("baz.txt", b"a")],
@ -363,12 +356,12 @@ mod test {
test_repository.repository.find_commit(result.head).unwrap();
assert!(!a_prime.is_conflicted());
assert_tree_matches(&test_repository.repository, &a_prime, &[("foo.txt", b"x")]);
assert_commit_tree_matches(&test_repository.repository, &a_prime, &[("foo.txt", b"x")]);
let b_prime: git2::Commit = a_prime.parent(0).unwrap();
assert!(b_prime.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&b_prime,
&[

View File

@ -425,7 +425,9 @@ mod test {
mod cherry_rebase_group {
use crate::repository_ext::RepositoryExt as _;
use gitbutler_commit::commit_ext::CommitExt;
use gitbutler_testsupport::testing_repository::{assert_tree_matches, TestingRepository};
use gitbutler_testsupport::testing_repository::{
assert_commit_tree_matches, TestingRepository,
};
use crate::{rebase::cherry_rebase_group, LogUntil};
@ -452,7 +454,7 @@ mod test {
assert!(commits.into_iter().all(|commit| !commit.is_conflicted()));
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&commit,
&[("foo.txt", b"c"), ("bar.txt", b"x")],
@ -475,7 +477,7 @@ mod test {
assert!(commit.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&commit,
&[
@ -508,7 +510,7 @@ mod test {
assert!(commit.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&commit,
&[
@ -537,7 +539,7 @@ mod test {
let commit: git2::Commit = test_repository.repository.find_commit(result).unwrap();
assert!(commit.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&commit,
&[
@ -555,7 +557,7 @@ mod test {
let commit: git2::Commit = test_repository.repository.find_commit(result).unwrap();
assert!(commit.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&commit,
&[
@ -591,7 +593,7 @@ mod test {
assert!(commits.iter().all(|commit| commit.is_conflicted()));
// Rebased version of B (B')
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&commits[1],
&[
@ -607,7 +609,7 @@ mod test {
);
// Rebased version of C
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&commits[0],
&[
@ -628,7 +630,9 @@ mod test {
mod gitbutler_merge_commits {
use crate::rebase::gitbutler_merge_commits;
use gitbutler_commit::commit_ext::CommitExt as _;
use gitbutler_testsupport::testing_repository::{assert_tree_matches, TestingRepository};
use gitbutler_testsupport::testing_repository::{
assert_commit_tree_matches, TestingRepository,
};
#[test]
fn unconflicting_merge() {
@ -645,7 +649,7 @@ mod test {
assert!(!result.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&result,
&[("foo.txt", b"b"), ("bar.txt", b"a")],
@ -667,7 +671,7 @@ mod test {
assert!(result.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&result,
&[
@ -711,7 +715,7 @@ mod test {
assert!(!result.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&result,
&[("foo.txt", b"c"), ("bar.txt", b"a")],
@ -760,7 +764,7 @@ mod test {
assert!(!result.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&result,
&[("foo.txt", b"c"), ("bar.txt", b"c")],
@ -808,7 +812,7 @@ mod test {
assert!(result.is_conflicted());
assert_tree_matches(
assert_commit_tree_matches(
&test_repository.repository,
&result,
&[

View File

@ -81,13 +81,19 @@ impl TestingRepository {
}
}
pub fn assert_tree_matches<'a>(
pub fn assert_commit_tree_matches<'a>(
repository: &'a git2::Repository,
commit: &git2::Commit<'a>,
files: &[(&str, &[u8])],
) {
let tree = commit.tree().unwrap();
assert_tree_matches(repository, &commit.tree().unwrap(), files);
}
pub fn assert_tree_matches<'a>(
repository: &'a git2::Repository,
tree: &git2::Tree<'a>,
files: &[(&str, &[u8])],
) {
for (path, content) in files {
let blob = tree.get_path(Path::new(path)).unwrap().id();
let blob: git2::Blob<'a> = repository.find_blob(blob).unwrap();