From 2103cf2f9d5f60117ed5b47c7a166ee544015717 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Wed, 5 Jun 2019 19:00:43 +0100 Subject: [PATCH] Add a pre-generation hook to avoid non-lowercase slugs - fixes #2042 --- hooks/pre_gen_project.py | 4 ++++ tests/test_cookiecutter_generation.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/hooks/pre_gen_project.py b/hooks/pre_gen_project.py index e7bc1e39..91332f14 100644 --- a/hooks/pre_gen_project.py +++ b/hooks/pre_gen_project.py @@ -22,6 +22,10 @@ if hasattr(project_slug, "isidentifier"): project_slug.isidentifier() ), "'{}' project slug is not a valid Python identifier.".format(project_slug) +assert ( + project_slug == project_slug.lower() +), "'{}' project slug should be all lowercase".format(project_slug) + assert ( "\\" not in "{{ cookiecutter.author_name }}" ), "Don't include backslashes in author name." diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index 4292f989..77f71df5 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -2,6 +2,7 @@ import os import re import pytest +from cookiecutter.exceptions import FailedHookException from pytest_cases import pytest_fixture_plus import sh import yaml @@ -145,3 +146,14 @@ def test_travis_invokes_pytest(cookies, context): assert yaml.load(travis_yml)["script"] == ["pytest"] except yaml.YAMLError as e: pytest.fail(e) + + +@pytest.mark.parametrize("slug", ["project slug", "Project_Slug"]) +def test_invalid_slug(cookies, context, slug): + """Invalid slug should failed pre-generation hook.""" + context.update({"project_slug": slug}) + + result = cookies.bake(extra_context=context) + + assert result.exit_code != 0 + assert isinstance(result.exception, FailedHookException)