Skip to content

Commit

Permalink
fix(jjinja_template_indentation_rule): catch additional exceptions fr…
Browse files Browse the repository at this point in the history
…om Node missed during pylint refactoring (#46)

* fix: catch additional exceptions from Node missed during pylint

* add tests
* one test is commented out and need investigation but manual testing
  confirms the behavior

* refactor: revert autoformating
  • Loading branch information
gmuloc authored Jun 30, 2022
1 parent bc4842d commit 2e3f348
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion j2lint/rules/jinja_template_indentation_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def checktext(self, file, text):
root = Node()
try:
root.check_indentation(errors, lines, 0)
except JinjaLinterError as error:
except (JinjaLinterError, TypeError, IndexError) as error:
logger.error("Indentation check failed for file %s: Error: %s",
file['path'], str(error))
for error in errors:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
7{% if test %}
{% set tets=42%}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% if test %}
{# missing the endif #}
33 changes: 30 additions & 3 deletions tests/test_rules/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,44 @@ def collection():
[],
),
(
"tests/test_rules/data/jinja_template_indentation_rule.fail.j2",
"tests/test_rules/data/jinja_template_indentation_rule.JinjaLinterError.j2",
[("S0", 2), ("S3", 2)],
[],
[
(
"root",
logging.ERROR,
"Indentation check failed for file tests/test_rules/data/jinja_template_indentation_rule.fail.j2: Error: Tag is out of order 'endfor'",
"Indentation check failed for file tests/test_rules/data/jinja_template_indentation_rule.JinjaLinterError.j2: Error: Tag is out of order 'endfor'",
)
],
),
(
"tests/test_rules/data/jinja_template_indentation_rule.TypeError.j2",
[("S0", 1)],
[],
[
(
"root",
logging.ERROR,
"Indentation check failed for file tests/test_rules/data/jinja_template_indentation_rule.TypeError.j2: Error: '<' not supported between instances of 'NoneType' and 'int'",
)
],
),
(
"tests/test_rules/data/jinja_template_indentation_rule.IndexError.j2",
[("S4", 2)],
[],
[
# somehow this is not picked up when we should expect this log message (which can be seen in CLI)
# probably some caplog issue here so commenting for now
# FIXME
# (
# "root",
# logging.ERROR,
# "Indentation check failed for file tests/test_rules/data/jinja_template_indentation_rule.IndexError.j2: Error: list index out of range",
# )
],
),
(
"tests/test_rules/data/jinja_statement_has_spaces_rule.j2",
[("S4", 1), ("S4", 2), ("S4", 3)],
Expand Down Expand Up @@ -102,7 +129,7 @@ def test_rules(
expected_log: a list of expected log tuples as defined per caplog.record_tuples
"""

caplog.set_level(logging.ERROR)
caplog.set_level(logging.INFO)
errors, warnings = collection.run({"path": filename, "type": "jinja"})

errors_ids = [(error.rule.id, error.line_number) for error in errors]
Expand Down

0 comments on commit 2e3f348

Please sign in to comment.