diff --git a/docs/src/docs/configuration.md b/docs/src/docs/configuration.md index edf9459..79a7694 100644 --- a/docs/src/docs/configuration.md +++ b/docs/src/docs/configuration.md @@ -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`. diff --git a/docs/src/fr/docs/configuration.md b/docs/src/fr/docs/configuration.md index f473de8..700a17f 100644 --- a/docs/src/fr/docs/configuration.md +++ b/docs/src/fr/docs/configuration.md @@ -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`. diff --git a/docs/src/ru/docs/configuration.md b/docs/src/ru/docs/configuration.md index 43b0677..bb8f761 100644 --- a/docs/src/ru/docs/configuration.md +++ b/docs/src/ru/docs/configuration.md @@ -6,7 +6,7 @@ keywords: облицовка шаблонов, форматер шаблонов # {{ "configuration" | i18n }} -Конфигурация выполняется либо через файл `pyproject.toml` вашего проекта, либо через файл `.djlintrc`. Арги командной строки всегда будут переопределять любые настройки в `pyproject.toml`. +Конфигурация выполняется либо через файл `pyproject.toml` вашего проекта, либо через файл `.djlintrc`. Арги командной строки всегда будут переопределять любые настройки в `pyproject.toml`. Локальные настройки проекта всегда будут иметь приоритет над глобальными конфигурационными файлами. ```ini [tool.djlint] diff --git a/src/djlint/settings.py b/src/djlint/settings.py index 7784300..5bb22ad 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -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( @@ -115,7 +118,7 @@ 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.") @@ -123,7 +126,10 @@ def load_project_settings(src: Path, config: Optional[str]) -> Dict: 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.") diff --git a/tests/test_config/test_files/.djlintrc b/tests/test_config/test_files/.djlintrc_global similarity index 90% rename from tests/test_config/test_files/.djlintrc rename to tests/test_config/test_files/.djlintrc_global index 4d3caad..11efc8d 100644 --- a/tests/test_config/test_files/.djlintrc +++ b/tests/test_config/test_files/.djlintrc_global @@ -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" diff --git a/tests/test_config/test_files/test_config.py b/tests/test_config/test_files/test_config.py index 91f730f..e8c38db 100644 --- a/tests/test_config/test_files/test_config.py +++ b/tests/test_config/test_files/test_config.py @@ -5,11 +5,14 @@ run:: 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 @@ -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 @@ -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 @@ -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 diff --git a/tests/test_config/test_json/.djlintrc b/tests/test_config/test_json/.djlintrc index 2f1c842..13351ea 100644 --- a/tests/test_config/test_json/.djlintrc +++ b/tests/test_config/test_json/.djlintrc @@ -1,5 +1,4 @@ { "profile": "django", - "indent": 2, "custom_blocks": "toc,example" } diff --git a/tests/test_config/test_json/test_config.py b/tests/test_config/test_json/test_config.py index f45397a..9c985e5 100644 --- a/tests/test_config/test_json/test_config.py +++ b/tests/test_config/test_json/test_config.py @@ -18,12 +18,10 @@ from src.djlint import main as djlint def test_config(runner: CliRunner) -> None: result = runner.invoke(djlint, ["tests/test_config/test_json/html.html", "--check"]) - print(result.output) - assert ( """-{% example stuff %}

this is a long paragraph

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

this is a long paragraph

++

this is a long paragraph

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

this is a long paragraph

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