mirror of
https://github.com/martinvonz/jj.git
synced 2025-02-06 20:42:10 +00:00
tests: use toml_edit to escape editor path, set ui.editor instead of $EDITOR
Most callers don't need the $EDITOR variable.
This commit is contained in:
parent
36b7f007eb
commit
d7e0ab6119
3 changed files with 36 additions and 31 deletions
|
@ -17,6 +17,7 @@ use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use indoc::formatdoc;
|
||||||
use itertools::Itertools as _;
|
use itertools::Itertools as _;
|
||||||
use regex::Captures;
|
use regex::Captures;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
@ -288,22 +289,16 @@ impl TestEnvironment {
|
||||||
/// Sets up the fake editor to read an edit script from the returned path
|
/// 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"
|
/// Also sets up the fake editor as a merge tool named "fake-editor"
|
||||||
pub fn set_up_fake_editor(&mut self) -> PathBuf {
|
pub fn set_up_fake_editor(&mut self) -> PathBuf {
|
||||||
let editor_path = assert_cmd::cargo::cargo_bin("fake-editor");
|
let editor_path = to_toml_value(fake_editor_path());
|
||||||
assert!(editor_path.is_file());
|
self.add_config(formatdoc! {r#"
|
||||||
// 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]
|
[ui]
|
||||||
|
editor = {editor_path}
|
||||||
merge-editor = "fake-editor"
|
merge-editor = "fake-editor"
|
||||||
|
|
||||||
[merge-tools]
|
[merge-tools]
|
||||||
fake-editor.program="{escaped_editor_path}"
|
fake-editor.program = {editor_path}
|
||||||
fake-editor.merge-args = ["$output"]
|
fake-editor.merge-args = ["$output"]
|
||||||
"###
|
"#});
|
||||||
));
|
|
||||||
let edit_script = self.env_root().join("edit_script");
|
let edit_script = self.env_root().join("edit_script");
|
||||||
std::fs::write(&edit_script, "").unwrap();
|
std::fs::write(&edit_script, "").unwrap();
|
||||||
self.add_env_var("EDIT_SCRIPT", edit_script.to_str().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
|
/// Sets up the fake diff-editor to read an edit script from the returned
|
||||||
/// path
|
/// path
|
||||||
pub fn set_up_fake_diff_editor(&mut self) -> PathBuf {
|
pub fn set_up_fake_diff_editor(&mut self) -> PathBuf {
|
||||||
let escaped_diff_editor_path = escaped_fake_diff_editor_path();
|
let diff_editor_path = to_toml_value(fake_diff_editor_path());
|
||||||
self.add_config(format!(
|
self.add_config(formatdoc! {r#"
|
||||||
r###"
|
|
||||||
ui.diff-editor = "fake-diff-editor"
|
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");
|
let edit_script = self.env_root().join("diff_edit_script");
|
||||||
std::fs::write(&edit_script, "").unwrap();
|
std::fs::write(&edit_script, "").unwrap();
|
||||||
self.add_env_var("DIFF_EDIT_SCRIPT", edit_script.to_str().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()
|
String::from_utf8(assert.get_output().stderr.clone()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn escaped_fake_diff_editor_path() -> String {
|
pub fn fake_editor_path() -> String {
|
||||||
let diff_editor_path = assert_cmd::cargo::cargo_bin("fake-diff-editor");
|
let path = assert_cmd::cargo::cargo_bin("fake-editor");
|
||||||
assert!(diff_editor_path.is_file());
|
assert!(path.is_file());
|
||||||
// Simplified TOML escaping, hoping that there are no '"' or control characters
|
path.into_os_string().into_string().unwrap()
|
||||||
// in it
|
}
|
||||||
diff_editor_path.to_str().unwrap().replace('\\', r"\\")
|
|
||||||
|
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.
|
/// Returns a string with the last line removed.
|
||||||
|
|
|
@ -18,6 +18,7 @@ use indoc::indoc;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
use crate::common::fake_editor_path;
|
||||||
use crate::common::TestEnvironment;
|
use crate::common::TestEnvironment;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -819,7 +820,8 @@ fn test_config_edit_user() {
|
||||||
fn test_config_edit_user_new_file() {
|
fn test_config_edit_user_new_file() {
|
||||||
let mut test_env = TestEnvironment::default();
|
let mut test_env = TestEnvironment::default();
|
||||||
let user_config_path = test_env.config_path().join("config").join("file.toml");
|
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);
|
test_env.set_config_path(&user_config_path);
|
||||||
assert!(!user_config_path.exists());
|
assert!(!user_config_path.exists());
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,9 @@
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
use itertools::Itertools;
|
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::strip_last_line;
|
||||||
|
use crate::common::to_toml_value;
|
||||||
use crate::common::TestEnvironment;
|
use crate::common::TestEnvironment;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -2109,8 +2110,8 @@ fn test_diff_external_tool() {
|
||||||
"###);
|
"###);
|
||||||
|
|
||||||
// Inlined command arguments
|
// Inlined command arguments
|
||||||
let command = escaped_fake_diff_editor_path();
|
let command_toml = to_toml_value(fake_diff_editor_path());
|
||||||
let config = format!(r#"--config=ui.diff.tool=["{command}", "$right", "$left"]"#);
|
let config = format!("--config=ui.diff.tool=[{command_toml}, '$right', '$left']");
|
||||||
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", &config]), @r###"
|
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["diff", &config]), @r###"
|
||||||
file2
|
file2
|
||||||
file3
|
file3
|
||||||
|
|
Loading…
Reference in a new issue