gitbutler/crates/gitbutler-branch-actions/tests/virtual_branches/create_commit.rs

135 lines
3.6 KiB
Rust
Raw Normal View History

use gitbutler_branch::{Branch, BranchCreateRequest, BranchUpdateRequest};
use gitbutler_branch_actions::VirtualBranch;
2024-07-09 13:39:18 +03:00
use gitbutler_id::id::Id;
use super::*;
#[test]
fn should_lock_updated_hunks() {
let Test {
project,
controller,
repository,
..
} = &Test::default();
controller
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
.unwrap();
let branch_id = controller
.create_virtual_branch(project, &BranchCreateRequest::default())
.unwrap();
{
// by default, hunks are not locked
repository.write_file("file.txt", &["content".to_string()]);
let branch = get_virtual_branch(controller, project, branch_id);
assert_eq!(branch.files.len(), 1);
assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
assert_eq!(branch.files[0].hunks.len(), 1);
assert!(!branch.files[0].hunks[0].locked);
}
controller
.create_commit(project, branch_id, "test", None, false)
.unwrap();
{
// change in the committed hunks leads to hunk locking
repository.write_file("file.txt", &["updated content".to_string()]);
let branch = controller
.list_virtual_branches(project)
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.files.len(), 1);
assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
assert_eq!(branch.files[0].hunks.len(), 1);
assert!(branch.files[0].hunks[0].locked);
}
}
#[test]
fn should_reset_into_same_branch() {
let Test {
project,
controller,
repository,
..
} = &Test::default();
let mut lines = repository.gen_file("file.txt", 7);
commit_and_push_initial(repository);
let base_branch = controller
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
.unwrap();
controller
.create_virtual_branch(project, &BranchCreateRequest::default())
.unwrap();
let branch_2_id = controller
.create_virtual_branch(
project,
&BranchCreateRequest {
selected_for_changes: Some(true),
..Default::default()
},
)
.unwrap();
lines[0] = "change 1".to_string();
repository.write_file("file.txt", &lines);
controller
.create_commit(project, branch_2_id, "commit to branch 2", None, false)
.unwrap();
let files = get_virtual_branch(controller, project, branch_2_id).files;
assert_eq!(files.len(), 0);
// Set target to branch 1 and verify the file resets into branch 2.
controller
.update_virtual_branch(
project,
BranchUpdateRequest {
id: branch_2_id,
selected_for_changes: Some(true),
..Default::default()
},
)
.unwrap();
controller
.reset_virtual_branch(project, branch_2_id, base_branch.base_sha)
.unwrap();
let files = get_virtual_branch(controller, project, branch_2_id).files;
assert_eq!(files.len(), 1);
}
fn commit_and_push_initial(repository: &TestProject) {
repository.commit_all("initial commit");
repository.push();
}
fn get_virtual_branch(
controller: &VirtualBranchActions,
project: &Project,
branch_id: Id<Branch>,
) -> VirtualBranch {
controller
.list_virtual_branches(project)
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap()
}