From 3daf9312816d108b23d655d55746247d497d1df7 Mon Sep 17 00:00:00 2001 From: Andrew Halberstadt Date: Tue, 17 Oct 2023 16:31:40 -0400 Subject: [PATCH] fix: ensure 'package_name' always uses underscores Without this fix, entering 'my-package' as the project name results in a directory called 'src/my-package', which is not importable in Python. Also adds a test. --- reps/templates/python/cookiecutter.json | 2 +- test/test_template_python.py | 49 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/reps/templates/python/cookiecutter.json b/reps/templates/python/cookiecutter.json index 256c1be..db0a5f5 100644 --- a/reps/templates/python/cookiecutter.json +++ b/reps/templates/python/cookiecutter.json @@ -1,7 +1,7 @@ { "project_name": "My Project", "__project_slug": "{{cookiecutter.project_name|lower|replace(' ', '-')}}", - "__package_name": "{{cookiecutter.project_name|lower|replace(' ', '_')}}", + "__package_name": "{{cookiecutter.project_name|lower|replace(' ', '_')|replace('-', '_')}}", "short_description": "", "author": "Mozilla Release Engineering ", "github_slug": "mozilla-releng/{{cookiecutter.__project_slug}}", diff --git a/test/test_template_python.py b/test/test_template_python.py index 688c511..1fe1549 100644 --- a/test/test_template_python.py +++ b/test/test_template_python.py @@ -1,3 +1,10 @@ +from pprint import pprint + +import pytest +from cookiecutter.generate import generate_context +from cookiecutter.prompt import prompt_for_config + + def test_generated_files(reps_new): name = "foo" expected = [ @@ -48,3 +55,45 @@ def test_generated_files(reps_new): actual.append(str(path.relative_to(project))) assert sorted(actual) == sorted(expected) + + +@pytest.mark.parametrize( + "extra_context,expected", + ( + pytest.param( + {"project_name": "My Package"}, + { + "__project_slug": "my-package", + "__package_name": "my_package", + "short_description": "", + "author": "Mozilla Release Engineering ", + "github_slug": "mozilla-releng/my-package", + "min_python_version": "3.7", + "__min_tox_python_version": "37", + "__max_tox_python_version": "311", + "trust_domain": "mozilla", + "trust_project": "my-package", + "level": "1", + "__codecov_secrets_path": "project/mozilla/my-package/level-any/codecov", # noqa + "_copy_without_render": [".github/workflows/codeql-analysis.yml"], + }, + id="defaults", + ), + pytest.param( + {"project_name": "foo-bar"}, + {"__package_name": "foo_bar"}, + id="package_name_normalized", + ), + ), +) +def test_cookiecutter_json(project_root, extra_context, expected): + cookiecutter_json = ( + project_root / "reps" / "templates" / "python" / "cookiecutter.json" + ) + context = generate_context(cookiecutter_json, extra_context=extra_context) + config = prompt_for_config(context, no_input=True) + pprint(config, indent=2) + + for key, val in expected.items(): + assert key in config + assert config[key] == val