Skip to content

Commit af4c19d

Browse files
committed
refactor: Update test setup and dependencies
Refactor tests to use TestingRepository, update git2 to 0.19.0 - Replace direct repository usage with TestingRepository in tests - Update git2 dependency to version 0.19.0 - Adjust test setup for better readability and maintainability - Remove unnecessary tempdir usage in tests
1 parent 9802afe commit af4c19d

File tree

6 files changed

+207
-127
lines changed

6 files changed

+207
-127
lines changed

Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ resolver = "2"
3737
[workspace.dependencies]
3838
bstr = "1.10.0"
3939
# Add the `tracing` or `tracing-detail` features to see more of gitoxide in the logs. Useful to see which programs it invokes.
40-
gix = { git = "https://github.com/Byron/gitoxide", rev = "72daa46bad9d397ef2cc48a3cffda23f414ccd8a", default-features = false, features = [] }
41-
git2 = { version = "0.18.3", features = [
40+
gix = { git = "https://github.com/Byron/gitoxide", rev = "72daa46bad9d397ef2cc48a3cffda23f414ccd8a", default-features = false, features = [
41+
] }
42+
git2 = { version = "0.19.0", features = [
4243
"vendored-openssl",
4344
"vendored-libgit2",
4445
] }
@@ -93,4 +94,4 @@ debug = true # Enable debug symbols, for profiling
9394
[profile.bench]
9495
codegen-units = 256
9596
lto = false
96-
opt-level = 3
97+
opt-level = 3

crates/gitbutler-branch-actions/src/upstream_integration.rs

+62-113
Original file line numberDiff line numberDiff line change
@@ -478,44 +478,12 @@ fn compute_resolutions(
478478

479479
#[cfg(test)]
480480
mod test {
481-
use std::fs;
482-
483481
use gitbutler_branch::BranchOwnershipClaims;
484-
use tempfile::tempdir;
482+
use gitbutler_testsupport::testing_repository::TestingRepository;
485483
use uuid::Uuid;
486484

487485
use super::*;
488486

489-
fn commit_file<'a>(
490-
repository: &'a git2::Repository,
491-
parent: Option<&git2::Commit>,
492-
files: &[(&str, &str)],
493-
) -> git2::Commit<'a> {
494-
for (file_name, contents) in files {
495-
fs::write(repository.path().join("..").join(file_name), contents).unwrap();
496-
}
497-
let mut index = repository.index().unwrap();
498-
// Make sure we're not having weird cached state
499-
index.read(true).unwrap();
500-
index
501-
.add_all(["*"], git2::IndexAddOption::DEFAULT, None)
502-
.unwrap();
503-
504-
let signature = git2::Signature::now("Caleb", "[email protected]").unwrap();
505-
let commit = repository
506-
.commit(
507-
None,
508-
&signature,
509-
&signature,
510-
"Committee",
511-
&repository.find_tree(index.write_tree().unwrap()).unwrap(),
512-
parent.map(|c| vec![c]).unwrap_or_default().as_slice(),
513-
)
514-
.unwrap();
515-
516-
repository.find_commit(commit).unwrap()
517-
}
518-
519487
fn make_branch(head: git2::Oid, tree: git2::Oid) -> Branch {
520488
Branch {
521489
id: Uuid::new_v4().into(),
@@ -540,16 +508,15 @@ mod test {
540508

541509
#[test]
542510
fn test_up_to_date_if_head_commits_equivalent() {
543-
let tempdir = tempdir().unwrap();
544-
let repository = git2::Repository::init(tempdir.path()).unwrap();
545-
let initial_commit = commit_file(&repository, None, &[("foo.txt", "bar")]);
546-
let head_commit = commit_file(&repository, Some(&initial_commit), &[("foo.txt", "baz")]);
511+
let test_repository = TestingRepository::open();
512+
let initial_commit = test_repository.commit_tree(None, &[("foo.txt", "bar")]);
513+
let head_commit = test_repository.commit_tree(Some(&initial_commit), &[("foo.txt", "baz")]);
547514

548515
let context = UpstreamIntegrationContext {
549516
_permission: None,
550517
old_target: head_commit.clone(),
551518
new_target: head_commit,
552-
repository: &repository,
519+
repository: &test_repository.repository,
553520
virtual_branches_in_workspace: vec![],
554521
target_branch_name: "main".to_string(),
555522
};
@@ -562,17 +529,16 @@ mod test {
562529

563530
#[test]
564531
fn test_updates_required_if_new_head_ahead() {
565-
let tempdir = tempdir().unwrap();
566-
let repository = git2::Repository::init(tempdir.path()).unwrap();
567-
let initial_commit = commit_file(&repository, None, &[("foo.txt", "bar")]);
568-
let old_target = commit_file(&repository, Some(&initial_commit), &[("foo.txt", "baz")]);
569-
let new_target = commit_file(&repository, Some(&old_target), &[("foo.txt", "qux")]);
532+
let test_repository = TestingRepository::open();
533+
let initial_commit = test_repository.commit_tree(None, &[("foo.txt", "bar")]);
534+
let old_target = test_repository.commit_tree(Some(&initial_commit), &[("foo.txt", "baz")]);
535+
let new_target = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "qux")]);
570536

571537
let context = UpstreamIntegrationContext {
572538
_permission: None,
573539
old_target,
574540
new_target,
575-
repository: &repository,
541+
repository: &test_repository.repository,
576542
virtual_branches_in_workspace: vec![],
577543
target_branch_name: "main".to_string(),
578544
};
@@ -585,19 +551,18 @@ mod test {
585551

586552
#[test]
587553
fn test_empty_branch() {
588-
let tempdir = tempdir().unwrap();
589-
let repository = git2::Repository::init(tempdir.path()).unwrap();
590-
let initial_commit = commit_file(&repository, None, &[("foo.txt", "bar")]);
591-
let old_target = commit_file(&repository, Some(&initial_commit), &[("foo.txt", "baz")]);
592-
let new_target = commit_file(&repository, Some(&old_target), &[("foo.txt", "qux")]);
554+
let test_repository = TestingRepository::open();
555+
let initial_commit = test_repository.commit_tree(None, &[("foo.txt", "bar")]);
556+
let old_target = test_repository.commit_tree(Some(&initial_commit), &[("foo.txt", "baz")]);
557+
let new_target = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "qux")]);
593558

594559
let branch = make_branch(old_target.id(), old_target.tree_id());
595560

596561
let context = UpstreamIntegrationContext {
597562
_permission: None,
598563
old_target,
599564
new_target,
600-
repository: &repository,
565+
repository: &test_repository.repository,
601566
virtual_branches_in_workspace: vec![branch.clone()],
602567
target_branch_name: "main".to_string(),
603568
};
@@ -610,22 +575,24 @@ mod test {
610575

611576
#[test]
612577
fn test_conflicted_head_branch() {
613-
let tempdir = tempdir().unwrap();
614-
let repository = git2::Repository::init(tempdir.path()).unwrap();
615-
let initial_commit = commit_file(&repository, None, &[("foo.txt", "bar")]);
578+
let test_repository = TestingRepository::open();
579+
let initial_commit = test_repository.commit_tree(None, &[("foo.txt", "bar")]);
616580
// Create refs/heads/master
617-
repository.branch("master", &initial_commit, false).unwrap();
618-
let old_target = commit_file(&repository, Some(&initial_commit), &[("foo.txt", "baz")]);
619-
let branch_head = commit_file(&repository, Some(&old_target), &[("foo.txt", "fux")]);
620-
let new_target = commit_file(&repository, Some(&old_target), &[("foo.txt", "qux")]);
581+
test_repository
582+
.repository
583+
.branch("master", &initial_commit, false)
584+
.unwrap();
585+
let old_target = test_repository.commit_tree(Some(&initial_commit), &[("foo.txt", "baz")]);
586+
let branch_head = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "fux")]);
587+
let new_target = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "qux")]);
621588

622589
let branch = make_branch(branch_head.id(), branch_head.tree_id());
623590

624591
let context = UpstreamIntegrationContext {
625592
_permission: None,
626593
old_target,
627594
new_target: new_target.clone(),
628-
repository: &repository,
595+
repository: &test_repository.repository,
629596
virtual_branches_in_workspace: vec![branch.clone()],
630597
target_branch_name: "main".to_string(),
631598
};
@@ -655,32 +622,32 @@ mod test {
655622
panic!("Should be variant UpdatedObjects")
656623
};
657624

658-
let head_commit = repository.find_commit(head).unwrap();
625+
let head_commit = test_repository.repository.find_commit(head).unwrap();
659626
assert_eq!(head_commit.parent(0).unwrap().id(), new_target.id());
660627
assert!(head_commit.is_conflicted());
661628

662-
let head_tree = repository
629+
let head_tree = test_repository
630+
.repository
663631
.find_real_tree(&head_commit, Default::default())
664632
.unwrap();
665633
assert_eq!(head_tree.id(), tree)
666634
}
667635

668636
#[test]
669637
fn test_conflicted_tree_branch() {
670-
let tempdir = tempdir().unwrap();
671-
let repository = git2::Repository::init(tempdir.path()).unwrap();
672-
let initial_commit = commit_file(&repository, None, &[("foo.txt", "bar")]);
673-
let old_target = commit_file(&repository, Some(&initial_commit), &[("foo.txt", "baz")]);
674-
let branch_head = commit_file(&repository, Some(&old_target), &[("foo.txt", "fux")]);
675-
let new_target = commit_file(&repository, Some(&old_target), &[("foo.txt", "qux")]);
638+
let test_repository = TestingRepository::open();
639+
let initial_commit = test_repository.commit_tree(None, &[("foo.txt", "bar")]);
640+
let old_target = test_repository.commit_tree(Some(&initial_commit), &[("foo.txt", "baz")]);
641+
let branch_head = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "fux")]);
642+
let new_target = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "qux")]);
676643

677644
let branch = make_branch(old_target.id(), branch_head.tree_id());
678645

679646
let context = UpstreamIntegrationContext {
680647
_permission: None,
681648
old_target,
682649
new_target,
683-
repository: &repository,
650+
repository: &test_repository.repository,
684651
virtual_branches_in_workspace: vec![branch.clone()],
685652
target_branch_name: "main".to_string(),
686653
};
@@ -698,21 +665,20 @@ mod test {
698665

699666
#[test]
700667
fn test_conflicted_head_and_tree_branch() {
701-
let tempdir = tempdir().unwrap();
702-
let repository = git2::Repository::init(tempdir.path()).unwrap();
703-
let initial_commit = commit_file(&repository, None, &[("foo.txt", "bar")]);
704-
let old_target = commit_file(&repository, Some(&initial_commit), &[("foo.txt", "baz")]);
705-
let branch_head = commit_file(&repository, Some(&old_target), &[("foo.txt", "fux")]);
706-
let branch_tree = commit_file(&repository, Some(&old_target), &[("foo.txt", "bax")]);
707-
let new_target = commit_file(&repository, Some(&old_target), &[("foo.txt", "qux")]);
668+
let test_repository = TestingRepository::open();
669+
let initial_commit = test_repository.commit_tree(None, &[("foo.txt", "bar")]);
670+
let old_target = test_repository.commit_tree(Some(&initial_commit), &[("foo.txt", "baz")]);
671+
let branch_head = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "fux")]);
672+
let branch_tree = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "bax")]);
673+
let new_target = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "qux")]);
708674

709675
let branch = make_branch(branch_head.id(), branch_tree.tree_id());
710676

711677
let context = UpstreamIntegrationContext {
712678
_permission: None,
713679
old_target,
714680
new_target,
715-
repository: &repository,
681+
repository: &test_repository.repository,
716682
virtual_branches_in_workspace: vec![branch.clone()],
717683
target_branch_name: "main".to_string(),
718684
};
@@ -730,19 +696,18 @@ mod test {
730696

731697
#[test]
732698
fn test_integrated() {
733-
let tempdir = tempdir().unwrap();
734-
let repository = git2::Repository::init(tempdir.path()).unwrap();
735-
let initial_commit = commit_file(&repository, None, &[("foo.txt", "bar")]);
736-
let old_target = commit_file(&repository, Some(&initial_commit), &[("foo.txt", "baz")]);
737-
let new_target = commit_file(&repository, Some(&old_target), &[("foo.txt", "qux")]);
699+
let test_repository = TestingRepository::open();
700+
let initial_commit = test_repository.commit_tree(None, &[("foo.txt", "bar")]);
701+
let old_target = test_repository.commit_tree(Some(&initial_commit), &[("foo.txt", "baz")]);
702+
let new_target = test_repository.commit_tree(Some(&old_target), &[("foo.txt", "qux")]);
738703

739704
let branch = make_branch(new_target.id(), new_target.tree_id());
740705

741706
let context = UpstreamIntegrationContext {
742707
_permission: None,
743708
old_target,
744709
new_target,
745-
repository: &repository,
710+
repository: &test_repository.repository,
746711
virtual_branches_in_workspace: vec![branch.clone()],
747712
target_branch_name: "main".to_string(),
748713
};
@@ -755,33 +720,25 @@ mod test {
755720

756721
#[test]
757722
fn test_integrated_commit_with_uncommited_changes() {
758-
let tempdir = tempdir().unwrap();
759-
let repository = git2::Repository::init(tempdir.path()).unwrap();
723+
let test_repository = TestingRepository::open();
760724
let initial_commit =
761-
commit_file(&repository, None, &[("foo.txt", "bar"), ("bar.txt", "bar")]);
762-
let old_target = commit_file(
763-
&repository,
725+
test_repository.commit_tree(None, &[("foo.txt", "bar"), ("bar.txt", "bar")]);
726+
let old_target = test_repository.commit_tree(
764727
Some(&initial_commit),
765728
&[("foo.txt", "baz"), ("bar.txt", "bar")],
766729
);
767-
let new_target = commit_file(
768-
&repository,
769-
Some(&old_target),
770-
&[("foo.txt", "qux"), ("bar.txt", "bar")],
771-
);
772-
let tree = commit_file(
773-
&repository,
774-
Some(&old_target),
775-
&[("foo.txt", "baz"), ("bar.txt", "qux")],
776-
);
730+
let new_target = test_repository
731+
.commit_tree(Some(&old_target), &[("foo.txt", "qux"), ("bar.txt", "bar")]);
732+
let tree = test_repository
733+
.commit_tree(Some(&old_target), &[("foo.txt", "baz"), ("bar.txt", "qux")]);
777734

778735
let branch = make_branch(new_target.id(), tree.tree_id());
779736

780737
let context = UpstreamIntegrationContext {
781738
_permission: None,
782739
old_target,
783740
new_target,
784-
repository: &repository,
741+
repository: &test_repository.repository,
785742
virtual_branches_in_workspace: vec![branch.clone()],
786743
target_branch_name: "main".to_string(),
787744
};
@@ -794,31 +751,23 @@ mod test {
794751

795752
#[test]
796753
fn test_safly_updatable() {
797-
let tempdir = tempdir().unwrap();
798-
let repository = git2::Repository::init(tempdir.path()).unwrap();
799-
let initial_commit = commit_file(
800-
&repository,
801-
None,
802-
&[("files-one.txt", "foo"), ("file-two.txt", "foo")],
803-
);
804-
let old_target = commit_file(
805-
&repository,
754+
let test_repository = TestingRepository::open();
755+
let initial_commit =
756+
test_repository.commit_tree(None, &[("files-one.txt", "foo"), ("file-two.txt", "foo")]);
757+
let old_target = test_repository.commit_tree(
806758
Some(&initial_commit),
807759
&[("file-one.txt", "bar"), ("file-two.txt", "foo")],
808760
);
809-
let new_target = commit_file(
810-
&repository,
761+
let new_target = test_repository.commit_tree(
811762
Some(&old_target),
812763
&[("file-one.txt", "baz"), ("file-two.txt", "foo")],
813764
);
814765

815-
let branch_head = commit_file(
816-
&repository,
766+
let branch_head = test_repository.commit_tree(
817767
Some(&old_target),
818768
&[("file-one.txt", "bar"), ("file-two.txt", "bar")],
819769
);
820-
let branch_tree = commit_file(
821-
&repository,
770+
let branch_tree = test_repository.commit_tree(
822771
Some(&branch_head),
823772
&[("file-one.txt", "bar"), ("file-two.txt", "baz")],
824773
);
@@ -829,7 +778,7 @@ mod test {
829778
_permission: None,
830779
old_target,
831780
new_target,
832-
repository: &repository,
781+
repository: &test_repository.repository,
833782
virtual_branches_in_workspace: vec![branch.clone()],
834783
target_branch_name: "main".to_string(),
835784
};

0 commit comments

Comments
 (0)