Skip to content

Commit 9ebdd4a

Browse files
committed
Fixed argparse type and added cli assertions
Fixes issue #588. There was a typo in the unit test. `pytest-cookies` is case sensitive so using `argparse` would actually make use of the click library in the generated template.
1 parent 731165b commit 9ebdd4a

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

tests/test_bake_project.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from cookiecutter.utils import rmtree
99

1010
from click.testing import CliRunner
11+
from pytest import raises
1112

1213
import importlib
1314

@@ -296,40 +297,50 @@ def test_bake_with_argparse_console_script_files(cookies):
296297
def test_bake_with_console_script_cli(cookies):
297298
context = {'command_line_interface': 'click'}
298299
result = cookies.bake(extra_context=context)
300+
299301
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+
300305
module_path = os.path.join(project_dir, 'cli.py')
301306
module_name = '.'.join([project_slug, 'cli'])
302307
spec = importlib.util.spec_from_file_location(module_name, module_path)
303308
cli = importlib.util.module_from_spec(spec)
304309
spec.loader.exec_module(cli)
305310
runner = CliRunner()
311+
306312
noarg_result = runner.invoke(cli.main)
307313
assert noarg_result.exit_code == 0
308314
noarg_output = ' '.join([
309315
'Replace this message by putting your code into',
310316
project_slug])
311317
assert noarg_output in noarg_result.output
318+
312319
help_result = runner.invoke(cli.main, ['--help'])
313320
assert help_result.exit_code == 0
314321
assert 'Show this message' in help_result.output
315322

316323

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'}
319326
result = cookies.bake(extra_context=context)
327+
320328
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+
321332
module_path = os.path.join(project_dir, 'cli.py')
322333
module_name = '.'.join([project_slug, 'cli'])
323334
spec = importlib.util.spec_from_file_location(module_name, module_path)
324335
cli = importlib.util.module_from_spec(spec)
325336
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
329339
noarg_output = ' '.join([
330340
'Replace this message by putting your code into',
331341
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

{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ def main(args=None):
1818
return 0
1919
{%- endif %}
2020
{%- if cookiecutter.command_line_interface|lower == 'argparse' %}
21-
def main():
21+
def main(args=None):
2222
"""Console script for {{cookiecutter.project_slug}}."""
2323
parser = argparse.ArgumentParser()
2424
parser.add_argument('_', nargs='*')
25-
args = parser.parse_args()
25+
args = parser.parse_args(args=args)
2626

2727
print("Arguments: " + str(args._))
2828
print("Replace this message by putting your code into "

0 commit comments

Comments
 (0)