Remove repetition in create_commit.rs test code

This commit is contained in:
Mattias Granlund 2024-04-25 15:27:43 +02:00
parent 812b4ea27c
commit d79b126c59

View File

@ -1,3 +1,8 @@
use gitbutler_core::{
id::Id,
virtual_branches::{Branch, VirtualBranch},
};
use super::*; use super::*;
#[tokio::test] #[tokio::test]
@ -21,17 +26,9 @@ async fn should_lock_updated_hunks() {
{ {
// by default, hunks are not locked // by default, hunks are not locked
write_file(repository, "file.txt", &vec!["content".to_string()]);
fs::write(repository.path().join("file.txt"), "content").unwrap(); let branch = get_virtual_branch(controller, project_id, branch_id).await;
let branch = controller
.list_virtual_branches(project_id)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.files.len(), 1); assert_eq!(branch.files.len(), 1);
assert_eq!(branch.files[0].path.display().to_string(), "file.txt"); assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
assert_eq!(branch.files[0].hunks.len(), 1); assert_eq!(branch.files[0].hunks.len(), 1);
@ -45,7 +42,7 @@ async fn should_lock_updated_hunks() {
{ {
// change in the committed hunks leads to hunk locking // change in the committed hunks leads to hunk locking
fs::write(repository.path().join("file.txt"), "updated content").unwrap(); write_file(repository, "file.txt", &vec!["updated content".to_string()]);
let branch = controller let branch = controller
.list_virtual_branches(project_id) .list_virtual_branches(project_id)
@ -72,7 +69,7 @@ async fn should_not_lock_disjointed_hunks() {
} = &Test::default(); } = &Test::default();
let mut lines: Vec<_> = (0_i32..24_i32).map(|i| format!("line {}", i)).collect(); let mut lines: Vec<_> = (0_i32..24_i32).map(|i| format!("line {}", i)).collect();
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap(); write_file(repository, "file.txt", &lines);
repository.commit_all("my commit"); repository.commit_all("my commit");
repository.push(); repository.push();
@ -89,15 +86,8 @@ async fn should_not_lock_disjointed_hunks() {
{ {
// new hunk in the middle of the file // new hunk in the middle of the file
lines[12] = "commited stuff".to_string(); lines[12] = "commited stuff".to_string();
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap(); write_file(repository, "file.txt", &lines);
let branch = controller let branch = get_virtual_branch(controller, project_id, branch_id).await;
.list_virtual_branches(project_id)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.files.len(), 1); assert_eq!(branch.files.len(), 1);
assert_eq!(branch.files[0].path.display().to_string(), "file.txt"); assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
assert_eq!(branch.files[0].hunks.len(), 1); assert_eq!(branch.files[0].hunks.len(), 1);
@ -117,83 +107,55 @@ async fn should_not_lock_disjointed_hunks() {
// hunk before the commited part is not locked // hunk before the commited part is not locked
let mut changed_lines = lines.clone(); let mut changed_lines = lines.clone();
changed_lines[8] = "updated line".to_string(); changed_lines[8] = "updated line".to_string();
fs::write(repository.path().join("file.txt"), changed_lines.join("\n")).unwrap(); write_file(repository, "file.txt", &changed_lines);
let branch = controller let branch = get_virtual_branch(controller, project_id, branch_id).await;
.list_virtual_branches(project_id)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.files.len(), 1); assert_eq!(branch.files.len(), 1);
assert_eq!(branch.files[0].path.display().to_string(), "file.txt"); assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
assert_eq!(branch.files[0].hunks.len(), 1); assert_eq!(branch.files[0].hunks.len(), 1);
assert!(!branch.files[0].hunks[0].locked); assert!(!branch.files[0].hunks[0].locked);
// cleanup // cleanup
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap(); write_file(repository, "file.txt", &lines);
} }
{ {
// hunk after the commited part is not locked // hunk after the commited part is not locked
let mut changed_lines = lines.clone(); let mut changed_lines = lines.clone();
changed_lines[16] = "updated line".to_string(); changed_lines[16] = "updated line".to_string();
fs::write(repository.path().join("file.txt"), changed_lines.join("\n")).unwrap(); write_file(repository, "file.txt", &changed_lines);
let branch = controller let branch = get_virtual_branch(controller, project_id, branch_id).await;
.list_virtual_branches(project_id)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.files.len(), 1); assert_eq!(branch.files.len(), 1);
assert_eq!(branch.files[0].path.display().to_string(), "file.txt"); assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
assert_eq!(branch.files[0].hunks.len(), 1); assert_eq!(branch.files[0].hunks.len(), 1);
assert!(!branch.files[0].hunks[0].locked); assert!(!branch.files[0].hunks[0].locked);
// cleanup // cleanup
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap(); write_file(repository, "file.txt", &lines);
} }
{ {
// hunk before the commited part but with overlapping context // hunk before the commited part but with overlapping context
let mut changed_lines = lines.clone(); let mut changed_lines = lines.clone();
changed_lines[10] = "updated line".to_string(); changed_lines[10] = "updated line".to_string();
fs::write(repository.path().join("file.txt"), changed_lines.join("\n")).unwrap(); write_file(repository, "file.txt", &changed_lines);
let branch = controller let branch = get_virtual_branch(controller, project_id, branch_id).await;
.list_virtual_branches(project_id)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.files.len(), 1); assert_eq!(branch.files.len(), 1);
assert_eq!(branch.files[0].path.display().to_string(), "file.txt"); assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
assert_eq!(branch.files[0].hunks.len(), 1); assert_eq!(branch.files[0].hunks.len(), 1);
// TODO: We lock this hunk, but can we afford not lock it? // TODO: We lock this hunk, but can we afford not lock it?
assert!(branch.files[0].hunks[0].locked); assert!(branch.files[0].hunks[0].locked);
// cleanup // cleanup
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap(); write_file(repository, "file.txt", &lines);
} }
{ {
// hunk after the commited part but with overlapping context // hunk after the commited part but with overlapping context
let mut changed_lines = lines.clone(); let mut changed_lines = lines.clone();
changed_lines[14] = "updated line".to_string(); changed_lines[14] = "updated line".to_string();
fs::write(repository.path().join("file.txt"), changed_lines.join("\n")).unwrap(); write_file(repository, "file.txt", &changed_lines);
let branch = controller let branch = get_virtual_branch(controller, project_id, branch_id).await;
.list_virtual_branches(project_id)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
assert_eq!(branch.files.len(), 1); assert_eq!(branch.files.len(), 1);
assert_eq!(branch.files[0].path.display().to_string(), "file.txt"); assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
assert_eq!(branch.files[0].hunks.len(), 1); assert_eq!(branch.files[0].hunks.len(), 1);
// TODO: We lock this hunk, but can we afford not lock it? // TODO: We lock this hunk, but can we afford not lock it?
assert!(branch.files[0].hunks[0].locked); assert!(branch.files[0].hunks[0].locked);
// cleanup // cleanup
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap(); write_file(repository, "file.txt", &lines);
} }
} }
@ -206,10 +168,9 @@ async fn should_double_lock() {
.. ..
} = &Test::default(); } = &Test::default();
let mut lines: Vec<_> = (0_i32..7_i32).map(|i| format!("line {}", i)).collect(); let mut lines = gen_file(repository, "file.txt", 7);
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap(); write_file(repository, "file.txt", &lines);
repository.commit_all("initial commit"); commit_and_push_initial(repository);
repository.push();
controller controller
.set_base_branch(project_id, &"refs/remotes/origin/master".parse().unwrap()) .set_base_branch(project_id, &"refs/remotes/origin/master".parse().unwrap())
@ -221,43 +182,59 @@ async fn should_double_lock() {
.await .await
.unwrap(); .unwrap();
{ lines[0] = "change 1".to_string();
lines[0] = "change 1".to_string(); write_file(repository, "file.txt", &lines);
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap();
}
let commit_1 = controller let commit_1 = controller
.create_commit(project_id, &branch_id, "commit 1", None, false) .create_commit(project_id, &branch_id, "commit 1", None, false)
.await .await
.unwrap(); .unwrap();
{ lines[6] = "change 2".to_string();
lines[6] = "change 2".to_string(); write_file(repository, "file.txt", &lines);
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap();
}
let commit_2 = controller let commit_2 = controller
.create_commit(project_id, &branch_id, "commit 2", None, false) .create_commit(project_id, &branch_id, "commit 2", None, false)
.await .await
.unwrap(); .unwrap();
{ lines[3] = "change3".to_string();
lines[3] = "change3".to_string(); write_file(repository, "file.txt", &lines);
fs::write(repository.path().join("file.txt"), lines.join("\n")).unwrap();
let branch = controller
.list_virtual_branches(project_id)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap();
let locks = &branch.files[0].hunks[0].locked_to.clone().unwrap(); let branch = get_virtual_branch(controller, project_id, branch_id).await;
assert_eq!(locks.len(), 2); let locks = &branch.files[0].hunks[0].locked_to.clone().unwrap();
assert_eq!(locks[0], commit_1);
assert_eq!(locks[1], commit_2); assert_eq!(locks.len(), 2);
// cleanup assert_eq!(locks[0], commit_1);
fs::write(repository.path().join("file.txt"), lines.clone().join("\n")).unwrap(); assert_eq!(locks[1], commit_2);
} }
fn write_file(repository: &TestProject, path: &str, lines: &Vec<String>) {
fs::write(repository.path().join(path), lines.join("\n")).unwrap()
}
fn gen_file(repository: &TestProject, path: &str, line_count: i32) -> Vec<String> {
let lines: Vec<_> = (0_i32..line_count).map(|i| format!("line {}", i)).collect();
write_file(repository, path, &lines);
lines
}
fn commit_and_push_initial(repository: &TestProject) {
repository.commit_all("initial commit");
repository.push();
}
async fn get_virtual_branch(
controller: &Controller,
project_id: &ProjectId,
branch_id: Id<Branch>,
) -> VirtualBranch {
controller
.list_virtual_branches(project_id)
.await
.unwrap()
.0
.into_iter()
.find(|b| b.id == branch_id)
.unwrap()
} }