From cbcc62681d08cdf88613ea7af39c58229dff9c9d Mon Sep 17 00:00:00 2001 From: Daniel Harding Date: Sun, 20 Mar 2022 16:45:49 +0300 Subject: [PATCH 1/4] Make .assertValidHTML() parse passed content Prior to this commit, the BaseTestCase.assertValidHTML() method was not parsing the content from the provided argument, but was instead parsing self.panel.content. For most usages, the passed in content was the same as self.panel.content, but for the TemplatesPanelTestCase.test_template_repr() test case it was not, resulting in an invalid test. --- tests/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/base.py b/tests/base.py index c09828b4f..58c1331a7 100644 --- a/tests/base.py +++ b/tests/base.py @@ -33,7 +33,7 @@ def get_response(self, request): def assertValidHTML(self, content, msg=None): parser = html5lib.HTMLParser() - parser.parseFragment(self.panel.content) + parser.parseFragment(content) if parser.errors: default_msg = ["Content is invalid HTML:"] lines = content.split("\n") From 4ef7432eb16126da360ae7285be66bce42c0bf47 Mon Sep 17 00:00:00 2001 From: Daniel Harding Date: Sun, 20 Mar 2022 17:51:13 +0300 Subject: [PATCH 2/4] Fix test failure exposed by previous commit --- tests/panels/test_template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/panels/test_template.py b/tests/panels/test_template.py index 9ff39543f..f666fc82e 100644 --- a/tests/panels/test_template.py +++ b/tests/panels/test_template.py @@ -47,7 +47,7 @@ def test_template_repr(self): User.objects.create(username="admin") bad_repr = TemplateReprForm() - t = Template("{{ bad_repr }}") + t = Template("{{ bad_repr }}
") c = Context({"bad_repr": bad_repr}) html = t.render(c) self.assertIsNotNone(html) From 70b836bcce8f8cdf9918f0cef5367c902f8babd9 Mon Sep 17 00:00:00 2001 From: Daniel Harding Date: Sun, 20 Mar 2022 17:07:26 +0300 Subject: [PATCH 3/4] Remove unused .assertValidHTML() msg argument Allows removal of a call to the internal TestCase._formatMessage() method. --- tests/base.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/base.py b/tests/base.py index 58c1331a7..56d2263b5 100644 --- a/tests/base.py +++ b/tests/base.py @@ -31,18 +31,16 @@ def tearDown(self): def get_response(self, request): return self._get_response(request) - def assertValidHTML(self, content, msg=None): + def assertValidHTML(self, content): parser = html5lib.HTMLParser() parser.parseFragment(content) if parser.errors: - default_msg = ["Content is invalid HTML:"] + msg_parts = ["Content is invalid HTML:"] lines = content.split("\n") for position, errorcode, datavars in parser.errors: - default_msg.append(" %s" % html5lib.constants.E[errorcode] % datavars) - default_msg.append(" %s" % lines[position[0] - 1]) - - msg = self._formatMessage(msg, "\n".join(default_msg)) - raise self.failureException(msg) + msg_parts.append(" %s" % html5lib.constants.E[errorcode] % datavars) + msg_parts.append(" %s" % lines[position[0] - 1]) + raise self.failureException("\n".join(msg_parts)) class IntegrationTestCase(TestCase): From 3cd9b9f862124263e6856893b37a05ac5ab88102 Mon Sep 17 00:00:00 2001 From: Daniel Harding Date: Sun, 20 Mar 2022 17:11:30 +0300 Subject: [PATCH 4/4] Tweak .assertValidHTML() exception message Since the passed in HTML does not necessarily represent the panel content, change the message to no longer reference "Content". --- tests/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/base.py b/tests/base.py index 56d2263b5..597a74f29 100644 --- a/tests/base.py +++ b/tests/base.py @@ -35,7 +35,7 @@ def assertValidHTML(self, content): parser = html5lib.HTMLParser() parser.parseFragment(content) if parser.errors: - msg_parts = ["Content is invalid HTML:"] + msg_parts = ["Invalid HTML:"] lines = content.split("\n") for position, errorcode, datavars in parser.errors: msg_parts.append(" %s" % html5lib.constants.E[errorcode] % datavars)