From 6bbf4c6fcff59ee1d8e812e484eb69d945767f02 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 28 Feb 2023 19:43:14 +0900 Subject: [PATCH] tests: extract log template function/variable for code readability Also rewrites some lengthy templates. That's why the test output slightly changed. --- tests/test_branch_command.rs | 3 +- tests/test_checkout.rs | 3 +- tests/test_commit_command.rs | 3 +- tests/test_commit_template.rs | 13 ++----- tests/test_concurrent_operations.rs | 13 +++++-- tests/test_duplicate_command.rs | 21 +++-------- tests/test_edit_command.rs | 3 +- tests/test_git_colocated.rs | 24 ++++++------ tests/test_git_fetch.rs | 12 +----- tests/test_log_command.rs | 57 ++++++++++++++--------------- tests/test_move_command.rs | 3 +- tests/test_new_command.rs | 19 +++------- tests/test_split_command.rs | 31 ++++++++-------- tests/test_squash_command.rs | 6 +-- tests/test_templater.rs | 6 +-- tests/test_unsquash_command.rs | 6 +-- tests/test_workspaces.rs | 19 ++++------ 17 files changed, 103 insertions(+), 139 deletions(-) diff --git a/tests/test_branch_command.rs b/tests/test_branch_command.rs index 8c1b6983c..b9937e70b 100644 --- a/tests/test_branch_command.rs +++ b/tests/test_branch_command.rs @@ -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]) } diff --git a/tests/test_checkout.rs b/tests/test_checkout.rs index 31df64422..fdf17273e 100644 --- a/tests/test_checkout.rs +++ b/tests/test_checkout.rs @@ -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]) } diff --git a/tests/test_commit_command.rs b/tests/test_commit_command.rs index b27d6c261..f8b52d95d 100644 --- a/tests/test_commit_command.rs +++ b/tests/test_commit_command.rs @@ -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]) } diff --git a/tests/test_commit_template.rs b/tests/test_commit_template.rs index 8f90eb968..b5418eff4 100644 --- a/tests/test_commit_template.rs +++ b/tests/test_commit_template.rs @@ -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###" diff --git a/tests/test_concurrent_operations.rs b/tests/test_concurrent_operations.rs index 92a9fe823..e5b0e4f6a 100644 --- a/tests/test_concurrent_operations.rs +++ b/tests/test_concurrent_operations.rs @@ -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]) +} diff --git a/tests/test_duplicate_command.rs b/tests/test_duplicate_command.rs index 65add59c7..5dbf52d93 100644 --- a/tests/test_duplicate_command.rs +++ b/tests/test_duplicate_command.rs @@ -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]) } diff --git a/tests/test_edit_command.rs b/tests/test_edit_command.rs index 53296b5ce..a8a984a84 100644 --- a/tests/test_edit_command.rs +++ b/tests/test_edit_command.rs @@ -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] diff --git a/tests/test_git_colocated.rs b/tests/test_git_colocated.rs index 594bf30fd..25f461058 100644 --- a/tests/test_git_colocated.rs +++ b/tests/test_git_colocated.rs @@ -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] diff --git a/tests/test_git_fetch.rs b/tests/test_git_fetch.rs index 9b4f45f84..a068d11ad 100644 --- a/tests/test_git_fetch.rs +++ b/tests/test_git_fetch.rs @@ -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] diff --git a/tests/test_log_command.rs b/tests/test_log_command.rs index f47eefc09..2a124792d 100644 --- a/tests/test_log_command.rs +++ b/tests/test_log_command.rs @@ -363,10 +363,15 @@ fn test_log_prefix_highlight_styled() { fn prefix_format(len: Option) -> 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! diff --git a/tests/test_move_command.rs b/tests/test_move_command.rs index 9805350c7..ab0025fef 100644 --- a/tests/test_move_command.rs +++ b/tests/test_move_command.rs @@ -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] diff --git a/tests/test_new_command.rs b/tests/test_new_command.rs index 399a6923e..54056c0d3 100644 --- a/tests/test_new_command.rs +++ b/tests/test_new_command.rs @@ -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]) } diff --git a/tests/test_split_command.rs b/tests/test_split_command.rs index 5bdd6867d..6f355808e 100644 --- a/tests/test_split_command.rs +++ b/tests/test_split_command.rs @@ -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]) +} diff --git a/tests/test_squash_command.rs b/tests/test_squash_command.rs index 9df686183..57f092481 100644 --- a/tests/test_squash_command.rs +++ b/tests/test_squash_command.rs @@ -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] diff --git a/tests/test_templater.rs b/tests/test_templater.rs index 09143ad69..dd8e65e6c 100644 --- a/tests/test_templater.rs +++ b/tests/test_templater.rs @@ -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 diff --git a/tests/test_unsquash_command.rs b/tests/test_unsquash_command.rs index 2564fc3b6..23555fbc4 100644 --- a/tests/test_unsquash_command.rs +++ b/tests/test_unsquash_command.rs @@ -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] diff --git a/tests/test_workspaces.rs b/tests/test_workspaces.rs index 6446e8dc5..f03342ee1 100644 --- a/tests/test_workspaces.rs +++ b/tests/test_workspaces.rs @@ -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()"]) }