2024-07-27 16:37:28 +03:00
|
|
|
use anyhow::Result;
|
2024-08-01 16:35:05 +03:00
|
|
|
use gitbutler_branch_actions::BranchListingFilter;
|
2024-07-27 16:37:28 +03:00
|
|
|
|
2024-07-27 20:15:02 +03:00
|
|
|
#[test]
|
2024-08-28 14:06:28 +03:00
|
|
|
fn one_vbranch_in_workspace() -> Result<()> {
|
2024-07-28 11:46:46 +03:00
|
|
|
init_env();
|
2024-08-28 14:06:28 +03:00
|
|
|
let list = list_branches(&project_ctx("one-vbranch-in-workspace")?, None)?;
|
2024-07-27 20:15:02 +03:00
|
|
|
assert_eq!(list.len(), 1);
|
|
|
|
|
2024-08-01 10:24:05 +03:00
|
|
|
assert_equal(
|
|
|
|
&list[0],
|
|
|
|
ExpectedBranchListing {
|
2024-08-03 16:10:42 +03:00
|
|
|
identity: "virtual".into(),
|
2024-08-01 10:24:05 +03:00
|
|
|
virtual_branch_given_name: Some("virtual"),
|
|
|
|
virtual_branch_in_workspace: true,
|
2024-08-05 15:17:46 +03:00
|
|
|
has_local: false,
|
2024-08-01 10:24:05 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
"It's a bare virtual branch with no commit",
|
2024-07-27 20:15:02 +03:00
|
|
|
);
|
2024-07-28 11:46:46 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-08-28 14:06:28 +03:00
|
|
|
fn one_vbranch_in_workspace_one_commit() -> Result<()> {
|
2024-07-28 11:46:46 +03:00
|
|
|
init_env();
|
2024-08-28 14:06:28 +03:00
|
|
|
let ctx = project_ctx("one-vbranch-in-workspace-one-commit")?;
|
2024-07-30 17:18:07 +03:00
|
|
|
let list = list_branches(&ctx, None)?;
|
2024-07-28 11:46:46 +03:00
|
|
|
assert_eq!(list.len(), 1);
|
|
|
|
|
2024-08-01 10:24:05 +03:00
|
|
|
assert_equal(
|
|
|
|
&list[0],
|
|
|
|
ExpectedBranchListing {
|
2024-08-03 16:10:42 +03:00
|
|
|
identity: "virtual".into(),
|
2024-08-01 10:24:05 +03:00
|
|
|
virtual_branch_given_name: Some("virtual"),
|
|
|
|
virtual_branch_in_workspace: true,
|
2024-08-05 15:17:46 +03:00
|
|
|
has_local: false,
|
2024-08-01 10:24:05 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
"It's a bare virtual branch with a single commit",
|
2024-07-28 11:46:46 +03:00
|
|
|
);
|
2024-07-27 20:15:02 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-07-30 17:57:55 +03:00
|
|
|
#[test]
|
2024-08-28 14:06:28 +03:00
|
|
|
fn two_vbranches_in_workspace_one_commit() -> Result<()> {
|
2024-07-30 17:57:55 +03:00
|
|
|
init_env();
|
2024-08-28 14:06:28 +03:00
|
|
|
let ctx = project_ctx("two-vbranches-in-workspace-one-applied")?;
|
2024-07-30 17:57:55 +03:00
|
|
|
let list = list_branches(
|
|
|
|
&ctx,
|
|
|
|
Some(BranchListingFilter {
|
2024-08-05 12:37:39 +03:00
|
|
|
local: Some(true),
|
2024-07-30 17:57:55 +03:00
|
|
|
applied: Some(true),
|
|
|
|
}),
|
|
|
|
)?;
|
|
|
|
assert_eq!(list.len(), 1, "only one of these is applied");
|
2024-08-01 10:24:05 +03:00
|
|
|
assert_equal(
|
|
|
|
&list[0],
|
|
|
|
ExpectedBranchListing {
|
2024-08-03 16:10:42 +03:00
|
|
|
identity: "other".into(),
|
2024-08-01 10:24:05 +03:00
|
|
|
virtual_branch_given_name: Some("other"),
|
|
|
|
virtual_branch_in_workspace: true,
|
2024-08-05 15:17:46 +03:00
|
|
|
has_local: false,
|
2024-08-01 10:24:05 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
"It's a bare virtual branch without any branches with the same identity",
|
2024-07-30 17:57:55 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
let list = list_branches(
|
|
|
|
&ctx,
|
|
|
|
Some(BranchListingFilter {
|
2024-08-05 12:37:39 +03:00
|
|
|
local: Some(true),
|
2024-07-30 17:57:55 +03:00
|
|
|
applied: Some(false),
|
|
|
|
}),
|
|
|
|
)?;
|
|
|
|
assert_eq!(list.len(), 1, "only one of these is *not* applied");
|
2024-08-01 10:24:05 +03:00
|
|
|
assert_equal(
|
|
|
|
&list[0],
|
|
|
|
ExpectedBranchListing {
|
2024-08-03 16:10:42 +03:00
|
|
|
identity: "virtual".into(),
|
2024-08-01 10:24:05 +03:00
|
|
|
virtual_branch_given_name: Some("virtual"),
|
|
|
|
virtual_branch_in_workspace: false,
|
2024-08-05 12:37:39 +03:00
|
|
|
has_local: true,
|
2024-08-01 10:24:05 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
"It's a bare virtual branch without any branches with the same identity",
|
2024-07-30 17:57:55 +03:00
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-08-28 14:06:28 +03:00
|
|
|
fn one_feature_branch_and_one_vbranch_in_workspace_one_commit() -> Result<()> {
|
2024-07-30 17:57:55 +03:00
|
|
|
init_env();
|
|
|
|
let ctx = project_ctx("a-vbranch-named-like-target-branch-short-name")?;
|
|
|
|
let list = list_branches(&ctx, None)?;
|
|
|
|
assert_eq!(
|
|
|
|
list.len(),
|
2024-08-01 10:07:33 +03:00
|
|
|
1,
|
2024-08-01 16:35:05 +03:00
|
|
|
"it finds our single virtual branch despite it having the same 'identity' as the target branch: 'main'"
|
2024-07-30 17:57:55 +03:00
|
|
|
);
|
2024-08-01 10:24:05 +03:00
|
|
|
assert_equal(
|
|
|
|
&list[0],
|
|
|
|
ExpectedBranchListing {
|
2024-08-03 16:10:42 +03:00
|
|
|
identity: "main".into(),
|
2024-08-01 16:22:03 +03:00
|
|
|
remotes: vec!["origin"],
|
2024-08-01 10:24:05 +03:00
|
|
|
virtual_branch_given_name: Some("main"),
|
|
|
|
virtual_branch_in_workspace: true,
|
2024-08-05 12:37:39 +03:00
|
|
|
has_local: true,
|
2024-08-01 10:24:05 +03:00
|
|
|
},
|
|
|
|
"virtual branches can have the name of the target, even though it's probably not going to work when pushing. \
|
|
|
|
The remotes of the local `refs/heads/main` are shown."
|
|
|
|
);
|
2024-07-30 17:57:55 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:24:05 +03:00
|
|
|
#[test]
|
2024-08-28 14:06:28 +03:00
|
|
|
fn one_branch_in_workspace_multiple_remotes() -> Result<()> {
|
2024-08-01 16:35:05 +03:00
|
|
|
init_env();
|
2024-08-28 14:06:28 +03:00
|
|
|
let ctx = project_ctx("one-vbranch-in-workspace-two-remotes")?;
|
2024-08-01 16:35:05 +03:00
|
|
|
let list = list_branches(&ctx, None)?;
|
|
|
|
assert_eq!(list.len(), 1, "a single virtual branch");
|
|
|
|
|
|
|
|
assert_equal(
|
|
|
|
&list[0],
|
|
|
|
ExpectedBranchListing {
|
2024-08-03 16:10:42 +03:00
|
|
|
identity: "main".into(),
|
2024-08-07 11:51:46 +03:00
|
|
|
remotes: vec!["origin", "other-remote"],
|
2024-08-01 16:35:05 +03:00
|
|
|
virtual_branch_given_name: Some("main"),
|
|
|
|
virtual_branch_in_workspace: true,
|
2024-08-05 12:37:39 +03:00
|
|
|
has_local: true,
|
2024-08-01 16:35:05 +03:00
|
|
|
},
|
|
|
|
"multiple remotes are detected",
|
|
|
|
);
|
2024-08-01 10:24:05 +03:00
|
|
|
Ok(())
|
2024-07-28 11:46:46 +03:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:24:05 +03:00
|
|
|
mod util {
|
2024-08-01 16:35:05 +03:00
|
|
|
use anyhow::Result;
|
2024-08-09 11:30:52 +03:00
|
|
|
use gitbutler_branch::BranchIdentity;
|
|
|
|
use gitbutler_branch_actions::{BranchListing, BranchListingFilter};
|
2024-08-01 10:24:05 +03:00
|
|
|
use gitbutler_command_context::CommandContext;
|
|
|
|
|
|
|
|
/// A flattened and simplified mirror of `BranchListing` for comparing the actual and expected data.
|
2024-08-03 16:10:42 +03:00
|
|
|
#[derive(Debug, PartialEq)]
|
2024-08-01 10:24:05 +03:00
|
|
|
pub struct ExpectedBranchListing<'a> {
|
2024-08-03 16:10:42 +03:00
|
|
|
pub identity: BranchIdentity,
|
2024-08-01 10:24:05 +03:00
|
|
|
pub remotes: Vec<&'a str>,
|
|
|
|
pub virtual_branch_given_name: Option<&'a str>,
|
|
|
|
pub virtual_branch_in_workspace: bool,
|
2024-08-05 12:37:39 +03:00
|
|
|
pub has_local: bool,
|
2024-08-01 10:24:05 +03:00
|
|
|
}
|
|
|
|
|
2024-08-03 16:10:42 +03:00
|
|
|
impl Default for ExpectedBranchListing<'static> {
|
|
|
|
fn default() -> Self {
|
|
|
|
ExpectedBranchListing {
|
2024-08-09 11:30:52 +03:00
|
|
|
identity: "invalid-identity-should-always-be-specified".into(),
|
2024-08-03 16:10:42 +03:00
|
|
|
remotes: vec![],
|
|
|
|
virtual_branch_given_name: None,
|
|
|
|
virtual_branch_in_workspace: false,
|
|
|
|
has_local: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:24:05 +03:00
|
|
|
pub fn assert_equal(
|
|
|
|
BranchListing {
|
|
|
|
name,
|
|
|
|
remotes,
|
|
|
|
virtual_branch,
|
|
|
|
updated_at: _,
|
|
|
|
head: _, // NOTE: can't have stable commits while `gitbutler-change-id` is not stable/is a UUID.
|
2024-08-05 12:37:39 +03:00
|
|
|
last_commiter: _,
|
|
|
|
has_local,
|
2024-08-01 10:24:05 +03:00
|
|
|
}: &BranchListing,
|
2024-08-05 12:37:39 +03:00
|
|
|
expected: ExpectedBranchListing,
|
2024-08-01 10:24:05 +03:00
|
|
|
msg: &str,
|
|
|
|
) {
|
2024-08-01 16:35:05 +03:00
|
|
|
assert_eq!(*name, expected.identity, "identity: {msg}");
|
2024-08-01 10:24:05 +03:00
|
|
|
assert_eq!(
|
|
|
|
*remotes,
|
|
|
|
expected
|
|
|
|
.remotes
|
|
|
|
.into_iter()
|
2024-08-09 11:30:52 +03:00
|
|
|
.map(|name| gix::remote::Name::Symbol(name.into()))
|
2024-08-01 10:24:05 +03:00
|
|
|
.collect::<Vec<_>>(),
|
2024-08-01 16:35:05 +03:00
|
|
|
"remotes: {msg}"
|
2024-08-01 10:24:05 +03:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
virtual_branch.as_ref().map(|b| b.given_name.as_str()),
|
|
|
|
expected.virtual_branch_given_name,
|
2024-08-01 16:35:05 +03:00
|
|
|
"virtual-branch-name: {msg}"
|
2024-08-01 10:24:05 +03:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
virtual_branch.as_ref().map_or(false, |b| b.in_workspace),
|
|
|
|
expected.virtual_branch_in_workspace,
|
2024-08-01 16:35:05 +03:00
|
|
|
"virtual-branch-in-workspace: {msg}"
|
|
|
|
);
|
2024-08-05 12:37:39 +03:00
|
|
|
assert_eq!(*has_local, expected.has_local, "{msg}");
|
2024-08-01 10:24:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This function affects all tests, but those who care should just call it, assuming
|
|
|
|
/// they all care for the same default value.
|
|
|
|
/// If not, they should be placed in their own integration test or run with `#[serial_test:serial]`.
|
|
|
|
/// For `list_branches` it's needed as it compares the current author with commit authors to determine ownership.
|
|
|
|
pub fn init_env() {
|
|
|
|
for (name, value) in [
|
|
|
|
("GIT_AUTHOR_DATE", "2000-01-01 00:00:00 +0000"),
|
|
|
|
("GIT_AUTHOR_EMAIL", "author@example.com"),
|
|
|
|
("GIT_AUTHOR_NAME", "author"),
|
|
|
|
("GIT_COMMITTER_DATE", "2000-01-02 00:00:00 +0000"),
|
|
|
|
("GIT_COMMITTER_EMAIL", "committer@example.com"),
|
|
|
|
("GIT_COMMITTER_NAME", "committer"),
|
|
|
|
] {
|
|
|
|
std::env::set_var(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 16:35:05 +03:00
|
|
|
pub fn project_ctx(name: &str) -> Result<CommandContext> {
|
2024-08-01 10:24:05 +03:00
|
|
|
gitbutler_testsupport::read_only::fixture("for-listing.sh", name)
|
|
|
|
}
|
2024-08-01 16:35:05 +03:00
|
|
|
|
|
|
|
pub fn list_branches(
|
|
|
|
ctx: &CommandContext,
|
|
|
|
filter: Option<BranchListingFilter>,
|
|
|
|
) -> Result<Vec<BranchListing>> {
|
2024-08-05 12:37:39 +03:00
|
|
|
let mut branches = gitbutler_branch_actions::list_branches(ctx, filter, None)?;
|
2024-08-01 16:35:05 +03:00
|
|
|
branches.sort_by(|a, b| a.name.cmp(&b.name));
|
|
|
|
Ok(branches)
|
|
|
|
}
|
2024-07-27 16:37:28 +03:00
|
|
|
}
|
2024-08-10 22:04:09 +03:00
|
|
|
pub use util::{assert_equal, init_env, list_branches, project_ctx, ExpectedBranchListing};
|