From 94154c76cdbe72b09a0302b65de5672c801feda1 Mon Sep 17 00:00:00 2001 From: Nick Ivons <14187277+Niicck@users.noreply.github.com> Date: Mon, 16 Oct 2023 00:27:33 -0700 Subject: [PATCH] Add tests to achieve 100% Test Coverage (#91) * create patch_settings fixture * 100% test coverage --------- Co-authored-by: Ben Beecher <120373+gone@users.noreply.github.com> Co-authored-by: Eddy Brown Co-authored-by: Thijs Kramer <1274867+thijskramer@users.noreply.github.com> Co-authored-by: Niicck --- django_vite/apps.py | 5 +- django_vite/exceptions.py | 10 + django_vite/templatetags/django_vite.py | 215 +++++++++++++++--- pyproject.toml | 4 + setup.py | 2 +- tests/conftest.py | 46 ++++ .../custom-motif-polyfills-manifest.json | 24 ++ .../staticfiles/custom/prefix/manifest.json | 72 ++++++ tests/data/staticfiles/manifest.json | 72 ++++++ .../data/staticfiles/polyfills-manifest.json | 24 ++ tests/settings.py | 13 +- tests/test_asset_loader.py | 8 - tests/test_templatetags.py | 150 ------------ tests/tests/templatetags/test_vite_asset.py | 124 ++++++++++ .../tests/templatetags/test_vite_asset_url.py | 43 ++++ .../templatetags/test_vite_hmr_client.py | 67 ++++++ .../templatetags/test_vite_legacy_asset.py | 49 ++++ .../test_vite_legacy_polyfills.py | 78 +++++++ .../templatetags/test_vite_preload_asset.py | 45 ++++ .../templatetags/test_vite_react_refresh.py | 74 ++++++ tests/tests/test_asset_loader.py | 33 +++ tox.ini | 21 ++ 22 files changed, 986 insertions(+), 193 deletions(-) create mode 100644 django_vite/exceptions.py create mode 100644 tests/conftest.py create mode 100644 tests/data/staticfiles/custom-motif-polyfills-manifest.json create mode 100644 tests/data/staticfiles/custom/prefix/manifest.json create mode 100644 tests/data/staticfiles/manifest.json create mode 100644 tests/data/staticfiles/polyfills-manifest.json delete mode 100644 tests/test_asset_loader.py delete mode 100644 tests/test_templatetags.py create mode 100644 tests/tests/templatetags/test_vite_asset.py create mode 100644 tests/tests/templatetags/test_vite_asset_url.py create mode 100644 tests/tests/templatetags/test_vite_hmr_client.py create mode 100644 tests/tests/templatetags/test_vite_legacy_asset.py create mode 100644 tests/tests/templatetags/test_vite_legacy_polyfills.py create mode 100644 tests/tests/templatetags/test_vite_preload_asset.py create mode 100644 tests/tests/templatetags/test_vite_react_refresh.py create mode 100644 tests/tests/test_asset_loader.py diff --git a/django_vite/apps.py b/django_vite/apps.py index 98dae96..29280ad 100644 --- a/django_vite/apps.py +++ b/django_vite/apps.py @@ -4,6 +4,7 @@ from django.core.checks import Warning, register from .templatetags.django_vite import DjangoViteAssetLoader +from .exceptions import DjangoViteManifestError class DjangoViteAppConfig(AppConfig): @@ -11,7 +12,7 @@ class DjangoViteAppConfig(AppConfig): verbose_name = "Django Vite" def ready(self) -> None: - with suppress(RuntimeError): + with suppress(DjangoViteManifestError): # Create Loader instance at startup to prevent threading problems, # but do not crash while doing so. DjangoViteAssetLoader.instance() @@ -25,7 +26,7 @@ def check_loader_instance(**kwargs): # Make Loader instance at startup to prevent threading problems DjangoViteAssetLoader.instance() return [] - except RuntimeError as exception: + except DjangoViteManifestError as exception: return [ Warning( exception, diff --git a/django_vite/exceptions.py b/django_vite/exceptions.py new file mode 100644 index 0000000..1030ca8 --- /dev/null +++ b/django_vite/exceptions.py @@ -0,0 +1,10 @@ +class DjangoViteManifestError(RuntimeError): + """Manifest parsing failed.""" + + pass + + +class DjangoViteAssetNotFoundError(RuntimeError): + """Vite Asset could not be found.""" + + pass diff --git a/django_vite/templatetags/django_vite.py b/django_vite/templatetags/django_vite.py index 4ff55de..52deacf 100644 --- a/django_vite/templatetags/django_vite.py +++ b/django_vite/templatetags/django_vite.py @@ -1,6 +1,6 @@ import json from pathlib import Path -from typing import Dict, List +from typing import Dict, List, Callable from urllib.parse import urljoin from django import template @@ -8,8 +8,9 @@ from django.conf import settings from django.utils.safestring import mark_safe -register = template.Library() +from django_vite.exceptions import DjangoViteManifestError, DjangoViteAssetNotFoundError +register = template.Library() # If using in development or production mode. DJANGO_VITE_DEV_MODE = getattr(settings, "DJANGO_VITE_DEV_MODE", False) @@ -87,9 +88,10 @@ def generate_vite_asset( **kwargs: Dict[str, str], ) -> str: """ - Generates a