##// END OF EJS Templates
merge-lists: make it possible to specify pattern to match...
Martin von Zweigbergk -
r49875:b999edb1 default
parent child Browse files
Show More
@@ -1,4 +1,4 b''
1 use clap::Parser;
1 use clap::{ArgGroup, Parser};
2 2 use itertools::Itertools;
3 3 use regex::bytes::Regex;
4 4 use similar::ChangeTag;
@@ -150,6 +150,7 b' fn resolve('
150 150 /// for partial merge tools (configured in `[partial-merge-tools]`).
151 151 #[derive(Parser, Debug)]
152 152 #[clap(version, about, long_about = None)]
153 #[clap(group(ArgGroup::new("match").required(true).args(&["pattern", "python-imports"])))]
153 154 struct Args {
154 155 /// Path to the file's content in the "local" side
155 156 local: OsString,
@@ -159,6 +160,26 b' struct Args {'
159 160
160 161 /// Path to the file's content in the "other" side
161 162 other: OsString,
163
164 /// Regular expression to use
165 #[clap(long, short)]
166 pattern: Option<String>,
167
168 /// Use built-in regular expression for Python imports
169 #[clap(long)]
170 python_imports: bool,
171 }
172
173 fn get_regex(args: &Args) -> Regex {
174 let pattern = if args.python_imports {
175 r"import \w+(\.\w+)*( +#.*)?\n|from (\w+(\.\w+)* import \w+( as \w+)?(, \w+( as \w+)?)*( +#.*)?)"
176 } else if let Some(pattern) = &args.pattern {
177 pattern
178 } else {
179 ".*"
180 };
181 let pattern = format!(r"{}\r?\n?", pattern);
182 regex::bytes::Regex::new(&pattern).unwrap()
162 183 }
163 184
164 185 fn main() {
@@ -172,8 +193,7 b' fn main() {'
172 193 let local_bytes = std::fs::read(&local_path).unwrap();
173 194 let other_bytes = std::fs::read(&other_path).unwrap();
174 195
175 let regex =
176 regex::bytes::Regex::new(r"import \w+(\.\w+)*( +#.*)?\n|from (\w+(\.\w+)* import \w+( as \w+)?(, \w+( as \w+)?)*( +#.*)?)\r?\n?").unwrap();
196 let regex = get_regex(&args);
177 197 let (new_base_bytes, new_local_bytes, new_other_bytes) =
178 198 resolve(&base_bytes, &local_bytes, &other_bytes, &regex);
179 199
@@ -1,7 +1,8 b''
1 1 use similar::DiffableStr;
2 use std::ffi::OsStr;
2 3 use tempdir::TempDir;
3 4
4 fn run_test(input: &str) -> String {
5 fn run_test(arg: &str, input: &str) -> String {
5 6 let mut cmd = assert_cmd::Command::cargo_bin("merge-lists").unwrap();
6 7 let temp_dir = TempDir::new("test").unwrap();
7 8 let base_path = temp_dir.path().join("base");
@@ -16,6 +17,7 b' fn run_test(input: &str) -> String {'
16 17 std::fs::write(&local_path, split.next().unwrap()).unwrap();
17 18 std::fs::write(&other_path, split.next().unwrap()).unwrap();
18 19 cmd.args(&[
20 OsStr::new(arg),
19 21 local_path.as_os_str(),
20 22 base_path.as_os_str(),
21 23 other_path.as_os_str(),
@@ -38,6 +40,7 b' fn run_test(input: &str) -> String {'
38 40 #[test]
39 41 fn test_merge_lists_basic() {
40 42 let output = run_test(
43 "--python-imports",
41 44 r"
42 45 base:
43 46 import lib1
@@ -72,6 +75,7 b' fn test_merge_lists_from() {'
72 75 // Test some "from x import y" statements and some non-import conflicts
73 76 // (unresolvable)
74 77 let output = run_test(
78 "--python-imports",
75 79 r"
76 80 base:
77 81 from . import x
@@ -116,6 +120,7 b' fn test_merge_lists_not_sorted() {'
116 120 // Test that nothing is done if the elements in the conflicting hunks are
117 121 // not sorted
118 122 let output = run_test(
123 "--python-imports",
119 124 r"
120 125 base:
121 126 import x
@@ -154,3 +159,46 b' 3+3'
154 159 3+3
155 160 "###);
156 161 }
162
163 #[test]
164 fn test_custom_regex() {
165 // Test merging of all lines (by matching anything)
166 let output = run_test(
167 "--pattern=.*",
168 r"
169 base:
170 aardvark
171 baboon
172 camel
173
174 local:
175 aardvark
176 camel
177 eagle
178
179 other:
180 aardvark
181 camel
182 deer
183 ",
184 );
185 insta::assert_snapshot!(output, @r###"
186 base:
187 aardvark
188 camel
189 deer
190 eagle
191
192 local:
193 aardvark
194 camel
195 deer
196 eagle
197
198 other:
199 aardvark
200 camel
201 deer
202 eagle
203 "###);
204 }
General Comments 0
You need to be logged in to leave comments. Login now