Merge pull request #1409 from gitbutlerapp/allow-non-unique-branch-names

move reset tests to integration
This commit is contained in:
Nikita Galaiko 2023-10-18 13:37:03 +02:00 committed by GitHub
commit b916eb8afe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 270 additions and 276 deletions

View File

@ -18,7 +18,7 @@ pub struct BaseBranch {
pub branch_name: String,
pub remote_name: String,
pub remote_url: String,
pub base_sha: String,
pub base_sha: git::Oid,
pub current_sha: String,
pub behind: usize,
pub upstream_commits: Vec<RemoteCommit>,
@ -483,7 +483,7 @@ pub fn target_to_base_branch(
branch_name: format!("{}/{}", target.branch.remote(), target.branch.branch()),
remote_name: target.branch.remote().to_string(),
remote_url: target.remote_url.clone(),
base_sha: target.sha.to_string(),
base_sha: target.sha,
current_sha: oid.to_string(),
behind: upstream_commits.len(),
upstream_commits,

View File

@ -3317,277 +3317,3 @@ fn test_verify_branch_not_integration() -> Result<()> {
Ok(())
}
#[test]
fn test_reset_to_head_commit() {
let Case {
project_repository,
gb_repository,
project,
..
} = Suite::default().new_case();
set_test_target(&gb_repository, &project_repository).unwrap();
let current_session = gb_repository.get_or_create_current_session().unwrap();
let current_session_reader = sessions::Reader::open(&gb_repository, &current_session).unwrap();
let branch_reader = branch::Reader::new(&current_session_reader);
let branch_id = create_virtual_branch(&gb_repository, &BranchCreateRequest::default())
.expect("failed to create virtual branch")
.id;
fs::write(project.path.join("file.txt"), "1").unwrap();
commit(
&gb_repository,
&project_repository,
&branch_id,
"commit 1",
None,
None,
None,
)
.unwrap();
let commit1_oid = branch_reader.read(&branch_id).unwrap().head;
reset_branch(&gb_repository, &project_repository, &branch_id, commit1_oid).unwrap();
let current_head = branch_reader.read(&branch_id).unwrap().head;
assert_eq!(current_head, commit1_oid);
assert_eq!(
fs::read_to_string(project.path.join("file.txt")).unwrap(),
"1"
);
}
#[test]
fn test_reset_to_existing_commit() {
let Case {
project_repository,
gb_repository,
project,
..
} = Suite::default().new_case();
set_test_target(&gb_repository, &project_repository).unwrap();
let current_session = gb_repository.get_or_create_current_session().unwrap();
let current_session_reader = sessions::Reader::open(&gb_repository, &current_session).unwrap();
let branch_reader = branch::Reader::new(&current_session_reader);
let branch_id = create_virtual_branch(&gb_repository, &BranchCreateRequest::default())
.expect("failed to create virtual branch")
.id;
fs::write(project.path.join("file.txt"), "1").unwrap();
commit(
&gb_repository,
&project_repository,
&branch_id,
"commit 1",
None,
None,
None,
)
.unwrap();
let commit1_oid = branch_reader.read(&branch_id).unwrap().head;
fs::write(project.path.join("file.txt"), "2").unwrap();
commit(
&gb_repository,
&project_repository,
&branch_id,
"commit 2",
None,
None,
None,
)
.unwrap();
let commit2_oid = branch_reader.read(&branch_id).unwrap().head;
assert_ne!(
commit1_oid, commit2_oid,
"expected commit to change the head"
);
reset_branch(&gb_repository, &project_repository, &branch_id, commit1_oid).unwrap();
let current_head = branch_reader.read(&branch_id).unwrap().head;
assert_eq!(current_head, commit1_oid);
assert_eq!(
fs::read_to_string(project.path.join("file.txt")).unwrap(),
"2"
);
}
#[test]
fn test_reset_to_non_existing_commit() {
let Case {
project_repository,
gb_repository,
..
} = Suite::default().new_case();
set_test_target(&gb_repository, &project_repository).unwrap();
let branch_id = create_virtual_branch(&gb_repository, &BranchCreateRequest::default())
.expect("failed to create virtual branch")
.id;
assert!(reset_branch(
&gb_repository,
&project_repository,
&branch_id,
"f535ba30d59bc60fc5d162a857b0f1a6475f4700".parse().unwrap(),
)
.is_err());
}
#[test]
fn test_reset_to_target() {
let Case {
project_repository,
gb_repository,
project,
..
} = Suite::default().new_case();
set_test_target(&gb_repository, &project_repository).unwrap();
let current_session = gb_repository.get_or_create_current_session().unwrap();
let current_session_reader = sessions::Reader::open(&gb_repository, &current_session).unwrap();
let branch_reader = branch::Reader::new(&current_session_reader);
let branch_id = create_virtual_branch(&gb_repository, &BranchCreateRequest::default())
.expect("failed to create virtual branch")
.id;
fs::write(project.path.join("file.txt"), "1").unwrap();
commit(
&gb_repository,
&project_repository,
&branch_id,
"commit 1",
None,
None,
None,
)
.unwrap();
let commit1_oid = branch_reader.read(&branch_id).unwrap().head;
fs::write(project.path.join("file.txt"), "2").unwrap();
commit(
&gb_repository,
&project_repository,
&branch_id,
"commit 2",
None,
None,
None,
)
.unwrap();
let commit2_oid = branch_reader.read(&branch_id).unwrap().head;
assert_ne!(
commit1_oid, commit2_oid,
"expected commit to change the head"
);
let target_reader = target::Reader::new(&current_session_reader);
let default_target = target_reader.read_default().unwrap();
reset_branch(
&gb_repository,
&project_repository,
&branch_id,
default_target.sha,
)
.unwrap();
let current_head = branch_reader.read(&branch_id).unwrap().head;
assert_eq!(current_head, default_target.sha);
assert_eq!(
fs::read_to_string(project.path.join("file.txt")).unwrap(),
"2"
);
}
#[test]
fn test_force_push() {
let suite = Suite::default();
let Case {
project_repository,
gb_repository,
project,
credentials,
..
} = suite.new_case();
set_test_target(&gb_repository, &project_repository).unwrap();
let branch_id = create_virtual_branch(&gb_repository, &BranchCreateRequest::default())
.expect("failed to create virtual branch")
.id;
fs::write(project.path.join("file.txt"), "1").unwrap();
commit(
&gb_repository,
&project_repository,
&branch_id,
"commit 1",
None,
None,
None,
)
.unwrap();
fs::write(project.path.join("file.txt"), "2").unwrap();
commit(
&gb_repository,
&project_repository,
&branch_id,
"commit 2",
None,
None,
None,
)
.unwrap();
push(
&project_repository,
&gb_repository,
&branch_id,
false,
&credentials,
)
.unwrap();
let statuses = list_virtual_branches(&gb_repository, &project_repository).unwrap();
assert!(!statuses[0].requires_force);
reset_branch(
&gb_repository,
&project_repository,
&branch_id,
statuses[0].commits[1].id,
)
.unwrap();
let statuses = list_virtual_branches(&gb_repository, &project_repository).unwrap();
assert!(statuses[0].requires_force);
assert!(push(
&project_repository,
&gb_repository,
&branch_id,
false,
&credentials,
)
.is_err());
push(
&project_repository,
&gb_repository,
&branch_id,
true,
&credentials,
)
.unwrap();
}

View File

@ -649,3 +649,271 @@ mod conflicts {
}
}
}
mod reset {
use super::*;
#[tokio::test]
async fn to_head() {
let Test {
repository,
project_id,
controller,
} = Test::default();
controller
.set_base_branch(
&project_id,
&git::RemoteBranchName::from_str("refs/remotes/origin/master").unwrap(),
)
.unwrap();
let branch1_id = controller
.create_virtual_branch(&project_id, &Default::default())
.await
.unwrap();
let oid = {
fs::write(repository.path().join("file.txt"), "content").unwrap();
// commit changes
let oid = controller
.create_commit(&project_id, &branch1_id, "commit", None)
.await
.unwrap();
let branches = controller.list_virtual_branches(&project_id).await.unwrap();
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].commits.len(), 1);
assert_eq!(branches[0].commits[0].id, oid);
assert_eq!(branches[0].files.len(), 0);
assert_eq!(
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"content"
);
oid
};
{
// reset changes to head
controller
.reset_virtual_branch(&project_id, &branch1_id, oid)
.await
.unwrap();
let branches = controller.list_virtual_branches(&project_id).await.unwrap();
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].commits.len(), 1);
assert_eq!(branches[0].commits[0].id, oid);
assert_eq!(branches[0].files.len(), 0);
assert_eq!(
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"content"
);
}
}
#[tokio::test]
async fn to_target() {
let Test {
repository,
project_id,
controller,
} = Test::default();
let base_branch = controller
.set_base_branch(
&project_id,
&git::RemoteBranchName::from_str("refs/remotes/origin/master").unwrap(),
)
.unwrap();
let branch1_id = controller
.create_virtual_branch(&project_id, &Default::default())
.await
.unwrap();
{
fs::write(repository.path().join("file.txt"), "content").unwrap();
// commit changes
let oid = controller
.create_commit(&project_id, &branch1_id, "commit", None)
.await
.unwrap();
let branches = controller.list_virtual_branches(&project_id).await.unwrap();
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].commits.len(), 1);
assert_eq!(branches[0].commits[0].id, oid);
assert_eq!(branches[0].files.len(), 0);
assert_eq!(
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"content"
);
}
{
// reset changes to head
controller
.reset_virtual_branch(&project_id, &branch1_id, base_branch.base_sha)
.await
.unwrap();
let branches = controller.list_virtual_branches(&project_id).await.unwrap();
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].commits.len(), 0);
assert_eq!(branches[0].files.len(), 1);
assert_eq!(
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"content"
);
}
}
#[tokio::test]
async fn to_commit() {
let Test {
repository,
project_id,
controller,
} = Test::default();
controller
.set_base_branch(
&project_id,
&git::RemoteBranchName::from_str("refs/remotes/origin/master").unwrap(),
)
.unwrap();
let branch1_id = controller
.create_virtual_branch(&project_id, &Default::default())
.await
.unwrap();
let first_commit_oid = {
// commit some changes
fs::write(repository.path().join("file.txt"), "content").unwrap();
let oid = controller
.create_commit(&project_id, &branch1_id, "commit", None)
.await
.unwrap();
let branches = controller.list_virtual_branches(&project_id).await.unwrap();
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].commits.len(), 1);
assert_eq!(branches[0].commits[0].id, oid);
assert_eq!(branches[0].files.len(), 0);
assert_eq!(
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"content"
);
oid
};
{
// commit some more
fs::write(repository.path().join("file.txt"), "more content").unwrap();
let second_commit_oid = controller
.create_commit(&project_id, &branch1_id, "commit", None)
.await
.unwrap();
let branches = controller.list_virtual_branches(&project_id).await.unwrap();
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].commits.len(), 2);
assert_eq!(branches[0].commits[0].id, second_commit_oid);
assert_eq!(branches[0].commits[1].id, first_commit_oid);
assert_eq!(branches[0].files.len(), 0);
assert_eq!(
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"more content"
);
}
{
// reset changes to the first commit
controller
.reset_virtual_branch(&project_id, &branch1_id, first_commit_oid)
.await
.unwrap();
let branches = controller.list_virtual_branches(&project_id).await.unwrap();
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].commits.len(), 1);
assert_eq!(branches[0].commits[0].id, first_commit_oid);
assert_eq!(branches[0].files.len(), 1);
assert_eq!(
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"more content"
);
}
}
#[tokio::test]
async fn to_non_existing() {
let Test {
repository,
project_id,
controller,
} = Test::default();
controller
.set_base_branch(
&project_id,
&git::RemoteBranchName::from_str("refs/remotes/origin/master").unwrap(),
)
.unwrap();
let branch1_id = controller
.create_virtual_branch(&project_id, &Default::default())
.await
.unwrap();
{
fs::write(repository.path().join("file.txt"), "content").unwrap();
// commit changes
let oid = controller
.create_commit(&project_id, &branch1_id, "commit", None)
.await
.unwrap();
let branches = controller.list_virtual_branches(&project_id).await.unwrap();
assert_eq!(branches.len(), 1);
assert_eq!(branches[0].id, branch1_id);
assert_eq!(branches[0].commits.len(), 1);
assert_eq!(branches[0].commits[0].id, oid);
assert_eq!(branches[0].files.len(), 0);
assert_eq!(
fs::read_to_string(repository.path().join("file.txt")).unwrap(),
"content"
);
oid
};
assert!(matches!(
controller
.reset_virtual_branch(
&project_id,
&branch1_id,
"fe14df8c66b73c6276f7bb26102ad91da680afcb".parse().unwrap()
)
.await,
Err(ControllerError::Other(_))
));
}
}