From 3bca159b1a43ee17ed712b4d9f764ad41b70f07b Mon Sep 17 00:00:00 2001 From: mlcui Date: Sun, 30 Jun 2024 21:16:40 +1000 Subject: [PATCH] fake-editor: replace expectpath with dump-path This allows us to assert the expected path in the test itself, rather than in the fake editor. --- cli/testing/fake-editor.rs | 13 +++++-------- cli/tests/test_config_command.rs | 25 ++++++++++++------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/cli/testing/fake-editor.rs b/cli/testing/fake-editor.rs index caa3f473d..b215c8190 100644 --- a/cli/testing/fake-editor.rs +++ b/cli/testing/fake-editor.rs @@ -50,6 +50,11 @@ fn main() { let dest_path = edit_script_path.parent().unwrap().join(dest); fs::copy(&args.file, dest_path).unwrap(); } + ["dump-path", dest] => { + let dest_path = edit_script_path.parent().unwrap().join(dest); + fs::write(&dest_path, args.file.to_str().unwrap()) + .unwrap_or_else(|err| panic!("Failed to write file {dest_path:?}: {err}")); + } ["expect"] => { let actual = String::from_utf8(fs::read(&args.file).unwrap()).unwrap(); if actual != payload { @@ -58,14 +63,6 @@ fn main() { exit(1) } } - ["expectpath"] => { - let actual = args.file.to_str().unwrap(); - if actual != payload { - eprintln!("fake-editor: Unexpected path.\n"); - eprintln!("EXPECTED: <{payload}>\nRECEIVED: <{actual}>"); - exit(1) - } - } ["write"] => { fs::write(&args.file, payload).unwrap_or_else(|_| { panic!("Failed to write file {}", args.file.to_str().unwrap()) diff --git a/cli/tests/test_config_command.rs b/cli/tests/test_config_command.rs index 54f3d2acb..a6fc3cbfd 100644 --- a/cli/tests/test_config_command.rs +++ b/cli/tests/test_config_command.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::path::PathBuf; + use insta::assert_snapshot; use itertools::Itertools; use regex::Regex; @@ -621,12 +623,12 @@ fn test_config_edit_user() { let repo_path = test_env.env_root().join("repo"); let edit_script = test_env.set_up_fake_editor(); - std::fs::write( - edit_script, - format!("expectpath\n{}", test_env.config_path().to_str().unwrap()), - ) - .unwrap(); + std::fs::write(edit_script, "dump-path path").unwrap(); test_env.jj_cmd_ok(&repo_path, &["config", "edit", "--user"]); + + let edited_path = + PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path")).unwrap()); + assert_eq!(&edited_path, test_env.config_path()); } #[test] @@ -636,15 +638,12 @@ fn test_config_edit_repo() { let repo_path = test_env.env_root().join("repo"); let edit_script = test_env.set_up_fake_editor(); - std::fs::write( - edit_script, - format!( - "expectpath\n{}", - repo_path.join(".jj/repo/config.toml").to_str().unwrap() - ), - ) - .unwrap(); + std::fs::write(edit_script, "dump-path path").unwrap(); test_env.jj_cmd_ok(&repo_path, &["config", "edit", "--repo"]); + + let edited_path = + PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path")).unwrap()); + assert_eq!(edited_path, repo_path.join(".jj/repo/config.toml")); } #[test]