From 76b5ff2cf67661c8d603e3f7f12eaf4ea3742f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Prchl=C3=ADk?= Date: Mon, 1 Jul 2024 09:14:06 +0200 Subject: [PATCH] Add a linter for Jinja2 templates (#3048) Maybe we would add also a formatter (same tool, different check and options), but starting small with linters. Few very minor issues reported, plus a genuine one. --- .pre-commit-config.yaml | 19 +++++++++++++++++++ docs/templates/lint-checks.rst.j2 | 2 +- docs/templates/plugins.rst.j2 | 4 ++-- docs/templates/story.rst.j2 | 2 +- docs/templates/template-filters.rst.j2 | 2 +- pyproject.toml | 4 ++++ tmt/steps/report/html/template.html.j2 | 7 ++++--- 7 files changed, 32 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a8f45c4f0e..8389b21883 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -161,3 +161,22 @@ repos: - id: codespell additional_dependencies: - tomli # Required for python < 3.11 + + - repo: https://github.com/djlint/djLint + rev: v1.34.1 + hooks: + - id: djlint + files: "\\.j2" + types_or: ['jinja'] + + - repo: https://github.com/aristanetworks/j2lint.git + rev: v1.1.0 + hooks: + - id: j2lint + args: + # j2lint does not consume pyproject.toml + - "--ignore" + - jinja-statements-indentation + - jinja-variable-lower-case + - single-statement-per-line + - "--" diff --git a/docs/templates/lint-checks.rst.j2 b/docs/templates/lint-checks.rst.j2 index 52c5d614be..214ccb4f42 100644 --- a/docs/templates/lint-checks.rst.j2 +++ b/docs/templates/lint-checks.rst.j2 @@ -34,7 +34,7 @@ Below you can find the list of available checks. See the - {{ linter.help }} {% endfor %} -{% endmacro%} +{% endmacro %} {{ emit_table('Test checks', TEST_LINTERS) }} {{ emit_table('Plan checks', PLAN_LINTERS) }} diff --git a/docs/templates/plugins.rst.j2 b/docs/templates/plugins.rst.j2 index 7f25a6ebc1..db01eef1b0 100644 --- a/docs/templates/plugins.rst.j2 +++ b/docs/templates/plugins.rst.j2 @@ -10,7 +10,7 @@ .. _plugins/{{ STEP }}/{{ PLUGIN_ID | trim }}: {{ PLUGIN_ID }} -{{ '-' * (PLUGIN_ID | length)}} +{{ '-' * (PLUGIN_ID | length) }} {# Emit the warning only for plugins that have not been reviewed yet. #} {% set plugin_full_id = STEP + "/" + PLUGIN_ID %} @@ -65,7 +65,7 @@ Configuration Default: *could not render default value correctly* {% endif %} {% endif %} -{%endmacro %} +{% endmacro %} {% set ignored_fields = container_ignored_fields(PLUGIN_DATA_CLASS) %} {% set inherited_fields = container_inherited_fields(PLUGIN_DATA_CLASS) | sort %} diff --git a/docs/templates/story.rst.j2 b/docs/templates/story.rst.j2 index 4e01aeedd4..59f3568956 100644 --- a/docs/templates/story.rst.j2 +++ b/docs/templates/story.rst.j2 @@ -219,7 +219,7 @@ {# Links pointing to websites #} {% elif link.target | match('^https?://') %} -* {{ printable_relation(link) }} `{{ link.target}} <{{ link.target }}>`_ +* {{ printable_relation(link) }} `{{ link.target }} <{{ link.target }}>`_ {# Links pointing to anything else #} {% else %} diff --git a/docs/templates/template-filters.rst.j2 b/docs/templates/template-filters.rst.j2 index ee0e2756bb..f47e1aa53d 100644 --- a/docs/templates/template-filters.rst.j2 +++ b/docs/templates/template-filters.rst.j2 @@ -25,7 +25,7 @@ __ https://jinja.palletsprojects.com/en/3.1.x/templates/#filters {% set filter_callable = TEMPLATES[filter_name] %} {{ filter_name }} -{{ '-' * (filter_name | length)}} +{{ '-' * (filter_name | length) }} {% if filter_callable.__doc__ %} {{ filter_callable.__doc__ | dedent | trim }} diff --git a/pyproject.toml b/pyproject.toml index 64c4cd8dac..fd5ade6893 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -400,3 +400,7 @@ markers = [ ignore-words = "docs/codespell.dic" exclude-file = "docs/codespell.ignore" skip = "tests/execute/weird/data/weird.txt,tests/lint/plan/data/invalid_attr.fmf,tests/lint/plan/test.sh" + +[tool.djlint] +use_gitignore=true +ignore="H005,H030,H031" diff --git a/tmt/steps/report/html/template.html.j2 b/tmt/steps/report/html/template.html.j2 index 88c118466b..7153110c41 100644 --- a/tmt/steps/report/html/template.html.j2 +++ b/tmt/steps/report/html/template.html.j2 @@ -1,4 +1,5 @@ + Test results of {{ plan.name }}