ok/jj
1
0
Fork 0
forked from mirrors/jj

Allow advance_branches tests to be parameterized

In a future commit these tests will run with both `jj commit` and `jj new` since
both will have the same semantics for advancing the branch pointer.

Parameterizing the tests allows us to run both variants without duplicating the
test bodies. Since the commit IDs are different when `jj describe` + `jj new`
is used instead of `jj commit`, this commit also drops the commit IDs from the
test snapshots. This is fine because the commit IDs are not important for these
tests.
This commit is contained in:
Evan Mesterhazy 2024-03-14 17:15:18 -04:00 committed by Evan Mesterhazy
parent bbd9c7c7cb
commit 3f0e4de64a

View file

@ -14,117 +14,134 @@
use std::path::Path; use std::path::Path;
use itertools::Itertools; use test_case::test_case;
use crate::common::TestEnvironment; use crate::common::TestEnvironment;
fn get_log_output_with_branches(test_env: &TestEnvironment, cwd: &Path) -> String { fn get_log_output_with_branches(test_env: &TestEnvironment, cwd: &Path) -> String {
let template = r#"commit_id.short() ++ " br:{" ++ local_branches ++ "} dsc: " ++ description"#; // Don't include commit IDs since they will be different depending on
// whether the test runs with `jj commit` or `jj describe` + `jj new`.
let template = r#""branches{" ++ local_branches ++ "} desc: " ++ description"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template]) test_env.jj_cmd_success(cwd, &["log", "-T", template])
} }
fn enable_advance_branches_for_patterns(test_env: &TestEnvironment, cwd: &Path, patterns: &[&str]) { fn set_advance_branches(test_env: &TestEnvironment, enabled: bool) {
#[rustfmt::skip] if enabled {
let pattern_string: String = patterns.iter().map(|x| format!("\"{}\"", x)).join(","); test_env.add_config(
test_env.jj_cmd_success( r#"[experimental-advance-branches]
cwd, enabled-branches = ["glob:*"]
&[ "#,
"config", );
"set",
"--repo",
"experimental-advance-branches.enabled-branches",
&format!("[{}]", pattern_string),
],
);
}
fn set_advance_branches(test_env: &TestEnvironment, cwd: &Path, value: bool) {
if value {
enable_advance_branches_for_patterns(test_env, cwd, &["glob:*"]);
} else { } else {
enable_advance_branches_for_patterns(test_env, cwd, &[""]); test_env.add_config(
r#"[experimental-advance-branches]
enabled-branches = [""]
"#,
);
} }
} }
// Runs a command in the specified test environment and workspace path that
// describes the current commit with `commit_message` and creates a new commit
// on top of it.
type CommitFn = fn(env: &TestEnvironment, workspace_path: &Path, commit_message: &str);
// Implements CommitFn using the `jj commit` command.
fn commit_cmd(env: &TestEnvironment, workspace_path: &Path, commit_message: &str) {
env.jj_cmd_ok(workspace_path, &["commit", "-m", commit_message]);
}
// Check that enabling and disabling advance-branches works as expected. // Check that enabling and disabling advance-branches works as expected.
#[test] #[test_case(commit_cmd ; "commit")]
fn test_advance_branches_enabled() { fn test_advance_branches_enabled(make_commit: CommitFn) {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let workspace_path = test_env.env_root().join("repo"); let workspace_path = test_env.env_root().join("repo");
// First, test with advance-branches enabled. Start by creating a branch on the // First, test with advance-branches enabled. Start by creating a branch on the
// root commit. // root commit.
set_advance_branches(&test_env, &workspace_path, true); set_advance_branches(&test_env, true);
test_env.jj_cmd_ok( test_env.jj_cmd_ok(
&workspace_path, &workspace_path,
&["branch", "create", "-r", "@-", "test_branch"], &["branch", "create", "-r", "@-", "test_branch"],
); );
// Check the initial state of the repo. // Check the initial state of the repo.
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 230dd059e1b0 br:{} dsc: @ branches{} desc:
000000000000 br:{test_branch} dsc: branches{test_branch} desc:
"###); "###);
}
// Run jj commit, which will advance the branch pointing to @-. // Run jj commit, which will advance the branch pointing to @-.
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=first"]); make_commit(&test_env, &workspace_path, "first");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 24bb7f9da598 br:{} dsc: @ branches{} desc:
95f2456c4bbd br:{test_branch} dsc: first branches{test_branch} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
}
// Now disable advance branches and commit again. The branch shouldn't move. // Now disable advance branches and commit again. The branch shouldn't move.
set_advance_branches(&test_env, &workspace_path, false); set_advance_branches(&test_env, false);
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=second"]); make_commit(&test_env, &workspace_path, "second");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ b29edd893970 br:{} dsc: @ branches{} desc:
ebf7d96fb6ad br:{} dsc: second branches{} desc: second
95f2456c4bbd br:{test_branch} dsc: first branches{test_branch} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
}
} }
// Check that only a branch pointing to @- advances. Branches pointing to @ are // Check that only a branch pointing to @- advances. Branches pointing to @ are
// not advanced. // not advanced.
#[test] #[test_case(commit_cmd ; "commit")]
fn test_advance_branches_at_minus() { fn test_advance_branches_at_minus(make_commit: CommitFn) {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let workspace_path = test_env.env_root().join("repo"); let workspace_path = test_env.env_root().join("repo");
set_advance_branches(&test_env, &workspace_path, true); set_advance_branches(&test_env, true);
test_env.jj_cmd_ok(&workspace_path, &["branch", "create", "test_branch"]); test_env.jj_cmd_ok(&workspace_path, &["branch", "create", "test_branch"]);
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 230dd059e1b0 br:{test_branch} dsc: @ branches{test_branch} desc:
000000000000 br:{} dsc: branches{} desc:
"###); "###);
}
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=first"]); make_commit(&test_env, &workspace_path, "first");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 24bb7f9da598 br:{} dsc: @ branches{} desc:
95f2456c4bbd br:{test_branch} dsc: first branches{test_branch} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
}
// Create a second branch pointing to @. On the next commit, only the first // Create a second branch pointing to @. On the next commit, only the first
// branch, which points to @-, will advance. // branch, which points to @-, will advance.
test_env.jj_cmd_ok(&workspace_path, &["branch", "create", "test_branch2"]); test_env.jj_cmd_ok(&workspace_path, &["branch", "create", "test_branch2"]);
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=second"]); make_commit(&test_env, &workspace_path, "second");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ b29edd893970 br:{} dsc: @ branches{} desc:
ebf7d96fb6ad br:{test_branch test_branch2} dsc: second branches{test_branch test_branch2} desc: second
95f2456c4bbd br:{} dsc: first branches{} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
}
} }
// Test that per-branch overrides invert the behavior of // Test that per-branch overrides invert the behavior of
// experimental-advance-branches.enabled. // experimental-advance-branches.enabled.
#[test] #[test_case(commit_cmd ; "commit")]
fn test_advance_branches_overrides() { fn test_advance_branches_overrides(make_commit: CommitFn) {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let workspace_path = test_env.env_root().join("repo"); let workspace_path = test_env.env_root().join("repo");
@ -136,18 +153,22 @@ fn test_advance_branches_overrides() {
); );
// Check the initial state of the repo. // Check the initial state of the repo.
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 230dd059e1b0 br:{} dsc: @ branches{} desc:
000000000000 br:{test_branch} dsc: branches{test_branch} desc:
"###); "###);
}
// Commit will not advance the branch since advance-branches is disabled. // Commit will not advance the branch since advance-branches is disabled.
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=first"]); make_commit(&test_env, &workspace_path, "first");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 7e3a6f5e0f15 br:{} dsc: @ branches{} desc:
307e33f70413 br:{} dsc: first branches{} desc: first
000000000000 br:{test_branch} dsc: branches{test_branch} desc:
"###); "###);
}
// Now enable advance branches for "test_branch", move the branch, and commit // Now enable advance branches for "test_branch", move the branch, and commit
// again. // again.
@ -160,18 +181,22 @@ fn test_advance_branches_overrides() {
&workspace_path, &workspace_path,
&["branch", "set", "test_branch", "-r", "@-"], &["branch", "set", "test_branch", "-r", "@-"],
); );
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 7e3a6f5e0f15 br:{} dsc: @ branches{} desc:
307e33f70413 br:{test_branch} dsc: first branches{test_branch} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=second"]); }
make_commit(&test_env, &workspace_path, "second");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 8c1bd3e7de60 br:{} dsc: @ branches{} desc:
468d1ab20fb3 br:{test_branch} dsc: second branches{test_branch} desc: second
307e33f70413 br:{} dsc: first branches{} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
}
// Now disable advance branches for "test_branch" and "second_branch", which // Now disable advance branches for "test_branch" and "second_branch", which
// we will use later. Disabling always takes precedence over enabling. // we will use later. Disabling always takes precedence over enabling.
@ -181,14 +206,16 @@ fn test_advance_branches_overrides() {
disabled-branches = ["test_branch"] disabled-branches = ["test_branch"]
"#, "#,
); );
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=third"]); make_commit(&test_env, &workspace_path, "third");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 5888a83948dd br:{} dsc: @ branches{} desc:
50e9c28e6d85 br:{} dsc: third branches{} desc: third
468d1ab20fb3 br:{test_branch} dsc: second branches{test_branch} desc: second
307e33f70413 br:{} dsc: first branches{} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
}
// If we create a new branch at @- and move test_branch there as well. When // If we create a new branch at @- and move test_branch there as well. When
// we commit, only "second_branch" will advance since "test_branch" is disabled. // we commit, only "second_branch" will advance since "test_branch" is disabled.
@ -200,32 +227,36 @@ fn test_advance_branches_overrides() {
&workspace_path, &workspace_path,
&["branch", "set", "test_branch", "-r", "@-"], &["branch", "set", "test_branch", "-r", "@-"],
); );
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 5888a83948dd br:{} dsc: @ branches{} desc:
50e9c28e6d85 br:{second_branch test_branch} dsc: third branches{second_branch test_branch} desc: third
468d1ab20fb3 br:{} dsc: second branches{} desc: second
307e33f70413 br:{} dsc: first branches{} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=fourth"]); }
make_commit(&test_env, &workspace_path, "fourth");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 666d42aedca7 br:{} dsc: @ branches{} desc:
f23aa63eeb99 br:{second_branch} dsc: fourth branches{second_branch} desc: fourth
50e9c28e6d85 br:{test_branch} dsc: third branches{test_branch} desc: third
468d1ab20fb3 br:{} dsc: second branches{} desc: second
307e33f70413 br:{} dsc: first branches{} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
}
} }
// If multiple eligible branches point to @-, all of them will be advanced. // If multiple eligible branches point to @-, all of them will be advanced.
#[test] #[test_case(commit_cmd ; "commit")]
fn test_advance_branches_multiple_branches() { fn test_advance_branches_multiple_branches(make_commit: CommitFn) {
let test_env = TestEnvironment::default(); let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let workspace_path = test_env.env_root().join("repo"); let workspace_path = test_env.env_root().join("repo");
set_advance_branches(&test_env, &workspace_path, true); set_advance_branches(&test_env, true);
test_env.jj_cmd_ok( test_env.jj_cmd_ok(
&workspace_path, &workspace_path,
&["branch", "create", "-r", "@-", "first_branch"], &["branch", "create", "-r", "@-", "first_branch"],
@ -234,17 +265,22 @@ fn test_advance_branches_multiple_branches() {
&workspace_path, &workspace_path,
&["branch", "create", "-r", "@-", "second_branch"], &["branch", "create", "-r", "@-", "second_branch"],
); );
insta::allow_duplicates! {
// Check the initial state of the repo. // Check the initial state of the repo.
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 230dd059e1b0 br:{} dsc: @ branches{} desc:
000000000000 br:{first_branch second_branch} dsc: branches{first_branch second_branch} desc:
"###); "###);
}
// Both branches are eligible and both will advance. // Both branches are eligible and both will advance.
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=first"]); make_commit(&test_env, &workspace_path, "first");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###" insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ f307e5d9f90b br:{} dsc: @ branches{} desc:
0fca5c9228e6 br:{first_branch second_branch} dsc: first branches{first_branch second_branch} desc: first
000000000000 br:{} dsc: branches{} desc:
"###); "###);
}
} }