Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix false-negative when running with newer versions of ruff #121

Merged
merged 10 commits into from
Dec 10, 2024
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ lint.ignore = [
"TRY003",
"TD",
"W293",
"EM101",
]
target-version = "py310"
line-length = 88
Expand Down
18 changes: 12 additions & 6 deletions riff/riff.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import subprocess
from collections.abc import Iterable, Sequence
from collections.abc import Iterable
from pathlib import Path
from typing import NoReturn

import typer
from packaging.version import InvalidVersion, Version

from riff.logger import logger
from riff.utils import (
Expand All @@ -19,9 +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.

Expand All @@ -46,14 +45,19 @@ def run_ruff(
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(
(
"ruff",
"check",
*ruff_args,
"--output-format=json",
)
Expand Down Expand Up @@ -114,7 +118,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
Expand Down Expand Up @@ -164,6 +167,9 @@ 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:
logger.error(f"Ruff failed running, stderr:\n{ruff_process_result.stderr}")
raise typer.Exit(1)

if not (
filtered_violations := filter_violations(
Expand Down
17 changes: 13 additions & 4 deletions riff/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.error("Could not parse Ruff output as JSON")
raise

violations = tuple(map(Violation.parse, raw_violations))
logger.debug(f"parsed {len(violations)} ruff violations")
Expand Down
Loading