gitbutler/crates/gitbutler-branch/tests/virtual_branches/update_commit_message.rs
Kiril Videlov 7d078de52f
start moving virtual_branches to separate crate
We want to move towards having each functional domain in a separate crate. 
The main benefit of that for our project is that this will enforce a unidirectional dependency graph (i.e. no cycles).
Starting off with virutal_branches - a lot of the implementation is still in core (i.e. virtual.rs), that will be moved in a separate PR. 

Furthermore, the virtual branches controller (as well as virtual.rs) contain functions not directly related to branches (e.g. commit reordering etc). That will be furthe separate in a crate.
2024-07-07 13:32:35 +02:00

374 lines
9.0 KiB
Rust

use git::CommitExt;
use super::*;
#[tokio::test]
async fn head() {
let Test {
repository,
project,
controller,
..
} = &Test::default();
controller
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
.await
.unwrap();
let branch_id = controller
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
.await
.unwrap();
{
fs::write(repository.path().join("file one.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit one", None, false)
.await
.unwrap()
};
{
fs::write(repository.path().join("file two.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit two", None, false)
.await
.unwrap()
};
let commit_three_oid = {
fs::write(repository.path().join("file three.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit three", None, false)
.await
.unwrap()
};
let commit_three = repository.find_commit(commit_three_oid).unwrap();
let before_change_id = &commit_three.change_id();
controller
.update_commit_message(project, branch_id, commit_three_oid, "commit three updated")
.await
.unwrap();
let branch = controller
.list_virtual_branches(project)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
let descriptions = branch
.commits
.iter()
.map(|c| c.description.clone())
.collect::<Vec<_>>();
// get the last commit
let commit = repository.find_commit(branch.head).unwrap();
// make sure the SHA changed, but the change ID did not
assert_ne!(&commit_three.id(), &commit.id());
assert_eq!(before_change_id, &commit.change_id());
assert_eq!(
descriptions,
vec!["commit three updated", "commit two", "commit one"]
);
}
#[tokio::test]
async fn middle() {
let Test {
repository,
project,
controller,
..
} = &Test::default();
controller
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
.await
.unwrap();
let branch_id = controller
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
.await
.unwrap();
{
fs::write(repository.path().join("file one.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit one", None, false)
.await
.unwrap()
};
let commit_two_oid = {
fs::write(repository.path().join("file two.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit two", None, false)
.await
.unwrap()
};
{
fs::write(repository.path().join("file three.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit three", None, false)
.await
.unwrap()
};
controller
.update_commit_message(project, branch_id, commit_two_oid, "commit two updated")
.await
.unwrap();
let branch = controller
.list_virtual_branches(project)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
let descriptions = branch
.commits
.iter()
.map(|c| c.description.clone())
.collect::<Vec<_>>();
assert_eq!(
descriptions,
vec!["commit three", "commit two updated", "commit one"]
);
}
#[tokio::test]
async fn forcepush_allowed() {
let Test {
repository,
project_id,
project,
controller,
projects,
..
} = &Test::default();
controller
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
.await
.unwrap();
projects
.update(&projects::UpdateRequest {
id: *project_id,
..Default::default()
})
.await
.unwrap();
let branch_id = controller
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
.await
.unwrap();
let commit_one_oid = {
fs::write(repository.path().join("file one.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit one", None, false)
.await
.unwrap()
};
controller
.push_virtual_branch(project, branch_id, false, None)
.await
.unwrap();
controller
.update_commit_message(project, branch_id, commit_one_oid, "commit one updated")
.await
.unwrap();
let branch = controller
.list_virtual_branches(project)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
let descriptions = branch
.commits
.iter()
.map(|c| c.description.clone())
.collect::<Vec<_>>();
assert_eq!(descriptions, vec!["commit one updated"]);
assert!(branch.requires_force);
}
#[tokio::test]
async fn forcepush_forbidden() {
let Test {
repository,
project,
controller,
..
} = &Test::default();
controller
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
.await
.unwrap();
let branch_id = controller
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
.await
.unwrap();
controller
.update_virtual_branch(
project,
branch::BranchUpdateRequest {
id: branch_id,
allow_rebasing: Some(false),
..Default::default()
},
)
.await
.unwrap();
let commit_one_oid = {
fs::write(repository.path().join("file one.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit one", None, false)
.await
.unwrap()
};
controller
.push_virtual_branch(project, branch_id, false, None)
.await
.unwrap();
assert_eq!(
controller
.update_commit_message(project, branch_id, commit_one_oid, "commit one updated",)
.await
.unwrap_err()
.to_string(),
"force push not allowed"
);
}
#[tokio::test]
async fn root() {
let Test {
repository,
project,
controller,
..
} = &Test::default();
controller
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
.await
.unwrap();
let branch_id = controller
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
.await
.unwrap();
let commit_one_oid = {
fs::write(repository.path().join("file one.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit one", None, false)
.await
.unwrap()
};
{
fs::write(repository.path().join("file two.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit two", None, false)
.await
.unwrap()
};
{
fs::write(repository.path().join("file three.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit three", None, false)
.await
.unwrap()
};
controller
.update_commit_message(project, branch_id, commit_one_oid, "commit one updated")
.await
.unwrap();
let branch = controller
.list_virtual_branches(project)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
let descriptions = branch
.commits
.iter()
.map(|c| c.description.clone())
.collect::<Vec<_>>();
assert_eq!(
descriptions,
vec!["commit three", "commit two", "commit one updated"]
);
}
#[tokio::test]
async fn empty() {
let Test {
repository,
project,
controller,
..
} = &Test::default();
controller
.set_base_branch(project, &"refs/remotes/origin/master".parse().unwrap())
.await
.unwrap();
let branch_id = controller
.create_virtual_branch(project, &branch::BranchCreateRequest::default())
.await
.unwrap();
let commit_one_oid = {
fs::write(repository.path().join("file one.txt"), "").unwrap();
controller
.create_commit(project, branch_id, "commit one", None, false)
.await
.unwrap()
};
assert_eq!(
controller
.update_commit_message(project, branch_id, commit_one_oid, "",)
.await
.unwrap_err()
.to_string(),
"commit message can not be empty"
);
}