Skip to content

Commit

Permalink
parameterize new and legacy settings in tests; revert dev_server_port…
Browse files Browse the repository at this point in the history
…=3000
  • Loading branch information
Niicck committed Oct 23, 2023
1 parent c5beb0e commit da7ee1b
Show file tree
Hide file tree
Showing 12 changed files with 353 additions and 101 deletions.
8 changes: 2 additions & 6 deletions django_vite/core/asset_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DjangoViteConfig(NamedTuple):
dev_server_host: str = "localhost"

# Default Vite server port.
dev_server_port: int = 5173
dev_server_port: int = 3000

# Prefix for STATIC_URL.
static_url_prefix: str = ""
Expand Down Expand Up @@ -720,11 +720,7 @@ def _apply_legacy_django_vite_settings(cls):
DeprecationWarning,
)

# Keep same default dev_server_port for legacy users that are still on vite v2.
legacy_config = {
"dev_server_port": 3000,
}

legacy_config = {}
for legacy_setting in applied_legacy_settings:
new_config_name = cls.LEGACY_DJANGO_VITE_SETTINGS[legacy_setting]
if new_config_name:
Expand Down
58 changes: 51 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Any, List
from typing import Dict, Any
import pytest

from django_vite.core.asset_loader import DjangoViteAssetLoader
Expand All @@ -13,7 +13,7 @@ def reload_django_vite():


@pytest.fixture()
def patch_settings(settings):
def patch_settings(request, settings):
"""
1. Patch new settings into django.conf.settings.
2. Recreate DjangoViteAssetLoader._instance with new settings applied.
Expand All @@ -31,7 +31,12 @@ def _patch_settings(new_settings: Dict[str, Any]):
setattr(settings, key, value)
reload_django_vite()

yield _patch_settings
# Apply pytest.mark.parametrize params, if patch_settings was invoked from
# @pytest.mark.parametrize("patch_settings", [param1, param2], indirect=True)
if hasattr(request, "param"):
yield _patch_settings(request.param)
else:
yield _patch_settings

for key, value in original_settings_cache.items():
if value == __PYTEST_EMPTY__:
Expand All @@ -49,11 +54,50 @@ def delete_settings(patch_settings):

def _delete_settings(*settings_to_delete: str):
new_settings = {key: __PYTEST_DELETE__ for key in settings_to_delete}
patch_settings(new_settings)
return patch_settings(new_settings)

return _delete_settings


@pytest.fixture()
def dev_mode_off(patch_settings):
patch_settings({"DJANGO_VITE_DEV_MODE": False})
@pytest.fixture(
params=[
{
"DJANGO_VITE_DEV_MODE": False,
},
{
"DJANGO_VITE": {
"default": {
"dev_mode": False,
}
}
},
]
)
def patch_dev_mode_false(request, patch_settings):
"""
Run a test with dev_mode=False, parameterized to run under both versions of
settings that we support.
"""
return patch_settings(request.param)


@pytest.fixture(
params=[
{
"DJANGO_VITE_DEV_MODE": True,
},
{
"DJANGO_VITE": {
"default": {
"dev_mode": True,
}
}
},
]
)
def patch_dev_mode_true(request, patch_settings):
"""
Run a test with dev_mode=True, parameterized to run under both versions of
settings that we support.
"""
return patch_settings(request.param)
2 changes: 0 additions & 2 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@
]

STATIC_ROOT = BASE_DIR / "data" / "staticfiles"

DJANGO_VITE_DEV_MODE = True
81 changes: 65 additions & 16 deletions tests/tests/templatetags/test_vite_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django_vite.core.exceptions import DjangoViteAssetNotFoundError


@pytest.mark.usefixtures("patch_dev_mode_true")
def test_vite_asset_returns_dev_tags():
template = Template(
"""
Expand All @@ -18,7 +19,7 @@ def test_vite_asset_returns_dev_tags():
assert script_tag["type"] == "module"


@pytest.mark.usefixtures("dev_mode_off")
@pytest.mark.usefixtures("patch_dev_mode_false")
def test_vite_asset_returns_production_tags():
template = Template(
"""
Expand All @@ -35,6 +36,7 @@ def test_vite_asset_returns_production_tags():
assert len(links) == 13


@pytest.mark.usefixtures("patch_dev_mode_true")
def test_vite_asset_raises_without_path():
with pytest.raises(TemplateSyntaxError):
Template(
Expand All @@ -45,7 +47,7 @@ def test_vite_asset_raises_without_path():
)


@pytest.mark.usefixtures("dev_mode_off")
@pytest.mark.usefixtures("patch_dev_mode_false")
def test_vite_asset_raises_nonexistent_entry():
with pytest.raises(DjangoViteAssetNotFoundError):
template = Template(
Expand All @@ -57,13 +59,37 @@ def test_vite_asset_raises_nonexistent_entry():
template.render(Context({}))


@pytest.mark.parametrize("prefix", ["custom/prefix", "custom/prefix/"])
def test_vite_asset_dev_prefix(prefix, patch_settings):
patch_settings(
@pytest.mark.parametrize(
"patch_settings",
[
{
"DJANGO_VITE_STATIC_URL_PREFIX": prefix,
}
)
"DJANGO_VITE_DEV_MODE": True,
"DJANGO_VITE_STATIC_URL_PREFIX": "custom/prefix",
},
{
"DJANGO_VITE_DEV_MODE": True,
"DJANGO_VITE_STATIC_URL_PREFIX": "custom/prefix/",
},
{
"DJANGO_VITE": {
"default": {
"dev_mode": True,
"static_url_prefix": "custom/prefix",
}
}
},
{
"DJANGO_VITE": {
"default": {
"dev_mode": True,
"static_url_prefix": "custom/prefix/",
}
}
},
],
indirect=True,
)
def test_vite_asset_dev_prefix(patch_settings):
template = Template(
"""
{% load django_vite %}
Expand All @@ -79,14 +105,37 @@ def test_vite_asset_dev_prefix(prefix, patch_settings):
assert script_tag["type"] == "module"


@pytest.mark.usefixtures("dev_mode_off")
@pytest.mark.parametrize("prefix", ["custom/prefix", "custom/prefix/"])
def test_vite_asset_production_prefix(prefix, patch_settings):
patch_settings(
@pytest.mark.parametrize(
"patch_settings",
[
{
"DJANGO_VITE_STATIC_URL_PREFIX": prefix,
}
)
"DJANGO_VITE_DEV_MODE": False,
"DJANGO_VITE_STATIC_URL_PREFIX": "custom/prefix",
},
{
"DJANGO_VITE_DEV_MODE": False,
"DJANGO_VITE_STATIC_URL_PREFIX": "custom/prefix/",
},
{
"DJANGO_VITE": {
"default": {
"dev_mode": False,
"static_url_prefix": "custom/prefix",
}
}
},
{
"DJANGO_VITE": {
"default": {
"dev_mode": False,
"static_url_prefix": "custom/prefix/",
}
}
},
],
indirect=True,
)
def test_vite_asset_production_prefix(patch_settings):
template = Template(
"""
{% load django_vite %}
Expand All @@ -102,7 +151,7 @@ def test_vite_asset_production_prefix(prefix, patch_settings):
assert len(links) == 13


@pytest.mark.usefixtures("dev_mode_off")
@pytest.mark.usefixtures("patch_dev_mode_false")
def test_vite_asset_production_staticfiles_storage(patch_settings):
patch_settings(
{
Expand Down
5 changes: 3 additions & 2 deletions tests/tests/templatetags/test_vite_asset_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django_vite.core.exceptions import DjangoViteAssetNotFoundError


@pytest.mark.usefixtures("patch_dev_mode_true")
def test_vite_asset_url_returns_dev_url():
template = Template(
"""
Expand All @@ -17,7 +18,7 @@ def test_vite_asset_url_returns_dev_url():
assert script_tag["src"] == "http://localhost:3000/static/src/entry.ts"


@pytest.mark.usefixtures("dev_mode_off")
@pytest.mark.usefixtures("patch_dev_mode_false")
def test_vite_asset_url_returns_production_url():
template = Template(
"""
Expand All @@ -31,7 +32,7 @@ def test_vite_asset_url_returns_production_url():
assert script_tag["src"] == "assets/entry-29e38a60.js"


@pytest.mark.usefixtures("dev_mode_off")
@pytest.mark.usefixtures("patch_dev_mode_false")
def test_vite_asset_url_raises_nonexistent_entry():
with pytest.raises(DjangoViteAssetNotFoundError):
template = Template(
Expand Down
36 changes: 27 additions & 9 deletions tests/tests/templatetags/test_vite_hmr_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.template import Context, Template


@pytest.mark.usefixtures("patch_dev_mode_true")
def test_vite_hmr_client_returns_script_tag():
template = Template(
"""
Expand All @@ -17,6 +18,7 @@ def test_vite_hmr_client_returns_script_tag():
assert script_tag["type"] == "module"


@pytest.mark.usefixtures("patch_dev_mode_true")
def test_vite_hmr_client_kwargs():
template = Template(
"""
Expand All @@ -31,8 +33,8 @@ def test_vite_hmr_client_kwargs():
assert script_tag["blocking"] == "render"


@pytest.mark.usefixtures("dev_mode_off")
def test_vite_hmr_client_returns_nothing_with_dev_mode_off():
@pytest.mark.usefixtures("patch_dev_mode_false")
def test_vite_hmr_client_returns_nothing_with_patch_dev_mode_false():
template = Template(
"""
{% load django_vite %}
Expand All @@ -44,17 +46,33 @@ def test_vite_hmr_client_returns_nothing_with_dev_mode_off():
assert str(soup).strip() == ""


def test_vite_hmr_client_uses_correct_settings(patch_settings):
patch_settings(
@pytest.mark.parametrize(
"patch_settings",
[
{
"DJANGO_VITE_DEV_MODE": True,
"DJANGO_VITE_DEV_SERVER_PROTOCOL": "https",
"DJANGO_VITE_DEV_SERVER_HOST": "127.0.0.2",
"DJANGO_VITE_DEV_SERVER_PORT": "5174",
"DJANGO_VITE_STATIC_URL": "static/",
"DJANGO_VITE_STATIC_URL_PREFIX": "custom/prefix",
"DJANGO_VITE_WS_CLIENT_URL": "foo/bar",
}
)

},
{
"DJANGO_VITE": {
"default": {
"dev_mode": True,
"dev_server_protocol": "https",
"dev_server_host": "127.0.0.2",
"dev_server_port": "5174",
"static_url_prefix": "custom/prefix",
"ws_client_url": "foo/bar",
}
}
},
],
indirect=True,
)
def test_vite_hmr_client_uses_correct_settings(patch_settings):
template = Template(
"""
{% load django_vite %}
Expand All @@ -64,4 +82,4 @@ def test_vite_hmr_client_uses_correct_settings(patch_settings):
html = template.render(Context({}))
soup = BeautifulSoup(html, "html.parser")
script_tag = soup.find("script")
assert script_tag["src"] == "https://127.0.0.2:5174/static/foo/bar"
assert script_tag["src"] == "https://127.0.0.2:5174/static/custom/prefix/foo/bar"
Loading

0 comments on commit da7ee1b

Please sign in to comment.