Skip to content

Commit

Permalink
fix: ensure 'package_name' always uses underscores
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ahal committed Oct 18, 2023
1 parent df56a17 commit 3daf931
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion reps/templates/python/cookiecutter.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"github_slug": "mozilla-releng/{{cookiecutter.__project_slug}}",
Expand Down
49 changes: 49 additions & 0 deletions test/test_template_python.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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 <[email protected]>",
"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

0 comments on commit 3daf931

Please sign in to comment.