1 | """Collection of tests around cookiecutter's command-line interface.""" |
||
2 | |||
3 | import json |
||
4 | import os |
||
5 | import re |
||
6 | from pathlib import Path |
||
7 | |||
8 | import pytest |
||
9 | from click.testing import CliRunner |
||
10 | |||
11 | from cookiecutter import utils |
||
12 | from cookiecutter.__main__ import main |
||
13 | from cookiecutter.environment import StrictEnvironment |
||
14 | from cookiecutter.exceptions import UnknownExtension |
||
15 | from cookiecutter.main import cookiecutter |
||
16 | |||
17 | |||
18 | @pytest.fixture(scope='session') |
||
19 | def cli_runner(): |
||
20 | """Fixture that returns a helper function to run the cookiecutter cli.""" |
||
21 | runner = CliRunner() |
||
22 | |||
23 | def cli_main(*cli_args, **cli_kwargs): |
||
24 | """Run cookiecutter cli main with the given args.""" |
||
25 | return runner.invoke(main, cli_args, **cli_kwargs) |
||
26 | |||
27 | return cli_main |
||
28 | |||
29 | |||
30 | @pytest.fixture |
||
31 | def remove_fake_project_dir(request): |
||
32 | """Remove the fake project directory created during the tests.""" |
||
33 | |||
34 | def fin_remove_fake_project_dir(): |
||
35 | if os.path.isdir('fake-project'): |
||
36 | utils.rmtree('fake-project') |
||
37 | |||
38 | request.addfinalizer(fin_remove_fake_project_dir) |
||
39 | |||
40 | |||
41 | @pytest.fixture |
||
42 | def make_fake_project_dir(request): |
||
43 | """Create a fake project to be overwritten in the according tests.""" |
||
44 | os.makedirs('fake-project') |
||
45 | |||
46 | |||
47 | @pytest.fixture(params=['-V', '--version']) |
||
48 | def version_cli_flag(request): |
||
49 | """Pytest fixture return both version invocation options.""" |
||
50 | return request.param |
||
51 | |||
52 | |||
53 | def test_cli_version(cli_runner, version_cli_flag): |
||
54 | """Verify Cookiecutter version output by `cookiecutter` on cli invocation.""" |
||
55 | result = cli_runner(version_cli_flag) |
||
56 | assert result.exit_code == 0 |
||
57 | assert result.output.startswith('Cookiecutter') |
||
58 | |||
59 | |||
60 | @pytest.mark.usefixtures('make_fake_project_dir', 'remove_fake_project_dir') |
||
61 | def test_cli_error_on_existing_output_directory(cli_runner): |
||
62 | """Test cli invocation without `overwrite-if-exists` fail if dir exist.""" |
||
63 | result = cli_runner('tests/fake-repo-pre/', '--no-input') |
||
64 | assert result.exit_code != 0 |
||
65 | expected_error_msg = 'Error: "fake-project" directory already exists\n' |
||
66 | assert result.output == expected_error_msg |
||
67 | |||
68 | |||
69 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
70 | def test_cli(cli_runner): |
||
71 | """Test cli invocation work without flags if directory not exist.""" |
||
72 | result = cli_runner('tests/fake-repo-pre/', '--no-input') |
||
73 | assert result.exit_code == 0 |
||
74 | assert os.path.isdir('fake-project') |
||
75 | content = Path("fake-project", "README.rst").read_text() |
||
76 | assert 'Project name: **Fake Project**' in content |
||
77 | |||
78 | |||
79 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
80 | def test_cli_verbose(cli_runner): |
||
81 | """Test cli invocation display log if called with `verbose` flag.""" |
||
82 | result = cli_runner('tests/fake-repo-pre/', '--no-input', '-v') |
||
83 | assert result.exit_code == 0 |
||
84 | assert os.path.isdir('fake-project') |
||
85 | content = Path("fake-project", "README.rst").read_text() |
||
86 | assert 'Project name: **Fake Project**' in content |
||
87 | |||
88 | |||
89 | View Code Duplication | @pytest.mark.usefixtures('remove_fake_project_dir') |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
90 | def test_cli_replay(mocker, cli_runner): |
||
91 | """Test cli invocation display log with `verbose` and `replay` flags.""" |
||
92 | mock_cookiecutter = mocker.patch('cookiecutter.cli.cookiecutter') |
||
93 | |||
94 | template_path = 'tests/fake-repo-pre/' |
||
95 | result = cli_runner(template_path, '--replay', '-v') |
||
96 | |||
97 | assert result.exit_code == 0 |
||
98 | mock_cookiecutter.assert_called_once_with( |
||
99 | template_path, |
||
100 | None, |
||
101 | False, |
||
102 | replay=True, |
||
103 | overwrite_if_exists=False, |
||
104 | skip_if_file_exists=False, |
||
105 | output_dir='.', |
||
106 | config_file=None, |
||
107 | default_config=False, |
||
108 | extra_context=None, |
||
109 | password=None, |
||
110 | directory=None, |
||
111 | accept_hooks=True, |
||
112 | keep_project_on_failure=False, |
||
113 | ) |
||
114 | |||
115 | |||
116 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
117 | def test_cli_replay_file(mocker, cli_runner): |
||
118 | """Test cli invocation correctly pass --replay-file option.""" |
||
119 | mock_cookiecutter = mocker.patch('cookiecutter.cli.cookiecutter') |
||
120 | |||
121 | template_path = 'tests/fake-repo-pre/' |
||
122 | result = cli_runner(template_path, '--replay-file', '~/custom-replay-file', '-v') |
||
123 | |||
124 | assert result.exit_code == 0 |
||
125 | mock_cookiecutter.assert_called_once_with( |
||
126 | template_path, |
||
127 | None, |
||
128 | False, |
||
129 | replay='~/custom-replay-file', |
||
130 | overwrite_if_exists=False, |
||
131 | skip_if_file_exists=False, |
||
132 | output_dir='.', |
||
133 | config_file=None, |
||
134 | default_config=False, |
||
135 | extra_context=None, |
||
136 | password=None, |
||
137 | directory=None, |
||
138 | accept_hooks=True, |
||
139 | keep_project_on_failure=False, |
||
140 | ) |
||
141 | |||
142 | |||
143 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
144 | def test_cli_exit_on_noinput_and_replay(mocker, cli_runner): |
||
145 | """Test cli invocation fail if both `no-input` and `replay` flags passed.""" |
||
146 | mock_cookiecutter = mocker.patch( |
||
147 | 'cookiecutter.cli.cookiecutter', side_effect=cookiecutter |
||
148 | ) |
||
149 | |||
150 | template_path = 'tests/fake-repo-pre/' |
||
151 | result = cli_runner(template_path, '--no-input', '--replay', '-v') |
||
152 | |||
153 | assert result.exit_code == 1 |
||
154 | |||
155 | expected_error_msg = ( |
||
156 | "You can not use both replay and no_input or extra_context at the same time." |
||
157 | ) |
||
158 | |||
159 | assert expected_error_msg in result.output |
||
160 | |||
161 | mock_cookiecutter.assert_called_once_with( |
||
162 | template_path, |
||
163 | None, |
||
164 | True, |
||
165 | replay=True, |
||
166 | overwrite_if_exists=False, |
||
167 | skip_if_file_exists=False, |
||
168 | output_dir='.', |
||
169 | config_file=None, |
||
170 | default_config=False, |
||
171 | extra_context=None, |
||
172 | password=None, |
||
173 | directory=None, |
||
174 | accept_hooks=True, |
||
175 | keep_project_on_failure=False, |
||
176 | ) |
||
177 | |||
178 | |||
179 | @pytest.fixture(params=['-f', '--overwrite-if-exists']) |
||
180 | def overwrite_cli_flag(request): |
||
181 | """Pytest fixture return all `overwrite-if-exists` invocation options.""" |
||
182 | return request.param |
||
183 | |||
184 | |||
185 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
186 | def test_run_cookiecutter_on_overwrite_if_exists_and_replay( |
||
187 | mocker, cli_runner, overwrite_cli_flag |
||
188 | ): |
||
189 | """Test cli invocation with `overwrite-if-exists` and `replay` flags.""" |
||
190 | mock_cookiecutter = mocker.patch('cookiecutter.cli.cookiecutter') |
||
191 | |||
192 | template_path = 'tests/fake-repo-pre/' |
||
193 | result = cli_runner(template_path, '--replay', '-v', overwrite_cli_flag) |
||
194 | |||
195 | assert result.exit_code == 0 |
||
196 | |||
197 | mock_cookiecutter.assert_called_once_with( |
||
198 | template_path, |
||
199 | None, |
||
200 | False, |
||
201 | replay=True, |
||
202 | overwrite_if_exists=True, |
||
203 | skip_if_file_exists=False, |
||
204 | output_dir='.', |
||
205 | config_file=None, |
||
206 | default_config=False, |
||
207 | extra_context=None, |
||
208 | password=None, |
||
209 | directory=None, |
||
210 | accept_hooks=True, |
||
211 | keep_project_on_failure=False, |
||
212 | ) |
||
213 | |||
214 | |||
215 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
216 | def test_cli_overwrite_if_exists_when_output_dir_does_not_exist( |
||
217 | cli_runner, overwrite_cli_flag |
||
218 | ): |
||
219 | """Test cli invocation with `overwrite-if-exists` and `no-input` flags. |
||
220 | |||
221 | Case when output dir not exist. |
||
222 | """ |
||
223 | result = cli_runner('tests/fake-repo-pre/', '--no-input', overwrite_cli_flag) |
||
224 | |||
225 | assert result.exit_code == 0 |
||
226 | assert os.path.isdir('fake-project') |
||
227 | |||
228 | |||
229 | @pytest.mark.usefixtures('make_fake_project_dir', 'remove_fake_project_dir') |
||
230 | def test_cli_overwrite_if_exists_when_output_dir_exists(cli_runner, overwrite_cli_flag): |
||
231 | """Test cli invocation with `overwrite-if-exists` and `no-input` flags. |
||
232 | |||
233 | Case when output dir already exist. |
||
234 | """ |
||
235 | result = cli_runner('tests/fake-repo-pre/', '--no-input', overwrite_cli_flag) |
||
236 | assert result.exit_code == 0 |
||
237 | assert os.path.isdir('fake-project') |
||
238 | |||
239 | |||
240 | @pytest.fixture(params=['-o', '--output-dir']) |
||
241 | def output_dir_flag(request): |
||
242 | """Pytest fixture return all output-dir invocation options.""" |
||
243 | return request.param |
||
244 | |||
245 | |||
246 | View Code Duplication | def test_cli_output_dir(mocker, cli_runner, output_dir_flag, output_dir): |
|
0 ignored issues
–
show
|
|||
247 | """Test cli invocation with `output-dir` flag changes output directory.""" |
||
248 | mock_cookiecutter = mocker.patch('cookiecutter.cli.cookiecutter') |
||
249 | |||
250 | template_path = 'tests/fake-repo-pre/' |
||
251 | result = cli_runner(template_path, output_dir_flag, output_dir) |
||
252 | |||
253 | assert result.exit_code == 0 |
||
254 | mock_cookiecutter.assert_called_once_with( |
||
255 | template_path, |
||
256 | None, |
||
257 | False, |
||
258 | replay=False, |
||
259 | overwrite_if_exists=False, |
||
260 | skip_if_file_exists=False, |
||
261 | output_dir=output_dir, |
||
262 | config_file=None, |
||
263 | default_config=False, |
||
264 | extra_context=None, |
||
265 | password=None, |
||
266 | directory=None, |
||
267 | accept_hooks=True, |
||
268 | keep_project_on_failure=False, |
||
269 | ) |
||
270 | |||
271 | |||
272 | @pytest.fixture(params=['-h', '--help', 'help']) |
||
273 | def help_cli_flag(request): |
||
274 | """Pytest fixture return all help invocation options.""" |
||
275 | return request.param |
||
276 | |||
277 | |||
278 | def test_cli_help(cli_runner, help_cli_flag): |
||
279 | """Test cli invocation display help message with `help` flag.""" |
||
280 | result = cli_runner(help_cli_flag) |
||
281 | assert result.exit_code == 0 |
||
282 | assert result.output.startswith('Usage') |
||
283 | |||
284 | |||
285 | @pytest.fixture |
||
286 | def user_config_path(tmp_path): |
||
287 | """Pytest fixture return `user_config` argument as string.""" |
||
288 | return str(tmp_path.joinpath("tests", "config.yaml")) |
||
289 | |||
290 | |||
291 | View Code Duplication | def test_user_config(mocker, cli_runner, user_config_path): |
|
0 ignored issues
–
show
|
|||
292 | """Test cli invocation works with `config-file` option.""" |
||
293 | mock_cookiecutter = mocker.patch('cookiecutter.cli.cookiecutter') |
||
294 | |||
295 | template_path = 'tests/fake-repo-pre/' |
||
296 | result = cli_runner(template_path, '--config-file', user_config_path) |
||
297 | |||
298 | assert result.exit_code == 0 |
||
299 | mock_cookiecutter.assert_called_once_with( |
||
300 | template_path, |
||
301 | None, |
||
302 | False, |
||
303 | replay=False, |
||
304 | overwrite_if_exists=False, |
||
305 | skip_if_file_exists=False, |
||
306 | output_dir='.', |
||
307 | config_file=user_config_path, |
||
308 | default_config=False, |
||
309 | extra_context=None, |
||
310 | password=None, |
||
311 | directory=None, |
||
312 | accept_hooks=True, |
||
313 | keep_project_on_failure=False, |
||
314 | ) |
||
315 | |||
316 | |||
317 | View Code Duplication | def test_default_user_config_overwrite(mocker, cli_runner, user_config_path): |
|
0 ignored issues
–
show
|
|||
318 | """Test cli invocation ignores `config-file` if `default-config` passed.""" |
||
319 | mock_cookiecutter = mocker.patch('cookiecutter.cli.cookiecutter') |
||
320 | |||
321 | template_path = 'tests/fake-repo-pre/' |
||
322 | result = cli_runner( |
||
323 | template_path, |
||
324 | '--config-file', |
||
325 | user_config_path, |
||
326 | '--default-config', |
||
327 | ) |
||
328 | |||
329 | assert result.exit_code == 0 |
||
330 | mock_cookiecutter.assert_called_once_with( |
||
331 | template_path, |
||
332 | None, |
||
333 | False, |
||
334 | replay=False, |
||
335 | overwrite_if_exists=False, |
||
336 | skip_if_file_exists=False, |
||
337 | output_dir='.', |
||
338 | config_file=user_config_path, |
||
339 | default_config=True, |
||
340 | extra_context=None, |
||
341 | password=None, |
||
342 | directory=None, |
||
343 | accept_hooks=True, |
||
344 | keep_project_on_failure=False, |
||
345 | ) |
||
346 | |||
347 | |||
348 | View Code Duplication | def test_default_user_config(mocker, cli_runner): |
|
0 ignored issues
–
show
|
|||
349 | """Test cli invocation accepts `default-config` flag correctly.""" |
||
350 | mock_cookiecutter = mocker.patch('cookiecutter.cli.cookiecutter') |
||
351 | |||
352 | template_path = 'tests/fake-repo-pre/' |
||
353 | result = cli_runner(template_path, '--default-config') |
||
354 | |||
355 | assert result.exit_code == 0 |
||
356 | mock_cookiecutter.assert_called_once_with( |
||
357 | template_path, |
||
358 | None, |
||
359 | False, |
||
360 | replay=False, |
||
361 | overwrite_if_exists=False, |
||
362 | skip_if_file_exists=False, |
||
363 | output_dir='.', |
||
364 | config_file=None, |
||
365 | default_config=True, |
||
366 | extra_context=None, |
||
367 | password=None, |
||
368 | directory=None, |
||
369 | accept_hooks=True, |
||
370 | keep_project_on_failure=False, |
||
371 | ) |
||
372 | |||
373 | |||
374 | def test_echo_undefined_variable_error(output_dir, cli_runner): |
||
375 | """Cli invocation return error if variable undefined in template.""" |
||
376 | template_path = 'tests/undefined-variable/file-name/' |
||
377 | |||
378 | result = cli_runner( |
||
379 | '--no-input', |
||
380 | '--default-config', |
||
381 | '--output-dir', |
||
382 | output_dir, |
||
383 | template_path, |
||
384 | ) |
||
385 | |||
386 | assert result.exit_code == 1 |
||
387 | |||
388 | error = "Unable to create file '{{cookiecutter.foobar}}'" |
||
389 | assert error in result.output |
||
390 | |||
391 | message = ( |
||
392 | "Error message: 'collections.OrderedDict object' has no attribute 'foobar'" |
||
393 | ) |
||
394 | assert message in result.output |
||
395 | |||
396 | context = { |
||
397 | 'cookiecutter': { |
||
398 | 'github_username': 'hackebrot', |
||
399 | 'project_slug': 'testproject', |
||
400 | '_template': template_path, |
||
401 | '_output_dir': output_dir, |
||
402 | } |
||
403 | } |
||
404 | context_str = json.dumps(context, indent=4, sort_keys=True) |
||
405 | assert context_str in result.output |
||
406 | |||
407 | |||
408 | def test_echo_unknown_extension_error(output_dir, cli_runner): |
||
409 | """Cli return error if extension incorrectly defined in template.""" |
||
410 | template_path = 'tests/test-extensions/unknown/' |
||
411 | |||
412 | result = cli_runner( |
||
413 | '--no-input', |
||
414 | '--default-config', |
||
415 | '--output-dir', |
||
416 | output_dir, |
||
417 | template_path, |
||
418 | ) |
||
419 | |||
420 | assert result.exit_code == 1 |
||
421 | |||
422 | assert 'Unable to load extension: ' in result.output |
||
423 | |||
424 | |||
425 | def test_local_extension(tmpdir, cli_runner): |
||
426 | """Test to verify correct work of extension, included in template.""" |
||
427 | output_dir = str(tmpdir.mkdir('output')) |
||
428 | template_path = 'tests/test-extensions/local_extension/' |
||
429 | |||
430 | result = cli_runner( |
||
431 | '--no-input', |
||
432 | '--default-config', |
||
433 | '--output-dir', |
||
434 | output_dir, |
||
435 | template_path, |
||
436 | ) |
||
437 | assert result.exit_code == 0 |
||
438 | content = Path(output_dir, 'Foobar', 'HISTORY.rst').read_text() |
||
439 | assert 'FoobarFoobar' in content |
||
440 | assert 'FOOBAR' in content |
||
441 | |||
442 | |||
443 | def test_local_extension_not_available(tmpdir, cli_runner): |
||
444 | """Test handling of included but unavailable local extension.""" |
||
445 | context = {'cookiecutter': {'_extensions': ['foobar']}} |
||
446 | |||
447 | with pytest.raises(UnknownExtension) as err: |
||
448 | StrictEnvironment(context=context, keep_trailing_newline=True) |
||
449 | |||
450 | assert 'Unable to load extension: ' in str(err.value) |
||
451 | |||
452 | |||
453 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
454 | def test_cli_extra_context(cli_runner): |
||
455 | """Cli invocation replace content if called with replacement pairs.""" |
||
456 | result = cli_runner( |
||
457 | 'tests/fake-repo-pre/', |
||
458 | '--no-input', |
||
459 | '-v', |
||
460 | 'project_name=Awesomez', |
||
461 | ) |
||
462 | assert result.exit_code == 0 |
||
463 | assert os.path.isdir('fake-project') |
||
464 | content = Path('fake-project', 'README.rst').read_text() |
||
465 | assert 'Project name: **Awesomez**' in content |
||
466 | |||
467 | |||
468 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
469 | def test_cli_extra_context_invalid_format(cli_runner): |
||
470 | """Cli invocation raise error if called with unknown argument.""" |
||
471 | result = cli_runner( |
||
472 | 'tests/fake-repo-pre/', |
||
473 | '--no-input', |
||
474 | '-v', |
||
475 | 'ExtraContextWithNoEqualsSoInvalid', |
||
476 | ) |
||
477 | assert result.exit_code == 2 |
||
478 | assert "Error: Invalid value for '[EXTRA_CONTEXT]...'" in result.output |
||
479 | assert 'should contain items of the form key=value' in result.output |
||
480 | |||
481 | |||
482 | @pytest.fixture |
||
483 | def debug_file(tmp_path): |
||
484 | """Pytest fixture return `debug_file` argument as path object.""" |
||
485 | return tmp_path.joinpath('fake-repo.log') |
||
486 | |||
487 | |||
488 | View Code Duplication | @pytest.mark.usefixtures('remove_fake_project_dir') |
|
0 ignored issues
–
show
|
|||
489 | def test_debug_file_non_verbose(cli_runner, debug_file): |
||
490 | """Test cli invocation writes log to `debug-file` if flag enabled. |
||
491 | |||
492 | Case for normal log output. |
||
493 | """ |
||
494 | assert not debug_file.exists() |
||
495 | |||
496 | result = cli_runner( |
||
497 | '--no-input', |
||
498 | '--debug-file', |
||
499 | str(debug_file), |
||
500 | 'tests/fake-repo-pre/', |
||
501 | ) |
||
502 | assert result.exit_code == 0 |
||
503 | |||
504 | assert debug_file.exists() |
||
505 | |||
506 | context_log = ( |
||
507 | "DEBUG cookiecutter.main: context_file is " |
||
508 | "tests/fake-repo-pre/cookiecutter.json" |
||
509 | ) |
||
510 | assert context_log in debug_file.read_text() |
||
511 | assert context_log not in result.output |
||
512 | |||
513 | |||
514 | View Code Duplication | @pytest.mark.usefixtures('remove_fake_project_dir') |
|
0 ignored issues
–
show
|
|||
515 | def test_debug_file_verbose(cli_runner, debug_file): |
||
516 | """Test cli invocation writes log to `debug-file` if flag enabled. |
||
517 | |||
518 | Case for verbose log output. |
||
519 | """ |
||
520 | assert not debug_file.exists() |
||
521 | |||
522 | result = cli_runner( |
||
523 | '--verbose', |
||
524 | '--no-input', |
||
525 | '--debug-file', |
||
526 | str(debug_file), |
||
527 | 'tests/fake-repo-pre/', |
||
528 | ) |
||
529 | assert result.exit_code == 0 |
||
530 | |||
531 | assert debug_file.exists() |
||
532 | |||
533 | context_log = ( |
||
534 | "DEBUG cookiecutter.main: context_file is " |
||
535 | "tests/fake-repo-pre/cookiecutter.json" |
||
536 | ) |
||
537 | assert context_log in debug_file.read_text() |
||
538 | assert context_log in result.output |
||
539 | |||
540 | |||
541 | @pytest.mark.usefixtures('make_fake_project_dir', 'remove_fake_project_dir') |
||
542 | def test_debug_list_installed_templates(cli_runner, debug_file, user_config_path): |
||
543 | """Verify --list-installed command correct invocation.""" |
||
544 | fake_template_dir = os.path.dirname(os.path.abspath('fake-project')) |
||
545 | os.makedirs(os.path.dirname(user_config_path)) |
||
546 | # Single quotes in YAML will not parse escape codes (\). |
||
547 | Path(user_config_path).write_text(f"cookiecutters_dir: '{fake_template_dir}'") |
||
548 | Path("fake-project", "cookiecutter.json").write_text('{}') |
||
549 | |||
550 | result = cli_runner( |
||
551 | '--list-installed', |
||
552 | '--config-file', |
||
553 | user_config_path, |
||
554 | str(debug_file), |
||
555 | ) |
||
556 | |||
557 | assert "1 installed templates:" in result.output |
||
558 | assert result.exit_code == 0 |
||
559 | |||
560 | |||
561 | def test_debug_list_installed_templates_failure( |
||
562 | cli_runner, debug_file, user_config_path |
||
563 | ): |
||
564 | """Verify --list-installed command error on invocation.""" |
||
565 | os.makedirs(os.path.dirname(user_config_path)) |
||
566 | Path(user_config_path).write_text('cookiecutters_dir: "/notarealplace/"') |
||
567 | |||
568 | result = cli_runner( |
||
569 | '--list-installed', '--config-file', user_config_path, str(debug_file) |
||
570 | ) |
||
571 | |||
572 | assert "Error: Cannot list installed templates." in result.output |
||
573 | assert result.exit_code == -1 |
||
574 | |||
575 | |||
576 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
577 | def test_directory_repo(cli_runner): |
||
578 | """Test cli invocation works with `directory` option.""" |
||
579 | result = cli_runner( |
||
580 | 'tests/fake-repo-dir/', |
||
581 | '--no-input', |
||
582 | '-v', |
||
583 | '--directory=my-dir', |
||
584 | ) |
||
585 | assert result.exit_code == 0 |
||
586 | assert os.path.isdir("fake-project") |
||
587 | content = Path("fake-project", "README.rst").read_text() |
||
588 | assert "Project name: **Fake Project**" in content |
||
589 | |||
590 | |||
591 | cli_accept_hook_arg_testdata = [ |
||
592 | ("--accept-hooks=yes", None, True), |
||
593 | ("--accept-hooks=no", None, False), |
||
594 | ("--accept-hooks=ask", "yes", True), |
||
595 | ("--accept-hooks=ask", "no", False), |
||
596 | ] |
||
597 | |||
598 | |||
599 | @pytest.mark.parametrize( |
||
600 | "accept_hooks_arg,user_input,expected", cli_accept_hook_arg_testdata |
||
601 | ) |
||
602 | def test_cli_accept_hooks( |
||
603 | mocker, |
||
604 | cli_runner, |
||
605 | output_dir_flag, |
||
606 | output_dir, |
||
607 | accept_hooks_arg, |
||
608 | user_input, |
||
609 | expected, |
||
610 | ): |
||
611 | """Test cli invocation works with `accept-hooks` option.""" |
||
612 | mock_cookiecutter = mocker.patch("cookiecutter.cli.cookiecutter") |
||
613 | |||
614 | template_path = "tests/fake-repo-pre/" |
||
615 | result = cli_runner( |
||
616 | template_path, output_dir_flag, output_dir, accept_hooks_arg, input=user_input |
||
617 | ) |
||
618 | |||
619 | assert result.exit_code == 0 |
||
620 | mock_cookiecutter.assert_called_once_with( |
||
621 | template_path, |
||
622 | None, |
||
623 | False, |
||
624 | replay=False, |
||
625 | overwrite_if_exists=False, |
||
626 | output_dir=output_dir, |
||
627 | config_file=None, |
||
628 | default_config=False, |
||
629 | extra_context=None, |
||
630 | password=None, |
||
631 | directory=None, |
||
632 | skip_if_file_exists=False, |
||
633 | accept_hooks=expected, |
||
634 | keep_project_on_failure=False, |
||
635 | ) |
||
636 | |||
637 | |||
638 | @pytest.mark.usefixtures('remove_fake_project_dir') |
||
639 | def test_cli_with_json_decoding_error(cli_runner): |
||
640 | """Test cli invocation with a malformed JSON file.""" |
||
641 | template_path = 'tests/fake-repo-bad-json/' |
||
642 | result = cli_runner(template_path, '--no-input') |
||
643 | assert result.exit_code != 0 |
||
644 | |||
645 | # Validate the error message. |
||
646 | # original message from json module should be included |
||
647 | pattern = 'Expecting \'{0,1}:\'{0,1} delimiter: line 1 column (19|20) \\(char 19\\)' |
||
648 | assert re.search(pattern, result.output) |
||
649 | # File name should be included too...for testing purposes, just test the |
||
650 | # last part of the file. If we wanted to test the absolute path, we'd have |
||
651 | # to do some additional work in the test which doesn't seem that needed at |
||
652 | # this point. |
||
653 | path = os.path.sep.join(['tests', 'fake-repo-bad-json', 'cookiecutter.json']) |
||
654 | assert path in result.output |
||
655 |