Skip to content

Commit a3b5db2

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 7696a30 commit a3b5db2

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

tests/test_bake_project.py

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

1111
from click.testing import CliRunner
12+
from pytest import raises
1213

1314
import importlib
1415

@@ -269,7 +270,7 @@ def test_bake_with_no_console_script(cookies):
269270

270271

271272
def test_bake_with_console_script_files(cookies):
272-
context = {'command_line_interface': 'click'}
273+
context = {'command_line_interface': 'Click'}
273274
result = cookies.bake(extra_context=context)
274275
project_path, project_slug, project_dir = project_info(result)
275276
found_project_files = os.listdir(project_dir)
@@ -281,7 +282,7 @@ def test_bake_with_console_script_files(cookies):
281282

282283

283284
def test_bake_with_argparse_console_script_files(cookies):
284-
context = {'command_line_interface': 'argparse'}
285+
context = {'command_line_interface': 'Argparse'}
285286
result = cookies.bake(extra_context=context)
286287
project_path, project_slug, project_dir = project_info(result)
287288
found_project_files = os.listdir(project_dir)
@@ -293,45 +294,55 @@ def test_bake_with_argparse_console_script_files(cookies):
293294

294295

295296
def test_bake_with_console_script_cli(cookies):
296-
context = {'command_line_interface': 'click'}
297+
context = {'command_line_interface': 'Click'}
297298
result = cookies.bake(extra_context=context)
299+
298300
project_path, project_slug, project_dir = project_info(result)
301+
cli_file_path = result.project.join('/'.join([project_slug, "cli.py"]))
302+
assert "import click" in cli_file_path.read(), "click import missing"
303+
299304
module_path = os.path.join(project_dir, 'cli.py')
300305
module_name = '.'.join([project_slug, 'cli'])
301306
spec = importlib.util.spec_from_file_location(module_name, module_path)
302307
cli = importlib.util.module_from_spec(spec)
303308
spec.loader.exec_module(cli)
304309
runner = CliRunner()
310+
305311
noarg_result = runner.invoke(cli.main)
306312
assert noarg_result.exit_code == 0
307313
noarg_output = ' '.join([
308314
'Replace this message by putting your code into',
309315
project_slug])
310316
assert noarg_output in noarg_result.output
317+
311318
help_result = runner.invoke(cli.main, ['--help'])
312319
assert help_result.exit_code == 0
313320
assert 'Show this message' in help_result.output
314321

315322

316-
def test_bake_with_argparse_console_script_cli(cookies):
317-
context = {'command_line_interface': 'argparse'}
323+
def test_bake_with_argparse_console_script_cli(cookies, capsys):
324+
context = {'command_line_interface': 'Argparse'}
318325
result = cookies.bake(extra_context=context)
326+
319327
project_path, project_slug, project_dir = project_info(result)
328+
cli_file_path = result.project.join('/'.join([project_slug, "cli.py"]))
329+
assert "import argparse" in cli_file_path.read(), "argparse import missing"
330+
320331
module_path = os.path.join(project_dir, 'cli.py')
321332
module_name = '.'.join([project_slug, 'cli'])
322333
spec = importlib.util.spec_from_file_location(module_name, module_path)
323334
cli = importlib.util.module_from_spec(spec)
324335
spec.loader.exec_module(cli)
325-
runner = CliRunner()
326-
noarg_result = runner.invoke(cli.main)
327-
assert noarg_result.exit_code == 0
336+
337+
cli.main() # type: ignore
328338
noarg_output = ' '.join([
329339
'Replace this message by putting your code into',
330340
project_slug])
331-
assert noarg_output in noarg_result.output
332-
help_result = runner.invoke(cli.main, ['--help'])
333-
assert help_result.exit_code == 0
334-
assert 'Show this message' in help_result.output
341+
assert noarg_output in capsys.readouterr().out
342+
343+
with raises(SystemExit):
344+
cli.main(["--help"]) # type: ignore
345+
assert "show this help message" in capsys.readouterr().out
335346

336347

337348
@pytest.mark.parametrize("use_black,expected", [("y", True), ("n", False)])

{{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)