test(restructured): restructured the tests

This commit is contained in:
Christopher Pickering 2022-05-13 15:36:55 -05:00
parent a830bc47a2
commit aee34ea64c
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
124 changed files with 1767 additions and 1074 deletions

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

@ -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,37 @@
"""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_custom_tags(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/test_config/test_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

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

@ -19,7 +19,7 @@ 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)

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,152 @@
"""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_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>
+ {{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,140 @@
"""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_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

View file

@ -3,18 +3,18 @@ Djlint base tests.
run::
pytest --cov=src/djlint --cov-branch --cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_djlint/test_djlint.py --cov=src/djlint --cov-branch --cov-report xml:coverage.xml --cov-report term-missing
for a single test::
pytest tests/test_djlint.py::test_version --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_djlint/test_djlint.py::test_version
or::
tox
"""
# pylint: disable=W1510
import subprocess
import sys
@ -32,8 +32,7 @@ from typing import TextIO
from click.testing import CliRunner
from src.djlint import main as djlint
from .conftest import write_to_file
from tests.conftest import write_to_file
def test_help(runner: CliRunner) -> None:
@ -59,14 +58,14 @@ def test_nonexisting_file(runner: CliRunner) -> None:
def test_existing_file(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/bad.html"])
result = runner.invoke(djlint, ["tests/test_djlint/bad.html"])
assert result.exit_code == 1
assert str(Path("tests/bad.html")) in result.output
assert str(Path("tests/test_djlint/bad.html")) in result.output
def test_multiple_files(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/multiple_files/a", "tests/multiple_files/b", "--check"]
djlint, ["tests/test_djlint/multiple_files/a", "tests/test_djlint/multiple_files/b", "--check"]
)
assert result.exit_code == 1
assert "3 files would be updated." in result.output
@ -79,20 +78,20 @@ def test_bad_path(runner: CliRunner) -> None:
def test_good_path_with_e(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/", "-e", "html"])
result = runner.invoke(djlint, ["tests/test_djlint/", "-e", "html"])
assert result.exit_code == 1
assert str(Path("tests/bad.html")) in result.output
assert str(Path("tests/test_djlint/bad.html")) in result.output
def test_good_path_with_extension(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/", "--extension", "html*"])
result = runner.invoke(djlint, ["tests/test_djlint/", "--extension", "html*"])
assert result.exit_code == 1
assert str(Path("tests/bad.html")) in result.output
assert str(Path("tests/bad.html.dj")) in result.output
assert str(Path("tests/test_djlint/bad.html")) in result.output
assert str(Path("tests/test_djlint/bad.html.dj")) in result.output
def test_good_path_with_bad_ext(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/", "-e", "html.alphabet"])
result = runner.invoke(djlint, ["tests/test_djlint/", "-e", "html.alphabet"])
assert result.exit_code == 0
assert "No files to check!" in result.output
@ -115,7 +114,7 @@ def test_stdin(runner: CliRunner) -> None:
# check with reformat
result = runner.invoke(djlint, ["-", "--reformat"], input="<div></div>")
assert "<div></div>\n" == result.output
assert result.output == "<div></div>\n"
def test_check(runner: CliRunner, tmp_file: TextIO) -> None:
@ -125,7 +124,7 @@ def test_check(runner: CliRunner, tmp_file: TextIO) -> None:
def test_check_non_existing_file(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/nothing.html", "--check"])
result = runner.invoke(djlint, ["tests/test_djlint/nothing.html", "--check"])
assert result.exit_code == 2
@ -167,7 +166,9 @@ def test_check_reformatter_no_error(runner: CliRunner, tmp_file: TextIO) -> None
def test_warn(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(tmp_file.name, b"<div style='color:pink;'><p>nice stuff here</p></div>")
write_to_file(
tmp_file.name, b"<div style='color:pink;'><p>nice stuff here</p></div>"
)
result = runner.invoke(djlint, [tmp_file.name, "--lint", "--warn"])
assert result.exit_code == 0
@ -180,18 +181,18 @@ def test_version(runner: CliRunner) -> None:
def test_python_call() -> None:
# give up fighting windows lol
if sys.platform != "win32":
x = subprocess.run(
py_sub = subprocess.run(
["python", "-m", "djlint", "-h"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert b"python -m djlint [OPTIONS] SRC ..." in x.stdout
assert x.returncode == 0
assert b"python -m djlint [OPTIONS] SRC ..." in py_sub.stdout
assert py_sub.returncode == 0
x = subprocess.run(
py_sub = subprocess.run(
["python", "-m", "djlint", "__init__", "-h"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert b"python -m djlint [OPTIONS] SRC ..." in x.stdout
assert x.returncode == 0
assert b"python -m djlint [OPTIONS] SRC ..." in py_sub.stdout
assert py_sub.returncode == 0

View file

View file

@ -17,14 +17,9 @@ from typing import TextIO
from click.testing import CliRunner
from .conftest import reformat
from tests.conftest import reformat
def test_if(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(tmp_file, runner, b"{{ if .condition }} {{ else }} {{ end }}")
assert output.exit_code == 0
def test_range(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(tmp_file, runner, b"{{ range .Items }} {{ end }}")
assert output.exit_code == 0

View file

@ -0,0 +1,25 @@
"""Djlint tests specific to go-lang.
run::
pytest tests/test_golang.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
for a single test, run::
pytest tests/test_golang.py::test_inline_comment --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_range(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(tmp_file, runner, b"{{ range .Items }} {{ end }}")
assert output.exit_code == 0

View file

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