From d8a059ac32027c956fbae762ce6de44f5d47826c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Tue, 19 Sep 2023 02:19:18 +0200 Subject: [PATCH 1/4] feat(configuration): Accept pyproject.toml as a --configuration argument --- src/djlint/settings.py | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/djlint/settings.py b/src/djlint/settings.py index 8b8c4621..1f3f739b 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -95,6 +95,18 @@ def find_djlint_rules(root: Path) -> Optional[Path]: return None +def load_pyproject_config(filepath: Path) -> Dict: + """ Load djlint config from pyproject.toml """ + data = tomllib.loads(filepath.resolve().read_text(encoding="utf-8")) + return data.get("tool", {}).get("djlint", {}) + + + +def load_djlintrc_config(filepath: Path) -> Dict: + """ Load djlint config from .djlintrc """ + return json.loads(filepath.resolve().read_text(encoding="utf-8")) + + def load_project_settings(src: Path, config: Optional[str]) -> Dict: """Load djlint config from pyproject.toml.""" @@ -102,9 +114,11 @@ def load_project_settings(src: Path, config: Optional[str]) -> Dict: if config: try: - djlint_content = json.loads( - Path(config).resolve().read_text(encoding="utf8") - ) + path = Path(config) + if path.name == "pyproject.toml": + djlint_content.update(load_pyproject_config(path)) + else: + djlint_content.update(load_djlintrc_config(path)) # pylint: disable=broad-except except BaseException as error: @@ -114,24 +128,24 @@ def load_project_settings(src: Path, config: Optional[str]) -> Dict: Path(config).resolve(), error, ) + print(djlint_content) pyproject_file = find_pyproject(src) if pyproject_file: - content = tomllib.loads(pyproject_file.read_text(encoding="utf8")) - try: - return {**djlint_content, **content["tool"]["djlint"]} # type: ignore - except KeyError: + content = load_pyproject_config(pyproject_file) + if content != {}: + djlint_content.update(content) + return content + else: logger.info("No pyproject.toml found.") djlintrc_file = find_djlintrc(src) if djlintrc_file: try: - return { - **djlint_content, - **json.loads(djlintrc_file.read_text(encoding="utf8")), - } + djlint_content.update(load_djlintrc_config(djlintrc_file)) + return content # pylint: disable=broad-except except BaseException as error: logger.error("%sFailed to load .djlintrc file. %s", Fore.RED, error) From a1ecea5bf9b75e6e10208260ea2198ea38339a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Tue, 19 Sep 2023 02:27:05 +0200 Subject: [PATCH 2/4] test(configuration): Add test for pyproject.toml in --configuration --- .../subfolder/pyproject.toml | 8 +++++ .../test_config_pyproject/test.html | 3 ++ .../test_config_pyproject.py | 29 +++++++++++++++++++ .../test_config_pyproject/test_two.html | 1 + 4 files changed, 41 insertions(+) create mode 100644 tests/test_config/test_config_pyproject/subfolder/pyproject.toml create mode 100644 tests/test_config/test_config_pyproject/test.html create mode 100644 tests/test_config/test_config_pyproject/test_config_pyproject.py create mode 100644 tests/test_config/test_config_pyproject/test_two.html diff --git a/tests/test_config/test_config_pyproject/subfolder/pyproject.toml b/tests/test_config/test_config_pyproject/subfolder/pyproject.toml new file mode 100644 index 00000000..19597069 --- /dev/null +++ b/tests/test_config/test_config_pyproject/subfolder/pyproject.toml @@ -0,0 +1,8 @@ +[tool.djlint] +profile = "django" +indent = 2 +ignore = "" +files = [ + "./tests/test_config/test_config_pyproject/test.html", + "./tests/test_config/test_config_pyproject/test_two.html" +] diff --git a/tests/test_config/test_config_pyproject/test.html b/tests/test_config/test_config_pyproject/test.html new file mode 100644 index 00000000..21ff30a1 --- /dev/null +++ b/tests/test_config/test_config_pyproject/test.html @@ -0,0 +1,3 @@ +
+

+
diff --git a/tests/test_config/test_config_pyproject/test_config_pyproject.py b/tests/test_config/test_config_pyproject/test_config_pyproject.py new file mode 100644 index 00000000..185fffe0 --- /dev/null +++ b/tests/test_config/test_config_pyproject/test_config_pyproject.py @@ -0,0 +1,29 @@ +"""Djlint tests specific to custom file path. + +run:: + + pytest tests/test_config/test_config_pyproject/test.py --cov=src/djlint --cov-branch \ + --cov-report xml:coverage.xml --cov-report term-missing + + pytest tests/test_config/test_config_pyproject/test.py::test_check_pyproject_as_config + +""" + +import os + +from click.testing import CliRunner + +from src.djlint import main as djlint + + +def test_check_pyproject_as_config(runner: CliRunner) -> None: + result = runner.invoke( + djlint, + [ + "-", + "--check", + "--configuration", + "tests/test_config/test_config_pyproject/subfolder/pyproject.toml", + ], + ) + assert """Checking 2/2 files""" in result.output diff --git a/tests/test_config/test_config_pyproject/test_two.html b/tests/test_config/test_config_pyproject/test_two.html new file mode 100644 index 00000000..e54a2732 --- /dev/null +++ b/tests/test_config/test_config_pyproject/test_two.html @@ -0,0 +1 @@ +
From cfcbafc89fd2b7c0973ebc74f5bd4780ad6852d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Tue, 19 Sep 2023 02:32:48 +0200 Subject: [PATCH 3/4] docs(configuration): Update cli.md about --configuration accepting pyproject.toml --- docs/src/_includes/cli.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/_includes/cli.md b/docs/src/_includes/cli.md index 22053cc0..006b4629 100644 --- a/docs/src/_includes/cli.md +++ b/docs/src/_includes/cli.md @@ -25,8 +25,8 @@ Options: --preserve-blank-lines Attempt to preserve blank lines. --format-css Also format contents of