Skip to content

Commit

Permalink
tests: use toml_edit to escape editor path, set ui.editor instead of …
Browse files Browse the repository at this point in the history
…$EDITOR

Most callers don't need the $EDITOR variable.
  • Loading branch information
yuja committed Dec 20, 2024
1 parent 36b7f00 commit d7e0ab6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
58 changes: 30 additions & 28 deletions cli/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
[merge-tools]
fake-editor.program="{escaped_editor_path}"
fake-editor.merge-args = ["$output"]
"###
));
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 = {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());
Expand All @@ -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());
Expand Down Expand Up @@ -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>) -> toml_edit::Value {
value.into()
}

/// Returns a string with the last line removed.
Expand Down
4 changes: 3 additions & 1 deletion cli/tests/test_config_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use indoc::indoc;
use itertools::Itertools;
use regex::Regex;

use crate::common::fake_editor_path;
use crate::common::TestEnvironment;

#[test]
Expand Down Expand Up @@ -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());

Expand Down
7 changes: 4 additions & 3 deletions cli/tests/test_diff_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d7e0ab6

Please sign in to comment.