From 683f207d4a71ae3505b015fe4972a5a58ba0a509 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Tue, 31 Dec 2024 13:18:39 +0000 Subject: [PATCH 1/4] Move API tests into a package Mirroring the structure of the code being tested --- hooks/post_gen_project.py | 4 +--- .../{{cookiecutter.project_slug}}/users/tests/api/__init__.py | 0 .../users/tests/{ => api}/test_swagger.py | 0 .../users/tests/{test_drf_urls.py => api/test_urls.py} | 0 .../users/tests/{test_drf_views.py => api/test_views.py} | 0 5 files changed, 1 insertion(+), 3 deletions(-) create mode 100644 {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/__init__.py rename {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/{ => api}/test_swagger.py (100%) rename {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/{test_drf_urls.py => api/test_urls.py} (100%) rename {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/{test_drf_views.py => api/test_views.py} (100%) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index ca1ffad32..c4934e637 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -433,9 +433,7 @@ def remove_aws_dockerfile(): def remove_drf_starter_files(): os.remove(os.path.join("config", "api_router.py")) shutil.rmtree(os.path.join("{{cookiecutter.project_slug}}", "users", "api")) - os.remove(os.path.join("{{cookiecutter.project_slug}}", "users", "tests", "test_drf_urls.py")) - os.remove(os.path.join("{{cookiecutter.project_slug}}", "users", "tests", "test_drf_views.py")) - os.remove(os.path.join("{{cookiecutter.project_slug}}", "users", "tests", "test_swagger.py")) + shutil.rmtree(os.path.join("{{cookiecutter.project_slug}}", "users", "tests", "api")) def main(): diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/__init__.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_swagger.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_swagger.py similarity index 100% rename from {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_swagger.py rename to {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_swagger.py diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_urls.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_urls.py similarity index 100% rename from {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_urls.py rename to {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_urls.py diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_views.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_views.py similarity index 100% rename from {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/test_drf_views.py rename to {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_views.py From 0434aea33f0edf54c1794d5e6e70388d7438741b Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Thu, 2 Jan 2025 20:20:08 +0000 Subject: [PATCH 2/4] Rename API docs tests to not mention Swagger --- .../users/tests/api/{test_swagger.py => test_openapi.py} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/{test_swagger.py => test_openapi.py} (81%) diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_swagger.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_openapi.py similarity index 81% rename from {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_swagger.py rename to {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_openapi.py index 5db371dbe..cb3f19d3d 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_swagger.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/tests/api/test_openapi.py @@ -4,14 +4,14 @@ import pytest from django.urls import reverse -def test_swagger_accessible_by_admin(admin_client): +def test_api_docs_accessible_by_admin(admin_client): url = reverse("api-docs") response = admin_client.get(url) assert response.status_code == HTTPStatus.OK @pytest.mark.django_db -def test_swagger_ui_not_accessible_by_normal_user(client): +def test_api_docs_not_accessible_by_anonymous_users(client): url = reverse("api-docs") response = client.get(url) assert response.status_code == HTTPStatus.FORBIDDEN From 2193a661c5a57805a35a2ef03df73f5d5c0abc9a Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 17 Jan 2025 02:23:58 +0000 Subject: [PATCH 3/4] Release 2025.01.16 --- CHANGELOG.md | 9 +++++++++ pyproject.toml | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 604cf7c83..ad8ec678f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,15 @@ All enhancements and patches to Cookiecutter Django will be documented in this f +## 2025.01.16 + + +### Updated + +- Update ruff to 0.9.2 ([#5646](https://github.com/cookiecutter/cookiecutter-django/pull/5646)) + +- Bump amazon/aws-cli from 2.22.1 to 2.23.0 ([#5645](https://github.com/cookiecutter/cookiecutter-django/pull/5645)) + ## 2025.01.15 diff --git a/pyproject.toml b/pyproject.toml index e1ec8bfd7..f5df2e072 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "cookiecutter-django" -version = "2025.01.15" +version = "2025.01.16" description = "A Cookiecutter template for creating production-ready Django projects quickly." readme = "README.md" keywords = [ From 7f4211ab16ae735fdf7a35e84b177f0c85cc2909 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 20 Jan 2025 13:28:59 +0000 Subject: [PATCH 4/4] Migrate test generation to pathlib (#5649) --- tests/test_cookiecutter_generation.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index 9669d78ba..1c03015c4 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -2,6 +2,8 @@ import glob import os import re import sys +from collections.abc import Iterable +from pathlib import Path import pytest @@ -146,19 +148,19 @@ def _fixture_id(ctx): return "-".join(f"{key}:{value}" for key, value in ctx.items()) -def build_files_list(base_dir): +def build_files_list(base_path: Path): """Build a list containing absolute paths to the generated files.""" - return [os.path.join(dirpath, file_path) for dirpath, subdirs, files in os.walk(base_dir) for file_path in files] + return [dirpath / file_path for dirpath, subdirs, files in base_path.walk() for file_path in files] -def check_paths(paths): +def check_paths(paths: Iterable[Path]): """Method to check all paths have correct substitutions.""" # Assert that no match is found in any of the files for path in paths: - if is_binary(path): + if is_binary(str(path)): continue - for line in open(path): + for line in path.open(): match = RE_OBJ.search(line) assert match is None, f"cookiecutter variable not replaced in {path}" @@ -173,7 +175,7 @@ def test_project_generation(cookies, context, context_override): assert result.project_path.name == context["project_slug"] assert result.project_path.is_dir() - paths = build_files_list(str(result.project_path)) + paths = build_files_list(result.project_path) assert paths check_paths(paths) @@ -286,7 +288,7 @@ def test_travis_invokes_pytest(cookies, context, use_docker, expected_test_scrip assert result.project_path.name == context["project_slug"] assert result.project_path.is_dir() - with open(f"{result.project_path}/.travis.yml") as travis_yml: + with (result.project_path / ".travis.yml").open() as travis_yml: try: yml = yaml.safe_load(travis_yml)["jobs"]["include"] assert yml[0]["script"] == ["ruff check ."] @@ -311,7 +313,7 @@ def test_gitlab_invokes_precommit_and_pytest(cookies, context, use_docker, expec assert result.project_path.name == context["project_slug"] assert result.project_path.is_dir() - with open(f"{result.project_path}/.gitlab-ci.yml") as gitlab_yml: + with (result.project_path / ".gitlab-ci.yml").open() as gitlab_yml: try: gitlab_config = yaml.safe_load(gitlab_yml) assert gitlab_config["precommit"]["script"] == [ @@ -338,7 +340,7 @@ def test_github_invokes_linter_and_pytest(cookies, context, use_docker, expected assert result.project_path.name == context["project_slug"] assert result.project_path.is_dir() - with open(f"{result.project_path}/.github/workflows/ci.yml") as github_yml: + with (result.project_path / ".github" / "workflows" / "ci.yml").open() as github_yml: try: github_config = yaml.safe_load(github_yml) linter_present = False @@ -389,9 +391,9 @@ def test_pycharm_docs_removed(cookies, context, editor, pycharm_docs_exist): context.update({"editor": editor}) result = cookies.bake(extra_context=context) - with open(f"{result.project_path}/docs/index.rst") as f: - has_pycharm_docs = "pycharm/configuration" in f.read() - assert has_pycharm_docs is pycharm_docs_exist + index_rst = result.project_path / "docs" / "index.rst" + has_pycharm_docs = "pycharm/configuration" in index_rst.read_text() + assert has_pycharm_docs is pycharm_docs_exist def test_trim_domain_email(cookies, context):