Skip to content

Commit

Permalink
Handle ruff config error (#25)
Browse files Browse the repository at this point in the history
* Fail when ruff config is broken

* Add a test with broken ruff config
  • Loading branch information
iurisilvio authored Jul 9, 2024
1 parent fd4c1e9 commit 759cacd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
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()

0 comments on commit 759cacd

Please sign in to comment.