From 48c2f32081fb2500132b05e859666a73267e7a79 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Fri, 26 Nov 2021 14:01:54 +0100 Subject: [PATCH] fixed #149, added test --- src/djlint/output.py | 28 ++++++++++++++++++---------- src/djlint/settings.py | 8 ++++---- src/djlint/src.py | 2 +- tests/test_config.py | 3 ++- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/djlint/output.py b/src/djlint/output.py index f7fed54..28d191d 100644 --- a/src/djlint/output.py +++ b/src/djlint/output.py @@ -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)]) diff --git a/src/djlint/settings.py b/src/djlint/settings.py index fc78f2d..c7fba60 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -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( diff --git a/src/djlint/src.py b/src/djlint/src.py index 26c9d39..d926698 100644 --- a/src/djlint/src.py +++ b/src/djlint/src.py @@ -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) diff --git a/tests/test_config.py b/tests/test_config.py index 885c57f..eec012c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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