tests: extract log template function/variable for code readability

Also rewrites some lengthy templates. That's why the test output slightly
changed.
This commit is contained in:
Yuya Nishihara 2023-02-28 19:43:14 +09:00
parent 99cb0ba7c5
commit 6bbf4c6fcf
17 changed files with 103 additions and 139 deletions

View file

@ -102,5 +102,6 @@ fn test_branch_forget_glob() {
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(cwd, &["log", "-T", r#"branches " " commit_id.short()"#])
let template = r#"branches " " commit_id.short()"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
}

View file

@ -100,5 +100,6 @@ fn test_checkout_not_single_rev() {
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(cwd, &["log", "-T", r#"commit_id " " description"#])
let template = r#"commit_id " " description"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
}

View file

@ -89,5 +89,6 @@ fn test_commit_without_working_copy() {
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(cwd, &["log", "-T", r#"commit_id.short() " " description"#])
let template = r#"commit_id.short() " " description"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
}

View file

@ -43,15 +43,8 @@ fn test_log_author_timestamp_ago() {
test_env.jj_cmd_success(&repo_path, &["describe", "-m", "first"]);
test_env.jj_cmd_success(&repo_path, &["new", "-m", "second"]);
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"--no-graph",
"-T",
r#"author.timestamp().ago() "\\n""#,
],
);
let template = r#"author.timestamp().ago() "\n""#;
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "--no-graph", "-T", template]);
let line_re = Regex::new(r"[0-9]+ years ago").unwrap();
assert!(
stdout.lines().all(|x| line_re.is_match(x)),
@ -184,7 +177,7 @@ fn test_log_customize_short_id() {
&[
"log",
"--config-toml",
&format!("{decl}='id.shortest(5).prefix().upper() \"_\" id.shortest(5).rest()'"),
&format!(r#"{decl}='id.shortest(5).prefix().upper() "_" id.shortest(5).rest()'"#),
],
);
insta::assert_snapshot!(stdout, @r###"

View file

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::path::Path;
use crate::common::TestEnvironment;
pub mod common;
@ -69,8 +71,7 @@ fn test_concurrent_operations_auto_rebase() {
);
// We should be informed about the concurrent modification
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id \" \" description"]);
insta::assert_snapshot!(stdout, @r###"
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
Concurrent modification detected, resolving automatically.
Rebased 1 descendant commits onto commits rewritten by other operation
o 3f06323826b4a293a9ee6d24cc0e07ad2961b5d5 new child
@ -101,8 +102,7 @@ fn test_concurrent_operations_wc_modified() {
std::fs::write(repo_path.join("file"), "modified\n").unwrap();
// We should be informed about the concurrent modification
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id \" \" description"]);
insta::assert_snapshot!(stdout, @r###"
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
Concurrent modification detected, resolving automatically.
@ 4eb0610031b7cd148ff9f729a673a3f815033170 new child1
o 4b20e61d23ee7d7c4d5e61e11e97c26e716f9c30 new child2
@ -136,3 +136,8 @@ fn test_concurrent_operations_wc_modified() {
o initialize repo
"###);
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
let template = r#"commit_id " " description"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
}

View file

@ -322,23 +322,12 @@ fn test_rebase_duplicates() {
}
fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(
repo_path,
&[
"log",
"-T",
r#"commit_id.short() " " description.first_line()"#,
],
)
let template = r#"commit_id.short() " " description.first_line()"#;
test_env.jj_cmd_success(repo_path, &["log", "-T", template])
}
fn get_log_output_with_ts(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(
repo_path,
&[
"log",
"-T",
r#"commit_id.short() " " description.first_line() " @ " committer.timestamp()"#,
],
)
let template =
r#"commit_id.short() " " description.first_line() " @ " committer.timestamp()"#;
test_env.jj_cmd_success(repo_path, &["log", "-T", template])
}

View file

@ -109,7 +109,8 @@ fn read_file(path: &Path) -> String {
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(cwd, &["log", "-T", r#"commit_id.short() " " description"#])
let template = r#"commit_id.short() " " description"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
}
#[test]

View file

@ -292,28 +292,28 @@ fn test_git_colocated_squash_undo() {
insta::assert_snapshot!(get_log_output_divergence(&test_env, &repo_path), @r###"
o qpvuntsmwlqt 2f376ea1478c A master !divergence!
@ rlvkpnrzqnoo 8f71e3b6a3be
o qpvuntsmwlqt a86754f975f9 A !divergence!
o qpvuntsmwlqt a86754f975f9 A !divergence!
o zzzzzzzzzzzz 000000000000
"###);
}
fn get_log_output_divergence(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(
repo_path,
&[
"log",
"-T",
r#"change_id.short() " " commit_id.short() " " description.first_line() " " branches if(divergent, " !divergence!")"#,
],
let template = r###"
separate(" ",
change_id.short(),
commit_id.short(),
description.first_line(),
branches,
if(divergent, "!divergence!"),
)
"###;
test_env.jj_cmd_success(repo_path, &["log", "-T", template])
}
fn get_log_output(test_env: &TestEnvironment, workspace_root: &Path) -> String {
test_env.jj_cmd_success(
workspace_root,
&["log", "-T", "commit_id \" \" branches", "-r=all()"],
)
let template = r#"commit_id " " branches"#;
test_env.jj_cmd_success(workspace_root, &["log", "-T", template, "-r=all()"])
}
#[test]

View file

@ -64,16 +64,8 @@ fn create_commit(test_env: &TestEnvironment, repo_path: &Path, name: &str, paren
}
fn get_log_output(test_env: &TestEnvironment, workspace_root: &Path) -> String {
test_env.jj_cmd_success(
workspace_root,
&[
"log",
"-T",
r#"commit_id.short() " " description.first_line() " " branches"#,
"-r",
"all()",
],
)
let template = r#"commit_id.short() " " description.first_line() " " branches"#;
test_env.jj_cmd_success(workspace_root, &["log", "-T", template, "-r", "all()"])
}
#[test]

View file

@ -363,10 +363,15 @@ fn test_log_prefix_highlight_styled() {
fn prefix_format(len: Option<usize>) -> String {
format!(
r#"
"Change " change_id.shortest({0}) " " description.first_line()
" " commit_id.shortest({0}) " " branches
"#,
r###"
separate(" ",
"Change",
change_id.shortest({0}),
description.first_line(),
commit_id.shortest({0}),
branches,
)
"###,
len.map(|l| l.to_string()).unwrap_or(String::default())
)
}
@ -424,7 +429,7 @@ fn test_log_prefix_highlight_styled() {
o Change royxmykxtrkr commit2 1f99a5e19891
o Change mzvwutvlkqwt commit1 7b1f7dee65b4
o Change qpvuntsmwlqt initial ba1a30916d29 original
o Change zzzzzzzzzzzz 000000000000
o Change zzzzzzzzzzzz 000000000000
"###
);
let stdout = test_env.jj_cmd_success(
@ -450,7 +455,7 @@ fn test_log_prefix_highlight_styled() {
o Change roy commit2 1f9
o Change mzv commit1 7b1
o Change qpv initial ba1 original
o Change zzz 000
o Change zzz 000
"###
);
let stdout = test_env.jj_cmd_success(
@ -476,7 +481,7 @@ fn test_log_prefix_highlight_styled() {
o Change ro commit2 1f
o Change mz commit1 7b
o Change qpv initial ba1 original
o Change zzz 00
o Change zzz 00
"###
);
}
@ -493,10 +498,15 @@ fn test_log_prefix_highlight_counts_hidden_commits() {
"###,
);
let prefix_format = r#"
"Change " format_id(change_id) " " description.first_line()
" " format_id(commit_id) " " branches
"#;
let prefix_format = r###"
separate(" ",
"Change",
format_id(change_id),
description.first_line(),
format_id(commit_id),
branches,
)
"###;
std::fs::write(repo_path.join("file"), "original file\n").unwrap();
test_env.jj_cmd_success(&repo_path, &["describe", "-m", "initial"]);
@ -505,7 +515,7 @@ fn test_log_prefix_highlight_counts_hidden_commits() {
test_env.jj_cmd_success(&repo_path, &["log", "-r", "all()", "-T", prefix_format]),
@r###"
@ Change q[pvuntsmwlqt] initial b[a1a30916d29] original
o Change z[zzzzzzzzzzz] 0[00000000000]
o Change z[zzzzzzzzzzz] 0[00000000000]
"###
);
@ -520,10 +530,10 @@ fn test_log_prefix_highlight_counts_hidden_commits() {
insta::assert_snapshot!(
test_env.jj_cmd_success(&repo_path, &["log", "-T", prefix_format]),
@r###"
@ Change w[qnwkozpkust] 44[4c3c5066d3]
@ Change w[qnwkozpkust] 44[4c3c5066d3]
o Change q[pvuntsmwlqt] initial ba[1a30916d29] original
o Change z[zzzzzzzzzzz] 00[0000000000]
o Change z[zzzzzzzzzzz] 00[0000000000]
"###
);
insta::assert_snapshot!(
@ -599,17 +609,11 @@ fn test_log_divergence() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
let template = r#"description.first_line() if(divergent, " !divergence!")"#;
std::fs::write(repo_path.join("file"), "foo\n").unwrap();
test_env.jj_cmd_success(&repo_path, &["describe", "-m", "description 1"]);
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-T",
r#"description.first_line() if(divergent, " !divergence!")"#,
],
);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", template]);
// No divergence
insta::assert_snapshot!(stdout, @r###"
@ description 1
@ -621,14 +625,7 @@ fn test_log_divergence() {
&repo_path,
&["describe", "-m", "description 2", "--at-operation", "@-"],
);
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-T",
r#"description.first_line() if(divergent, " !divergence!")"#,
],
);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", template]);
insta::assert_snapshot!(stdout, @r###"
Concurrent modification detected, resolving automatically.
o description 2 !divergence!

View file

@ -320,7 +320,8 @@ fn test_move_partial() {
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(cwd, &["log", "-T", r#"commit_id.short() " " branches"#])
let template = r#"commit_id.short() " " branches"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
}
#[test]

View file

@ -299,14 +299,8 @@ fn test_new_insert_before_no_loop() {
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
setup_before_insertion(&test_env, &repo_path);
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-T",
r#"commit_id.short() " " if(description, description, "root")"#,
],
);
let template = r#"commit_id.short() " " if(description, description, "root")"#;
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", template]);
insta::assert_snapshot!(stdout, @r###"
@ 7705d353bf5d F
@ -409,12 +403,11 @@ fn setup_before_insertion(test_env: &TestEnvironment, repo_path: &Path) {
}
fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(repo_path, &["log", "-T", "commit_id \" \" description"])
let template = r#"commit_id " " description"#;
test_env.jj_cmd_success(repo_path, &["log", "-T", template])
}
fn get_short_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(
repo_path,
&["log", "-T", r#"if(description, description, "root")"#],
)
let template = r#"if(description, description, "root")"#;
test_env.jj_cmd_success(repo_path, &["log", "-T", template])
}

View file

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::path::Path;
use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment};
pub mod common;
@ -26,10 +28,9 @@ fn test_split_by_paths() {
std::fs::write(repo_path.join("file2"), "foo").unwrap();
std::fs::write(repo_path.join("file3"), "foo").unwrap();
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "change_id.short()"]);
insta::assert_snapshot!(stdout, @r###"
@ qpvuntsmwlqt
o zzzzzzzzzzzz
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ qpvuntsmwlqt false
o zzzzzzzzzzzz true
"###);
let edit_script = test_env.set_up_fake_editor();
@ -64,11 +65,10 @@ fn test_split_by_paths() {
JJ: Lines starting with "JJ: " (like this one) will be removed.
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "change_id.short()"]);
insta::assert_snapshot!(stdout, @r###"
@ kkmpptxzrspx
o qpvuntsmwlqt
o zzzzzzzzzzzz
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ kkmpptxzrspx false
o qpvuntsmwlqt false
o zzzzzzzzzzzz true
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s", "-r", "@-"]);
@ -91,9 +91,7 @@ fn test_split_by_paths() {
Working copy now at: 28d4ec20efa9 (no description set)
"###);
let stdout =
test_env.jj_cmd_success(&repo_path, &["log", "-T", r#"change_id.short() " " empty"#]);
insta::assert_snapshot!(stdout, @r###"
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ kkmpptxzrspx false
o yqosqzytrlsw true
o qpvuntsmwlqt false
@ -124,9 +122,7 @@ fn test_split_by_paths() {
The given paths do not match any file: nonexistent
"###);
let stdout =
test_env.jj_cmd_success(&repo_path, &["log", "-T", r#"change_id.short() " " empty"#]);
insta::assert_snapshot!(stdout, @r###"
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ kkmpptxzrspx false
o kpqxywonksrl false
o qpvuntsmwlqt true
@ -138,3 +134,8 @@ fn test_split_by_paths() {
A file2
"###);
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
let template = r#"change_id.short() " " empty"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
}

View file

@ -233,10 +233,8 @@ fn test_squash_partial() {
}
fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(
repo_path,
&["log", "-T", r#"commit_id.short() " " branches"#],
)
let template = r#"commit_id.short() " " branches"#;
test_env.jj_cmd_success(repo_path, &["log", "-T", template])
}
#[test]

View file

@ -63,10 +63,8 @@ fn test_templater_branches() {
test_env.jj_cmd_success(&origin_path, &["git", "export"]);
test_env.jj_cmd_success(&workspace_root, &["git", "fetch"]);
let output = test_env.jj_cmd_success(
&workspace_root,
&["log", "-T", r#"commit_id.short() " " branches"#],
);
let template = r#"commit_id.short() " " branches"#;
let output = test_env.jj_cmd_success(&workspace_root, &["log", "-T", template]);
insta::assert_snapshot!(output, @r###"
o b1bb3766d584 branch3??
@ a5b4d15489cc branch2* new-branch

View file

@ -199,10 +199,8 @@ fn test_unsquash_partial() {
}
fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(
repo_path,
&["log", "-T", r#"commit_id.short() " " branches"#],
)
let template = r#"commit_id.short() " " branches"#;
test_env.jj_cmd_success(repo_path, &["log", "-T", template])
}
#[test]

View file

@ -406,17 +406,12 @@ fn test_workspaces_root() {
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(
cwd,
&[
"log",
"-T",
r#"separate(" ",
commit_id,
working_copies,
if(divergent, "(divergent)"))"#,
"-r",
"all()",
],
let template = r###"
separate(" ",
commit_id,
working_copies,
if(divergent, "(divergent)"),
)
"###;
test_env.jj_cmd_success(cwd, &["log", "-T", template, "-r", "all()"])
}