closes #175, updated tests

This commit is contained in:
Christopher Pickering 2022-02-18 13:07:30 -06:00
parent c340cdd3df
commit 2acb8c3c28
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
10 changed files with 61 additions and 46 deletions

View file

@ -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

View file

@ -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)

View file

@ -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]

View file

@ -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

View file

@ -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)

View file

@ -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]

View file

@ -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",

View file

@ -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")

View file

@ -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

View file

@ -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'<link src="/static/there">')
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'<link src="/static/there">')
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'<a href="/Collections?handler=RemoveAgreement&id=@a.Id">\n<form action="/Collections"></form></a>',
)
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'<a href="javascript:abc()">\n<form action="javascript:abc()"></form></a>',
)
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'<a href="onclick:abc()">\n<form action="onclick:abc()"></form></a>',
)
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'<div class="em-ajaxLogs" data-src="/table/task/{{ t.id }}/log"></div>',
)
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"<a href=\"{% blah 'asdf' -%}\">")
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"<a href=\"{%- if 'asdf' %}\">")
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"<a href=\"{%- if 'asdf' %}\">")
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"<a href=\"{{- blah 'asdf' }}\">")
result = runner.invoke(djlint, [tmp_file.name])
assert "T028" not in result.output
write_to_file(tmp_file.name, b"<a href=\"{{ 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"<a {{ 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"<a {% 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"{% 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'<input class="{% if %}{% endif %}" />')
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