Merge pull request #25 from matthiask/mk/multiple-src

Allowed more than one path at a time
This commit is contained in:
sur.la.route 2021-09-22 08:57:15 +02:00 committed by GitHub
commit e828aeb286
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 19 deletions

View file

@ -1,6 +1,8 @@
Changelog
=========
- Added support for processing several files or folders at once.
0.4.0
-----
- Fixed formatting of django ``{# ... #}`` tags

View file

@ -34,21 +34,24 @@ from .reformat import reformat_file
from .settings import Config
def get_src(src: Path, config: Config) -> List[Path]:
def get_src(src: List[Path], config: Config) -> List[Path]:
"""Get source files."""
if Path.is_file(src):
return [src]
paths = []
for item in src:
if Path.is_file(item):
paths.append(item)
# remove leading . from extension
extension = str(config.extension)
extension = extension[1:] if extension.startswith(".") else extension
else:
# remove leading . from extension
extension = str(config.extension)
extension = extension[1:] if extension.startswith(".") else extension
paths = list(
filter(
lambda x: not re.search(config.exclude, str(x), re.VERBOSE),
list(src.glob(f"**/*.{extension}")),
)
)
paths.extend(
filter(
lambda x: not re.search(config.exclude, str(x), re.VERBOSE),
list(item.glob(f"**/*.{extension}")),
)
)
if len(paths) == 0:
echo(Fore.BLUE + "No files to check! 😢")
@ -150,7 +153,8 @@ def build_quantity_tense(size: int) -> str:
type=click.Path(
exists=True, file_okay=True, dir_okay=True, readable=True, allow_dash=True
),
nargs=1,
nargs=-1,
required=True,
metavar="SRC ...",
)
@click.version_option(package_name="djlint")
@ -186,14 +190,19 @@ def build_quantity_tense(size: int) -> str:
help="Do not print diff when reformatting.",
)
def main(
src: str, extension: str, ignore: str, reformat: bool, check: bool, quiet: bool
src: List[str],
extension: str,
ignore: str,
reformat: bool,
check: bool,
quiet: bool,
) -> None:
"""djLint · lint and reformat HTML templates."""
config = Config(src, extension=extension, ignore=ignore, quiet=quiet)
config = Config(src[0], extension=extension, ignore=ignore, quiet=quiet)
temp_file = None
if src == "-":
if "-" in src:
stdin_stream = click.get_text_stream("stdin")
stdin_text = stdin_stream.read()
@ -201,10 +210,10 @@ def main(
temp_file.write(str.encode(stdin_text))
temp_file.seek(0)
file_list = get_src(Path(temp_file.name), config)
file_list = get_src([Path(temp_file.name)], config)
else:
file_list = get_src(Path(src), config)
file_list = get_src([Path(x) for x in src], config)
if len(file_list) == 0:
return

View file

@ -0,0 +1 @@
{{fail}}

View file

@ -0,0 +1 @@
{{fail}}

View file

@ -0,0 +1 @@
{{fail}}

View file

@ -7,7 +7,7 @@ run::
for a single test::
pytest tests/test_djlint.py::test_python_call --cov=src/djlint \
pytest tests/test_djlint.py::test_stdin --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
or::
@ -58,6 +58,14 @@ def test_existing_file(runner: CliRunner) -> None:
assert str(Path("tests/bad.html")) in result.output
def test_multiple_files(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/multiple_files/a", "tests/multiple_files/b", "--check"]
)
assert result.exit_code == 1
assert "3 files would be updated." in result.output
def test_bad_path(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/nowhere"])
assert result.exit_code == 2
@ -92,6 +100,12 @@ def test_empty_file(runner: CliRunner, tmp_file: TextIO) -> None:
def test_stdin(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["-"], input="<div></div>")
assert result.exit_code == 0
assert "Linted 1 file" in result.output
# check with multiple inputs
result = runner.invoke(djlint, ["-", "-"], input="<div></div>")
assert result.exit_code == 0
assert "Linted 1 file" in result.output
def test_check(runner: CliRunner, tmp_file: TextIO) -> None: