##// 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 use itertools::Itertools;
2 use itertools::Itertools;
3 use regex::bytes::Regex;
3 use regex::bytes::Regex;
4 use similar::ChangeTag;
4 use similar::ChangeTag;
@@ -150,6 +150,7 b' fn resolve('
150 /// for partial merge tools (configured in `[partial-merge-tools]`).
150 /// for partial merge tools (configured in `[partial-merge-tools]`).
151 #[derive(Parser, Debug)]
151 #[derive(Parser, Debug)]
152 #[clap(version, about, long_about = None)]
152 #[clap(version, about, long_about = None)]
153 #[clap(group(ArgGroup::new("match").required(true).args(&["pattern", "python-imports"])))]
153 struct Args {
154 struct Args {
154 /// Path to the file's content in the "local" side
155 /// Path to the file's content in the "local" side
155 local: OsString,
156 local: OsString,
@@ -159,6 +160,26 b' struct Args {'
159
160
160 /// Path to the file's content in the "other" side
161 /// Path to the file's content in the "other" side
161 other: OsString,
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 fn main() {
185 fn main() {
@@ -172,8 +193,7 b' fn main() {'
172 let local_bytes = std::fs::read(&local_path).unwrap();
193 let local_bytes = std::fs::read(&local_path).unwrap();
173 let other_bytes = std::fs::read(&other_path).unwrap();
194 let other_bytes = std::fs::read(&other_path).unwrap();
174
195
175 let regex =
196 let regex = get_regex(&args);
176 regex::bytes::Regex::new(r"import \w+(\.\w+)*( +#.*)?\n|from (\w+(\.\w+)* import \w+( as \w+)?(, \w+( as \w+)?)*( +#.*)?)\r?\n?").unwrap();
177 let (new_base_bytes, new_local_bytes, new_other_bytes) =
197 let (new_base_bytes, new_local_bytes, new_other_bytes) =
178 resolve(&base_bytes, &local_bytes, &other_bytes, &regex);
198 resolve(&base_bytes, &local_bytes, &other_bytes, &regex);
179
199
@@ -1,7 +1,8 b''
1 use similar::DiffableStr;
1 use similar::DiffableStr;
2 use std::ffi::OsStr;
2 use tempdir::TempDir;
3 use tempdir::TempDir;
3
4
4 fn run_test(input: &str) -> String {
5 fn run_test(arg: &str, input: &str) -> String {
5 let mut cmd = assert_cmd::Command::cargo_bin("merge-lists").unwrap();
6 let mut cmd = assert_cmd::Command::cargo_bin("merge-lists").unwrap();
6 let temp_dir = TempDir::new("test").unwrap();
7 let temp_dir = TempDir::new("test").unwrap();
7 let base_path = temp_dir.path().join("base");
8 let base_path = temp_dir.path().join("base");
@@ -16,6 +17,7 b' fn run_test(input: &str) -> String {'
16 std::fs::write(&local_path, split.next().unwrap()).unwrap();
17 std::fs::write(&local_path, split.next().unwrap()).unwrap();
17 std::fs::write(&other_path, split.next().unwrap()).unwrap();
18 std::fs::write(&other_path, split.next().unwrap()).unwrap();
18 cmd.args(&[
19 cmd.args(&[
20 OsStr::new(arg),
19 local_path.as_os_str(),
21 local_path.as_os_str(),
20 base_path.as_os_str(),
22 base_path.as_os_str(),
21 other_path.as_os_str(),
23 other_path.as_os_str(),
@@ -38,6 +40,7 b' fn run_test(input: &str) -> String {'
38 #[test]
40 #[test]
39 fn test_merge_lists_basic() {
41 fn test_merge_lists_basic() {
40 let output = run_test(
42 let output = run_test(
43 "--python-imports",
41 r"
44 r"
42 base:
45 base:
43 import lib1
46 import lib1
@@ -72,6 +75,7 b' fn test_merge_lists_from() {'
72 // Test some "from x import y" statements and some non-import conflicts
75 // Test some "from x import y" statements and some non-import conflicts
73 // (unresolvable)
76 // (unresolvable)
74 let output = run_test(
77 let output = run_test(
78 "--python-imports",
75 r"
79 r"
76 base:
80 base:
77 from . import x
81 from . import x
@@ -116,6 +120,7 b' fn test_merge_lists_not_sorted() {'
116 // Test that nothing is done if the elements in the conflicting hunks are
120 // Test that nothing is done if the elements in the conflicting hunks are
117 // not sorted
121 // not sorted
118 let output = run_test(
122 let output = run_test(
123 "--python-imports",
119 r"
124 r"
120 base:
125 base:
121 import x
126 import x
@@ -154,3 +159,46 b' 3+3'
154 3+3
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