##// END OF EJS Templates
obsutil: sort metadata before comparing in geteffectflag()...
obsutil: sort metadata before comparing in geteffectflag() This is probably less important now that we dropped Python 2. We do still support Python 3.6 though, and the dictionaries aren't ordered there either (that was a big change that came with 3.7). Still, maybe it's a good idea to sort metadata explicitly.

File last commit:

r49875:b999edb1 default
r52395:a5d8f261 stable
Show More
test-merge-lists.rs
204 lines | 3.2 KiB | application/rls-services+xml | RustLexer
Martin von Zweigbergk
contrib: add a partial-merge tool for sorted lists (such as Python imports)...
r49874 use similar::DiffableStr;
Martin von Zweigbergk
merge-lists: make it possible to specify pattern to match...
r49875 use std::ffi::OsStr;
Martin von Zweigbergk
contrib: add a partial-merge tool for sorted lists (such as Python imports)...
r49874 use tempdir::TempDir;
Martin von Zweigbergk
merge-lists: make it possible to specify pattern to match...
r49875 fn run_test(arg: &str, input: &str) -> String {
Martin von Zweigbergk
contrib: add a partial-merge tool for sorted lists (such as Python imports)...
r49874 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(&[
Martin von Zweigbergk
merge-lists: make it possible to specify pattern to match...
r49875 OsStr::new(arg),
Martin von Zweigbergk
contrib: add a partial-merge tool for sorted lists (such as Python imports)...
r49874 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(
Martin von Zweigbergk
merge-lists: make it possible to specify pattern to match...
r49875 "--python-imports",
Martin von Zweigbergk
contrib: add a partial-merge tool for sorted lists (such as Python imports)...
r49874 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(
Martin von Zweigbergk
merge-lists: make it possible to specify pattern to match...
r49875 "--python-imports",
Martin von Zweigbergk
contrib: add a partial-merge tool for sorted lists (such as Python imports)...
r49874 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(
Martin von Zweigbergk
merge-lists: make it possible to specify pattern to match...
r49875 "--python-imports",
Martin von Zweigbergk
contrib: add a partial-merge tool for sorted lists (such as Python imports)...
r49874 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
"###);
}
Martin von Zweigbergk
merge-lists: make it possible to specify pattern to match...
r49875
#[test]
fn test_custom_regex() {
// Test merging of all lines (by matching anything)
let output = run_test(
"--pattern=.*",
r"
base:
aardvark
baboon
camel
local:
aardvark
camel
eagle
other:
aardvark
camel
deer
",
);
insta::assert_snapshot!(output, @r###"
base:
aardvark
camel
deer
eagle
local:
aardvark
camel
deer
eagle
other:
aardvark
camel
deer
eagle
"###);
}