Skip to content

Commit

Permalink
Move a couple of test utils to test_project.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsgrd committed Jun 25, 2024
1 parent 881e996 commit 1d0b6ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
48 changes: 19 additions & 29 deletions crates/gitbutler-core/tests/suite/virtual_branches/create_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn should_lock_updated_hunks() {

{
// by default, hunks are not locked
write_file(repository, "file.txt", &["content".to_string()]);
repository.write_file("file.txt", &["content".to_string()]);

let branch = get_virtual_branch(controller, *project_id, branch_id).await;
assert_eq!(branch.files.len(), 1);
Expand All @@ -42,7 +42,7 @@ async fn should_lock_updated_hunks() {

{
// change in the committed hunks leads to hunk locking
write_file(repository, "file.txt", &["updated content".to_string()]);
repository.write_file("file.txt", &["updated content".to_string()]);

let branch = controller
.list_virtual_branches(*project_id)
Expand All @@ -69,7 +69,7 @@ async fn should_not_lock_disjointed_hunks() {
} = &Test::default();

let mut lines: Vec<_> = (0_i32..24_i32).map(|i| format!("line {}", i)).collect();
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);
repository.commit_all("my commit");
repository.push();

Expand All @@ -86,7 +86,7 @@ async fn should_not_lock_disjointed_hunks() {
{
// new hunk in the middle of the file
lines[12] = "commited stuff".to_string();
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);
let branch = get_virtual_branch(controller, *project_id, branch_id).await;
assert_eq!(branch.files.len(), 1);
assert_eq!(branch.files[0].path.display().to_string(), "file.txt");
Expand All @@ -107,55 +107,55 @@ async fn should_not_lock_disjointed_hunks() {
// hunk before the commited part is not locked
let mut changed_lines = lines.clone();
changed_lines[8] = "updated line".to_string();
write_file(repository, "file.txt", &changed_lines);
repository.write_file("file.txt", &changed_lines);
let branch = get_virtual_branch(controller, *project_id, branch_id).await;
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);
// cleanup
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);
}
{
// hunk after the commited part is not locked
let mut changed_lines = lines.clone();
changed_lines[16] = "updated line".to_string();
write_file(repository, "file.txt", &changed_lines);
repository.write_file("file.txt", &changed_lines);
let branch = get_virtual_branch(controller, *project_id, branch_id).await;
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);
// cleanup
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);
}
{
// hunk before the commited part but with overlapping context
let mut changed_lines = lines.clone();
changed_lines[10] = "updated line".to_string();
write_file(repository, "file.txt", &changed_lines);
repository.write_file("file.txt", &changed_lines);
let branch = get_virtual_branch(controller, *project_id, branch_id).await;
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);
// TODO: We lock this hunk, but can we afford not lock it?
assert!(branch.files[0].hunks[0].locked);
// cleanup
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);
}
{
// hunk after the commited part but with overlapping context
let mut changed_lines = lines.clone();
changed_lines[14] = "updated line".to_string();
write_file(repository, "file.txt", &changed_lines);
repository.write_file("file.txt", &changed_lines);
let branch = get_virtual_branch(controller, *project_id, branch_id).await;
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);
// TODO: We lock this hunk, but can we afford not lock it?
assert!(branch.files[0].hunks[0].locked);
// cleanup
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);
}
}

Expand All @@ -168,7 +168,7 @@ async fn should_reset_into_same_branch() {
..
} = &Test::default();

let mut lines = gen_file(repository, "file.txt", 7);
let mut lines = repository.gen_file("file.txt", 7);
commit_and_push_initial(repository);

let base_branch = controller
Expand All @@ -193,7 +193,7 @@ async fn should_reset_into_same_branch() {
.unwrap();

lines[0] = "change 1".to_string();
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);

controller
.create_commit(*project_id, branch_2_id, "commit to branch 2", None, false)
Expand Down Expand Up @@ -238,8 +238,8 @@ async fn should_double_lock() {
..
} = &Test::default();

let mut lines = gen_file(repository, "file.txt", 7);
write_file(repository, "file.txt", &lines);
let mut lines = repository.gen_file("file.txt", 7);
repository.write_file("file.txt", &lines);
commit_and_push_initial(repository);

controller
Expand All @@ -253,23 +253,23 @@ async fn should_double_lock() {
.unwrap();

lines[0] = "change 1".to_string();
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);

let commit_1 = controller
.create_commit(*project_id, branch_id, "commit 1", None, false)
.await
.unwrap();

lines[6] = "change 2".to_string();
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);

let commit_2 = controller
.create_commit(*project_id, branch_id, "commit 2", None, false)
.await
.unwrap();

lines[3] = "change3".to_string();
write_file(repository, "file.txt", &lines);
repository.write_file("file.txt", &lines);

let branch = get_virtual_branch(controller, *project_id, branch_id).await;
let locks = &branch.files[0].hunks[0].locked_to.clone().unwrap();
Expand All @@ -279,16 +279,6 @@ async fn should_double_lock() {
assert_eq!(locks[1].commit_id, commit_2);
}

fn write_file(repository: &TestProject, path: &str, lines: &[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();
Expand Down
12 changes: 11 additions & 1 deletion crates/gitbutler-testsupport/src/test_project.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path;
use std::path::PathBuf;
use std::{fs, path};

use gitbutler_core::git::{self, RepositoryExt};
use tempfile::TempDir;
Expand Down Expand Up @@ -395,6 +395,16 @@ impl TestProject {
repo.set_head("refs/heads/master").unwrap();
submodule.add_finalize().unwrap();
}

pub fn write_file(&self, path: &str, lines: &[String]) {
fs::write(self.path().join(path), lines.join("\n")).unwrap()
}

pub fn gen_file(&self, path: &str, line_count: i32) -> Vec<String> {
let lines: Vec<_> = (0_i32..line_count).map(|i| format!("line {}", i)).collect();
self.write_file(path, &lines);
lines
}
}

fn setup_config(config: &git2::Config) -> anyhow::Result<()> {
Expand Down

0 comments on commit 1d0b6ac

Please sign in to comment.