From ffb394cec81b9905e59df4ce6cdc1673b6a7a69d Mon Sep 17 00:00:00 2001 From: Sam Cunliffe Date: Tue, 24 Oct 2023 16:42:00 +0100 Subject: [PATCH] Hyphens vs underscores: the bug reported in #127. (#128) Cheers @matt-graham. Good spot. --------- Co-authored-by: Patrick Roddy --- .pre-commit-config.yaml | 2 ++ cookiecutter.json | 1 + tests/conftest.py | 4 +++- tests/test_package_gen.py | 17 ++++++++++------- {{cookiecutter.project_slug}}/pyproject.toml | 2 +- .../__init__.py} | 0 6 files changed, 17 insertions(+), 9 deletions(-) rename {{cookiecutter.project_slug}}/src/{{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}.py => {{cookiecutter.package_name}}/__init__.py} (100%) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 73743f9f..74ac3c16 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,6 +7,7 @@ repos: hooks: - id: ruff args: ["--config", "pyproject.toml"] + exclude: "{{cookiecutter.project_slug}}/src/{{cookiecutter.package_name}}" - repo: https://github.com/codespell-project/codespell rev: v2.2.6 hooks: @@ -27,6 +28,7 @@ repos: - id: mypy args: [--config-file, pyproject.toml] additional_dependencies: ["pytest"] + exclude: "{{cookiecutter.project_slug}}/src/{{cookiecutter.package_name}}" - repo: https://github.com/pre-commit/mirrors-prettier rev: v3.0.3 hooks: diff --git a/cookiecutter.json b/cookiecutter.json index 6bf2b668..a3f25cd5 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -4,6 +4,7 @@ "github_username": "{{ cookiecutter.author_name.replace(' ', '-') }}", "project_name": "Python Template", "project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '-').replace('_', '-') }}", + "package_name": "{{ cookiecutter.project_slug.replace('-', '_') }}", "project_short_description": "A cookiecutter template with ARC recommendations.", "funder": "JBFC: The Joe Bloggs Funding Council", "license": ["MIT", "BSD-3", "GPL-3.0"], diff --git a/tests/conftest.py b/tests/conftest.py index 10c287e5..8d4ea660 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,5 +15,7 @@ def project_config(): as defined in the cookiecutter.json """ return { - "project_slug": "cookiecutter_test", + "project_name": "Cookiecutter Test", + "expected_repo_name": "cookiecutter-test", + "expected_package_name": "cookiecutter_test", } diff --git a/tests/test_package_gen.py b/tests/test_package_gen.py index 19db582e..6738286d 100644 --- a/tests/test_package_gen.py +++ b/tests/test_package_gen.py @@ -33,31 +33,34 @@ def test_package_generation( "--no-input", "--output-dir", str(tmp_path), - f"project_slug={project_config['project_slug']}", + f"project_name={project_config['project_name']}", ], check=False, shell=False, # noqa: S603 ) - test_project_dir = tmp_path / project_config["project_slug"] - - # Check parent directory exists + # Check project directory exists + test_project_dir = tmp_path / project_config["expected_repo_name"] assert test_project_dir.exists() - # Check main files and directories inside parent directory + # Check main files and directories inside expected_files = [ "README.md", ".pre-commit-config.yaml", "LICENSE.md", "pyproject.toml", "src", - Path("src") / project_config["project_slug"], + Path("src") / project_config["expected_package_name"], + Path("src") / project_config["expected_package_name"] / "__init__.py", "tests", Path(".github"), Path(".github") / "workflows", ] for f in expected_files: - assert (test_project_dir / f).exists() + full_path = test_project_dir / f + assert ( + full_path.exists() + ), f"Expected file/folder: {full_path}, but didn't find it." # Need a .git directory in the project root subprocess.run(["git", "init", test_project_dir], check=False) # noqa: S603,S607 diff --git a/{{cookiecutter.project_slug}}/pyproject.toml b/{{cookiecutter.project_slug}}/pyproject.toml index db6efe55..991d71ad 100644 --- a/{{cookiecutter.project_slug}}/pyproject.toml +++ b/{{cookiecutter.project_slug}}/pyproject.toml @@ -130,7 +130,7 @@ pep8-naming.classmethod-decorators = [ [tool.setuptools_scm] local_scheme = "no-local-version" -write_to = "src/{{ cookiecutter.project_slug }}/_version.py" +write_to = "src/{{ cookiecutter.package_name }}/_version.py" [tool.tomlsort] all = true diff --git a/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}.py b/{{cookiecutter.project_slug}}/src/{{cookiecutter.package_name}}/__init__.py similarity index 100% rename from {{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}.py rename to {{cookiecutter.project_slug}}/src/{{cookiecutter.package_name}}/__init__.py