From a7f554698d2cdc6f0a3aecac599e52b890687036 Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Sun, 16 Oct 2022 15:06:02 -0700 Subject: [PATCH 1/6] Fix IDOM preloader trying to parse commented out HTML templates --- CHANGELOG.md | 1 + src/django_idom/utils.py | 4 +++- tests/test_app/tests/test_regex.py | 29 ++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index caf95b4a..c5a79eec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ 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. ## [1.2.0] - 2022-09-19 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_regex.py b/tests/test_app/tests/test_regex.py index 4fff532d..fe800b1f 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,30 @@ def test_component_regex(self): r'{{ component "" }}', }: self.assertNotRegex(fake_component, COMPONENT_REGEX) + + def test_comment_regex(self): + for comment in {r""}: + self.assertRegex(comment, COMMENT_REGEX) + + for fake_comment in { + 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" %} + + """, + }: + comment = COMMENT_REGEX.sub("", embedded_comment) + if comment.strip() != '{% component "my.component" %}': + raise self.failureException( + f"Regex pattern {COMMENT_REGEX.pattern} failed to remove comment from {embedded_comment}" + ) From 74e298ea2c268d5792c3e0a90a6d07243855478c Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Sun, 16 Oct 2022 15:12:13 -0700 Subject: [PATCH 2/6] add a couple more tests --- tests/test_app/tests/test_regex.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/test_app/tests/test_regex.py b/tests/test_app/tests/test_regex.py index fe800b1f..fbfc5898 100644 --- a/tests/test_app/tests/test_regex.py +++ b/tests/test_app/tests/test_regex.py @@ -43,12 +43,27 @@ def test_component_regex(self): self.assertNotRegex(fake_component, COMPONENT_REGEX) def test_comment_regex(self): - for comment in {r""}: + 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) @@ -57,12 +72,12 @@ def test_comment_regex(self): r'{% component "my.component" %} ', r' {% component "my.component" %}', r' {% component "my.component" %} ', - r""" {% component "my.component" %} """, - }: + }: # noqa: W291 comment = COMMENT_REGEX.sub("", embedded_comment) if comment.strip() != '{% component "my.component" %}': raise self.failureException( From ac65c6801733475d41329d89ad97548c186d5afc Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Sun, 16 Oct 2022 15:12:35 -0700 Subject: [PATCH 3/6] fix channels >= 4.0.0 --- requirements/test-env.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements/test-env.txt b/requirements/test-env.txt index 32187f96..c3f8583a 100644 --- a/requirements/test-env.txt +++ b/requirements/test-env.txt @@ -1,3 +1,4 @@ django playwright twisted +daphne From 88efd8ae35479a07a5502782554af580d49722f2 Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Sun, 16 Oct 2022 15:22:52 -0700 Subject: [PATCH 4/6] Fix windows tests with channels >=4.0.0 --- requirements/test-env.txt | 2 +- tests/test_app/tests/test_components.py | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/requirements/test-env.txt b/requirements/test-env.txt index c3f8583a..5cad346d 100644 --- a/requirements/test-env.txt +++ b/requirements/test-env.txt @@ -1,4 +1,4 @@ django playwright twisted -daphne +channels[daphne]>=4.0.0 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. From 6c62f456d218741e634808612b624ba9572c06bc Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Sun, 16 Oct 2022 15:29:54 -0700 Subject: [PATCH 5/6] add changelog for tests --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5a79eec..033c7d85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ Using the following categories, list your changes in this order: - `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 From 7df6d813a2e55e31d6126ed52c0edcaf5b6cfe13 Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Sun, 16 Oct 2022 20:22:24 -0700 Subject: [PATCH 6/6] test var cleanup --- tests/test_app/tests/test_regex.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_app/tests/test_regex.py b/tests/test_app/tests/test_regex.py index fbfc5898..8e211820 100644 --- a/tests/test_app/tests/test_regex.py +++ b/tests/test_app/tests/test_regex.py @@ -78,8 +78,8 @@ def test_comment_regex(self): """, }: # noqa: W291 - comment = COMMENT_REGEX.sub("", embedded_comment) - if comment.strip() != '{% component "my.component" %}': + 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}" )