From bbcde2b426e95662ae8f3fd7760cd76c376ffb3b Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Mon, 12 Sep 2022 12:43:04 +0200 Subject: [PATCH] feat(config): allow source files to be specified in the config file This update allows a file list to be set in the config file. closes #371 --- requirements.txt | 9 ++-- src/djlint/__init__.py | 20 +++++--- src/djlint/reformat.py | 2 +- src/djlint/settings.py | 4 +- tests/test_config/test_files/.djlintrc | 8 +++ tests/test_config/test_files/test.html | 3 ++ tests/test_config/test_files/test_config.py | 54 +++++++++++++++++++++ tests/test_config/test_files/test_two.html | 1 + 8 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 tests/test_config/test_files/.djlintrc create mode 100644 tests/test_config/test_files/test.html create mode 100644 tests/test_config/test_files/test_config.py create mode 100644 tests/test_config/test_files/test_two.html diff --git a/requirements.txt b/requirements.txt index eb29ee1..8af618a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,7 +18,7 @@ iniconfig==1.1.1; python_version >= "3.7" isort==5.10.1; python_full_version >= "3.6.1" and python_version < "4.0" jsbeautifier==1.14.6 lazy-object-proxy==1.7.1; python_version >= "3.6" and python_full_version >= "3.7.2" -mccabe==0.7.0; python_full_version >= "3.7.2" and python_version >= "3.7" +mccabe==0.6.1; python_full_version >= "3.7.2" and python_version >= "3.7" mypy-extensions==0.4.3; python_full_version >= "3.6.2" and python_version >= "3.6" mypy==0.971; python_version >= "3.6" packaging==21.3; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.7" @@ -27,8 +27,8 @@ pep8-naming==0.13.2; python_version >= "3.7" platformdirs==2.5.2; python_version >= "3.7" and python_full_version >= "3.7.2" and (python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.7") pluggy==1.0.0; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.7" py==1.11.0; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.7" -pycodestyle==2.9.1; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.7" -pyflakes==2.5.0; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.7" +pycodestyle==2.7.0; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.7" +pyflakes==2.3.1; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.7" pylint==2.15.2; python_full_version >= "3.7.2" pyparsing==3.0.9; python_full_version >= "3.6.8" and python_version >= "3.7" pytest-cov==3.0.0; python_version >= "3.6" @@ -38,8 +38,7 @@ pytest==7.1.3; python_version >= "3.7" pyyaml==6.0; python_version >= "3.6" regex==2022.9.11; python_version >= "3.6" six==1.16.0; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" -toml==0.10.2; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" -tomli==2.0.1; python_full_version <= "3.11.0a6" and python_full_version >= "3.7.2" and python_version >= "3.7" and python_version < "3.11" or python_version < "3.11" +tomli==2.0.1; python_full_version <= "3.11.0a6" and python_full_version >= "3.7.2" and python_version >= "3.7" and python_version < "3.11" and (python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0") or python_version < "3.11" tomlkit==0.11.4; python_version >= "3.6" and python_version < "4.0" and python_full_version >= "3.7.2" tox==3.26.0; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.5.0") tqdm==4.64.1; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.4.0") diff --git a/src/djlint/__init__.py b/src/djlint/__init__.py index 9fc0ba3..7b10f12 100644 --- a/src/djlint/__init__.py +++ b/src/djlint/__init__.py @@ -166,17 +166,21 @@ def main( temp_file = None if "-" in src: - stdin_stream = click.get_text_stream("stdin", encoding="utf8") - stdin_text = stdin_stream.read() + if config.files: + file_list = get_src([Path(x) for x in config.files], config) - temp_file = tempfile.NamedTemporaryFile(delete=False) - temp_file.write(str.encode(stdin_text)) - temp_file.seek(0) + else: + stdin_stream = click.get_text_stream("stdin", encoding="utf8") + stdin_text = stdin_stream.read() - # cannot use gitignore for stdin paths. - config.use_gitignore = False + temp_file = tempfile.NamedTemporaryFile(delete=False) + temp_file.write(str.encode(stdin_text)) + temp_file.seek(0) - file_list = get_src([Path(temp_file.name)], config) + # cannot use gitignore for stdin paths. + config.use_gitignore = False + + file_list = get_src([Path(temp_file.name)], config) else: file_list = get_src([Path(x) for x in src], config) diff --git a/src/djlint/reformat.py b/src/djlint/reformat.py index c27f778..58cbe32 100644 --- a/src/djlint/reformat.py +++ b/src/djlint/reformat.py @@ -38,7 +38,7 @@ def reformat_file(config: Config, this_file: Path) -> dict: this_file.write_text(beautified_code, encoding="utf8") out = { - this_file: list( + str(this_file): list( difflib.unified_diff(rawcode.splitlines(), beautified_code.splitlines()) ) } diff --git a/src/djlint/settings.py b/src/djlint/settings.py index 8412b42..ae847a3 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -212,7 +212,6 @@ class Config: self.check = check self.lint = lint self.warn = warn - self.stdin = "-" in src self.project_root = find_project_root(Path(src)) @@ -262,6 +261,9 @@ class Config: # ignore is based on input and also profile self.ignore: str = str(ignore or djlint_settings.get("ignore", "")) + self.files: Optional[List[str]] = djlint_settings.get("files", None) + self.stdin = "-" in src and self.files is None + # codes to exclude profile_dict: Dict[str, List[str]] = { "html": ["D", "J", "T", "N", "M"], diff --git a/tests/test_config/test_files/.djlintrc b/tests/test_config/test_files/.djlintrc new file mode 100644 index 0000000..4d3caad --- /dev/null +++ b/tests/test_config/test_files/.djlintrc @@ -0,0 +1,8 @@ +{ + "profile": "django", + "indent": 2, + "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.html b/tests/test_config/test_files/test.html new file mode 100644 index 0000000..21ff30a --- /dev/null +++ b/tests/test_config/test_files/test.html @@ -0,0 +1,3 @@ +
+

+
diff --git a/tests/test_config/test_files/test_config.py b/tests/test_config/test_files/test_config.py new file mode 100644 index 0000000..91f730f --- /dev/null +++ b/tests/test_config/test_files/test_config.py @@ -0,0 +1,54 @@ +"""Djlint tests specific to custom file path. + +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 + +""" +# pylint: disable=C0116 + +from click.testing import CliRunner + +from src.djlint import main as djlint + + +def test_check_custom_file_src(runner: CliRunner) -> None: + result = runner.invoke( + djlint, + [ + "-", + "--check", + "--configuration", + "tests/test_config/test_files/.djlintrc", + ], + ) + assert """Checking 2/2 files""" in result.output + + +def test_lint_custom_file_src(runner: CliRunner) -> None: + result = runner.invoke( + djlint, + [ + "-", + "--lint", + "--configuration", + "tests/test_config/test_files/.djlintrc", + ], + ) + assert """Linting 2/2 files""" in result.output + + +def test_reformat_custom_file_src(runner: CliRunner) -> None: + result = runner.invoke( + djlint, + [ + "-", + "--reformat", + "--configuration", + "tests/test_config/test_files/.djlintrc", + ], + ) + assert """Reformatting 2/2 files""" in result.output diff --git a/tests/test_config/test_files/test_two.html b/tests/test_config/test_files/test_two.html new file mode 100644 index 0000000..e54a273 --- /dev/null +++ b/tests/test_config/test_files/test_two.html @@ -0,0 +1 @@ +