Mercurial > public > mercurial-scm > hg-stable
diff contrib/merge-lists/tests/test-merge-lists.rs @ 49075:681b25ea579e
contrib: add a partial-merge tool for sorted lists (such as Python imports)
This is a pretty naive tool that uses a regular expression for
matching lines. It is based on a Google-internal tool that worked in a
similar way.
For now, the regular expression is hard-coded to attempt to match
single-line Python imports. The only commit I've found in the hg core
repo where the tool helped was commit 9cd6292abfdf. I think that's
because we often use multiple imports per import statement. I think
this tool is still a decent first step (especially once the regex is
made configurable in the next patch). The merging should ideally use a
proper Python parser and do the merge at the AST (or CST?) level, but
that's significantly harder, especially if you want to preserve
comments and whitespace. It's also less generic.
Differential Revision: https://phab.mercurial-scm.org/D12380
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Fri, 04 Mar 2022 16:12:56 -0800 |
parents | |
children | b999edb15f8c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contrib/merge-lists/tests/test-merge-lists.rs Fri Mar 04 16:12:56 2022 -0800 @@ -0,0 +1,156 @@ +use similar::DiffableStr; +use tempdir::TempDir; + +fn run_test(input: &str) -> String { + let mut cmd = assert_cmd::Command::cargo_bin("merge-lists").unwrap(); + let temp_dir = TempDir::new("test").unwrap(); + let base_path = temp_dir.path().join("base"); + let local_path = temp_dir.path().join("local"); + let other_path = temp_dir.path().join("other"); + + let rest = input.strip_prefix("\nbase:\n").unwrap(); + let mut split = rest.split("\nlocal:\n"); + std::fs::write(&base_path, split.next().unwrap()).unwrap(); + let rest = split.next().unwrap(); + let mut split = rest.split("\nother:\n"); + std::fs::write(&local_path, split.next().unwrap()).unwrap(); + std::fs::write(&other_path, split.next().unwrap()).unwrap(); + cmd.args(&[ + local_path.as_os_str(), + base_path.as_os_str(), + other_path.as_os_str(), + ]) + .assert() + .success(); + + let new_base_bytes = std::fs::read(&base_path).unwrap(); + let new_local_bytes = std::fs::read(&local_path).unwrap(); + let new_other_bytes = std::fs::read(&other_path).unwrap(); + // No newline before "base:" because of https://github.com/mitsuhiko/insta/issues/117 + format!( + "base:\n{}\nlocal:\n{}\nother:\n{}", + new_base_bytes.as_str().unwrap(), + new_local_bytes.as_str().unwrap(), + new_other_bytes.as_str().unwrap() + ) +} + +#[test] +fn test_merge_lists_basic() { + let output = run_test( + r" +base: +import lib1 +import lib2 + +local: +import lib2 +import lib3 + +other: +import lib3 +import lib4 +", + ); + insta::assert_snapshot!(output, @r###" + base: + import lib3 + import lib4 + + local: + import lib3 + import lib4 + + other: + import lib3 + import lib4 + "###); +} + +#[test] +fn test_merge_lists_from() { + // Test some "from x import y" statements and some non-import conflicts + // (unresolvable) + let output = run_test( + r" +base: +from . import x + +1+1 + +local: +from . import x +from a import b + +2+2 + +other: +from a import c + +3+3 +", + ); + insta::assert_snapshot!(output, @r###" + base: + from a import b + from a import c + + 1+1 + + local: + from a import b + from a import c + + 2+2 + + other: + from a import b + from a import c + + 3+3 + "###); +} + +#[test] +fn test_merge_lists_not_sorted() { + // Test that nothing is done if the elements in the conflicting hunks are + // not sorted + let output = run_test( + r" +base: +import x + +1+1 + +local: +import a +import x + +2+2 + +other: +import z +import y + +3+3 +", + ); + insta::assert_snapshot!(output, @r###" + base: + import x + + 1+1 + + local: + import a + import x + + 2+2 + + other: + import z + import y + + 3+3 + "###); +}