Add django-upgrade to pre-commit hooks (#4298)

This commit is contained in:
Bruno Alla 2023-04-28 08:56:22 +01:00 committed by GitHub
parent 9d821ee82b
commit c88f8ef37a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -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

View File

@ -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"],
[

View File

@ -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: