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
This commit is contained in:
Christopher Pickering 2022-09-12 12:43:04 +02:00
parent 2c46e976db
commit bbcde2b426
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
8 changed files with 86 additions and 15 deletions

View file

@ -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")

View file

@ -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)

View file

@ -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())
)
}

View file

@ -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"],

View file

@ -0,0 +1,8 @@
{
"profile": "django",
"indent": 2,
"files": [
"./tests/test_config/test_files/test.html",
"./tests/test_config/test_files/test_two.html"
]
}

View file

@ -0,0 +1,3 @@
<div>
<p></p>
</div>

View file

@ -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

View file

@ -0,0 +1 @@
<div>