Merge pull request #246 from Riverside-Healthcare/dev

test(restructured): restructured the tests
This commit is contained in:
sur.la.route 2022-05-18 08:58:41 -05:00 committed by GitHub
commit 7c7842ea60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
167 changed files with 9041 additions and 1836 deletions

9
.editorconfig Normal file
View file

@ -0,0 +1,9 @@
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

View file

@ -1,57 +0,0 @@
name: Build and Format [Weekly]
on:
schedule:
- cron: 0 0 * * 0
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
with:
fetch-depth: '0'
- name: Update python deps
run: |
python -m pip install tox poetry tox-poetry poetry-up
poetry-up --install --no-commit
- name: python format
run: |
python -m pip install tox poetry tox-poetry
tox -e isort,black
- name: Pre-commit update
run: |
pip install pre-commit
pre-commit install
pre-commit autoupdate
- name: Update npm packages
run: |
cd docs
npm install
npm install -g npm-check-updates
# update deps
ncu -u
# update package lock
npm install
- name: npm format
run: |
cd docs
npm run format
- name: Stage, commit and push files
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_user_name: djlint-bot
commit_user_email: djlint-bot@users.noreply.github.com
commit_author: djlint-bot <djlint-bot@users.noreply.github.com>
commit_message: 'chore(build): update packages [Weekly]'
continue-on-error: true

26
poetry.lock generated
View file

@ -77,6 +77,22 @@ tomli = {version = "*", optional = true, markers = "extra == \"toml\""}
[package.extras]
toml = ["tomli"]
[[package]]
name = "html-tag-names"
version = "0.1.2"
description = "List of known HTML tag names"
category = "main"
optional = false
python-versions = ">=3.7,<4.0"
[[package]]
name = "html-void-elements"
version = "0.1.0"
description = "List of HTML void tag names."
category = "main"
optional = false
python-versions = ">=3.7,<4.0"
[[package]]
name = "importlib-metadata"
version = "4.11.0"
@ -300,7 +316,7 @@ test = ["coverage", "pytest", "pytest-cov"]
[metadata]
lock-version = "1.1"
python-versions = "^3.7,<4.0"
content-hash = "b9e4cbf6e84631a25896da5849568d316919a5e1b5880d210fde7818c864ddb4"
content-hash = "a72c15aeda26733105d97f016255c240b0180f09b404ffde3497271717b4f103"
[metadata.files]
atomicwrites = [
@ -387,6 +403,14 @@ coverage = [
{file = "coverage-6.3.1-pp36.pp37.pp38-none-any.whl", hash = "sha256:463e52616ea687fd323888e86bf25e864a3cc6335a043fad6bbb037dbf49bbe2"},
{file = "coverage-6.3.1.tar.gz", hash = "sha256:6c3f6158b02ac403868eea390930ae64e9a9a2a5bbfafefbb920d29258d9f2f8"},
]
html-tag-names = [
{file = "html-tag-names-0.1.2.tar.gz", hash = "sha256:04924aca48770f36b5a41c27e4d917062507be05118acb0ba869c97389084297"},
{file = "html_tag_names-0.1.2-py3-none-any.whl", hash = "sha256:eeb69ef21078486b615241f0393a72b41352c5219ee648e7c61f5632d26f0420"},
]
html-void-elements = [
{file = "html-void-elements-0.1.0.tar.gz", hash = "sha256:931b88f84cd606fee0b582c28fcd00e41d7149421fb673e1e1abd2f0c4f231f0"},
{file = "html_void_elements-0.1.0-py3-none-any.whl", hash = "sha256:784cf39db03cdeb017320d9301009f8f3480f9d7b254d0974272e80e0cb5e0d2"},
]
importlib-metadata = [
{file = "importlib_metadata-4.11.0-py3-none-any.whl", hash = "sha256:6affcdb3aec542dd98df8211e730bba6c5f2bec8288d47bacacde898f548c9ad"},
{file = "importlib_metadata-4.11.0.tar.gz", hash = "sha256:9e5e553bbba1843cb4a00823014b907616be46ee503d2b9ba001d214a8da218f"},

View file

@ -41,6 +41,8 @@ pytest = { version = "^7.0.1", optional = true }
pytest-cov = { version = "^3.0.0", optional = true }
pathspec = "^0.9.0"
importlib-metadata = "^4.11.0"
html-void-elements = "^0.1.0"
html-tag-names = "^0.1.2"
[tool.poetry.dev-dependencies]
black = "^22.1.0"

View file

@ -1,6 +1,8 @@
black==22.1.0; python_full_version >= "3.6.2"
click==8.0.3; python_version >= "3.6"
colorama==0.4.4; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.5.0")
html-tag-names==0.1.2; python_version >= "3.7" and python_version < "4.0"
html-void-elements==0.1.0; python_version >= "3.7" and python_version < "4.0"
importlib-metadata==4.11.0; python_version >= "3.7"
isort==5.10.1; python_full_version >= "3.6.1" and python_version < "4.0"
mypy-extensions==0.4.3; python_full_version >= "3.6.2"

View file

@ -3,3 +3,6 @@ ignore_missing_imports = True
[flake8]
ignore = D103, F401, E501, W503, SIM114, D403, RST219, RST299
per-file-ignores =
tests/*: S404,S101,E800,S607,N802,T201,S603,B001,E722
**/__init__.py: D104

View file

@ -128,7 +128,7 @@ def main(
temp_file = None
if "-" in src:
stdin_stream = click.get_text_stream("stdin")
stdin_stream = click.get_text_stream("stdin", encoding="utf8")
stdin_text = stdin_stream.read()
temp_file = tempfile.NamedTemporaryFile(delete=False)

View file

@ -133,14 +133,14 @@
message: (Django) Internal links should use the {% url ... %} pattern.
flags: re.DOTALL|re.I
patterns:
- <(?:a|div|span|input)\s+?[^>]*?(?:href|data-url|data-src|action)=[\"|'](?!(?:https?://)|javascript:|on\w+:|mailto:)[\w|/]+
- <(?:a|div|span|input)\b[^>]*?\s(?:href|data-url|data-src|action)=[\"|'](?!(?:https?://)|javascript:|on\w+:|mailto:)[\w|/]+
- <form\s+?[^>]*?(?:action)=[\"|'](?!(?:https?://)|javascript:|on\w+:|mailto:)[\w|/]+
- rule:
name: J018
message: (Jinja) Internal links should use the {{ url_for() ... }} pattern.
flags: re.DOTALL|re.I
patterns:
- <(?:a|div|span|input)\s+?[^>]*?(?:href|data-url|data-src|action)=[\"|'](?!(?:https?://)|javascript:|on\w+:|mailto:)[\w|/]+
- <(?:a|div|span|input)\b[^>]*?\s(?:href|data-url|data-src|action)=[\"|'](?!(?:https?://)|javascript:|on\w+:|mailto:)[\w|/]+
- <form\s+?[^>]*?(?:action)=[\"|'](?!(?:https?://)|javascript:|on\w+:|mailto:)[\w|/]+
- rule:
name: H019

View file

@ -13,6 +13,8 @@ from typing import Dict, List, Optional, Union
import yaml
from click import echo
from colorama import Fore
from HtmlTagNames import html_tag_names
from HtmlVoidElements import html_void_elements
from pathspec import PathSpec
from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
@ -364,134 +366,7 @@ class Config:
"""
# all html tags possible
self.indent_html_tags: str = (
r""" a
| abbr
| acronym
| address
| applet
| area
| article
| aside
| audio
| b
| base
| basefont
| bdi
| bdo
| big
| blockquote
| body
| br
| button
| canvas
| caption
| center
| cite
| code
| col
| colgroup
| data
| datalist
| dd
| del
| details
| dfn
| dialog
| dir
| div
| dl
| dt
| em
| embed
| fieldset
| figcaption
| figure
| font
| footer
| form
| frame
| frameset
| h1
| h2
| h3
| h4
| h5
| h6
| head
| header
| hr
| html
| i
| iframe
| icon
| img
| input
| ins
| kbd
| label
| legend
| li
| link
| main
| map
| mark
| meta
| meter
| nav
| noframes
| noscript
| object
| ol
| optgroup
| option
| output
| p
| pre
| path
| param
| picture
| progress
| q
| rp
| rt
| ruby
| s
| samp
| script
| section
| select
| small
| source
| span
| strike
| strong
| style
| sub
| summary
| sup
| svg
| table
| tbody
| td
| template
| textarea
| tfoot
| th
| thead
| time
| title
| tr
| track
| tt
| u
| ul
| var
| video
| wbr
"""
+ self.custom_html
)
self.indent_html_tags: str = "|".join(html_tag_names) + self.custom_html
self.indent_template_tags: str = (
r""" if
@ -726,22 +601,12 @@ class Config:
| li
| script
| style
| head
| body
| p
"""
self.always_self_closing_html_tags: str = r"""
link
| img
| meta
| source
| br
| input
| hr
| area
| col
| embed
| track
| wbr
"""
self.always_self_closing_html_tags: str = "|".join(html_void_elements)
self.optional_single_line_template_tags: str = r"""
if

View file

@ -1 +0,0 @@
{% example stuff %}<p>this is a long paragraph</p>{% endexample %}

View file

@ -1,4 +1,5 @@
"""Djlint test config."""
# pylint: disable=W0621,C0116
import os
import tempfile
from pathlib import Path
@ -37,5 +38,8 @@ def reformat(the_file: TextIO, runner: CliRunner, the_text: bytes) -> SimpleName
write_to_file(the_file.name, the_text)
result = runner.invoke(djlint, [the_file.name, "--reformat"])
return SimpleNamespace(
**{"text": Path(the_file.name).read_text(), "exit_code": result.exit_code}
**{
"text": Path(the_file.name).read_text(encoding="utf8"),
"exit_code": result.exit_code,
}
)

View file

@ -1,332 +0,0 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from src.djlint import main as djlint
from .conftest import reformat
def test_custom_tags(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_custom_tags/html.html", "--check"])
assert (
"""-{% example stuff %}<p>this is a long paragraph</p>{% endexample %}
+{% example stuff %}
+ <p>
+ this is a long paragraph
+ </p>
+{% endexample %}
"""
in result.output
)
assert result.exit_code == 1
def test_custom_html(runner: CliRunner, tmp_file: TextIO) -> None:
result = runner.invoke(djlint, ["tests/config_custom_html/html.html", "--check"])
print(result.output)
assert (
"""-<mjml><mj-body>this is a email text</mj-body></mjml>
+<mjml>
+ <mj-body>
+ this is a email text
+ </mj-body>
+</mjml>
"""
in result.output
)
assert result.exit_code == 1
# https://github.com/Riverside-Healthcare/djLint/issues/236
output = reformat(tmp_file, runner, b"<some-long-custom-element></some-long-custom-element>\n")
assert output.exit_code == 0
def test_extension(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_extension", "--check"])
assert """Checking""" in result.output
assert """1/1""" in result.output
assert """0 files would be updated.""" in result.output
assert result.exit_code == 0
def test_ignores(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_ignores"])
assert """Linted 1 file, found 0 errors.""" in result.output
assert result.exit_code == 0
def test_indent(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_indent", "--check"])
print(result.output)
assert (
"""-<section><p><div><span></span></div></p></section>
+<section>
+ <p>
+ <div>
+ <span></span>
+ </div>
+ </p>
+</section>"""
in result.output
)
assert result.exit_code == 1
result = runner.invoke(djlint, ["tests/config_indent", "--check", "--indent", 3])
assert (
"""-<section><p><div><span></span></div></p></section>
+<section>
+ <p>
+ <div>
+ <span></span>
+ </div>
+ </p>
+</section>"""
in result.output
)
assert result.exit_code == 1
def test_exclude(runner: CliRunner) -> None:
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
assert """foo/excluded.html""" not in result.output
assert result.exit_code == 1
def test_blank_lines_after_tag(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/config_blank_lines_after_tag/html.html", "--check"]
)
assert (
"""+{% extends "nothing.html" %}
+
+{% load stuff %}
+{% load stuff 2 %}
+
+{% include "html_two.html" %}
+
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint, ["tests/config_blank_lines_after_tag/html_two.html", "--check"]
)
assert (
""" {% load stuff %}
+
<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
# check blocks that do not start on a newline - they should be left as is.
result = runner.invoke(
djlint, ["tests/config_blank_lines_after_tag/html_three.html", "--check"]
)
assert """0 files would be updated.""" in result.output
assert result.exit_code == 0
result = runner.invoke(
djlint, ["tests/config_blank_lines_after_tag/html_four.html", "--check"]
)
assert result.exit_code == 1
assert (
""" {% block this %}
-{% load i18n %}
+ {% load i18n %}
+
{% endblock this %}
"""
in result.output
)
# something perfect should stay perfect :)
result = runner.invoke(
djlint, ["tests/config_blank_lines_after_tag/html_five.html", "--check"]
)
assert result.exit_code == 0
# something perfect should stay perfect :)
result = runner.invoke(
djlint, ["tests/config_blank_lines_after_tag/html_six.html", "--check"]
)
assert result.exit_code == 0
# make sure endblock doesn't pick up endblocktrans :)
result = runner.invoke(
djlint, ["tests/config_blank_lines_after_tag/html_seven.html", "--check"]
)
assert result.exit_code == 0
def test_profile(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_profile/html.html"])
assert "T001" in result.output
assert "J018" not in result.output
assert "D018" in result.output
result = runner.invoke(
djlint, ["tests/config_profile/html.html", "--profile", "jinja"]
)
assert "T001" in result.output
assert "J018" in result.output
assert "D018" not in result.output
result = runner.invoke(
djlint, ["tests/config_profile/html.html", "--profile", "handlebars"]
)
assert "T001" not in result.output
assert "J018" not in result.output
assert "D018" not in result.output
result = runner.invoke(
djlint, ["tests/config_profile/html.html", "--check", "--profile", "handlebars"]
)
assert result.exit_code == 0
result = runner.invoke(
djlint, ["tests/config_profile/html.html", "--check", "--profile", "jinja"]
)
assert result.exit_code == 1
assert (
"""-{{test}}
+{{ test }}"""
in result.output
)
def test_require_pragma(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
[
"tests/config_pragmas/html_one.html",
"--lint",
"--check",
"--profile",
"django",
],
)
assert """No files to check!""" in result.output
assert result.exit_code == 0
result = runner.invoke(
djlint, ["tests/config_pragmas/html_two.html", "--check", "--profile", "django"]
)
assert (
""" {# djlint:on #}
-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>
+{% extends "nothing.html" %}
+{% load stuff %}
+{% load stuff 2 %}
+{% include "html_two.html" %}
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint,
["tests/config_pragmas/html_three.html", "--check", "--profile", "handlebars"],
)
assert (
""" {{!-- djlint:on --}}
<p>
-
-{{firstname}} </p><p>{{lastname}}</p>
+ {{firstname}}
+</p>
+<p>
+ {{lastname}}
+</p>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint,
["tests/config_pragmas/html_four.html", "--check", "--profile", "golang"],
)
assert (
""" {{ /* djlint:on */ }}
-<h1>Test</h1><p>{{ .Variable }}</p>
-{{ range .Items }} <p>{{ . }}
-
-</p>{{ end }}
+<h1>Test</h1>
+<p>
+ {{ .Variable }}
+</p>
+{{ range .Items }}
+<p>
+ {{ . }}
+</p>
+{{ end }}
1 file would be updated."""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(djlint, ["tests/config_pragmas/html_five.html", "--check"])
assert (
""" <!-- djlint:on -->
-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>
+{% extends "nothing.html" %}
+{% load stuff %}
+{% load stuff 2 %}
+{% include "html_two.html" %}
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint, ["tests/config_pragmas/html_six.html", "--check", "--profile", "django"]
)
assert (
""" {% comment %} djlint:on {% endcomment %}
-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>
+{% extends "nothing.html" %}
+{% load stuff %}
+{% load stuff 2 %}
+{% include "html_two.html" %}
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1

View file

View file

@ -0,0 +1,96 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from click.testing import CliRunner
from src.djlint import main as djlint
def test_blank_lines_after_tag(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/test_config/test_blank_lines_after_tag/html.html", "--check"]
)
assert (
"""+{% extends "nothing.html" %}
+
+{% load stuff %}
+{% load stuff 2 %}
+
+{% include "html_two.html" %}
+
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_after_tag/html_two.html", "--check"],
)
assert (
""" {% load stuff %}
+
<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
# check blocks that do not start on a newline - they should be left as is.
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_after_tag/html_three.html", "--check"],
)
assert """0 files would be updated.""" in result.output
assert result.exit_code == 0
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_after_tag/html_four.html", "--check"],
)
assert result.exit_code == 1
assert (
""" {% block this %}
-{% load i18n %}
+ {% load i18n %}
+
{% endblock this %}
"""
in result.output
)
# something perfect should stay perfect :)
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_after_tag/html_five.html", "--check"],
)
assert result.exit_code == 0
# something perfect should stay perfect :)
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_after_tag/html_six.html", "--check"],
)
assert result.exit_code == 0
# make sure endblock doesn't pick up endblocktrans :)
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_after_tag/html_seven.html", "--check"],
)
assert result.exit_code == 0

View file

@ -0,0 +1,45 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config/test_custom_html/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from src.djlint import main as djlint
from tests.conftest import reformat
def test_custom_html(runner: CliRunner, tmp_file: TextIO) -> None:
result = runner.invoke(
djlint, ["tests/test_config/test_custom_html/html.html", "--check"]
)
print(result.output)
assert (
"""-<mjml><mj-body>this is a email text</mj-body></mjml>
+<mjml>
+ <mj-body>
+ this is a email text
+ </mj-body>
+</mjml>
"""
in result.output
)
assert result.exit_code == 1
# https://github.com/Riverside-Healthcare/djLint/issues/236
output = reformat(
tmp_file, runner, b"<some-long-custom-element></some-long-custom-element>\n"
)
assert output.exit_code == 0

View file

@ -0,0 +1 @@
{% example stuff %}<p>this is a very very long paragraph that does nothing except be a long paragraph asdfasdfasdfasdfasdf fasdf asdfasdfasdf</p>{% endexample %}

View file

@ -0,0 +1,36 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config/test_custom_tags/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config/test_custom_tags/test_config.py::test_custom_tags
"""
# pylint: disable=C0116
from click.testing import CliRunner
from src.djlint import main as djlint
def test_custom_tags(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/test_config/test_custom_tags/html.html", "--check"]
)
print(result.output)
assert (
"""-{% example stuff %}<p>this is a very very long paragraph that does nothing except be a long paragraph asdfasdfasdfasdfasdf fasdf asdfasdfasdf</p>{% endexample %}
+{% example stuff %}
+ <p>
+ this is a very very long paragraph that does nothing except be a long paragraph asdfasdfasdfasdfasdf fasdf asdfasdfasdf
+ </p>
+{% endexample %}
"""
in result.output
)
assert result.exit_code == 1

View file

@ -0,0 +1,30 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from click.testing import CliRunner
from src.djlint import main as djlint
def test_exclude(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/test_config/test_excludes", "--profile", "django"]
)
print(result.output)
assert """html.html""" in result.output
assert """excluded.html""" not in result.output
assert """foo/excluded.html""" not in result.output
assert result.exit_code == 1

View file

@ -0,0 +1,27 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from click.testing import CliRunner
from src.djlint import main as djlint
def test_extension(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/test_config/test_extension", "--check"])
assert """Checking""" in result.output
assert """1/1""" in result.output
assert """0 files would be updated.""" in result.output
assert result.exit_code == 0

View file

@ -18,13 +18,12 @@ from typing import TextIO
from click.testing import CliRunner
from src.djlint import main as djlint
from .conftest import reformat
from tests.conftest import reformat
def test_with_config(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/config_format_attribute_template_tags", "--check"]
djlint, ["tests/test_config/test_format_attribute_template_tags", "--check"]
)
print(result.output)
assert """0 files would be updated.""" in result.output

View file

@ -0,0 +1,140 @@
"""Djlint tests specific to gitignore configuration.
run::
pytest tests/test_config_gitignore.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config_gitignore.py::test_ignored_path --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116,W0702
import os
import shutil
from pathlib import Path
import pytest
from click.testing import CliRunner
from src.djlint import main as djlint
@pytest.mark.xdist_group(name="group1")
def test_cli(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/test_config/test_gitignore/html_two.html", "--lint"]
)
assert result.exit_code == 1
# create .git folder to make root
Path("tests/test_config/test_gitignore/.git").mkdir(parents=True, exist_ok=True)
# add a gitignore file
with open(
"tests/test_config/test_gitignore/.gitignore", "w", encoding="utf8"
) as git:
git.write("html_two.html")
result = runner.invoke(
djlint,
[
"tests/test_config/test_gitignore/html_two.html",
"--check",
"--use-gitignore",
],
)
assert result.exit_code == 0
result = runner.invoke(
djlint, ["tests/test_config/test_gitignore/html_two.html", "--check"]
)
assert result.exit_code == 1
try:
os.remove("tests/test_config/test_gitignore/.gitignore")
shutil.rmtree("tests/test_config/test_gitignore/.git")
except:
print("cleanup failed")
@pytest.mark.xdist_group(name="group1")
def test_pyproject(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/test_config/test_gitignore/html_two.html", "--check"]
)
assert result.exit_code == 1
# make a root
Path("tests/test_config/test_gitignore/.git").mkdir(parents=True, exist_ok=True)
# add a gitignore file
with open(
"tests/test_config/test_gitignore/.gitignore", "w", encoding="utf8"
) as git:
git.write("html_two.html")
with open(
"tests/test_config/test_gitignore/pyproject.toml", "w", encoding="utf8"
) as git:
git.write("[tool]\n[tool.djlint]\nuse_gitignore=true")
result = runner.invoke(
djlint, ["tests/test_config/test_gitignore/html_two.html", "--check"]
)
assert result.exit_code == 0
with open(
"tests/test_config/test_gitignore/pyproject.toml", "w", encoding="utf8"
) as git:
git.write("[tool]\n[tool.djlint]\nuse_gitignore=false")
result = runner.invoke(
djlint, ["tests/test_config/test_gitignore/html_two.html", "--check"]
)
assert result.exit_code == 1
# verify cli overrides pyproject
result = runner.invoke(
djlint,
[
"tests/test_config/test_gitignore/html_two.html",
"--check",
"--use-gitignore",
],
)
assert result.exit_code == 0
try:
os.remove("tests/test_config/test_gitignore/.gitignore")
os.remove("tests/test_config/test_gitignore/pyproject.toml")
shutil.rmtree("tests/test_config/test_gitignore/.git")
except:
print("cleanup failed")
@pytest.mark.xdist_group(name="group1")
def test_ignored_path(runner: CliRunner) -> None:
# test for https://github.com/Riverside-Healthcare/djLint/issues/224
# create .git folder to make root
Path("tests/test_config/test_gitignore/.git").mkdir(parents=True, exist_ok=True)
# add a gitignore file
with open(
"tests/test_config/test_gitignore/.gitignore", "w", encoding="utf8"
) as git:
git.write("var")
result = runner.invoke(
djlint, ["-", "--use-gitignore"], input='<div><p id="a"></p></div>'
)
print(result.output)
assert result.exit_code == 0
assert "Linted 1 file" in result.output
try:
os.remove("tests/test_config/test_gitignore/.gitignore")
shutil.rmtree("tests/test_config/test_gitignore/.git")
except:
print("cleanup failed")

View file

@ -0,0 +1,25 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from click.testing import CliRunner
from src.djlint import main as djlint
def test_ignores(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/test_config/test_ignores"])
assert """Linted 1 file, found 0 errors.""" in result.output
assert result.exit_code == 0

View file

@ -0,0 +1,53 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from click.testing import CliRunner
from src.djlint import main as djlint
def test_indent(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/test_config/test_indent", "--check"])
print(result.output)
assert (
"""-<section><p><div><span></span></div></p></section>
+<section>
+ <p>
+ <div>
+ <span></span>
+ </div>
+ </p>
+</section>"""
in result.output
)
assert result.exit_code == 1
result = runner.invoke(
djlint, ["tests/test_config/test_indent", "--check", "--indent", 3] # type: ignore
)
assert (
"""-<section><p><div><span></span></div></p></section>
+<section>
+ <p>
+ <div>
+ <span></span>
+ </div>
+ </p>
+</section>"""
in result.output
)
assert result.exit_code == 1

View file

View file

@ -2,13 +2,10 @@
run::
pytest tests/test_config_json.py --cov=src/djlint --cov-branch \
pytest tests/test_config/test_json/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config_json.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_config/test_json/test_config.py::test_config
"""
# pylint: disable=C0116
@ -19,16 +16,14 @@ from src.djlint import main as djlint
def test_config(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_json/html.html", "--check"])
result = runner.invoke(djlint, ["tests/test_config/test_json/html.html", "--check"])
print(result.output)
assert (
"""-{% example stuff %}<p>this is a long paragraph</p>{% endexample %}
+{% example stuff %}
+ <p>
+ this is a long paragraph
+ </p>
+ <p>this is a long paragraph</p>
+{% endexample %}
"""
in result.output

View file

@ -19,7 +19,9 @@ from src.djlint import main as djlint
def test_with_config(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_linter_output_format", "--lint"])
result = runner.invoke(
djlint, ["tests/test_config/test_linter_output_format", "--lint"]
)
assert result.exit_code == 1
print(result.output)

View file

@ -0,0 +1,142 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config/test_pragmas/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_config/test_pragmas/test_config.py::test_require_pragma
"""
# pylint: disable=C0116
from click.testing import CliRunner
from src.djlint import main as djlint
def test_require_pragma(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
[
"tests/test_config/test_pragmas/html_one.html",
"--lint",
"--check",
"--profile",
"django",
],
)
assert """No files to check!""" in result.output
assert result.exit_code == 0
result = runner.invoke(
djlint,
[
"tests/test_config/test_pragmas/html_two.html",
"--check",
"--profile",
"django",
],
)
assert (
""" {# djlint:on #}
-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>
+{% extends "nothing.html" %}
+{% load stuff %}
+{% load stuff 2 %}
+{% include "html_two.html" %}
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint,
[
"tests/test_config/test_pragmas/html_three.html",
"--check",
"--profile",
"handlebars",
],
)
assert (
""" {{!-- djlint:on --}}
-<p>
-
-{{firstname}} </p><p>{{lastname}}</p>
+<p>{{firstname}}</p>
+<p>{{lastname}}</p>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint,
[
"tests/test_config/test_pragmas/html_four.html",
"--check",
"--profile",
"golang",
],
)
assert (
""" {{ /* djlint:on */ }}
-<h1>Test</h1><p>{{ .Variable }}</p>
-{{ range .Items }} <p>{{ . }}
-
-</p>{{ end }}
+<h1>Test</h1>
+<p>{{ .Variable }}</p>
+{{ range .Items }}
+<p>{{ . }}</p>
+{{ end }}
1 file would be updated."""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint, ["tests/test_config/test_pragmas/html_five.html", "--check"]
)
assert (
""" <!-- djlint:on -->
-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>
+{% extends "nothing.html" %}
+{% load stuff %}
+{% load stuff 2 %}
+{% include "html_two.html" %}
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint,
[
"tests/test_config/test_pragmas/html_six.html",
"--check",
"--profile",
"django",
],
)
assert (
""" {% comment %} djlint:on {% endcomment %}
-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>
+{% extends "nothing.html" %}
+{% load stuff %}
+{% load stuff 2 %}
+{% include "html_two.html" %}
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1

View file

@ -0,0 +1,64 @@
"""Djlint tests specific to pyproject.toml configuration.
run::
pytest tests/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from click.testing import CliRunner
from src.djlint import main as djlint
def test_profile(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/test_config/test_profile/html.html"])
assert "T001" in result.output
assert "J018" not in result.output
assert "D018" in result.output
result = runner.invoke(
djlint, ["tests/test_config/test_profile/html.html", "--profile", "jinja"]
)
assert "T001" in result.output
assert "J018" in result.output
assert "D018" not in result.output
result = runner.invoke(
djlint, ["tests/test_config/test_profile/html.html", "--profile", "handlebars"]
)
assert "T001" not in result.output
assert "J018" not in result.output
assert "D018" not in result.output
result = runner.invoke(
djlint,
[
"tests/test_config/test_profile/html.html",
"--check",
"--profile",
"handlebars",
],
)
assert result.exit_code == 0
result = runner.invoke(
djlint,
["tests/test_config/test_profile/html.html", "--check", "--profile", "jinja"],
)
assert result.exit_code == 1
assert (
"""-{{test}}
+{{ test }}"""
in result.output
)

View file

@ -1,94 +0,0 @@
"""Djlint tests specific to gitignore configuration.
run::
pytest tests/test_config_gitignore.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_config_gitignore.py::test_ignored_path --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
import os
import shutil
from pathlib import Path
from click.testing import CliRunner
from src.djlint import main as djlint
def test_cli(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_gitignore/html_two.html", "--lint"])
assert result.exit_code == 1
# create .git folder to make root
Path("tests/config_gitignore/.git").mkdir(parents=True, exist_ok=True)
# add a gitignore file
with open("tests/config_gitignore/.gitignore", "w") as git:
git.write("html_two.html")
result = runner.invoke(
djlint, ["tests/config_gitignore/html_two.html", "--check", "--use-gitignore"]
)
assert result.exit_code == 0
result = runner.invoke(djlint, ["tests/config_gitignore/html_two.html", "--check"])
assert result.exit_code == 1
os.remove("tests/config_gitignore/.gitignore")
shutil.rmtree("tests/config_gitignore/.git")
def test_pyproject(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_gitignore/html_two.html", "--check"])
assert result.exit_code == 1
# make a root
Path("tests/config_gitignore/.git").mkdir(parents=True, exist_ok=True)
# add a gitignore file
with open("tests/config_gitignore/.gitignore", "w") as git:
git.write("html_two.html")
with open("tests/config_gitignore/pyproject.toml", "w") as git:
git.write("[tool]\n[tool.djlint]\nuse_gitignore=true")
result = runner.invoke(djlint, ["tests/config_gitignore/html_two.html", "--check"])
assert result.exit_code == 0
with open("tests/config_gitignore/pyproject.toml", "w") as git:
git.write("[tool]\n[tool.djlint]\nuse_gitignore=false")
result = runner.invoke(djlint, ["tests/config_gitignore/html_two.html", "--check"])
assert result.exit_code == 1
# verify cli overrides pyproject
result = runner.invoke(
djlint, ["tests/config_gitignore/html_two.html", "--check", "--use-gitignore"]
)
assert result.exit_code == 0
os.remove("tests/config_gitignore/.gitignore")
os.remove("tests/config_gitignore/pyproject.toml")
shutil.rmtree("tests/config_gitignore/.git")
def test_ignored_path(runner: CliRunner) -> None:
# test for https://github.com/Riverside-Healthcare/djLint/issues/224
# create .git folder to make root
Path("tests/config_gitignore/.git").mkdir(parents=True, exist_ok=True)
# add a gitignore file
with open("tests/config_gitignore/.gitignore", "w") as git:
git.write("var")
result = runner.invoke(djlint, ["-", "--use-gitignore"], input='<div><p id="a"></p></div>')
print(result.output)
assert result.exit_code == 0
assert "Linted 1 file" in result.output
os.remove("tests/config_gitignore/.gitignore")
shutil.rmtree("tests/config_gitignore/.git")

View file

@ -1,454 +0,0 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from .conftest import reformat
def test_empty_tags_on_one_line(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(tmp_file, runner, b"{% if stuff %}\n{% endif %}")
assert output.text == """{% if stuff %}{% endif %}\n"""
assert output.exit_code == 1
def test_dj_comments_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"{# comment #}\n{% if this %}<div></div>{% endif %}"
)
assert output.text == """{# comment #}\n{% if this %}<div></div>{% endif %}\n"""
# no change was required
assert output.exit_code == 0
def test_reformat_asset_tag(runner: CliRunner, tmp_file: TextIO) -> None:
# pylint: disable=C0301
output = reformat(
tmp_file,
runner,
b"""{% block css %}{% assets "css_error" %}<link type="text/css" rel="stylesheet" href="{{ ASSET_URL }}" />{% endassets %}{% endblock css %}""",
) # noqa: E501
assert (
output.text
== """{% block css %}
{% assets "css_error" %}
<link type="text/css" rel="stylesheet" href="{{ ASSET_URL }}" />
{% endassets %}
{% endblock css %}
"""
)
assert output.exit_code == 1
def test_alpine_js(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"""<div id="collapse"
x-data="{ show: true }"
x-show="show"
x-transition.duration.500ms></div>"""
)
assert output.exit_code == 0
def test_autoescape(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"{% autoescape on %}{{ body }}{% endautoescape %}"
)
assert output.exit_code == 1
assert (
output.text
== r"""{% autoescape on %}
{{ body }}
{% endautoescape %}
"""
)
def test_comment(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"""{% comment "Optional note" %}{{ body }}{% endcomment %}"""
)
assert output.exit_code == 0
# too short to put on multiple lines
assert (
output.text
== r"""{% comment "Optional note" %}{{ body }}{% endcomment %}
"""
)
output = reformat(
tmp_file,
runner,
b"""<div class="hi">
<div class="poor">
<p class="format">
Lorem ipsum dolor
<span class="bold">sit</span>
amet
</p>
<img src="./pic.jpg">
</div>
<script src="file1.js"></script>
{% comment %} <script src="file2.js"></script>
<script src="file3.js"></script> {% endcomment %}
<script src="file4.js"></script>
</div>""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<div class="hi">
<div class="poor">
{# djlint:off #}
<p class="format">
Lorem ipsum dolor <span class="bold">sit</span> amet
</p>
{# djlint:on #}
<img src="./pic.jpg">
</div>
<ul>
{% for i in items %}
<li>item {{i}}</li>
{% if i > 10 %}{% endif %}
<li>item {{i}}</li>
{% endfor %}
</ul>
</div>
""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<html>
<head>
<script src="file1.js"></script>
{% comment %}
<script src="file2.js"></script>
<script src="file3.js"></script>
<script src="file4.js"></script>
{% endcomment %}
<script src="file5.js"></script>
</head>
<body>
</body>
</html>
""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<html>
<head>
<script src="file1.js"></script>
{# djlint:off #}
{% comment %}
<script src="file2.js"></script>
<script src="file3.js"></script>
<script src="file4.js"></script>
{% endcomment %}
{# djlint:on #}
<script src="file5.js"></script>
</head>
<body>
</body>
</html>
""",
)
assert output.exit_code == 0
def test_inline_comment(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"{# <div></div> #}\n{% if this %}<div></div>{% endif %}"
)
assert output.text == """{# <div></div> #}\n{% if this %}<div></div>{% endif %}\n"""
assert output.exit_code == 0
def test_for_loop(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""<ul>{% for athlete in athlete_list %}<li>{{ athlete.name }}</li>{% empty %}<li>Sorry, no athletes in this list.</li>{% endfor %}</ul>""",
)
assert output.exit_code == 1
assert (
output.text
== r"""<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
<li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>
"""
)
def test_filter(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% filter force_escape|lower %}This text will be HTML-escaped, and will appear in all lowercase.{% endfilter %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% filter force_escape|lower %}
This text will be HTML-escaped, and will appear in all lowercase.
{% endfilter %}
"""
)
def test_if(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% if athlete_list %}Number of athletes: {{ athlete_list|length }}{% elif athlete_in_locker_room_list %}Athletes should be out of the locker room soon!{% else %}No athletes.{% endif %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
Athletes should be out of the locker room soon!
{% else %}
No athletes.
{% endif %}
"""
)
def test_ifchanged(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% for match in matches %}<div style="background-color:"pink">{% ifchanged match.ballot_id %}{% cycle "red" "blue" %}{% else %}gray{% endifchanged %}{{ match }}</div>{% endfor %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% for match in matches %}
<div style="background-color:"pink">
{% ifchanged match.ballot_id %}
{% cycle "red" "blue" %}
{% else %}
gray
{% endifchanged %}
{{ match }}
</div>
{% endfor %}
"""
)
def test_include(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(tmp_file, runner, b"""{% include "this" %}{% include "that"%}""")
assert output.exit_code == 1
assert (
output.text
== r"""{% include "this" %}
{% include "that" %}
"""
)
def test_spaceless(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% spaceless %}<p><a href="foo/">Foo</a></p>{% endspaceless %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% spaceless %}
<p>
<a href="foo/">Foo</a>
</p>
{% endspaceless %}
"""
)
def test_templatetag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% templatetag openblock %} url 'entry_list' {% templatetag closeblock %}""",
)
assert output.exit_code == 0
assert (
output.text
== r"""{% templatetag openblock %} url 'entry_list' {% templatetag closeblock %}
"""
)
def test_verbatim(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"""{% verbatim %}Still alive.{% endverbatim %}"""
)
assert output.exit_code == 1
assert (
output.text
== r"""{% verbatim %}
Still alive.
{% endverbatim %}
"""
)
def test_blocktranslate(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% blocktranslate %}The width is: {{ width }}{% endblocktranslate %}""",
)
assert output.exit_code == 0
assert (
output.text
== r"""{% blocktranslate %}The width is: {{ width }}{% endblocktranslate %}
"""
)
output = reformat(
tmp_file,
runner,
b"""{% blocktranslate trimmed %}The width is: {{ width }}{% endblocktranslate %}""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""{% blocktrans %}The width is: {{ width }}{% endblocktrans %}""",
)
assert output.exit_code == 0
assert (
output.text
== r"""{% blocktrans %}The width is: {{ width }}{% endblocktrans %}
"""
)
output = reformat(
tmp_file,
runner,
b"""{% blocktrans trimmed %}The width is: {{ width }}{% endblocktrans %}""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<p>
{% blocktrans %}If you have not created an account yet, then please
<a href="{{ signup_url }}">sign up</a> first.{% endblocktrans %}
</p>\n""",
)
assert output.exit_code == 0
# def test_trans(runner: CliRunner, tmp_file: TextIO) -> None:
# output = reformat(
# tmp_file, runner, b"""<p>{% trans 'Please do <b>Blah</b>.' %}</p>"""
# )
# assert output.exit_code == 1
# assert (
# """<p>
# {% trans 'Please do <b>Blah</b>.' %}
# </p>
# """
# in output.text
# )
def test_with(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% with total=business.employees.count %}{{ total }}<div>employee</div>{{ total|pluralize }}{% endwith %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% with total=business.employees.count %}
{{ total }}
<div>employee</div>
{{ total|pluralize }}
{% endwith %}
"""
)
def test_load_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% block content %}{% load i18n %}{% endblock %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% block content %}
{% load i18n %}
{% endblock %}
"""
)
def test_single_line_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% if messages|length %}{% for message in messages %}{{ message }}{% endfor %}{% endif %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% if messages|length %}
{% for message in messages %}{{ message }}{% endfor %}
{% endif %}
"""
)
def test_multiple_endblocks(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% block content %}{% block scripts %}{% endblock %}{% endblock %}""",
)
assert output.exit_code == 1
assert (
"""{% block content %}\n {% block scripts %}{% endblock %}\n{% endblock %}
"""
== output.text
)

View file

View file

@ -0,0 +1,39 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_reformat_asset_tag(runner: CliRunner, tmp_file: TextIO) -> None:
# pylint: disable=C0301
output = reformat(
tmp_file,
runner,
b"""{% block css %}{% assets "css_error" %}<link type="text/css" rel="stylesheet" href="{{ ASSET_URL }}" />{% endassets %}{% endblock css %}""",
) # noqa: E501
assert (
output.text
== """{% block css %}
{% assets "css_error" %}
<link type="text/css" rel="stylesheet" href="{{ ASSET_URL }}" />
{% endassets %}
{% endblock css %}
"""
)
assert output.exit_code == 1

View file

@ -0,0 +1,34 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_autoescape(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"{% autoescape on %}{{ body }}{% endautoescape %}"
)
assert output.exit_code == 1
assert (
output.text
== r"""{% autoescape on %}
{{ body }}
{% endautoescape %}
"""
)

View file

@ -0,0 +1,33 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_multiple_endblocks(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% block content %}{% block scripts %}{% endblock %}{% endblock %}""",
)
assert output.exit_code == 1
assert output.text == (
"""{% block content %}\n {% block scripts %}{% endblock %}\n{% endblock %}
"""
)

View file

@ -0,0 +1,84 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_blocktranslate(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% blocktranslate %}The width is: {{ width }}{% endblocktranslate %}""",
)
assert output.exit_code == 0
assert (
output.text
== r"""{% blocktranslate %}The width is: {{ width }}{% endblocktranslate %}
"""
)
output = reformat(
tmp_file,
runner,
b"""{% blocktranslate trimmed %}The width is: {{ width }}{% endblocktranslate %}""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""{% blocktrans %}The width is: {{ width }}{% endblocktrans %}""",
)
assert output.exit_code == 0
assert (
output.text
== r"""{% blocktrans %}The width is: {{ width }}{% endblocktrans %}
"""
)
output = reformat(
tmp_file,
runner,
b"""{% blocktrans trimmed %}The width is: {{ width }}{% endblocktrans %}""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<p>
{% blocktrans %}If you have not created an account yet, then please
<a href="{{ signup_url }}">sign up</a> first.{% endblocktrans %}
</p>\n""",
)
assert output.exit_code == 0
# def test_trans(runner: CliRunner, tmp_file: TextIO) -> None:
# output = reformat(
# tmp_file, runner, b"""<p>{% trans 'Please do <b>Blah</b>.' %}</p>"""
# )
# assert output.exit_code == 1
# assert (
# """<p>
# {% trans 'Please do <b>Blah</b>.' %}
# </p>
# """
# in output.text
# )

View file

@ -0,0 +1,135 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django/test_comments.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_django/test_comments.py::test_comment
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_dj_comments_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"{# comment #}\n{% if this %}<div></div>{% endif %}"
)
assert output.text == """{# comment #}\n{% if this %}<div></div>{% endif %}\n"""
# no change was required
assert output.exit_code == 0
def test_comment(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"""{% comment "Optional note" %}{{ body }}{% endcomment %}"""
)
assert output.exit_code == 0
# too short to put on multiple lines
assert (
output.text
== r"""{% comment "Optional note" %}{{ body }}{% endcomment %}
"""
)
output = reformat(
tmp_file,
runner,
b"""<div class="hi">
<div class="poor">
<p class="format">
Lorem ipsum dolor
<span class="bold">sit</span>
amet
</p>
<img src="./pic.jpg">
</div>
<script src="file1.js"></script>
{% comment %} <script src="file2.js"></script>
<script src="file3.js"></script> {% endcomment %}
<script src="file4.js"></script>
</div>""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<div class="hi">
<div class="poor">
{# djlint:off #}
<p class="format">
Lorem ipsum dolor <span class="bold">sit</span> amet
</p>
{# djlint:on #}
<img src="./pic.jpg">
</div>
<ul>
{% for i in items %}
<li>item {{i}}</li>
{% if i > 10 %}{% endif %}
<li>item {{i}}</li>
{% endfor %}
</ul>
</div>
""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<html>
<head>
<script src="file1.js"></script>
{% comment %}
<script src="file2.js"></script>
<script src="file3.js"></script>
<script src="file4.js"></script>
{% endcomment %}
<script src="file5.js"></script>
</head>
<body></body>
</html>
""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<html>
<head>
<script src="file1.js"></script>
{# djlint:off #}
{% comment %}
<script src="file2.js"></script>
<script src="file3.js"></script>
<script src="file4.js"></script>
{% endcomment %}
{# djlint:on #}
<script src="file5.js"></script>
</head>
<body></body>
</html>
""",
)
assert output.exit_code == 0
def test_inline_comment(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"{# <div></div> #}\n{% if this %}<div></div>{% endif %}"
)
assert output.text == """{# <div></div> #}\n{% if this %}<div></div>{% endif %}\n"""
assert output.exit_code == 0

View file

@ -0,0 +1,36 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_filter(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% filter force_escape|lower %}This text will be HTML-escaped, and will appear in all lowercase.{% endfilter %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% filter force_escape|lower %}
This text will be HTML-escaped, and will appear in all lowercase.
{% endfilter %}
"""
)

View file

@ -0,0 +1,40 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_for_loop(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""<ul>{% for athlete in athlete_list %}<li>{{ athlete.name }}</li>{% empty %}<li>Sorry, no athletes in this list.</li>{% endfor %}</ul>""",
)
assert output.exit_code == 1
assert (
output.text
== r"""<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
<li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>
"""
)

View file

@ -0,0 +1,40 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_if(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% if athlete_list %}Number of athletes: {{ athlete_list|length }}{% elif athlete_in_locker_room_list %}Athletes should be out of the locker room soon!{% else %}No athletes.{% endif %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
Athletes should be out of the locker room soon!
{% else %}
No athletes.
{% endif %}
"""
)

View file

@ -0,0 +1,43 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_ifchanged(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% for match in matches %}<div style="background-color:"pink">{% ifchanged match.ballot_id %}{% cycle "red" "blue" %}{% else %}gray{% endifchanged %}{{ match }}</div>{% endfor %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% for match in matches %}
<div style="background-color:"pink">
{% ifchanged match.ballot_id %}
{% cycle "red" "blue" %}
{% else %}
gray
{% endifchanged %}
{{ match }}
</div>
{% endfor %}
"""
)

View file

@ -0,0 +1,31 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_include(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(tmp_file, runner, b"""{% include "this" %}{% include "that"%}""")
assert output.exit_code == 1
assert (
output.text
== r"""{% include "this" %}
{% include "that" %}
"""
)

View file

@ -0,0 +1,36 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_load_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% block content %}{% load i18n %}{% endblock %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% block content %}
{% load i18n %}
{% endblock %}
"""
)

View file

@ -0,0 +1,38 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_spaceless(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% spaceless %}<p><a href="foo/">Foo</a></p>{% endspaceless %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% spaceless %}
<p>
<a href="foo/">Foo</a>
</p>
{% endspaceless %}
"""
)

View file

@ -0,0 +1,56 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_empty_tags_on_one_line(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(tmp_file, runner, b"{% if stuff %}\n{% endif %}")
assert output.text == """{% if stuff %}{% endif %}\n"""
assert output.exit_code == 1
def test_templatetag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% templatetag openblock %} url 'entry_list' {% templatetag closeblock %}""",
)
assert output.exit_code == 0
assert (
output.text
== r"""{% templatetag openblock %} url 'entry_list' {% templatetag closeblock %}
"""
)
def test_single_line_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% if messages|length %}{% for message in messages %}{{ message }}{% endfor %}{% endif %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% if messages|length %}
{% for message in messages %}{{ message }}{% endfor %}
{% endif %}
"""
)

View file

@ -0,0 +1,34 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_verbatim(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file, runner, b"""{% verbatim %}Still alive.{% endverbatim %}"""
)
assert output.exit_code == 1
assert (
output.text
== r"""{% verbatim %}
Still alive.
{% endverbatim %}
"""
)

View file

@ -0,0 +1,38 @@
"""Djlint tests specific to django.
run::
pytest tests/test_django.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_django.py::test_alpine_js --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_with(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% with total=business.employees.count %}{{ total }}<div>employee</div>{{ total|pluralize }}{% endwith %}""",
)
assert output.exit_code == 1
assert (
output.text
== r"""{% with total=business.employees.count %}
{{ total }}
<div>employee</div>
{{ total|pluralize }}
{% endwith %}
"""
)

View file

Some files were not shown because too many files have changed in this diff Show more