diff --git a/CHANGELOG.md b/CHANGELOG.md index caf95b4a..033c7d85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ Using the following categories, list your changes in this order: - Allow `use_mutation` to have `refetch=None`, as the docs suggest is possible. - `use_query` will now prefetch all fields to prevent `SynchronousOnlyOperation` exceptions. - `view_to_component`, `django_css`, and `django_js` type hints will now display like normal functions. +- IDOM preloader no longer attempts to parse commented out IDOM components. +- Tests are now fully functional on Windows ## [1.2.0] - 2022-09-19 diff --git a/requirements/test-env.txt b/requirements/test-env.txt index 32187f96..5cad346d 100644 --- a/requirements/test-env.txt +++ b/requirements/test-env.txt @@ -1,3 +1,4 @@ django playwright twisted +channels[daphne]>=4.0.0 diff --git a/src/django_idom/utils.py b/src/django_idom/utils.py index c6b308bb..6b34145d 100644 --- a/src/django_idom/utils.py +++ b/src/django_idom/utils.py @@ -18,6 +18,7 @@ _component_tag = r"(?Pcomponent)" _component_path = r"(?P(\"[^\"'\s]+\")|('[^\"'\s]+'))" _component_kwargs = r"(?P(.*?|\s*?)*)" +COMMENT_REGEX = re.compile(r"()") COMPONENT_REGEX = re.compile( r"{%\s*" + _component_tag @@ -112,7 +113,8 @@ def _get_components(self, templates: set[str]) -> set[str]: for template in templates: with contextlib.suppress(Exception): with open(template, "r", encoding="utf-8") as template_file: - regex_iterable = COMPONENT_REGEX.finditer(template_file.read()) + clean_template = COMMENT_REGEX.sub("", template_file.read()) + regex_iterable = COMPONENT_REGEX.finditer(clean_template) component_paths = [ match.group("path").replace('"', "").replace("'", "") for match in regex_iterable diff --git a/tests/test_app/tests/test_components.py b/tests/test_app/tests/test_components.py index c11e5b33..83bfedcd 100644 --- a/tests/test_app/tests/test_components.py +++ b/tests/test_app/tests/test_components.py @@ -1,6 +1,6 @@ +import asyncio import os import sys -from unittest import SkipTest from channels.testing import ChannelsLiveServerTestCase from playwright.sync_api import TimeoutError, sync_playwright @@ -13,13 +13,7 @@ class TestIdomCapabilities(ChannelsLiveServerTestCase): @classmethod def setUpClass(cls): if sys.platform == "win32": - raise SkipTest("These tests are broken on Windows.") - - # FIXME: The following lines will be needed once Django channels fixes Windows tests - # See: https://github.com/django/channels/issues/1207 - - # import asyncio - # asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) + asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) # FIXME: This is required otherwise the tests will throw a `SynchronousOnlyOperation` # error when deleting the test datatabase. Potentially a Django bug. diff --git a/tests/test_app/tests/test_regex.py b/tests/test_app/tests/test_regex.py index 4fff532d..8e211820 100644 --- a/tests/test_app/tests/test_regex.py +++ b/tests/test_app/tests/test_regex.py @@ -1,6 +1,6 @@ from django.test import TestCase -from django_idom.utils import COMPONENT_REGEX +from django_idom.utils import COMMENT_REGEX, COMPONENT_REGEX class RegexTests(TestCase): @@ -41,3 +41,45 @@ def test_component_regex(self): r'{{ component "" }}', }: self.assertNotRegex(fake_component, COMPONENT_REGEX) + + def test_comment_regex(self): + for comment in { + r"", + r"""""", + r"""""", + r"""""", + r"""""", # noqa: W291 + }: + self.assertRegex(comment, COMMENT_REGEX) + + for fake_comment in { + r"", + r"", + r'{% component "my.component" %}', + }: + self.assertNotRegex(fake_comment, COMMENT_REGEX) + + for embedded_comment in { + r'{% component "my.component" %} ', + r' {% component "my.component" %}', + r' {% component "my.component" %} ', + r""" {% component "my.component" %} + + """, + }: # noqa: W291 + text = COMMENT_REGEX.sub("", embedded_comment) + if text.strip() != '{% component "my.component" %}': + raise self.failureException( + f"Regex pattern {COMMENT_REGEX.pattern} failed to remove comment from {embedded_comment}" + )