Commit 801bb288 by Robert Raposa

Merge pull request #11945 from edx/robrap/linter

Add check for multiple page tags
parents 6bbcbbf0 0707e0dd
...@@ -150,7 +150,8 @@ class Rules(Enum): ...@@ -150,7 +150,8 @@ class Rules(Enum):
""" """
An Enum of each rule which the linter will check. An Enum of each rule which the linter will check.
""" """
mako_missing_default = ('mako-missing-default', 'The default page directive with h filter is missing.') mako_missing_default = ('mako-missing-default', 'Missing default <%page expression_filter="h"/>.')
mako_multiple_page_tags = ('mako-multiple-page-tags', 'A Mako template can only have one <%page> tag.')
mako_unparsable_expression = ('mako-unparsable-expression', 'The expression could not be properly parsed.') mako_unparsable_expression = ('mako-unparsable-expression', 'The expression could not be properly parsed.')
mako_unwanted_html_filter = ('mako-unwanted-html-filter', 'Remove explicit h filters when it is provided by the page directive.') mako_unwanted_html_filter = ('mako-unwanted-html-filter', 'Remove explicit h filters when it is provided by the page directive.')
mako_invalid_html_filter = ('mako-invalid-html-filter', 'The expression is using an invalid filter in an HTML context.') mako_invalid_html_filter = ('mako-invalid-html-filter', 'The expression is using an invalid filter in an HTML context.')
...@@ -398,23 +399,37 @@ class MakoTemplateLinter(object): ...@@ -398,23 +399,37 @@ class MakoTemplateLinter(object):
results: A file results objects to which violations will be added. results: A file results objects to which violations will be added.
""" """
has_page_default = self._has_page_default(mako_template, results) has_page_default = False
if not has_page_default: if self._has_multiple_page_tags(mako_template):
results.violations.append(RuleViolation(Rules.mako_missing_default)) results.violations.append(RuleViolation(Rules.mako_multiple_page_tags))
else:
has_page_default = self._has_page_default(mako_template)
if not has_page_default:
results.violations.append(RuleViolation(Rules.mako_missing_default))
self._check_mako_expressions(mako_template, has_page_default, results) self._check_mako_expressions(mako_template, has_page_default, results)
results.prepare_results(mako_template) results.prepare_results(mako_template)
def _has_page_default(self, mako_template, results): def _has_multiple_page_tags(self, mako_template):
"""
Checks if the Mako template contains more than one page expression.
Arguments:
mako_template: The contents of the Mako template.
"""
count = len(re.findall('<%page ', mako_template, re.IGNORECASE))
return count > 1
def _has_page_default(self, mako_template):
""" """
Checks if the Mako template contains the page expression marking it as Checks if the Mako template contains the page expression marking it as
safe by default. safe by default.
Arguments: Arguments:
mako_template: The contents of the Mako template. mako_template: The contents of the Mako template.
results: A list of results into which violations will be added.
""" """
page_h_filter_regex = re.compile('<%page expression_filter=(?:"h"|\'h\')\s*/>') page_h_filter_regex = re.compile('<%page[^>]*expression_filter=(?:"h"|\'h\')[^>]*/>')
page_match = page_h_filter_regex.search(mako_template) page_match = page_h_filter_regex.search(mako_template)
return page_match return page_match
......
...@@ -31,32 +31,54 @@ class TestMakoTemplateLinter(TestCase): ...@@ -31,32 +31,54 @@ class TestMakoTemplateLinter(TestCase):
self.assertEqual(linter._is_mako_directory(data['directory']), data['expected']) self.assertEqual(linter._is_mako_directory(data['directory']), data['expected'])
def test_check_page_default_with_default_provided(self): @data(
""" {
Test _check_mako_file_is_safe with default causes no violation 'template': '\n <%page expression_filter="h"/>',
""" 'violations': 0,
linter = MakoTemplateLinter() 'rule': None
results = FileResults('') },
mako_template = """ {
<%page expression_filter="h"/> 'template':
""" '\n <%page args="section_data" expression_filter="h" /> ',
'violations': 0,
linter._check_mako_file_is_safe(mako_template, results) 'rule': None
},
self.assertEqual(len(results.violations), 0) {
'template':
def test_check_page_default_with_no_default_provided(self): '\n <%page expression_filter="h" /> '
'\n <%page args="section_data"/>',
'violations': 1,
'rule': Rules.mako_multiple_page_tags
},
{
'template': '\n <%page args="section_data" /> ',
'violations': 1,
'rule': Rules.mako_missing_default
},
{
'template':
'\n <%page args="section_data"/> <some-other-tag expression_filter="h" /> ',
'violations': 1,
'rule': Rules.mako_missing_default
},
{
'template': '\n',
'violations': 1,
'rule': Rules.mako_missing_default
},
)
def test_check_page_default(self, data):
""" """
Test _check_mako_file_is_safe with no default causes violation Test _check_mako_file_is_safe with different page defaults
""" """
linter = MakoTemplateLinter() linter = MakoTemplateLinter()
results = FileResults('') results = FileResults('')
mako_template = ""
linter._check_mako_file_is_safe(mako_template, results) linter._check_mako_file_is_safe(data['template'], results)
self.assertEqual(len(results.violations), 1) self.assertEqual(len(results.violations), data['violations'])
self.assertEqual(results.violations[0].rule, Rules.mako_missing_default) if data['violations'] > 0:
self.assertEqual(results.violations[0].rule, data['rule'])
def test_check_mako_expressions_in_html(self): def test_check_mako_expressions_in_html(self):
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment