From 59d33b460d765e632d5c2f34fbe661bc7707e2e6 Mon Sep 17 00:00:00 2001 From: dorschw <81086590+dorschw@users.noreply.github.com> Date: Wed, 4 Dec 2024 18:41:44 +0200 Subject: [PATCH 1/8] add "check" to the command, fail&log if stderr not empty --- riff/riff.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/riff/riff.py b/riff/riff.py index b3fd2c2..6f25253 100644 --- a/riff/riff.py +++ b/riff/riff.py @@ -4,6 +4,7 @@ from typing import NoReturn import typer +from packaging.version import InvalidVersion, Version from riff.logger import logger from riff.utils import ( @@ -19,9 +20,7 @@ class ArgumentNotSupportedError(Exception): ... -def run_ruff( - ruff_args: Sequence[str], -) -> subprocess.CompletedProcess: +def run_ruff(ruff_args: Sequence[str]) -> subprocess.CompletedProcess: """ Run Ruff with the given arguments. @@ -54,6 +53,7 @@ def run_ruff( ruff_command = " ".join( ( "ruff", + "check", *ruff_args, "--output-format=json", ) @@ -114,7 +114,6 @@ def validate_ruff_installation() -> None: """ Check whether ruff is installed, and its version is supported. """ - from packaging.version import InvalidVersion, Version try: ruff_version_process = subprocess.run( # noqa: S603 @@ -164,6 +163,10 @@ def main( # dead: disable ruff_process_result = run_ruff(context.args) except ArgumentNotSupportedError: raise typer.Exit(1) from None # no need for whole stack trace + if ruff_process_result.stderr: + raise RuntimeError( + f"Ruff failed running, stderr:\n{ruff_process_result.stderr}" + ) if not ( filtered_violations := filter_violations( From 8e14f1d38adbd1d67cdfe2d6150ed957dcb5264f Mon Sep 17 00:00:00 2001 From: dorschw <81086590+dorschw@users.noreply.github.com> Date: Wed, 4 Dec 2024 18:41:53 +0200 Subject: [PATCH 2/8] remove EM101 --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index ed4cdad..259cf4f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,6 +54,7 @@ lint.ignore = [ "TRY003", "TD", "W293", + "EM101", ] target-version = "py310" line-length = 88 From 110f695f0b94952ed24ddf5b4b9911940aadad45 Mon Sep 17 00:00:00 2001 From: dorschw <81086590+dorschw@users.noreply.github.com> Date: Wed, 4 Dec 2024 18:42:06 +0200 Subject: [PATCH 3/8] add logs to parse_ruff_output --- riff/utils.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/riff/utils.py b/riff/utils.py index 746384e..8f64e54 100644 --- a/riff/utils.py +++ b/riff/utils.py @@ -11,12 +11,21 @@ from riff.violation import Violation -def parse_ruff_output(ruff_result_raw: str) -> tuple[Violation, ...]: - if not ruff_result_raw: +def parse_ruff_output(ruff_stdout: str) -> tuple[Violation, ...]: + """ + This method assumes stderr was empty + """ + logger.debug(f"{ruff_stdout=}") + + if not ruff_stdout: + logger.debug("No ruff output, assuming no violations") return () - with logger.catch(json.JSONDecodeError, reraise=True): - raw_violations = json.loads(ruff_result_raw) + try: + raw_violations = json.loads(ruff_stdout) + except json.JSONDecodeError: + logger.exception("Could not parse Ruff output as JSON") + raise violations = tuple(map(Violation.parse, raw_violations)) logger.debug(f"parsed {len(violations)} ruff violations") From ee9b957c3e10f64455a8a2037a87569498530aeb Mon Sep 17 00:00:00 2001 From: dorschw <81086590+dorschw@users.noreply.github.com> Date: Wed, 4 Dec 2024 18:46:03 +0200 Subject: [PATCH 4/8] add "check" to the command, remove from args if received --- riff/riff.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/riff/riff.py b/riff/riff.py index 6f25253..44eb90b 100644 --- a/riff/riff.py +++ b/riff/riff.py @@ -20,7 +20,7 @@ class ArgumentNotSupportedError(Exception): ... -def run_ruff(ruff_args: Sequence[str]) -> subprocess.CompletedProcess: +def run_ruff(ruff_args: list[str]) -> subprocess.CompletedProcess: """ Run Ruff with the given arguments. @@ -45,10 +45,14 @@ def run_ruff(ruff_args: Sequence[str]) -> subprocess.CompletedProcess: of the returned CompletedProcess object will reflect that status code. """ if not ruff_args: - ruff_args = (".",) + logger.debug("No ruff arguments provided, using default: '.'") + ruff_args = ["."] elif "--output-format" in ruff_args: logger.error("the `--output-format` argument is not (yet) supported") raise ArgumentNotSupportedError + elif "check" in ruff_args: + logger.debug("Removing 'check' from ruff_args, it will be added later") + ruff_args.remove("check") ruff_command = " ".join( ( From b8c6fc35fab60c6c8c5fa8de92ff253dd901bca1 Mon Sep 17 00:00:00 2001 From: dorschw <81086590+dorschw@users.noreply.github.com> Date: Wed, 4 Dec 2024 18:55:31 +0200 Subject: [PATCH 5/8] format --- riff/riff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/riff/riff.py b/riff/riff.py index 44eb90b..6a21554 100644 --- a/riff/riff.py +++ b/riff/riff.py @@ -1,5 +1,5 @@ import subprocess -from collections.abc import Iterable, Sequence +from collections.abc import Iterable from pathlib import Path from typing import NoReturn From e9645e91d83072a28bb5223b0831d44c933c278f Mon Sep 17 00:00:00 2001 From: dorschw <81086590+dorschw@users.noreply.github.com> Date: Tue, 10 Dec 2024 17:02:38 +0200 Subject: [PATCH 6/8] use exception --- riff/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/riff/utils.py b/riff/utils.py index 8f64e54..40446f8 100644 --- a/riff/utils.py +++ b/riff/utils.py @@ -24,7 +24,7 @@ def parse_ruff_output(ruff_stdout: str) -> tuple[Violation, ...]: try: raw_violations = json.loads(ruff_stdout) except json.JSONDecodeError: - logger.exception("Could not parse Ruff output as JSON") + logger.error("Could not parse Ruff output as JSON:") raise violations = tuple(map(Violation.parse, raw_violations)) From f4694b0822e29eb9f92339742c77a0de2790094a Mon Sep 17 00:00:00 2001 From: dorschw <81086590+dorschw@users.noreply.github.com> Date: Tue, 10 Dec 2024 17:02:57 +0200 Subject: [PATCH 7/8] remove `:` --- riff/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/riff/utils.py b/riff/utils.py index 40446f8..0d83ed8 100644 --- a/riff/utils.py +++ b/riff/utils.py @@ -24,7 +24,7 @@ def parse_ruff_output(ruff_stdout: str) -> tuple[Violation, ...]: try: raw_violations = json.loads(ruff_stdout) except json.JSONDecodeError: - logger.error("Could not parse Ruff output as JSON:") + logger.error("Could not parse Ruff output as JSON") raise violations = tuple(map(Violation.parse, raw_violations)) From 0c7af83d45d94bb19345fc9a4019f3c108223df7 Mon Sep 17 00:00:00 2001 From: dorschw <81086590+dorschw@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:07:09 +0200 Subject: [PATCH 8/8] prettier exit on stderr --- riff/riff.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/riff/riff.py b/riff/riff.py index 6a21554..88fce81 100644 --- a/riff/riff.py +++ b/riff/riff.py @@ -168,9 +168,8 @@ def main( # dead: disable except ArgumentNotSupportedError: raise typer.Exit(1) from None # no need for whole stack trace if ruff_process_result.stderr: - raise RuntimeError( - f"Ruff failed running, stderr:\n{ruff_process_result.stderr}" - ) + logger.error(f"Ruff failed running, stderr:\n{ruff_process_result.stderr}") + raise typer.Exit(1) if not ( filtered_violations := filter_violations(