From 066f74a1b4464090ad9b9c94a85c39d2663c79c8 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 2 Apr 2022 22:22:33 -0700 Subject: [PATCH] tests: check set of files passed to diff-editor in `jj edit` --- testing/fake-diff-editor.rs | 39 +++++++++++++++++++++++++++++++++++++ tests/test_edit_command.rs | 6 +++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/testing/fake-diff-editor.rs b/testing/fake-diff-editor.rs index 13cd5d5c0..58d30e72b 100644 --- a/testing/fake-diff-editor.rs +++ b/testing/fake-diff-editor.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::collections::HashSet; use std::path::PathBuf; use std::process::exit; @@ -29,6 +30,22 @@ struct Args { after: PathBuf, } +fn files_recursively(dir: &PathBuf) -> HashSet { + let mut files = HashSet::new(); + for dir_entry in std::fs::read_dir(dir).unwrap() { + let dir_entry = dir_entry.unwrap(); + let base_name = dir_entry.file_name().to_str().unwrap().to_string(); + if dir_entry.path().is_dir() { + for sub_path in files_recursively(&dir_entry.path()) { + files.insert(format!("{base_name}/{sub_path}")); + } + } else { + files.insert(base_name); + } + } + files +} + fn main() { let args: Args = Args::parse(); let edit_script_path = PathBuf::from(std::env::var_os("EDIT_SCRIPT").unwrap()); @@ -39,6 +56,28 @@ fn main() { match parts.as_slice() { [""] => {} ["fail"] => exit(1), + ["files-before", ..] => { + let expected = parts[1..].iter().copied().map(str::to_string).collect(); + let actual = files_recursively(&args.before); + if actual != expected { + eprintln!( + "unexpected files before: {:?}", + actual.iter().sorted().collect_vec() + ); + exit(1) + } + } + ["files-after", ..] => { + let expected = parts[1..].iter().copied().map(str::to_string).collect(); + let actual = files_recursively(&args.after); + if actual != expected { + eprintln!( + "unexpected files after: {:?}", + actual.iter().sorted().collect_vec() + ); + exit(1) + } + } ["rm", file] => { std::fs::remove_file(args.after.join(file)).unwrap(); } diff --git a/tests/test_edit_command.rs b/tests/test_edit_command.rs index 1b649af7c..1fb871e4c 100644 --- a/tests/test_edit_command.rs +++ b/tests/test_edit_command.rs @@ -32,7 +32,11 @@ fn test_edit() { let edit_script = test_env.set_up_fake_diff_editor(); // Nothing happens if we make no changes - std::fs::write(&edit_script, "").unwrap(); + std::fs::write( + &edit_script, + "files-before file1 file2\0files-after JJ-INSTRUCTIONS file2", + ) + .unwrap(); let stdout = test_env.jj_cmd_success(&repo_path, &["edit"]); insta::assert_snapshot!(stdout, @"Nothing changed. ");