fixed #149, added test

This commit is contained in:
Christopher Pickering 2021-11-26 14:01:54 +01:00
parent 3281b55f61
commit 48c2f32081
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
4 changed files with 25 additions and 16 deletions

View file

@ -1,5 +1,6 @@
"""Build djLint console output."""
import shutil
from pathlib import Path
from typing import Any, Dict, List
import regex as re
@ -26,13 +27,11 @@ def print_output(
for error in sorted(file_errors, key=lambda x: next(iter(list(x.values())[0]))):
if error.get("format_message") and config.stdin is False:
# reformat message
format_error_count += build_check_output(
error["format_message"], config.quiet
)
format_error_count += build_check_output(error["format_message"], config)
if error.get("lint_message"):
# lint message
lint_error_count += build_output(error["lint_message"])
lint_error_count += build_output(error["lint_message"], config)
tense_message = (
build_quantity(format_error_count) + " would be"
@ -67,7 +66,15 @@ def print_output(
return lint_error_count + format_error_count
def build_output(error: dict) -> int:
def build_relative_path(url: str, project_root: Path) -> str:
"""Get path relative to project."""
if project_root != url:
return str(Path(url).relative_to(project_root.resolve()))
return url
def build_output(error: dict, config: Config) -> int:
"""Build output for file errors."""
errors = sorted(
list(error.values())[0], key=lambda x: tuple(map(int, x["line"].split(":")))
@ -78,7 +85,7 @@ def build_output(error: dict) -> int:
return 0
echo(
f"{Fore.GREEN}{Style.BRIGHT}\n{list(error.keys())[0]}\n{Style.DIM}"
f"{Fore.GREEN}{Style.BRIGHT}\n{build_relative_path(list(error.keys())[0],config.project_root)}\n{Style.DIM}"
+ "".join(["" for x in range(1, width)])
+ Style.RESET_ALL
)
@ -102,7 +109,7 @@ def build_output(error: dict) -> int:
return len(errors)
def build_check_output(errors: dict, quiet: bool) -> int:
def build_check_output(errors: dict, config: Config) -> int:
"""Build output for reformat check."""
if len(errors) == 0:
return 0
@ -110,21 +117,22 @@ def build_check_output(errors: dict, quiet: bool) -> int:
color = {"-": Fore.YELLOW, "+": Fore.GREEN, "@": Style.BRIGHT + Fore.BLUE}
width, _ = shutil.get_terminal_size()
if quiet is True and len(list(errors.values())[0]) > 0:
if config.quiet is True and len(list(errors.values())[0]) > 0:
echo(
Fore.GREEN
+ Style.BRIGHT
+ build_relative_path(list(errors.keys())[0], config.project_root)
+ str(list(errors.keys())[0])
+ Style.DIM
+ Style.RESET_ALL
)
elif quiet is False and len(list(errors.values())[0]) > 0:
elif config.quiet is False and len(list(errors.values())[0]) > 0:
echo(
Fore.GREEN
+ Style.BRIGHT
+ "\n"
+ str(list(errors.keys())[0])
+ build_relative_path(list(errors.keys())[0], config.project_root)
+ "\n"
+ Style.DIM
+ "".join(["" for x in range(1, width)])

View file

@ -160,11 +160,11 @@ class Config:
self.lint = lint
self.stdin = "-" in src
project_root = find_project_root(Path(src))
self.project_root = find_project_root(Path(src))
djlint_settings = load_pyproject_settings(project_root)
djlint_settings = load_pyproject_settings(self.project_root)
self.gitignore = load_gitignore(project_root)
self.gitignore = load_gitignore(self.project_root)
# custom configuration options
self.use_gitignore: bool = use_gitignore or djlint_settings.get(
@ -205,7 +205,7 @@ class Config:
(Path(__file__).parent / "rules.yaml").read_text(encoding="utf8"),
Loader=yaml.SafeLoader,
)
+ load_custom_rules(project_root)
+ load_custom_rules(self.project_root)
)
self.linter_rules = list(

View file

@ -15,7 +15,7 @@ def get_src(src: List[Path], config: Config) -> List[Path]:
for item in src:
# normalize path
normalized_item = Path(item).resolve()
normalized_item = item.resolve()
if (
Path.is_file(normalized_item)

View file

@ -7,7 +7,7 @@ run::
for a single test, run::
pytest tests/test_config.py::test_blank_lines_after_tag --cov=src/djlint \
pytest tests/test_config.py::test_exclude --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
@ -82,6 +82,7 @@ def test_indent(runner: CliRunner) -> None:
def test_exclude(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_excludes"])
print(result.output)
assert """html.html""" in result.output
assert """excluded.html""" not in result.output
assert """foo/excluded.html""" not in result.output