diff --git a/requirements.txt b/requirements.txt index 0748004e..5e3b3f46 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ binaryornot==0.4.4 black==23.3.0 isort==5.12.0 flake8==6.0.0 +django-upgrade==1.13.0 pre-commit==3.2.2 # Testing diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index 9837be3e..778e3411 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -1,3 +1,4 @@ +import glob import os import re import sys @@ -24,6 +25,7 @@ elif sys.platform.startswith("darwin") and os.getenv("CI"): # automatically by running pre-commit after generation however they are tedious # to fix in the template, so we don't insist too much in fixing them. AUTOFIXABLE_STYLES = os.getenv("AUTOFIXABLE_STYLES") == "1" +auto_fixable = pytest.mark.skipif(not AUTOFIXABLE_STYLES, reason="auto-fixable") @pytest.fixture @@ -185,7 +187,7 @@ def test_flake8_passes(cookies, context_override): pytest.fail(e.stdout.decode()) -@pytest.mark.skipif(not AUTOFIXABLE_STYLES, reason="Black is auto-fixable") +@auto_fixable @pytest.mark.parametrize("context_override", SUPPORTED_COMBINATIONS, ids=_fixture_id) def test_black_passes(cookies, context_override): """Check whether generated project passes black style.""" @@ -204,7 +206,7 @@ def test_black_passes(cookies, context_override): pytest.fail(e.stdout.decode()) -@pytest.mark.skipif(not AUTOFIXABLE_STYLES, reason="isort is auto-fixable") +@auto_fixable @pytest.mark.parametrize("context_override", SUPPORTED_COMBINATIONS, ids=_fixture_id) def test_isort_passes(cookies, context_override): """Check whether generated project passes isort style.""" @@ -216,6 +218,27 @@ def test_isort_passes(cookies, context_override): pytest.fail(e.stdout.decode()) +@auto_fixable +@pytest.mark.parametrize("context_override", SUPPORTED_COMBINATIONS, ids=_fixture_id) +def test_django_upgrade_passes(cookies, context_override): + """Check whether generated project passes django-upgrade.""" + result = cookies.bake(extra_context=context_override) + + python_files = [ + file_path.removeprefix(f"{result.project_path}/") + for file_path in glob.glob(str(result.project_path / "**" / "*.py"), recursive=True) + ] + try: + sh.django_upgrade( + "--target-version", + "4.1", + *python_files, + _cwd=str(result.project_path), + ) + except sh.ErrorReturnCode as e: + pytest.fail(e.stdout.decode()) + + @pytest.mark.parametrize( ["use_docker", "expected_test_script"], [ diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml index d638c776..cd64098a 100644 --- a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -24,6 +24,12 @@ repos: args: ['--tab-width', '2', '--single-quote'] exclude: '{{cookiecutter.project_slug}}/templates/' + - repo: https://github.com/adamchainz/django-upgrade + rev: "1.13.0" + hooks: + - id: django-upgrade + args: ["--target-version", "4.1"] + - repo: https://github.com/asottile/pyupgrade rev: v3.3.2 hooks: