|
| 1 | +"""Djlint tests specific to --stdin-filename. |
| 2 | +
|
| 3 | +run:: |
| 4 | +
|
| 5 | + pytest tests/test_config/test_stdin_filename/test_config.py --cov=src/djlint \ |
| 6 | + --cov-branch --cov-report xml:coverage.xml --cov-report term-missing |
| 7 | +
|
| 8 | +for a single test, run:: |
| 9 | +
|
| 10 | + pytest tests/test_config/test_stdin_filename/test_config.py::test_stdin_filename_matches_per_file_ignores \ |
| 11 | + --cov=src/djlint --cov-branch --cov-report xml:coverage.xml --cov-report term-missing |
| 12 | +
|
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from pathlib import Path |
| 18 | +from typing import TYPE_CHECKING |
| 19 | + |
| 20 | +from djlint import main as djlint |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from click.testing import CliRunner |
| 24 | + |
| 25 | +# the pyproject.toml in this directory has: |
| 26 | +# "myfile.html" = "H025" |
| 27 | +# "templates/index.html" = "H025" |
| 28 | +# "^-$" = "H020" |
| 29 | +# and the html below triggers both H025 and H020 when neither is ignored. |
| 30 | +_HTML = "<div>\n <div></div>" |
| 31 | +_CONFIG = "tests/test_config/test_stdin_filename/pyproject.toml" |
| 32 | + |
| 33 | + |
| 34 | +def test_stdin_filename_matches_per_file_ignores(runner: CliRunner) -> None: |
| 35 | + """A --stdin-filename matching a per-file-ignores pattern suppresses it.""" |
| 36 | + result = runner.invoke( |
| 37 | + djlint, |
| 38 | + ("-", "--stdin-filename", "myfile.html", "--configuration", _CONFIG), |
| 39 | + input=_HTML, |
| 40 | + ) |
| 41 | + assert "H025" not in result.output |
| 42 | + assert "H020" in result.output |
| 43 | + |
| 44 | + |
| 45 | +def test_stdin_filename_not_matching_per_file_ignores( |
| 46 | + runner: CliRunner, |
| 47 | +) -> None: |
| 48 | + """A --stdin-filename that matches no pattern reports every rule.""" |
| 49 | + result = runner.invoke( |
| 50 | + djlint, |
| 51 | + ("-", "--stdin-filename", "other.html", "--configuration", _CONFIG), |
| 52 | + input=_HTML, |
| 53 | + ) |
| 54 | + assert "H025" in result.output |
| 55 | + assert "H020" in result.output |
| 56 | + |
| 57 | + |
| 58 | +def test_stdin_filename_default_unchanged(runner: CliRunner) -> None: |
| 59 | + """Without --stdin-filename, per-file-ignores still match against "-".""" |
| 60 | + result = runner.invoke( |
| 61 | + djlint, ("-", "--configuration", _CONFIG), input=_HTML |
| 62 | + ) |
| 63 | + assert "H020" not in result.output |
| 64 | + assert "H025" in result.output |
| 65 | + |
| 66 | + |
| 67 | +def test_stdin_filename_uses_native_separators(runner: CliRunner) -> None: |
| 68 | + """A native-separator path matches a pattern written with "/".""" |
| 69 | + result = runner.invoke( |
| 70 | + djlint, |
| 71 | + ( |
| 72 | + "-", |
| 73 | + "--stdin-filename", |
| 74 | + str(Path("templates", "index.html")), |
| 75 | + "--configuration", |
| 76 | + _CONFIG, |
| 77 | + ), |
| 78 | + input=_HTML, |
| 79 | + ) |
| 80 | + assert "H025" not in result.output |
| 81 | + assert "H020" in result.output |
| 82 | + |
| 83 | + |
| 84 | +def test_stdin_filename_used_in_lint_message(runner: CliRunner) -> None: |
| 85 | + """The --stdin-filename value is used as the error dict's filename key.""" |
| 86 | + result = runner.invoke( |
| 87 | + djlint, |
| 88 | + ( |
| 89 | + "-", |
| 90 | + "--stdin-filename", |
| 91 | + "myfile.html", |
| 92 | + "--configuration", |
| 93 | + _CONFIG, |
| 94 | + "--linter-output-format", |
| 95 | + "{filename} {code}", |
| 96 | + ), |
| 97 | + input=_HTML, |
| 98 | + ) |
| 99 | + assert "myfile.html H020" in result.output |
0 commit comments