Skip to content

Commit

Permalink
Merge pull request #424 from Riverside-Healthcare/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherpickering authored Oct 12, 2022
2 parents 400639d + b4cbcb4 commit 118b9f2
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/src/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ date: Last Modified

# Configuration

Configuration is done either through your projects `pyproject.toml` file, or a `.djlintrc` file. Command line args will always override any settings in `pyproject.toml`.
Configuration is done either through your projects `pyproject.toml` file, or a `.djlintrc` file. Command line args will always override any settings in `pyproject.toml`. Local project settings will always override global configuration files.

The format for `pyproject.toml` is `toml`.

Expand Down
2 changes: 1 addition & 1 deletion docs/src/fr/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keywords: template linter, template formatter, djLint, HTML, templates, formatte

# {{ "configuration" | i18n }}

La configuration se fait soit à travers le fichier `pyproject.toml` de votre projet, soit à travers un fichier `.djlintrc`. Les arguments de la ligne de commande remplaceront toujours les paramètres du fichier `pyproject.toml`.
La configuration se fait soit à travers le fichier `pyproject.toml` de votre projet, soit à travers un fichier `.djlintrc`. Les arguments de la ligne de commande remplaceront toujours les paramètres du fichier `pyproject.toml`. Les paramètres locaux du projet prévaudront toujours sur les fichiers de configuration globaux.

Le format du fichier `pyproject.toml` est `toml`.

Expand Down
2 changes: 1 addition & 1 deletion docs/src/ru/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keywords: облицовка шаблонов, форматер шаблонов

# {{ "configuration" | i18n }}

Конфигурация выполняется либо через файл `pyproject.toml` вашего проекта, либо через файл `.djlintrc`. Арги командной строки всегда будут переопределять любые настройки в `pyproject.toml`.
Конфигурация выполняется либо через файл `pyproject.toml` вашего проекта, либо через файл `.djlintrc`. Арги командной строки всегда будут переопределять любые настройки в `pyproject.toml`. Локальные настройки проекта всегда будут иметь приоритет над глобальными конфигурационными файлами.

```ini
[tool.djlint]
Expand Down
5 changes: 3 additions & 2 deletions src/djlint/formatter/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def indent_html(rawcode: str, config: Config) -> str:

if (
re.findall(
config.ignored_inline_blocks, item, flags=re.IGNORECASE | re.VERBOSE
rf"^\s*?(?:{config.ignored_inline_blocks})",
item,
flags=re.IGNORECASE | re.VERBOSE | re.MULTILINE,
)
and is_block_raw is False
):
Expand Down Expand Up @@ -150,7 +152,6 @@ def indent_html(rawcode: str, config: Config) -> str:
)
and is_block_raw is False
):

tmp = (indent * indent_level) + item + "\n"
indent_level = indent_level + 1

Expand Down
12 changes: 9 additions & 3 deletions src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ def load_project_settings(src: Path, config: Optional[str]) -> Dict:

if config:
try:
return json.loads(Path(config).resolve().read_text(encoding="utf8"))
djlint_content = json.loads(
Path(config).resolve().read_text(encoding="utf8")
)

# pylint: disable=broad-except
except BaseException:
logger.info(
Expand All @@ -115,15 +118,18 @@ def load_project_settings(src: Path, config: Optional[str]) -> Dict:
if pyproject_file:
content = tomllib.load(pyproject_file.open("rb"))
try:
return content["tool"]["djlint"] # type: ignore
return {**djlint_content, **content["tool"]["djlint"]} # type: ignore
except KeyError:
logger.info("No pyproject.toml found.")

djlintrc_file = find_djlintrc(src)

if djlintrc_file:
try:
return json.loads(djlintrc_file.read_text(encoding="utf8"))
return {
**djlint_content,
**json.loads(djlintrc_file.read_text(encoding="utf8")),
}
# pylint: disable=broad-except
except BaseException:
logger.info("Failed to load .djlintrc file.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"profile": "django",
"indent": 2,
"ignore": "",
"files": [
"./tests/test_config/test_files/test.html",
"./tests/test_config/test_files/test_two.html"
Expand Down
78 changes: 74 additions & 4 deletions tests/test_config/test_files/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
pytest tests/test_config/test_files/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_config/test_files/test_config.py::test_check_custom_file_src
pytest tests/test_config/test_files/test_config.py::test_global_override
"""
# pylint: disable=C0116

import os
from pathlib import Path

from click.testing import CliRunner

from src.djlint import main as djlint
Expand All @@ -22,7 +25,7 @@ def test_check_custom_file_src(runner: CliRunner) -> None:
"-",
"--check",
"--configuration",
"tests/test_config/test_files/.djlintrc",
"tests/test_config/test_files/.djlintrc_global",
],
)
assert """Checking 2/2 files""" in result.output
Expand All @@ -35,7 +38,7 @@ def test_lint_custom_file_src(runner: CliRunner) -> None:
"-",
"--lint",
"--configuration",
"tests/test_config/test_files/.djlintrc",
"tests/test_config/test_files/.djlintrc_global",
],
)
assert """Linting 2/2 files""" in result.output
Expand All @@ -48,7 +51,74 @@ def test_reformat_custom_file_src(runner: CliRunner) -> None:
"-",
"--reformat",
"--configuration",
"tests/test_config/test_files/.djlintrc",
"tests/test_config/test_files/.djlintrc_global",
],
)
assert """Reformatting 2/2 files""" in result.output


def test_global_override(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
[
"-",
"--lint",
"--configuration",
"tests/test_config/test_files/.djlintrc_global",
],
)
# fails
assert result.exit_code == 1

# check cli override
result = runner.invoke(
djlint,
[
"-",
"--lint",
"--configuration",
"tests/test_config/test_files/.djlintrc_global",
"--ignore",
"H025,H020",
],
)
# passes
assert result.exit_code == 0

# check project settings override

# create project settings folder
# add a gitignore file
with open(
"tests/test_config/test_files/.djlintrc", "w", encoding="utf8"
) as local_settings:
local_settings.write('{ "ignore":"H025"}')

result = runner.invoke(
djlint,
[
"tests/test_config/test_files/test_two.html",
"--lint",
"--configuration",
"tests/test_config/test_files/.djlintrc_global",
],
)

result_two = runner.invoke(
djlint,
[
"tests/test_config/test_files/test.html",
"--lint",
"--configuration",
"tests/test_config/test_files/.djlintrc_global",
],
)
try:
os.remove("tests/test_config/test_files/.djlintrc")
except BaseException as e:
print("cleanup failed")
print(e)

# H025 should be ignored, but H022 not
assert "H025" not in result.output
assert "H020" in result_two.output
1 change: 0 additions & 1 deletion tests/test_config/test_json/.djlintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"profile": "django",
"indent": 2,
"custom_blocks": "toc,example"
}
5 changes: 1 addition & 4 deletions tests/test_config/test_json/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
def test_config(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/test_config/test_json/html.html", "--check"])

print(result.output)

assert (
"""-{% example stuff %}<p>this is a long paragraph</p>{% endexample %}
+{% example stuff %}
+ <p>this is a long paragraph</p>
+ <p>this is a long paragraph</p>
+{% endexample %}
"""
in result.output
Expand All @@ -41,7 +39,6 @@ def test_custom_config(runner: CliRunner) -> None:
"tests/test_config/test_json/.djlint-cust",
],
)

assert (
"""-{% example stuff %}<p>this is a long paragraph</p>{% endexample %}
+{% example stuff %}
Expand Down
21 changes: 20 additions & 1 deletion tests/test_django/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
pytest tests/test_django/test_comments.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_django/test_comments.py::test_comment
pytest tests/test_django/test_comments.py::test_nested_inline_comment
"""
# pylint: disable=C0116
Expand Down Expand Up @@ -133,3 +133,22 @@ def test_inline_comment(runner: CliRunner, tmp_file: TextIO) -> None:
)
assert output.text == """{# <div></div> #}\n{% if this %}<div></div>{% endif %}\n"""
assert output.exit_code == 0


def test_nested_inline_comment(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""<div>
{% if 1 %}
<div class="{% if 1 %}class {% else %} class {% endif %}">
<div class="class"
data-parameters="{#?@ViewBag.DefaultFilters#}"
data-target="profile-{{ profile_type }}-{{ profile_id }}">
</div>
</div>
{% endif %}
""",
)

assert output.exit_code == 0

0 comments on commit 118b9f2

Please sign in to comment.