djLint/tests/test_djlint.py

198 lines
6.2 KiB
Python
Raw Normal View History

2021-07-13 17:25:29 +00:00
"""
Djlint base tests.
2021-07-13 15:45:57 +00:00
run::
2021-08-17 13:56:40 +00:00
pytest --cov=src/djlint --cov-branch --cov-report xml:coverage.xml --cov-report term-missing
2021-07-13 15:45:57 +00:00
for a single test::
2022-01-24 18:03:44 +00:00
pytest tests/test_djlint.py::test_version --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
2021-07-13 15:45:57 +00:00
or::
tox
2021-08-17 13:56:40 +00:00
2021-07-13 15:45:57 +00:00
"""
import subprocess
2021-09-20 09:49:31 +00:00
import sys
try:
from importlib import metadata
except ImportError:
# Running on pre-3.8 Python; use importlib-metadata package
import importlib_metadata as metadata # type: ignore
# pylint: disable=C0116
2021-07-13 17:45:25 +00:00
from pathlib import Path
from typing import TextIO
2021-07-13 15:45:57 +00:00
from click.testing import CliRunner
2021-07-13 15:45:57 +00:00
from src.djlint import main as djlint
2021-07-13 15:45:57 +00:00
from .conftest import write_to_file
2021-07-13 17:53:19 +00:00
def test_help(runner: CliRunner) -> None:
2021-07-13 15:45:57 +00:00
result = runner.invoke(djlint, ["-h"])
assert result.exit_code == 0
2021-12-01 09:36:51 +00:00
assert "djLint · HTML template linter and formatter." in result.output
2021-07-13 15:45:57 +00:00
def test_bad_args(runner: CliRunner) -> None:
2021-07-13 15:45:57 +00:00
result = runner.invoke(djlint, ["-a"])
assert result.exit_code == 2
assert "Error: No such option: -a" in result.output
result = runner.invoke(djlint, ["--aasdf"])
assert result.exit_code == 2
assert "Error: No such option: --aasdf" in result.output
def test_nonexisting_file(runner: CliRunner) -> None:
2021-07-13 15:45:57 +00:00
result = runner.invoke(djlint, ["not_a_file.html"])
assert result.exit_code == 2
assert "Path 'not_a_file.html' does not exist." in result.output
def test_existing_file(runner: CliRunner) -> None:
2021-07-13 15:45:57 +00:00
result = runner.invoke(djlint, ["tests/bad.html"])
2021-08-19 15:16:41 +00:00
assert result.exit_code == 1
2021-07-13 17:45:25 +00:00
assert str(Path("tests/bad.html")) in result.output
2021-07-13 15:45:57 +00:00
2021-09-21 18:04:57 +00:00
def test_multiple_files(runner: CliRunner) -> None:
2021-09-22 06:54:28 +00:00
result = runner.invoke(
djlint, ["tests/multiple_files/a", "tests/multiple_files/b", "--check"]
)
2021-09-21 18:04:57 +00:00
assert result.exit_code == 1
assert "3 files would be updated." in result.output
def test_bad_path(runner: CliRunner) -> None:
2021-07-13 15:45:57 +00:00
result = runner.invoke(djlint, ["tests/nowhere"])
assert result.exit_code == 2
2021-07-13 17:45:25 +00:00
assert "does not exist." in result.output
2021-07-13 15:45:57 +00:00
def test_good_path_with_e(runner: CliRunner) -> None:
2021-07-13 15:45:57 +00:00
result = runner.invoke(djlint, ["tests/", "-e", "html"])
2021-08-19 15:16:41 +00:00
assert result.exit_code == 1
2021-07-13 17:45:25 +00:00
assert str(Path("tests/bad.html")) in result.output
2021-07-13 15:45:57 +00:00
def test_good_path_with_extension(runner: CliRunner) -> None:
2021-07-13 15:45:57 +00:00
result = runner.invoke(djlint, ["tests/", "--extension", "html*"])
2021-08-19 15:16:41 +00:00
assert result.exit_code == 1
2021-07-13 17:45:25 +00:00
assert str(Path("tests/bad.html")) in result.output
assert str(Path("tests/bad.html.dj")) in result.output
2021-07-13 15:45:57 +00:00
def test_good_path_with_bad_ext(runner: CliRunner) -> None:
2021-07-13 15:45:57 +00:00
result = runner.invoke(djlint, ["tests/", "-e", "html.alphabet"])
assert result.exit_code == 0
2021-07-23 21:02:24 +00:00
assert "No files to check!" in result.output
2021-07-13 15:45:57 +00:00
def test_empty_file(runner: CliRunner, tmp_file: TextIO) -> None:
2021-07-13 17:53:19 +00:00
write_to_file(tmp_file.name, b"")
2021-07-13 15:45:57 +00:00
result = runner.invoke(djlint, [tmp_file.name])
assert result.exit_code == 0
2021-09-16 11:36:44 +00:00
def test_stdin(runner: CliRunner) -> None:
2021-10-06 07:42:08 +00:00
result = runner.invoke(djlint, ["-"], input='<div><p id="a"></p></div>')
2021-09-16 11:36:44 +00:00
assert result.exit_code == 0
2021-09-22 06:54:28 +00:00
assert "Linted 1 file" in result.output
# check with multiple inputs
2021-10-06 07:42:08 +00:00
result = runner.invoke(djlint, ["-", "-"], input='<div><p id="a"></p></div>')
2021-09-22 06:54:28 +00:00
assert result.exit_code == 0
assert "Linted 1 file" in result.output
2021-09-16 11:36:44 +00:00
2021-10-05 11:11:08 +00:00
# check with reformat
result = runner.invoke(djlint, ["-", "--reformat"], input="<div></div>")
assert "<div></div>\n" == result.output
2021-09-16 11:36:44 +00:00
def test_check(runner: CliRunner, tmp_file: TextIO) -> None:
2021-07-29 18:41:34 +00:00
write_to_file(tmp_file.name, b"<div></div>")
2021-08-17 13:56:40 +00:00
result = runner.invoke(djlint, [tmp_file.name, "--check"])
2021-07-29 18:41:34 +00:00
assert result.exit_code == 0
def test_check_non_existing_file(runner: CliRunner) -> None:
2021-08-17 13:56:40 +00:00
result = runner.invoke(djlint, ["tests/nothing.html", "--check"])
2021-07-29 18:41:34 +00:00
assert result.exit_code == 2
def test_check_non_existing_folder(runner: CliRunner) -> None:
2021-08-17 13:56:40 +00:00
result = runner.invoke(djlint, ["tests/nothing", "--check"])
2021-07-29 18:41:34 +00:00
assert result.exit_code == 2
def test_check_reformatter_simple_error(runner: CliRunner, tmp_file: TextIO) -> None:
2021-08-17 13:56:40 +00:00
write_to_file(tmp_file.name, b"<div><p>nice stuff here</p></div>")
result = runner.invoke(djlint, [tmp_file.name, "--check"])
2021-08-19 15:16:41 +00:00
assert result.exit_code == 1
2021-08-17 13:56:40 +00:00
assert "1 file would be updated." in result.output
def test_reformatter_simple_error(runner: CliRunner, tmp_file: TextIO) -> None:
2021-08-17 13:56:40 +00:00
write_to_file(tmp_file.name, b"<div><p>nice stuff here</p></div>")
result = runner.invoke(djlint, [tmp_file.name, "--reformat"])
2021-08-19 15:16:41 +00:00
assert result.exit_code == 1
2021-08-17 13:56:40 +00:00
assert "1 file was updated." in result.output
def test_check_reformatter_simple_error_quiet(
runner: CliRunner, tmp_file: TextIO
) -> None:
2021-08-17 13:56:40 +00:00
write_to_file(tmp_file.name, b"<div><p>nice stuff here</p></div>")
result = runner.invoke(djlint, [tmp_file.name, "--check", "--quiet"])
2021-08-19 15:16:41 +00:00
assert result.exit_code == 1
2021-08-17 13:56:40 +00:00
assert "1 file would be updated." in result.output
def test_check_reformatter_no_error(runner: CliRunner, tmp_file: TextIO) -> None:
2021-08-17 13:56:40 +00:00
write_to_file(
tmp_file.name, b"<div>\n <p>\n nice stuff here\n </p>\n</div>"
)
result = runner.invoke(djlint, [tmp_file.name, "--check"])
assert result.exit_code == 0
assert "0 files would be updated." in result.output
2022-03-14 14:24:28 +00:00
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>")
result = runner.invoke(djlint, [tmp_file.name, "--lint", "--warn"])
assert result.exit_code == 0
def test_version(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["--version"])
assert metadata.version("djlint") in result.output
def test_python_call() -> None:
2021-09-20 09:49:31 +00:00
# give up fighting windows lol
if sys.platform != "win32":
2021-09-21 06:51:16 +00:00
x = subprocess.run(
["python", "-m", "djlint", "-h"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
2021-09-20 09:49:31 +00:00
assert b"python -m djlint [OPTIONS] SRC ..." in x.stdout
assert x.returncode == 0
x = subprocess.run(
2021-09-21 06:51:16 +00:00
["python", "-m", "djlint", "__init__", "-h"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
2021-09-20 09:49:31 +00:00
)
assert b"python -m djlint [OPTIONS] SRC ..." in x.stdout
assert x.returncode == 0