|
11 | 11 |
|
12 | 12 | import json |
13 | 13 | import logging |
| 14 | +import re |
14 | 15 |
|
15 | 16 | import pytest |
16 | 17 | import yaml |
|
20 | 21 |
|
21 | 22 |
|
22 | 23 | class TestCli: |
| 24 | + """Tests of the CLI.""" |
| 25 | + |
23 | 26 | @pytest.fixture |
24 | 27 | def config(self, tmp_path): |
25 | 28 | """The config as a dict.""" |
@@ -179,50 +182,86 @@ def test_diff_tree( |
179 | 182 | ) |
180 | 183 | assert (tmp_path / "diff-pdf" / "file.pdf" / "diff-1.png").exists() |
181 | 184 |
|
| 185 | + # Test with files and formatted files |
| 186 | + caplog.set_level(logging.DEBUG, logger="dir-content-diff") |
| 187 | + caplog.clear() |
| 188 | + ref_file = ref_tree / "file.json" |
| 189 | + res_file = res_tree_diff / "file.json" |
| 190 | + result = cli_runner.invoke( |
| 191 | + dir_content_diff.cli.main, |
| 192 | + [str(ref_file), str(res_file), "--config", config_file_str, "-f"], |
| 193 | + catch_exceptions=False, |
| 194 | + ) |
| 195 | + assert result.stdout == "" |
| 196 | + nb_format_msg = 0 |
| 197 | + for i in caplog.messages: |
| 198 | + if not i.startswith("Format:"): |
| 199 | + continue |
| 200 | + match = re.match(r"Format: \S+ into \S+", i) |
| 201 | + assert match is not None |
| 202 | + nb_format_msg += 1 |
| 203 | + assert nb_format_msg == 2 |
| 204 | + assert (tmp_path / "ref" / "file_FORMATTED.json").exists() |
| 205 | + assert (tmp_path / "res" / "file_FORMATTED.json").exists() |
| 206 | + |
182 | 207 | class TestFailures: |
183 | | - def test_dir_file(self, cli_runner, caplog, ref_tree, res_tree_diff): |
| 208 | + """Test that the proper exceptions are raised.""" |
| 209 | + |
| 210 | + def test_dir_file(self, cli_runner, ref_tree, res_tree_diff): |
184 | 211 | """Test exception when comparing a directory with a file.""" |
185 | 212 | ref_file = ref_tree / "file.pdf" |
186 | 213 | res_file = res_tree_diff / "file.pdf" |
187 | 214 | with pytest.raises( |
188 | 215 | ValueError, |
189 | | - match=r"The reference and compared inputs must both be either two directories or two files\.", |
| 216 | + match=( |
| 217 | + r"The reference and compared inputs must both be either two directories or two" |
| 218 | + r" files\." |
| 219 | + ), |
190 | 220 | ): |
191 | | - result = cli_runner.invoke( |
| 221 | + cli_runner.invoke( |
192 | 222 | dir_content_diff.cli.main, |
193 | 223 | [str(ref_tree), str(res_file)], |
194 | 224 | catch_exceptions=False, |
195 | 225 | ) |
196 | 226 | with pytest.raises( |
197 | 227 | ValueError, |
198 | | - match=r"The reference and compared inputs must both be either two directories or two files\.", |
| 228 | + match=( |
| 229 | + r"The reference and compared inputs must both be either two directories or two" |
| 230 | + r" files\." |
| 231 | + ), |
199 | 232 | ): |
200 | | - result = cli_runner.invoke( |
| 233 | + cli_runner.invoke( |
201 | 234 | dir_content_diff.cli.main, |
202 | 235 | [str(ref_file), str(res_tree_diff)], |
203 | 236 | catch_exceptions=False, |
204 | 237 | ) |
205 | 238 |
|
206 | | - def test_not_existing_config(self, cli_runner, caplog): |
| 239 | + def test_not_existing_config(self, cli_runner): |
207 | 240 | """Test exception when the config file does not exist.""" |
208 | 241 | with pytest.raises( |
209 | 242 | FileNotFoundError, |
210 | 243 | match=r"The file '/NOT/EXISTING/FILE' does not exist\.", |
211 | 244 | ): |
212 | | - result = cli_runner.invoke( |
| 245 | + cli_runner.invoke( |
213 | 246 | dir_content_diff.cli.main, |
214 | 247 | ["/A/FILE", "/ANOTHER/FILE", "--config", "/NOT/EXISTING/FILE"], |
215 | 248 | catch_exceptions=False, |
216 | 249 | ) |
217 | 250 |
|
218 | | - def test_bad_yaml_config(self, tmp_path, cli_runner, caplog): |
| 251 | + def test_bad_yaml_config(self, tmp_path, cli_runner): |
219 | 252 | """Test exception when the config file does not exist.""" |
220 | 253 | filepath = tmp_path / "config_file.yaml" |
221 | 254 | with filepath.open("w", encoding="utf-8") as f: |
222 | 255 | f.write("entry: &A !!!") |
223 | 256 |
|
224 | | - with pytest.raises(yaml.constructor.ConstructorError): |
225 | | - result = cli_runner.invoke( |
| 257 | + with pytest.raises( |
| 258 | + SyntaxError, |
| 259 | + match=( |
| 260 | + r"Could not load the configuration because it could not be parsed as a JSON " |
| 261 | + r"string nor as a YAML file\." |
| 262 | + ), |
| 263 | + ): |
| 264 | + cli_runner.invoke( |
226 | 265 | dir_content_diff.cli.main, |
227 | 266 | ["/A/FILE", "/ANOTHER/FILE", "--config", str(filepath)], |
228 | 267 | catch_exceptions=False, |
|
0 commit comments