Skip to content

Commit

Permalink
Allow advance_branches tests to be parameterized
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
emesterhazy committed Mar 31, 2024
1 parent 006171d commit 4e83afd
Showing 1 changed file with 129 additions and 93 deletions.
222 changes: 129 additions & 93 deletions cli/tests/test_advance_branches.rs
Original file line number Diff line number Diff line change
@@ -1,116 +1,133 @@
use std::path::Path;

use itertools::Itertools;
use test_case::test_case;

use crate::common::TestEnvironment;

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])
}

fn enable_advance_branches_for_patterns(test_env: &TestEnvironment, cwd: &Path, patterns: &[&str]) {
#[rustfmt::skip]
let pattern_string: String = patterns.iter().map(|x| format!("\"{}\"", x)).join(",");
test_env.jj_cmd_success(
cwd,
&[
"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:*"]);
fn set_advance_branches(test_env: &TestEnvironment, enabled: bool) {
if enabled {
test_env.add_config(
r#"[experimental-advance-branches]
enabled-branches = ["glob:*"]
"#,
);
} 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.
#[test]
fn test_advance_branches_enabled() {
#[test_case(commit_cmd ; "commit")]
fn test_advance_branches_enabled(make_commit: CommitFn) {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let workspace_path = test_env.env_root().join("repo");

// First, test with advance-branches enabled. Start by creating a branch on the
// root commit.
set_advance_branches(&test_env, &workspace_path, true);
set_advance_branches(&test_env, true);
test_env.jj_cmd_ok(
&workspace_path,
&["branch", "create", "-r", "@-", "test_branch"],
);

// Check the initial state of the repo.
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 230dd059e1b0 br:{} dsc:
000000000000 br:{test_branch} dsc:
@ branches{} desc:
branches{test_branch} desc:
"###);
}

// 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###"
@ 24bb7f9da598 br:{} dsc:
95f2456c4bbd br:{test_branch} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{test_branch} desc: first
branches{} desc:
"###);
}

// Now disable advance branches and commit again. The branch shouldn't move.
set_advance_branches(&test_env, &workspace_path, false);
test_env.jj_cmd_ok(&workspace_path, &["commit", "-m=second"]);
set_advance_branches(&test_env, false);
make_commit(&test_env, &workspace_path, "second");
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ b29edd893970 br:{} dsc:
ebf7d96fb6ad br:{} dsc: second
95f2456c4bbd br:{test_branch} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{} desc: second
branches{test_branch} desc: first
branches{} desc:
"###);
}
}

// Check that only a branch pointing to @- advances. Branches pointing to @ are
// not advanced.
#[test]
fn test_advance_branches_at_minus() {
#[test_case(commit_cmd ; "commit")]
fn test_advance_branches_at_minus(make_commit: CommitFn) {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "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"]);

insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 230dd059e1b0 br:{test_branch} dsc:
000000000000 br:{} dsc:
@ branches{test_branch} desc:
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###"
@ 24bb7f9da598 br:{} dsc:
95f2456c4bbd br:{test_branch} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{test_branch} desc: first
branches{} desc:
"###);
}

// Create a second branch pointing to @. On the next commit, only the first
// branch, which points to @-, will advance.
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###"
@ b29edd893970 br:{} dsc:
ebf7d96fb6ad br:{test_branch test_branch2} dsc: second
95f2456c4bbd br:{} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{test_branch test_branch2} desc: second
branches{} desc: first
branches{} desc:
"###);
}
}

// Test that per-branch overrides invert the behavior of
// experimental-advance-branches.enabled.
#[test]
fn test_advance_branches_overrides() {
#[test_case(commit_cmd ; "commit")]
fn test_advance_branches_overrides(make_commit: CommitFn) {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let workspace_path = test_env.env_root().join("repo");
Expand All @@ -122,18 +139,22 @@ fn test_advance_branches_overrides() {
);

// Check the initial state of the repo.
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 230dd059e1b0 br:{} dsc:
000000000000 br:{test_branch} dsc:
@ branches{} desc:
branches{test_branch} desc:
"###);
}

// 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###"
@ 7e3a6f5e0f15 br:{} dsc:
307e33f70413 br:{} dsc: first
000000000000 br:{test_branch} dsc:
@ branches{} desc:
branches{} desc: first
branches{test_branch} desc:
"###);
}

// Now enable advance branches for "test_branch", move the branch, and commit
// again.
Expand All @@ -146,18 +167,22 @@ fn test_advance_branches_overrides() {
&workspace_path,
&["branch", "set", "test_branch", "-r", "@-"],
);
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 7e3a6f5e0f15 br:{} dsc:
307e33f70413 br:{test_branch} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{test_branch} desc: first
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###"
@ 8c1bd3e7de60 br:{} dsc:
468d1ab20fb3 br:{test_branch} dsc: second
307e33f70413 br:{} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{test_branch} desc: second
branches{} desc: first
branches{} desc:
"###);
}

// Now disable advance branches for "test_branch" and "second_branch", which
// we will use later. Disabling always takes precedence over enabling.
Expand All @@ -167,14 +192,16 @@ fn test_advance_branches_overrides() {
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###"
@ 5888a83948dd br:{} dsc:
50e9c28e6d85 br:{} dsc: third
468d1ab20fb3 br:{test_branch} dsc: second
307e33f70413 br:{} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{} desc: third
branches{test_branch} desc: second
branches{} desc: first
branches{} desc:
"###);
}

// 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.
Expand All @@ -186,32 +213,36 @@ fn test_advance_branches_overrides() {
&workspace_path,
&["branch", "set", "test_branch", "-r", "@-"],
);
insta::allow_duplicates! {
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 5888a83948dd br:{} dsc:
50e9c28e6d85 br:{second_branch test_branch} dsc: third
468d1ab20fb3 br:{} dsc: second
307e33f70413 br:{} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{second_branch test_branch} desc: third
branches{} desc: second
branches{} desc: first
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###"
@ 666d42aedca7 br:{} dsc:
f23aa63eeb99 br:{second_branch} dsc: fourth
50e9c28e6d85 br:{test_branch} dsc: third
468d1ab20fb3 br:{} dsc: second
307e33f70413 br:{} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{second_branch} desc: fourth
branches{test_branch} desc: third
branches{} desc: second
branches{} desc: first
branches{} desc:
"###);
}
}

// If multiple eligible branches point to @-, all of them will be advanced.
#[test]
fn test_advance_branches_multiple_branches() {
#[test_case(commit_cmd ; "commit")]
fn test_advance_branches_multiple_branches(make_commit: CommitFn) {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "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", "-r", "@-", "first_branch"],
Expand All @@ -220,17 +251,22 @@ fn test_advance_branches_multiple_branches() {
&workspace_path,
&["branch", "create", "-r", "@-", "second_branch"],
);

insta::allow_duplicates! {
// Check the initial state of the repo.
insta::assert_snapshot!(get_log_output_with_branches(&test_env, &workspace_path), @r###"
@ 230dd059e1b0 br:{} dsc:
000000000000 br:{first_branch second_branch} dsc:
@ branches{} desc:
branches{first_branch second_branch} desc:
"###);
}

// 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###"
@ f307e5d9f90b br:{} dsc:
0fca5c9228e6 br:{first_branch second_branch} dsc: first
000000000000 br:{} dsc:
@ branches{} desc:
branches{first_branch second_branch} desc: first
branches{} desc:
"###);
}
}

0 comments on commit 4e83afd

Please sign in to comment.