From 2acb8c3c28e360bcdc4266cb434554f92072e453 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Fri, 18 Feb 2022 13:07:30 -0600 Subject: [PATCH] closes #175, updated tests --- docs/src/docs/changelog.md | 1 + docs/src/docs/configuration.md | 4 +- docs/src/docs/getting-started.md | 3 +- docs/src/ru/docs/changelog.md | 1 + docs/src/ru/docs/configuration.md | 5 +- docs/src/ru/docs/getting-started.md | 3 +- src/djlint/__init__.py | 2 +- src/djlint/settings.py | 2 +- tests/test_config.py | 2 +- tests/test_linter.py | 84 ++++++++++++++++------------- 10 files changed, 61 insertions(+), 46 deletions(-) diff --git a/docs/src/docs/changelog.md b/docs/src/docs/changelog.md index 52e2048..04f7c74 100644 --- a/docs/src/docs/changelog.md +++ b/docs/src/docs/changelog.md @@ -16,6 +16,7 @@ keywords: template linter, template formatter, djLint, HTML, templates, formatte - Added better support for ``yaml`` front matter in template files - Added rule T032 for [#123](https://github.com/Riverside-Healthcare/djLint/issues/123) - Added rule H033 for [#124](https://github.com/Riverside-Healthcare/djLint/issues/124) +- Changed linter profiles to be inclusive vs exclusive for [#178](https://github.com/Riverside-Healthcare/djLint/issues/178) ::: ## 0.7.4 diff --git a/docs/src/docs/configuration.md b/docs/src/docs/configuration.md index 2ad1ed2..5fe0eed 100644 --- a/docs/src/docs/configuration.md +++ b/docs/src/docs/configuration.md @@ -95,13 +95,13 @@ blank_line_after_tag="load,extends,include" ## profile -Set a default profile for the template language. The profile will disable linter rules that do not apply to your template language, and may also change reformatting. For example, in `handlebars` there are no spaces inside {% raw %}`{{#if}}`{% endraw %} tags. +Set a profile for the template language. The profile will enable linter rules that apply to your template language, and may also change reformatting. For example, in `handlebars` there are no spaces inside {% raw %}`{{#if}}`{% endraw %} tags. Options: :::content -- html +- html (default) - django - jinja - nunjucks (for nunjucks and twig) diff --git a/docs/src/docs/getting-started.md b/docs/src/docs/getting-started.md index 5e13252..3f13f46 100644 --- a/docs/src/docs/getting-started.md +++ b/docs/src/docs/getting-started.md @@ -32,7 +32,8 @@ Options: --indent INTEGER Indent spacing. [default: 4] --quiet Do not print diff when reformatting. --profile TEXT Enable defaults by template language. ops: django, - jinja, nunjucks, handlebars, golang + jinja, nunjucks, handlebars, golang, angular, + html [default: html] --require-pragma Only format or lint files that starts with a comment with the text 'djlint:on' --lint Lint for common issues. [default option] diff --git a/docs/src/ru/docs/changelog.md b/docs/src/ru/docs/changelog.md index b6f65ee..9331d92 100644 --- a/docs/src/ru/docs/changelog.md +++ b/docs/src/ru/docs/changelog.md @@ -16,6 +16,7 @@ keywords: облицовка шаблонов, форматер шаблонов - Добавлена улучшенная поддержка ``yaml`` front matter в файлах шаблонов - Добавлено правило T032 для [#123](https://github.com/Riverside-Healthcare/djLint/issues/123) - Добавлено правило H033 для [#124](https://github.com/Riverside-Healthcare/djLint/issues/124) +- Изменены профили линтеров, чтобы они были инклюзивными, а не эксклюзивными для [#178](https://github.com/Riverside-Healthcare/djLint/issues/178) ::: ## 0.7.4 diff --git a/docs/src/ru/docs/configuration.md b/docs/src/ru/docs/configuration.md index 4ba9d81..327b224 100644 --- a/docs/src/ru/docs/configuration.md +++ b/docs/src/ru/docs/configuration.md @@ -95,13 +95,12 @@ blank_line_after_tag="load,extends,include" ## profile -Установите профиль по умолчанию для языка шаблона. Профиль отключает правила линтера, которые не применимы к языку шаблона, а также может изменить переформатирование. Например, в `handlebars` нет пробелов внутри тегов {% raw %}`{{#if}}`{% endraw %}. - +Установите профиль для языка шаблона. Профиль будет включать правила линтера, применимые к языку шаблонов, а также может изменять переформатирование. Например, в `handlebars` нет пробелов внутри тегов {% raw %}`{{#if}}`{% endraw %}. Параметры: :::content -- html +- html (по умолчанию) - django - jinja - nunjucks (для nunjucks и twig) diff --git a/docs/src/ru/docs/getting-started.md b/docs/src/ru/docs/getting-started.md index 956f53b..c63533e 100644 --- a/docs/src/ru/docs/getting-started.md +++ b/docs/src/ru/docs/getting-started.md @@ -32,7 +32,8 @@ Options: --indent INTEGER Indent spacing. [default: 4] --quiet Do not print diff when reformatting. --profile TEXT Enable defaults by template language. ops: django, - jinja, nunjucks, handlebars, golang + jinja, nunjucks, handlebars, golang, angular, + html [default: html] --require-pragma Only format or lint files that starts with a comment with the text 'djlint:on' --lint Lint for common issues. [default option] diff --git a/src/djlint/__init__.py b/src/djlint/__init__.py index 24c887e..f2b9b93 100644 --- a/src/djlint/__init__.py +++ b/src/djlint/__init__.py @@ -72,7 +72,7 @@ from .src import get_src @click.option( "--profile", type=str, - help="Enable defaults by template language. ops: django, jinja, nunjucks, handlebars, golang", + help="Enable defaults by template language. ops: django, jinja, nunjucks, handlebars, golang, angular, html [default: html]", ) @click.option( "--require-pragma", diff --git a/src/djlint/settings.py b/src/djlint/settings.py index cabef65..d65db55 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -211,7 +211,7 @@ class Config: } self.profile_code: List[str] = profile_dict.get( - str(profile or djlint_settings.get("profile", "all")).lower(), [] + str(profile or djlint_settings.get("profile", "html")).lower(), [] ) self.profile: str = str( profile or djlint_settings.get("profile", "all") diff --git a/tests/test_config.py b/tests/test_config.py index e47e7b1..d72497f 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -97,7 +97,7 @@ def test_indent(runner: CliRunner) -> None: def test_exclude(runner: CliRunner) -> None: - result = runner.invoke(djlint, ["tests/config_excludes"]) + result = runner.invoke(djlint, ["tests/config_excludes", "--profile", "django"]) print(result.output) assert """html.html""" in result.output assert """excluded.html""" not in result.output diff --git a/tests/test_linter.py b/tests/test_linter.py index cea279d..db5459e 100644 --- a/tests/test_linter.py +++ b/tests/test_linter.py @@ -7,7 +7,7 @@ run:: # for a single test - pytest tests/test_linter.py::test_H033 --cov=src/djlint --cov-branch \ + pytest tests/test_linter.py::test_T028 --cov=src/djlint --cov-branch \ --cov-report xml:coverage.xml --cov-report term-missing """ @@ -24,45 +24,49 @@ from .conftest import write_to_file def test_T001(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b"{{test }}\n{% test%}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert result.exit_code == 1 assert "T001 1:" in result.output assert "T001 2:" in result.output write_to_file(tmp_file.name, b"{%- test-%}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "nunjucks"]) assert result.exit_code == 1 assert "T001 1:" in result.output write_to_file(tmp_file.name, b"{%-test -%}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "nunjucks"]) assert result.exit_code == 1 assert "T001 1:" in result.output write_to_file(tmp_file.name, b"{%- test -%}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "nunjucks"]) assert result.exit_code == 0 def test_T002(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b"{% extends 'this' %}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert result.exit_code == 1 assert "T002 1:" in result.output def test_T003(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b"{% endblock %}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert result.exit_code == 1 assert "T003 1:" in result.output def test_DJ004(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b'') - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert result.exit_code == 1 assert "D004 1:" in result.output + + write_to_file(tmp_file.name, b'') + result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) + assert result.exit_code == 1 assert "J004 1:" in result.output @@ -228,11 +232,14 @@ def test_DJ018(runner: CliRunner, tmp_file: TextIO) -> None: tmp_file.name, b'\n
', ) - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert result.exit_code == 1 assert "D018 1:" in result.output - assert "J018 1:" in result.output assert "D018 2:" in result.output + + result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) + assert result.exit_code == 1 + assert "J018 1:" in result.output assert "J018 2:" in result.output # test javascript functions @@ -240,11 +247,14 @@ def test_DJ018(runner: CliRunner, tmp_file: TextIO) -> None: tmp_file.name, b'\n
', ) - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) # don't check status code. will fail on other rules here. assert "D018 1:" not in result.output - assert "J018 1:" not in result.output assert "D018 2:" not in result.output + + result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) + # don't check status code. will fail on other rules here. + assert "J018 1:" not in result.output assert "J018 2:" not in result.output # test on_ events @@ -252,11 +262,12 @@ def test_DJ018(runner: CliRunner, tmp_file: TextIO) -> None: tmp_file.name, b'\n
', ) - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert result.exit_code == 0 assert "D018 1:" not in result.output - assert "J018 1:" not in result.output assert "D018 2:" not in result.output + result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) + assert "J018 1:" not in result.output assert "J018 2:" not in result.output # test hash urls @@ -272,9 +283,10 @@ def test_DJ018(runner: CliRunner, tmp_file: TextIO) -> None: tmp_file.name, b'
', ) - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert result.exit_code == 1 assert "D018 1:" in result.output + result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) assert "J018 1:" in result.output # test mailto: @@ -519,16 +531,16 @@ def test_H026(runner: CliRunner, tmp_file: TextIO) -> None: def test_T027(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b"{% blah 'asdf %}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert result.exit_code == 1 assert "T027" in result.output write_to_file(tmp_file.name, b"{% blah 'asdf' %}{{ blah \"asdf\" }}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T027" not in result.output write_to_file(tmp_file.name, b"{% blah 'asdf' 'blah %}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert result.exit_code == 1 assert "T027" in result.output @@ -542,48 +554,48 @@ def test_T027(runner: CliRunner, tmp_file: TextIO) -> None: def test_T028(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b"") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) assert result.exit_code == 1 assert "T028" not in result.output write_to_file(tmp_file.name, b"") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) assert result.exit_code == 1 assert "T028" in result.output # django should not trigger write_to_file(tmp_file.name, b"") - result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "jinja"]) assert result.exit_code == 1 - assert "T028" not in result.output + assert "T028" in result.output write_to_file(tmp_file.name, b"") result = runner.invoke(djlint, [tmp_file.name]) assert "T028" not in result.output write_to_file(tmp_file.name, b"") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T028" not in result.output write_to_file(tmp_file.name, b"") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T028" not in result.output write_to_file(tmp_file.name, b"") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T028" not in result.output write_to_file(tmp_file.name, b"{% blah 'asdf' %}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T028" not in result.output write_to_file(tmp_file.name, b"{% for 'asdf' %}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T028" not in result.output # class should not trigger write_to_file(tmp_file.name, b'') - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T028" not in result.output @@ -630,27 +642,27 @@ def test_H031(runner: CliRunner, tmp_file: TextIO) -> None: def test_T032(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b"{% static '' \" \" 'foo/bar.min.css' %}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T032" in result.output write_to_file(tmp_file.name, b"{% static '' %}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T032" in result.output write_to_file(tmp_file.name, b"{% static '' \" \" 'foo/bar.min.css' %}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T032" not in result.output write_to_file(tmp_file.name, b"{{ static '' \" \" 'foo/bar.min.css' }}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T032" in result.output write_to_file(tmp_file.name, b"{{ static '' }}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T032" in result.output write_to_file(tmp_file.name, b"{{ static '' \" \" 'foo/bar.min.css' }}") - result = runner.invoke(djlint, [tmp_file.name]) + result = runner.invoke(djlint, [tmp_file.name, "--profile", "django"]) assert "T032" not in result.output @@ -704,7 +716,7 @@ def test_rules_not_matched_in_ignored_block( def test_custom_rules(runner: CliRunner, tmp_file: TextIO) -> None: - result = runner.invoke(djlint, ["tests/custom_rules"]) + result = runner.invoke(djlint, ["tests/custom_rules", "--profile", "django"]) assert """Linting""" in result.output assert """1/1""" in result.output assert """T001 1:""" in result.output @@ -712,7 +724,7 @@ def test_custom_rules(runner: CliRunner, tmp_file: TextIO) -> None: def test_custom_rules_bad_config(runner: CliRunner, tmp_file: TextIO) -> None: - result = runner.invoke(djlint, ["tests/custom_rules_bad"]) + result = runner.invoke(djlint, ["tests/custom_rules_bad", "--profile", "django"]) assert """Linting""" in result.output assert """1/1""" in result.output assert """T001 1:""" in result.output