Debian Codegraph @

contrib/merge-lists/tests/test-merge-lists.rs

raw
1 use std::ffi::OsStr;
2
3 use similar::DiffableStr;
4 use tempdir::TempDir;
5
6 fn run_test(arg: &str, input: &str) -> String {
7 let mut cmd = assert_cmd::Command::cargo_bin("merge-lists").unwrap();
8 let temp_dir = TempDir::new("test").unwrap();
9 let base_path = temp_dir.path().join("base");
10 let local_path = temp_dir.path().join("local");
11 let other_path = temp_dir.path().join("other");
12
13 let rest = input.strip_prefix("\nbase:\n").unwrap();
14 let mut split = rest.split("\nlocal:\n");
15 std::fs::write(&base_path, split.next().unwrap()).unwrap();
16 let rest = split.next().unwrap();
17 let mut split = rest.split("\nother:\n");
18 std::fs::write(&local_path, split.next().unwrap()).unwrap();
19 std::fs::write(&other_path, split.next().unwrap()).unwrap();
20 cmd.args(&[
21 OsStr::new(arg),
22 local_path.as_os_str(),
23 base_path.as_os_str(),
24 other_path.as_os_str(),
25 ])
26 .assert()
27 .success();
28
29 let new_base_bytes = std::fs::read(&base_path).unwrap();
30 let new_local_bytes = std::fs::read(&local_path).unwrap();
31 let new_other_bytes = std::fs::read(&other_path).unwrap();
32 // No newline before "base:" because of https://github.com/mitsuhiko/insta/issues/117
33 format!(
34 "base:\n{}\nlocal:\n{}\nother:\n{}",
35 new_base_bytes.as_str().unwrap(),
36 new_local_bytes.as_str().unwrap(),
37 new_other_bytes.as_str().unwrap()
38 )
39 }
40
41 #[test]
42 fn test_merge_lists_basic() {
43 let output = run_test(
44 "--python-imports",
45 r"
46 base:
47 import lib1
48 import lib2
49
50 local:
51 import lib2
52 import lib3
53
54 other:
55 import lib3
56 import lib4
57 ",
58 );
59 insta::assert_snapshot!(output, @r###"
60 base:
61 import lib3
62 import lib4
63
64 local:
65 import lib3
66 import lib4
67
68 other:
69 import lib3
70 import lib4
71 "###);
72 }
73
74 #[test]
75 fn test_merge_lists_from() {
76 // Test some "from x import y" statements and some non-import conflicts
77 // (unresolvable)
78 let output = run_test(
79 "--python-imports",
80 r"
81 base:
82 from . import x
83
84 1+1
85
86 local:
87 from . import x
88 from a import b
89
90 2+2
91
92 other:
93 from a import c
94
95 3+3
96 ",
97 );
98 insta::assert_snapshot!(output, @r###"
99 base:
100 from a import b
101 from a import c
102
103 1+1
104
105 local:
106 from a import b
107 from a import c
108
109 2+2
110
111 other:
112 from a import b
113 from a import c
114
115 3+3
116 "###);
117 }
118
119 #[test]
120 fn test_merge_lists_not_sorted() {
121 // Test that nothing is done if the elements in the conflicting hunks are
122 // not sorted
123 let output = run_test(
124 "--python-imports",
125 r"
126 base:
127 import x
128
129 1+1
130
131 local:
132 import a
133 import x
134
135 2+2
136
137 other:
138 import z
139 import y
140
141 3+3
142 ",
143 );
144 insta::assert_snapshot!(output, @r###"
145 base:
146 import x
147
148 1+1
149
150 local:
151 import a
152 import x
153
154 2+2
155
156 other:
157 import z
158 import y
159
160 3+3
161 "###);
162 }
163
164 #[test]
165 fn test_custom_regex() {
166 // Test merging of all lines (by matching anything)
167 let output = run_test(
168 "--pattern=.*",
169 r"
170 base:
171 aardvark
172 baboon
173 camel
174
175 local:
176 aardvark
177 camel
178 eagle
179
180 other:
181 aardvark
182 camel
183 deer
184 ",
185 );
186 insta::assert_snapshot!(output, @r###"
187 base:
188 aardvark
189 camel
190 deer
191 eagle
192
193 local:
194 aardvark
195 camel
196 deer
197 eagle
198
199 other:
200 aardvark
201 camel
202 deer
203 eagle
204 "###);
205 }