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

Handle ruff config error #25

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pytest_ruff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ def check_file(path):
"--force-exclude",
]
child = Popen(command, stdout=PIPE, stderr=PIPE)
stdout, _ = child.communicate()
if stdout:
stdout, stderr = child.communicate()

if child.returncode == 1:
raise RuffError(stdout.decode())

if child.returncode == 2:
raise RuffError(stderr.decode())


def format_file(path):
ruff = find_ruff_bin()
Expand All @@ -109,6 +113,9 @@ def format_file(path):
if child.returncode == 1:
raise RuffError("File would be reformatted")

if child.returncode == 2:
raise RuffError("Ruff terminated abnormally")


def pytest_exception_interact(node, call, report):
if isinstance(call.excinfo.value, RuffError):
Expand Down
1 change: 1 addition & 0 deletions tests/assets/broken_config/empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty file to try to run ruff, but config in ruff.toml is broken.
1 change: 1 addition & 0 deletions tests/assets/broken_config/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
broken = true
17 changes: 17 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,20 @@ def test_pytest_ruff_noformat():
).communicate()
assert err.decode() == ""
assert "File would be reformatted" not in out.decode("utf-8")


def test_broken_ruff_config():
process = subprocess.Popen(
[
sys.executable,
"-m",
"pytest",
"--ruff",
"tests/assets/broken_config/empty.py",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, err = process.communicate()
assert err.decode() == ""
assert "unknown field `broken`" in out.decode()
Loading