|
8 | 8 | from cookiecutter.utils import rmtree |
9 | 9 |
|
10 | 10 | from click.testing import CliRunner |
| 11 | +from pytest import raises |
11 | 12 |
|
12 | 13 | import importlib |
13 | 14 |
|
@@ -296,40 +297,50 @@ def test_bake_with_argparse_console_script_files(cookies): |
296 | 297 | def test_bake_with_console_script_cli(cookies): |
297 | 298 | context = {'command_line_interface': 'click'} |
298 | 299 | result = cookies.bake(extra_context=context) |
| 300 | + |
299 | 301 | project_path, project_slug, project_dir = project_info(result) |
| 302 | + cli_file_path = result.project.join('/'.join([project_slug, "cli.py"])) |
| 303 | + assert "import click" in cli_file_path.read() |
| 304 | + |
300 | 305 | module_path = os.path.join(project_dir, 'cli.py') |
301 | 306 | module_name = '.'.join([project_slug, 'cli']) |
302 | 307 | spec = importlib.util.spec_from_file_location(module_name, module_path) |
303 | 308 | cli = importlib.util.module_from_spec(spec) |
304 | 309 | spec.loader.exec_module(cli) |
305 | 310 | runner = CliRunner() |
| 311 | + |
306 | 312 | noarg_result = runner.invoke(cli.main) |
307 | 313 | assert noarg_result.exit_code == 0 |
308 | 314 | noarg_output = ' '.join([ |
309 | 315 | 'Replace this message by putting your code into', |
310 | 316 | project_slug]) |
311 | 317 | assert noarg_output in noarg_result.output |
| 318 | + |
312 | 319 | help_result = runner.invoke(cli.main, ['--help']) |
313 | 320 | assert help_result.exit_code == 0 |
314 | 321 | assert 'Show this message' in help_result.output |
315 | 322 |
|
316 | 323 |
|
317 | | -def test_bake_with_argparse_console_script_cli(cookies): |
318 | | - context = {'command_line_interface': 'argparse'} |
| 324 | +def test_bake_with_argparse_console_script_cli(cookies, capsys): |
| 325 | + context = {'command_line_interface': 'Argparse'} |
319 | 326 | result = cookies.bake(extra_context=context) |
| 327 | + |
320 | 328 | project_path, project_slug, project_dir = project_info(result) |
| 329 | + cli_file_path = result.project.join('/'.join([project_slug, "cli.py"])) |
| 330 | + assert "import argparse" in cli_file_path.read() |
| 331 | + |
321 | 332 | module_path = os.path.join(project_dir, 'cli.py') |
322 | 333 | module_name = '.'.join([project_slug, 'cli']) |
323 | 334 | spec = importlib.util.spec_from_file_location(module_name, module_path) |
324 | 335 | cli = importlib.util.module_from_spec(spec) |
325 | 336 | spec.loader.exec_module(cli) |
326 | | - runner = CliRunner() |
327 | | - noarg_result = runner.invoke(cli.main) |
328 | | - assert noarg_result.exit_code == 0 |
| 337 | + |
| 338 | + cli.main() # type: ignore |
329 | 339 | noarg_output = ' '.join([ |
330 | 340 | 'Replace this message by putting your code into', |
331 | 341 | project_slug]) |
332 | | - assert noarg_output in noarg_result.output |
333 | | - help_result = runner.invoke(cli.main, ['--help']) |
334 | | - assert help_result.exit_code == 0 |
335 | | - assert 'Show this message' in help_result.output |
| 342 | + assert noarg_output in capsys.readouterr().out |
| 343 | + |
| 344 | + with raises(SystemExit): |
| 345 | + cli.main(["--help"]) # type: ignore |
| 346 | + assert "show this help message" in capsys.readouterr().out |
0 commit comments