diff --git a/cli/tests/common/mod.rs b/cli/tests/common/mod.rs index 2b5f93dff..054d18d93 100644 --- a/cli/tests/common/mod.rs +++ b/cli/tests/common/mod.rs @@ -17,6 +17,7 @@ use std::collections::HashMap; use std::path::Path; use std::path::PathBuf; +use indoc::formatdoc; use itertools::Itertools as _; use regex::Captures; use regex::Regex; @@ -288,22 +289,16 @@ impl TestEnvironment { /// Sets up the fake editor to read an edit script from the returned path /// Also sets up the fake editor as a merge tool named "fake-editor" pub fn set_up_fake_editor(&mut self) -> PathBuf { - let editor_path = assert_cmd::cargo::cargo_bin("fake-editor"); - assert!(editor_path.is_file()); - // Simplified TOML escaping, hoping that there are no '"' or control characters - // in it - let escaped_editor_path = editor_path.to_str().unwrap().replace('\\', r"\\"); - self.add_env_var("EDITOR", &escaped_editor_path); - self.add_config(format!( - r###" - [ui] - merge-editor = "fake-editor" + let editor_path = to_toml_value(fake_editor_path()); + self.add_config(formatdoc! {r#" + [ui] + editor = {editor_path} + merge-editor = "fake-editor" - [merge-tools] - fake-editor.program="{escaped_editor_path}" - fake-editor.merge-args = ["$output"] - "### - )); + [merge-tools] + fake-editor.program = {editor_path} + fake-editor.merge-args = ["$output"] + "#}); let edit_script = self.env_root().join("edit_script"); std::fs::write(&edit_script, "").unwrap(); self.add_env_var("EDIT_SCRIPT", edit_script.to_str().unwrap()); @@ -313,13 +308,11 @@ impl TestEnvironment { /// Sets up the fake diff-editor to read an edit script from the returned /// path pub fn set_up_fake_diff_editor(&mut self) -> PathBuf { - let escaped_diff_editor_path = escaped_fake_diff_editor_path(); - self.add_config(format!( - r###" + let diff_editor_path = to_toml_value(fake_diff_editor_path()); + self.add_config(formatdoc! {r#" ui.diff-editor = "fake-diff-editor" - merge-tools.fake-diff-editor.program = "{escaped_diff_editor_path}" - "### - )); + merge-tools.fake-diff-editor.program = {diff_editor_path} + "#}); let edit_script = self.env_root().join("diff_edit_script"); std::fs::write(&edit_script, "").unwrap(); self.add_env_var("DIFF_EDIT_SCRIPT", edit_script.to_str().unwrap()); @@ -363,12 +356,21 @@ pub fn get_stderr_string(assert: &assert_cmd::assert::Assert) -> String { String::from_utf8(assert.get_output().stderr.clone()).unwrap() } -pub fn escaped_fake_diff_editor_path() -> String { - let diff_editor_path = assert_cmd::cargo::cargo_bin("fake-diff-editor"); - assert!(diff_editor_path.is_file()); - // Simplified TOML escaping, hoping that there are no '"' or control characters - // in it - diff_editor_path.to_str().unwrap().replace('\\', r"\\") +pub fn fake_editor_path() -> String { + let path = assert_cmd::cargo::cargo_bin("fake-editor"); + assert!(path.is_file()); + path.into_os_string().into_string().unwrap() +} + +pub fn fake_diff_editor_path() -> String { + let path = assert_cmd::cargo::cargo_bin("fake-diff-editor"); + assert!(path.is_file()); + path.into_os_string().into_string().unwrap() +} + +/// Coerces the value type to serialize it as TOML. +pub fn to_toml_value(value: impl Into) -> toml_edit::Value { + value.into() } /// Returns a string with the last line removed. diff --git a/cli/tests/test_config_command.rs b/cli/tests/test_config_command.rs index e902f316c..03443d295 100644 --- a/cli/tests/test_config_command.rs +++ b/cli/tests/test_config_command.rs @@ -18,6 +18,7 @@ use indoc::indoc; use itertools::Itertools; use regex::Regex; +use crate::common::fake_editor_path; use crate::common::TestEnvironment; #[test] @@ -819,7 +820,8 @@ fn test_config_edit_user() { fn test_config_edit_user_new_file() { let mut test_env = TestEnvironment::default(); let user_config_path = test_env.config_path().join("config").join("file.toml"); - test_env.set_up_fake_editor(); // set $EDITOR, but added configuration is ignored + test_env.set_up_fake_editor(); // set $EDIT_SCRIPT, but added configuration is ignored + test_env.add_env_var("EDITOR", fake_editor_path()); test_env.set_config_path(&user_config_path); assert!(!user_config_path.exists()); diff --git a/cli/tests/test_diff_command.rs b/cli/tests/test_diff_command.rs index 5e5ad4cec..0c07eeaeb 100644 --- a/cli/tests/test_diff_command.rs +++ b/cli/tests/test_diff_command.rs @@ -15,8 +15,9 @@ use indoc::indoc; use itertools::Itertools; -use crate::common::escaped_fake_diff_editor_path; +use crate::common::fake_diff_editor_path; use crate::common::strip_last_line; +use crate::common::to_toml_value; use crate::common::TestEnvironment; #[test] @@ -2109,8 +2110,8 @@ fn test_diff_external_tool() { "###); // Inlined command arguments - let command = escaped_fake_diff_editor_path(); - let config = format!(r#"--config=ui.diff.tool=["{command}", "$right", "$left"]"#); + let command_toml = to_toml_value(fake_diff_editor_path()); + let config = format!("--config=ui.diff.tool=[{command_toml}, '$right', '$left']"); insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", &config]), @r###" file2 file3