diff --git a/setup.cfg b/setup.cfg index 854cb0e..a0cc312 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index cdffb34..e56c24c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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, + } ) diff --git a/tests/test_config.py b/tests/test_config.py deleted file mode 100644 index 2539017..0000000 --- a/tests/test_config.py +++ /dev/null @@ -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 %}

this is a long paragraph

{% endexample %} -+{% example stuff %} -+

-+ this is a long paragraph -+

-+{% 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 ( - """-this is a email text -+ -+ -+ this is a email text -+ -+ -""" - in result.output - ) - assert result.exit_code == 1 - - # https://github.com/Riverside-Healthcare/djLint/issues/236 - output = reformat(tmp_file, runner, b"\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 ( - """-

-+
-+

-+

-+ -+
-+

-+
""" - in result.output - ) - assert result.exit_code == 1 - - result = runner.invoke(djlint, ["tests/config_indent", "--check", "--indent", 3]) - - assert ( - """-

-+
-+

-+

-+ -+
-+

-+
""" - 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" %} -+ -+
""" - 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 %} -+ -
""" - 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" %}
-+{% extends "nothing.html" %} -+{% load stuff %} -+{% load stuff 2 %} -+{% include "html_two.html" %} -+
""" - 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 --}} -

-- --{{firstname}}

{{lastname}}

-+ {{firstname}} -+

-+

-+ {{lastname}} -+

""" - 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 */ }} --

Test

{{ .Variable }}

--{{ range .Items }}

{{ . }} -- --

{{ end }} -+

Test

-+

-+ {{ .Variable }} -+

-+{{ range .Items }} -+

-+ {{ . }} -+

-+{{ 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 ( - """ --{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}
-+{% extends "nothing.html" %} -+{% load stuff %} -+{% load stuff 2 %} -+{% include "html_two.html" %} -+
""" - 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" %}
-+{% extends "nothing.html" %} -+{% load stuff %} -+{% load stuff 2 %} -+{% include "html_two.html" %} -+
""" - in result.output - ) - assert """1 file would be updated.""" in result.output - assert result.exit_code == 1 diff --git a/tests/test_config/__init__.py b/tests/test_config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_config/test_blank_lines_after_tag/__init__.py b/tests/test_config/test_blank_lines_after_tag/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_blank_lines_after_tag/html.html b/tests/test_config/test_blank_lines_after_tag/html.html similarity index 100% rename from tests/config_blank_lines_after_tag/html.html rename to tests/test_config/test_blank_lines_after_tag/html.html diff --git a/tests/config_blank_lines_after_tag/html_five.html b/tests/test_config/test_blank_lines_after_tag/html_five.html similarity index 100% rename from tests/config_blank_lines_after_tag/html_five.html rename to tests/test_config/test_blank_lines_after_tag/html_five.html diff --git a/tests/config_blank_lines_after_tag/html_four.html b/tests/test_config/test_blank_lines_after_tag/html_four.html similarity index 100% rename from tests/config_blank_lines_after_tag/html_four.html rename to tests/test_config/test_blank_lines_after_tag/html_four.html diff --git a/tests/config_blank_lines_after_tag/html_seven.html b/tests/test_config/test_blank_lines_after_tag/html_seven.html similarity index 100% rename from tests/config_blank_lines_after_tag/html_seven.html rename to tests/test_config/test_blank_lines_after_tag/html_seven.html diff --git a/tests/config_blank_lines_after_tag/html_six.html b/tests/test_config/test_blank_lines_after_tag/html_six.html similarity index 100% rename from tests/config_blank_lines_after_tag/html_six.html rename to tests/test_config/test_blank_lines_after_tag/html_six.html diff --git a/tests/config_blank_lines_after_tag/html_three.html b/tests/test_config/test_blank_lines_after_tag/html_three.html similarity index 100% rename from tests/config_blank_lines_after_tag/html_three.html rename to tests/test_config/test_blank_lines_after_tag/html_three.html diff --git a/tests/config_blank_lines_after_tag/html_two.html b/tests/test_config/test_blank_lines_after_tag/html_two.html similarity index 100% rename from tests/config_blank_lines_after_tag/html_two.html rename to tests/test_config/test_blank_lines_after_tag/html_two.html diff --git a/tests/config_blank_lines_after_tag/pyproject.toml b/tests/test_config/test_blank_lines_after_tag/pyproject.toml similarity index 100% rename from tests/config_blank_lines_after_tag/pyproject.toml rename to tests/test_config/test_blank_lines_after_tag/pyproject.toml diff --git a/tests/test_config/test_blank_lines_after_tag/test_config.py b/tests/test_config/test_blank_lines_after_tag/test_config.py new file mode 100644 index 0000000..bc1ba85 --- /dev/null +++ b/tests/test_config/test_blank_lines_after_tag/test_config.py @@ -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" %} ++ ++
""" + 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 %} ++ +
""" + 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 diff --git a/tests/test_config/test_custom_html/__init__.py b/tests/test_config/test_custom_html/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_custom_html/html.html b/tests/test_config/test_custom_html/html.html similarity index 100% rename from tests/config_custom_html/html.html rename to tests/test_config/test_custom_html/html.html diff --git a/tests/config_custom_html/pyproject.toml b/tests/test_config/test_custom_html/pyproject.toml similarity index 100% rename from tests/config_custom_html/pyproject.toml rename to tests/test_config/test_custom_html/pyproject.toml diff --git a/tests/test_config/test_custom_html/test_config.py b/tests/test_config/test_custom_html/test_config.py new file mode 100644 index 0000000..5047b28 --- /dev/null +++ b/tests/test_config/test_custom_html/test_config.py @@ -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 ( + """-this is a email text ++ ++ ++ this is a email text ++ ++ +""" + in result.output + ) + assert result.exit_code == 1 + + # https://github.com/Riverside-Healthcare/djLint/issues/236 + output = reformat( + tmp_file, runner, b"\n" + ) + assert output.exit_code == 0 diff --git a/tests/test_config/test_custom_tags/__init__.py b/tests/test_config/test_custom_tags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_custom_tags/html.html b/tests/test_config/test_custom_tags/html.html similarity index 100% rename from tests/config_custom_tags/html.html rename to tests/test_config/test_custom_tags/html.html diff --git a/tests/config_custom_tags/pyproject.toml b/tests/test_config/test_custom_tags/pyproject.toml similarity index 100% rename from tests/config_custom_tags/pyproject.toml rename to tests/test_config/test_custom_tags/pyproject.toml diff --git a/tests/test_config/test_custom_tags/test_config.py b/tests/test_config/test_custom_tags/test_config.py new file mode 100644 index 0000000..ff6eb0f --- /dev/null +++ b/tests/test_config/test_custom_tags/test_config.py @@ -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 %}

this is a long paragraph

{% endexample %} ++{% example stuff %} ++

++ this is a long paragraph ++

++{% endexample %} +""" + in result.output + ) + assert result.exit_code == 1 diff --git a/tests/test_config/test_excludes/__init__.py b/tests/test_config/test_excludes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_excludes/excluded.html b/tests/test_config/test_excludes/excluded.html similarity index 100% rename from tests/config_excludes/excluded.html rename to tests/test_config/test_excludes/excluded.html diff --git a/tests/config_excludes/foo/excluded.html b/tests/test_config/test_excludes/foo/excluded.html similarity index 100% rename from tests/config_excludes/foo/excluded.html rename to tests/test_config/test_excludes/foo/excluded.html diff --git a/tests/config_excludes/html.html b/tests/test_config/test_excludes/html.html similarity index 100% rename from tests/config_excludes/html.html rename to tests/test_config/test_excludes/html.html diff --git a/tests/config_excludes/pyproject.toml b/tests/test_config/test_excludes/pyproject.toml similarity index 100% rename from tests/config_excludes/pyproject.toml rename to tests/test_config/test_excludes/pyproject.toml diff --git a/tests/test_config/test_excludes/test_config.py b/tests/test_config/test_excludes/test_config.py new file mode 100644 index 0000000..8a9b2bf --- /dev/null +++ b/tests/test_config/test_excludes/test_config.py @@ -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 diff --git a/tests/test_config/test_extension/__init__.py b/tests/test_config/test_extension/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_extension/html.test b/tests/test_config/test_extension/html.test similarity index 100% rename from tests/config_extension/html.test rename to tests/test_config/test_extension/html.test diff --git a/tests/config_extension/pyproject.toml b/tests/test_config/test_extension/pyproject.toml similarity index 100% rename from tests/config_extension/pyproject.toml rename to tests/test_config/test_extension/pyproject.toml diff --git a/tests/test_config/test_extension/test_config.py b/tests/test_config/test_extension/test_config.py new file mode 100644 index 0000000..3a63646 --- /dev/null +++ b/tests/test_config/test_extension/test_config.py @@ -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 diff --git a/tests/test_config/test_format_attribute_template_tags/__init__.py b/tests/test_config/test_format_attribute_template_tags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_format_attribute_template_tags/html-one.html b/tests/test_config/test_format_attribute_template_tags/html-one.html similarity index 100% rename from tests/config_format_attribute_template_tags/html-one.html rename to tests/test_config/test_format_attribute_template_tags/html-one.html diff --git a/tests/config_format_attribute_template_tags/pyproject.toml b/tests/test_config/test_format_attribute_template_tags/pyproject.toml similarity index 100% rename from tests/config_format_attribute_template_tags/pyproject.toml rename to tests/test_config/test_format_attribute_template_tags/pyproject.toml diff --git a/tests/test_config_format_attribute_template_tags.py b/tests/test_config/test_format_attribute_template_tags/test_config.py similarity index 98% rename from tests/test_config_format_attribute_template_tags.py rename to tests/test_config/test_format_attribute_template_tags/test_config.py index cfa9599..a0417bc 100644 --- a/tests/test_config_format_attribute_template_tags.py +++ b/tests/test_config/test_format_attribute_template_tags/test_config.py @@ -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 diff --git a/tests/test_config/test_gitignore/__init__.py b/tests/test_config/test_gitignore/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_gitignore/html_one.html b/tests/test_config/test_gitignore/html_one.html similarity index 100% rename from tests/config_gitignore/html_one.html rename to tests/test_config/test_gitignore/html_one.html diff --git a/tests/config_gitignore/html_two.html b/tests/test_config/test_gitignore/html_two.html similarity index 100% rename from tests/config_gitignore/html_two.html rename to tests/test_config/test_gitignore/html_two.html diff --git a/tests/test_config/test_gitignore/test_config.py b/tests/test_config/test_gitignore/test_config.py new file mode 100644 index 0000000..f2a9cfa --- /dev/null +++ b/tests/test_config/test_gitignore/test_config.py @@ -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='

' + ) + 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") diff --git a/tests/test_config/test_ignores/__init__.py b/tests/test_config/test_ignores/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_ignores/html.html b/tests/test_config/test_ignores/html.html similarity index 100% rename from tests/config_ignores/html.html rename to tests/test_config/test_ignores/html.html diff --git a/tests/config_ignores/pyproject.toml b/tests/test_config/test_ignores/pyproject.toml similarity index 100% rename from tests/config_ignores/pyproject.toml rename to tests/test_config/test_ignores/pyproject.toml diff --git a/tests/test_config/test_ignores/test_config.py b/tests/test_config/test_ignores/test_config.py new file mode 100644 index 0000000..6bfe971 --- /dev/null +++ b/tests/test_config/test_ignores/test_config.py @@ -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 diff --git a/tests/test_config/test_indent/__init__.py b/tests/test_config/test_indent/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_indent/html.html b/tests/test_config/test_indent/html.html similarity index 100% rename from tests/config_indent/html.html rename to tests/test_config/test_indent/html.html diff --git a/tests/config_indent/pyproject.toml b/tests/test_config/test_indent/pyproject.toml similarity index 100% rename from tests/config_indent/pyproject.toml rename to tests/test_config/test_indent/pyproject.toml diff --git a/tests/test_config/test_indent/test_config.py b/tests/test_config/test_indent/test_config.py new file mode 100644 index 0000000..e80c55e --- /dev/null +++ b/tests/test_config/test_indent/test_config.py @@ -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 ( + """-

++
++

++

++ ++
++

++
""" + in result.output + ) + assert result.exit_code == 1 + + result = runner.invoke( + djlint, ["tests/test_config/test_indent", "--check", "--indent", 3] # type: ignore + ) + + assert ( + """-

++
++

++

++ ++
++

++
""" + in result.output + ) + assert result.exit_code == 1 diff --git a/tests/config_json/.djlintrc b/tests/test_config/test_json/.djlintrc similarity index 100% rename from tests/config_json/.djlintrc rename to tests/test_config/test_json/.djlintrc diff --git a/tests/test_config/test_json/__init__.py b/tests/test_config/test_json/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_json/html.html b/tests/test_config/test_json/html.html similarity index 100% rename from tests/config_json/html.html rename to tests/test_config/test_json/html.html diff --git a/tests/test_config_json.py b/tests/test_config/test_json/test_config.py similarity index 89% rename from tests/test_config_json.py rename to tests/test_config/test_json/test_config.py index f37b2db..0aa30a6 100644 --- a/tests/test_config_json.py +++ b/tests/test_config/test_json/test_config.py @@ -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) diff --git a/tests/test_config/test_linter_output_format/__init__.py b/tests/test_config/test_linter_output_format/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_linter_output_format/html-one.html b/tests/test_config/test_linter_output_format/html-one.html similarity index 100% rename from tests/config_linter_output_format/html-one.html rename to tests/test_config/test_linter_output_format/html-one.html diff --git a/tests/config_linter_output_format/html-two.html b/tests/test_config/test_linter_output_format/html-two.html similarity index 100% rename from tests/config_linter_output_format/html-two.html rename to tests/test_config/test_linter_output_format/html-two.html diff --git a/tests/config_linter_output_format/pyproject.toml b/tests/test_config/test_linter_output_format/pyproject.toml similarity index 100% rename from tests/config_linter_output_format/pyproject.toml rename to tests/test_config/test_linter_output_format/pyproject.toml diff --git a/tests/test_config_linter_output_format.py b/tests/test_config/test_linter_output_format/test_config.py similarity index 89% rename from tests/test_config_linter_output_format.py rename to tests/test_config/test_linter_output_format/test_config.py index edd2ec7..cae7c05 100644 --- a/tests/test_config_linter_output_format.py +++ b/tests/test_config/test_linter_output_format/test_config.py @@ -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) diff --git a/tests/test_config/test_pragmas/__init__.py b/tests/test_config/test_pragmas/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_pragmas/html_five.html b/tests/test_config/test_pragmas/html_five.html similarity index 100% rename from tests/config_pragmas/html_five.html rename to tests/test_config/test_pragmas/html_five.html diff --git a/tests/config_pragmas/html_four.html b/tests/test_config/test_pragmas/html_four.html similarity index 100% rename from tests/config_pragmas/html_four.html rename to tests/test_config/test_pragmas/html_four.html diff --git a/tests/config_pragmas/html_one.html b/tests/test_config/test_pragmas/html_one.html similarity index 100% rename from tests/config_pragmas/html_one.html rename to tests/test_config/test_pragmas/html_one.html diff --git a/tests/config_pragmas/html_six.html b/tests/test_config/test_pragmas/html_six.html similarity index 100% rename from tests/config_pragmas/html_six.html rename to tests/test_config/test_pragmas/html_six.html diff --git a/tests/config_pragmas/html_three.html b/tests/test_config/test_pragmas/html_three.html similarity index 100% rename from tests/config_pragmas/html_three.html rename to tests/test_config/test_pragmas/html_three.html diff --git a/tests/config_pragmas/html_two.html b/tests/test_config/test_pragmas/html_two.html similarity index 100% rename from tests/config_pragmas/html_two.html rename to tests/test_config/test_pragmas/html_two.html diff --git a/tests/config_pragmas/pyproject.toml b/tests/test_config/test_pragmas/pyproject.toml similarity index 100% rename from tests/config_pragmas/pyproject.toml rename to tests/test_config/test_pragmas/pyproject.toml diff --git a/tests/test_config/test_pragmas/test_config.py b/tests/test_config/test_pragmas/test_config.py new file mode 100644 index 0000000..0be0417 --- /dev/null +++ b/tests/test_config/test_pragmas/test_config.py @@ -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" %}
++{% extends "nothing.html" %} ++{% load stuff %} ++{% load stuff 2 %} ++{% include "html_two.html" %} ++
""" + 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 --}} +

+- +-{{firstname}}

{{lastname}}

++ {{firstname}} ++

++

++ {{lastname}} ++

""" + 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 */ }} +-

Test

{{ .Variable }}

+-{{ range .Items }}

{{ . }} +- +-

{{ end }} ++

Test

++

++ {{ .Variable }} ++

++{{ range .Items }} ++

++ {{ . }} ++

++{{ 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 ( + """ +-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}
++{% extends "nothing.html" %} ++{% load stuff %} ++{% load stuff 2 %} ++{% include "html_two.html" %} ++
""" + 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" %}
++{% extends "nothing.html" %} ++{% load stuff %} ++{% load stuff 2 %} ++{% include "html_two.html" %} ++
""" + in result.output + ) + assert """1 file would be updated.""" in result.output + assert result.exit_code == 1 diff --git a/tests/test_config/test_profile/__init__.py b/tests/test_config/test_profile/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/config_profile/html.html b/tests/test_config/test_profile/html.html similarity index 100% rename from tests/config_profile/html.html rename to tests/test_config/test_profile/html.html diff --git a/tests/config_profile/pyproject.toml b/tests/test_config/test_profile/pyproject.toml similarity index 100% rename from tests/config_profile/pyproject.toml rename to tests/test_config/test_profile/pyproject.toml diff --git a/tests/test_config/test_profile/test_config.py b/tests/test_config/test_profile/test_config.py new file mode 100644 index 0000000..15fc628 --- /dev/null +++ b/tests/test_config/test_profile/test_config.py @@ -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 + ) diff --git a/tests/test_config_gitignore.py b/tests/test_config_gitignore.py deleted file mode 100644 index 9bb1f70..0000000 --- a/tests/test_config_gitignore.py +++ /dev/null @@ -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='

') - 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") diff --git a/tests/test_django.py b/tests/test_django.py deleted file mode 100644 index a8eae93..0000000 --- a/tests/test_django.py +++ /dev/null @@ -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 %}
{% endif %}" - ) - assert output.text == """{# comment #}\n{% if this %}
{% 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" %}{% endassets %}{% endblock css %}""", - ) # noqa: E501 - assert ( - output.text - == """{% block css %} - {% assets "css_error" %} - - {% endassets %} -{% endblock css %} -""" - ) - assert output.exit_code == 1 - - -def test_alpine_js(runner: CliRunner, tmp_file: TextIO) -> None: - output = reformat( - tmp_file, runner, b"""
""" - ) - - 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"""
-
-

- Lorem ipsum dolor - sit - amet -

- -
- - {% comment %} - {% endcomment %} - -
""", - ) - - assert output.exit_code == 0 - - output = reformat( - tmp_file, - runner, - b"""
-
- {# djlint:off #} -

- Lorem ipsum dolor sit amet -

- {# djlint:on #} - -
- -
-""", - ) - - assert output.exit_code == 0 - - output = reformat( - tmp_file, - runner, - b""" - - - {% comment %} - - - - {% endcomment %} - - - - - -""", - ) - - assert output.exit_code == 0 - - output = reformat( - tmp_file, - runner, - b""" - - - {# djlint:off #} - {% comment %} - - - - {% endcomment %} - {# djlint:on #} - - - - - -""", - ) - - assert output.exit_code == 0 - - -def test_inline_comment(runner: CliRunner, tmp_file: TextIO) -> None: - output = reformat( - tmp_file, runner, b"{#
#}\n{% if this %}
{% endif %}" - ) - assert output.text == """{#
#}\n{% if this %}
{% endif %}\n""" - assert output.exit_code == 0 - - -def test_for_loop(runner: CliRunner, tmp_file: TextIO) -> None: - output = reformat( - tmp_file, - runner, - b"""""", - ) - assert output.exit_code == 1 - assert ( - output.text - == r""" -""" - ) - - -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 %}
{% ifchanged match.ballot_id %}{% cycle "red" "blue" %}{% else %}gray{% endifchanged %}{{ match }}
{% endfor %}""", - ) - assert output.exit_code == 1 - assert ( - output.text - == r"""{% for match in matches %} -
- {% ifchanged match.ballot_id %} - {% cycle "red" "blue" %} - {% else %} - gray - {% endifchanged %} - {{ match }} -
-{% 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 %}

Foo

{% endspaceless %}""", - ) - assert output.exit_code == 1 - assert ( - output.text - == r"""{% spaceless %} -

- Foo -

-{% 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"""

- {% blocktrans %}If you have not created an account yet, then please - sign up first.{% endblocktrans %} -

\n""", - ) - assert output.exit_code == 0 - - -# def test_trans(runner: CliRunner, tmp_file: TextIO) -> None: -# output = reformat( -# tmp_file, runner, b"""

{% trans 'Please do Blah.' %}

""" -# ) -# assert output.exit_code == 1 -# assert ( -# """

-# {% trans 'Please do Blah.' %} -#

-# """ -# in output.text -# ) - - -def test_with(runner: CliRunner, tmp_file: TextIO) -> None: - output = reformat( - tmp_file, - runner, - b"""{% with total=business.employees.count %}{{ total }}
employee
{{ total|pluralize }}{% endwith %}""", - ) - assert output.exit_code == 1 - assert ( - output.text - == r"""{% with total=business.employees.count %} - {{ total }} -
employee
- {{ 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 - ) diff --git a/tests/test_django/__init__.py b/tests/test_django/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_django/test_asset.py b/tests/test_django/test_asset.py new file mode 100644 index 0000000..30a0767 --- /dev/null +++ b/tests/test_django/test_asset.py @@ -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" %}{% endassets %}{% endblock css %}""", + ) # noqa: E501 + assert ( + output.text + == """{% block css %} + {% assets "css_error" %} + + {% endassets %} +{% endblock css %} +""" + ) + assert output.exit_code == 1 diff --git a/tests/test_django/test_autoescape.py b/tests/test_django/test_autoescape.py new file mode 100644 index 0000000..b63aaa6 --- /dev/null +++ b/tests/test_django/test_autoescape.py @@ -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 %} +""" + ) diff --git a/tests/test_django/test_block.py b/tests/test_django/test_block.py new file mode 100644 index 0000000..da06551 --- /dev/null +++ b/tests/test_django/test_block.py @@ -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 %} +""" + ) diff --git a/tests/test_django/test_blocktrans.py b/tests/test_django/test_blocktrans.py new file mode 100644 index 0000000..5607a94 --- /dev/null +++ b/tests/test_django/test_blocktrans.py @@ -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"""

+ {% blocktrans %}If you have not created an account yet, then please + sign up first.{% endblocktrans %} +

\n""", + ) + assert output.exit_code == 0 + + +# def test_trans(runner: CliRunner, tmp_file: TextIO) -> None: +# output = reformat( +# tmp_file, runner, b"""

{% trans 'Please do Blah.' %}

""" +# ) +# assert output.exit_code == 1 +# assert ( +# """

+# {% trans 'Please do Blah.' %} +#

+# """ +# in output.text +# ) diff --git a/tests/test_django/test_comments.py b/tests/test_django/test_comments.py new file mode 100644 index 0000000..bbb8d54 --- /dev/null +++ b/tests/test_django/test_comments.py @@ -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 %}
{% endif %}" + ) + assert output.text == """{# comment #}\n{% if this %}
{% 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"""
+
+

+ Lorem ipsum dolor + sit + amet +

+ +
+ + {% comment %} + {% endcomment %} + +
""", + ) + + assert output.exit_code == 0 + + output = reformat( + tmp_file, + runner, + b"""
+
+ {# djlint:off #} +

+ Lorem ipsum dolor sit amet +

+ {# djlint:on #} + +
+ +
+""", + ) + + assert output.exit_code == 0 + + output = reformat( + tmp_file, + runner, + b""" + + + {% comment %} + + + + {% endcomment %} + + + + + +""", + ) + + assert output.exit_code == 0 + + output = reformat( + tmp_file, + runner, + b""" + + + {# djlint:off #} + {% comment %} + + + + {% endcomment %} + {# djlint:on #} + + + + + +""", + ) + + assert output.exit_code == 0 + + +def test_inline_comment(runner: CliRunner, tmp_file: TextIO) -> None: + output = reformat( + tmp_file, runner, b"{#
#}\n{% if this %}
{% endif %}" + ) + assert output.text == """{#
#}\n{% if this %}
{% endif %}\n""" + assert output.exit_code == 0 diff --git a/tests/test_django/test_filter.py b/tests/test_django/test_filter.py new file mode 100644 index 0000000..3294698 --- /dev/null +++ b/tests/test_django/test_filter.py @@ -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 %} +""" + ) diff --git a/tests/test_django/test_for.py b/tests/test_django/test_for.py new file mode 100644 index 0000000..3a5d813 --- /dev/null +++ b/tests/test_django/test_for.py @@ -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"""""", + ) + assert output.exit_code == 1 + assert ( + output.text + == r""" +""" + ) diff --git a/tests/test_django/test_if.py b/tests/test_django/test_if.py new file mode 100644 index 0000000..cea2d2d --- /dev/null +++ b/tests/test_django/test_if.py @@ -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 %} +""" + ) diff --git a/tests/test_django/test_ifchanged.py b/tests/test_django/test_ifchanged.py new file mode 100644 index 0000000..a6f454b --- /dev/null +++ b/tests/test_django/test_ifchanged.py @@ -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 %}
{% ifchanged match.ballot_id %}{% cycle "red" "blue" %}{% else %}gray{% endifchanged %}{{ match }}
{% endfor %}""", + ) + assert output.exit_code == 1 + assert ( + output.text + == r"""{% for match in matches %} +
+ {% ifchanged match.ballot_id %} + {% cycle "red" "blue" %} + {% else %} + gray + {% endifchanged %} + {{ match }} +
+{% endfor %} +""" + ) diff --git a/tests/test_django/test_include.py b/tests/test_django/test_include.py new file mode 100644 index 0000000..318ecb6 --- /dev/null +++ b/tests/test_django/test_include.py @@ -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" %} +""" + ) diff --git a/tests/test_django/test_load.py b/tests/test_django/test_load.py new file mode 100644 index 0000000..f4b79a7 --- /dev/null +++ b/tests/test_django/test_load.py @@ -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 %} +""" + ) diff --git a/tests/test_django/test_spaceless.py b/tests/test_django/test_spaceless.py new file mode 100644 index 0000000..b5de4cf --- /dev/null +++ b/tests/test_django/test_spaceless.py @@ -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 %}

Foo

{% endspaceless %}""", + ) + assert output.exit_code == 1 + assert ( + output.text + == r"""{% spaceless %} +

+ Foo +

+{% endspaceless %} +""" + ) diff --git a/tests/test_django/test_templatetag.py b/tests/test_django/test_templatetag.py new file mode 100644 index 0000000..2dfaaf7 --- /dev/null +++ b/tests/test_django/test_templatetag.py @@ -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 %} +""" + ) diff --git a/tests/test_django/test_verbatim.py b/tests/test_django/test_verbatim.py new file mode 100644 index 0000000..2f8ca73 --- /dev/null +++ b/tests/test_django/test_verbatim.py @@ -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 %} +""" + ) diff --git a/tests/test_django/test_with.py b/tests/test_django/test_with.py new file mode 100644 index 0000000..ab52ab2 --- /dev/null +++ b/tests/test_django/test_with.py @@ -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 }}
employee
{{ total|pluralize }}{% endwith %}""", + ) + assert output.exit_code == 1 + assert ( + output.text + == r"""{% with total=business.employees.count %} + {{ total }} +
employee
+ {{ total|pluralize }} +{% endwith %} +""" + ) diff --git a/tests/test_djlint/__init__.py b/tests/test_djlint/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/bad.html b/tests/test_djlint/bad.html similarity index 100% rename from tests/bad.html rename to tests/test_djlint/bad.html diff --git a/tests/bad.html.dj b/tests/test_djlint/bad.html.dj similarity index 100% rename from tests/bad.html.dj rename to tests/test_djlint/bad.html.dj diff --git a/tests/bad.html.dj2 b/tests/test_djlint/bad.html.dj2 similarity index 100% rename from tests/bad.html.dj2 rename to tests/test_djlint/bad.html.dj2 diff --git a/tests/multiple_files/a/a.html b/tests/test_djlint/multiple_files/a/a.html similarity index 100% rename from tests/multiple_files/a/a.html rename to tests/test_djlint/multiple_files/a/a.html diff --git a/tests/multiple_files/b/b1.html b/tests/test_djlint/multiple_files/b/b1.html similarity index 100% rename from tests/multiple_files/b/b1.html rename to tests/test_djlint/multiple_files/b/b1.html diff --git a/tests/multiple_files/b/b2.html b/tests/test_djlint/multiple_files/b/b2.html similarity index 100% rename from tests/multiple_files/b/b2.html rename to tests/test_djlint/multiple_files/b/b2.html diff --git a/tests/test_djlint.py b/tests/test_djlint/test_djlint.py similarity index 77% rename from tests/test_djlint.py rename to tests/test_djlint/test_djlint.py index aaf38cc..ff2542f 100644 --- a/tests/test_djlint.py +++ b/tests/test_djlint/test_djlint.py @@ -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="
") - assert "
\n" == result.output + assert result.output == "
\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"

nice stuff here

") + write_to_file( + tmp_file.name, b"

nice stuff here

" + ) 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 diff --git a/tests/test_golang/__init__.py b/tests/test_golang/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_golang.py b/tests/test_golang/test_if.py similarity index 75% rename from tests/test_golang.py rename to tests/test_golang/test_if.py index 689238c..374764f 100644 --- a/tests/test_golang.py +++ b/tests/test_golang/test_if.py @@ -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 diff --git a/tests/test_golang/test_range.py b/tests/test_golang/test_range.py new file mode 100644 index 0000000..3e8d89d --- /dev/null +++ b/tests/test_golang/test_range.py @@ -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 diff --git a/tests/test_handlebars/__init__.py b/tests/test_handlebars/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_handlebars.py b/tests/test_handlebars/test_each.py similarity index 59% rename from tests/test_handlebars.py rename to tests/test_handlebars/test_each.py index 985f891..db72056 100644 --- a/tests/test_handlebars.py +++ b/tests/test_handlebars/test_each.py @@ -15,12 +15,7 @@ from typing import TextIO from click.testing import CliRunner -from .conftest import reformat - - -def test_handlebars_else(runner: CliRunner, tmp_file: TextIO) -> None: - output = reformat(tmp_file, runner, b"{{^}}") - assert output.exit_code == 0 +from tests.conftest import reformat def test_each(runner: CliRunner, tmp_file: TextIO) -> None: @@ -41,21 +36,3 @@ def test_each(runner: CliRunner, tmp_file: TextIO) -> None: {{/each }} """ ) - - -def test_with(runner: CliRunner, tmp_file: TextIO) -> None: - output = reformat( - tmp_file, - runner, - b"""{{#with person}}

{{firstname}} {{lastname}}

{{/with}}""", - ) - assert output.exit_code == 1 - assert ( - output.text - == r"""{{#with person }} -

- {{ firstname }} {{ lastname }} -

-{{/with }} -""" - ) diff --git a/tests/test_handlebars/test_else.py b/tests/test_handlebars/test_else.py new file mode 100644 index 0000000..60d9a89 --- /dev/null +++ b/tests/test_handlebars/test_else.py @@ -0,0 +1,23 @@ +"""Djlint tests specific to Handlebars.js. + +run:: + + pytest tests/test_handlebars.py --cov=src/djlint --cov-branch \ + --cov-report xml:coverage.xml --cov-report term-missing + + pytest tests/test_handlebars.py::test_each --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_handlebars_else(runner: CliRunner, tmp_file: TextIO) -> None: + output = reformat(tmp_file, runner, b"{{^}}") + assert output.exit_code == 0 diff --git a/tests/test_handlebars/test_with.py b/tests/test_handlebars/test_with.py new file mode 100644 index 0000000..6ab09e3 --- /dev/null +++ b/tests/test_handlebars/test_with.py @@ -0,0 +1,36 @@ +"""Djlint tests specific to Handlebars.js. + +run:: + + pytest tests/test_handlebars.py --cov=src/djlint --cov-branch \ + --cov-report xml:coverage.xml --cov-report term-missing + + pytest tests/test_handlebars.py::test_each --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 person}}

{{firstname}} {{lastname}}

{{/with}}""", + ) + assert output.exit_code == 1 + assert ( + output.text + == r"""{{#with person }} +

+ {{ firstname }} {{ lastname }} +

+{{/with }} +""" + ) diff --git a/tests/test_html/__init__.py b/tests/test_html/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_html/test_alpinejs.py b/tests/test_html/test_alpinejs.py new file mode 100644 index 0000000..d5089f8 --- /dev/null +++ b/tests/test_html/test_alpinejs.py @@ -0,0 +1,20 @@ +"""DjLint tests for alpine.js.""" +# pylint: disable=C0116 +from typing import TextIO + +from click.testing import CliRunner + +from tests.conftest import reformat + + +def test_alpine_js(runner: CliRunner, tmp_file: TextIO) -> None: + output = reformat( + tmp_file, + runner, + b"""
""", + ) + + assert output.exit_code == 0 diff --git a/tests/test_html.py b/tests/test_html/test_html.py similarity index 82% rename from tests/test_html.py rename to tests/test_html/test_html.py index 7be665b..9b5d09c 100644 --- a/tests/test_html.py +++ b/tests/test_html/test_html.py @@ -17,8 +17,7 @@ from typing import TextIO from click.testing import CliRunner from src.djlint import main as djlint - -from .conftest import reformat, write_to_file +from tests.conftest import reformat, write_to_file def test_front_matter(runner: CliRunner, tmp_file: TextIO) -> None: @@ -45,6 +44,7 @@ def test_code_tag(runner: CliRunner, tmp_file: TextIO) -> None: ) assert output.exit_code == 0 + def test_pre_tag(runner: CliRunner, tmp_file: TextIO) -> None: # added for https://github.com/Riverside-Healthcare/djLint/issues/187 output = reformat( @@ -62,81 +62,80 @@ def test_pre_tag(runner: CliRunner, tmp_file: TextIO) -> None: ) assert output.exit_code == 0 + # def test_textarea_tag(runner: CliRunner, tmp_file: TextIO) -> None: + # write_to_file(tmp_file.name, b"""
""") + # runner.invoke(djlint, [tmp_file.name, "--reformat"]) + # assert ( + # Path(tmp_file.name).read_text(encoding="utf8") + # == """
+ # + #
+ # """ + # ) + # # check double nesting + # output = reformat( + # tmp_file, + # runner, + # b"""
+ #
+ # + #
+ #
+ # """, + # ) -# def test_textarea_tag(runner: CliRunner, tmp_file: TextIO) -> None: -# write_to_file(tmp_file.name, b"""
""") -# runner.invoke(djlint, [tmp_file.name, "--reformat"]) -# assert ( -# Path(tmp_file.name).read_text() -# == """
-# -#
-# """ -# ) -# # check double nesting -# output = reformat( -# tmp_file, -# runner, -# b"""
-#
-# -#
-#
-# """, -# ) + # assert output.exit_code == 0 -# assert output.exit_code == 0 + # # check attributes + # output = reformat( + # tmp_file, + # runner, + # b"""
+ #
+ # + #
+ #
+ # """, + # ) -# # check attributes -# output = reformat( -# tmp_file, -# runner, -# b"""
-#
-# -#
-#
-# """, -# ) + # assert ( + # output.text + # == """
+ #
+ # + #
+ #
+ # """ + # ) -# assert ( -# output.text -# == """
-#
-# -#
-#
-# """ -# ) + # def test_a_tag(runner: CliRunner, tmp_file: TextIO) -> None: + # output = reformat( + # tmp_file, + # runner, + # b"""

+ # some nice text asdf, ok + #

""", + # ) + # assert output.exit_code == 0 -# def test_a_tag(runner: CliRunner, tmp_file: TextIO) -> None: -# output = reformat( -# tmp_file, -# runner, -# b"""

-# some nice text asdf, ok -#

""", -# ) - -# assert output.exit_code == 0 - -# # test added for https://github.com/Riverside-Healthcare/djLint/issues/189 -# output = reformat( -# tmp_file, -# runner, -# b""" -# hihi -#
-#

{{ _("Options") }}

-#
-# """) + # # test added for https://github.com/Riverside-Healthcare/djLint/issues/189 + # output = reformat( + # tmp_file, + # runner, + # b""" + # hihi + #
+ #

{{ _("Options") }}

+ #
+ # """) assert output.exit_code == 0 + def test_script_tag(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file( tmp_file.name, @@ -145,7 +144,7 @@ def test_script_tag(runner: CliRunner, tmp_file: TextIO) -> None: runner.invoke(djlint, [tmp_file.name, "--reformat"]) assert ( - Path(tmp_file.name).read_text() + Path(tmp_file.name).read_text(encoding="utf8") == """