From 759cacd7cae8decaec4ff8b7c9aa4e447ba5a9c5 Mon Sep 17 00:00:00 2001 From: Iuri de Silvio Date: Tue, 9 Jul 2024 21:27:37 +0200 Subject: [PATCH] Handle ruff config error (#25) * Fail when ruff config is broken * Add a test with broken ruff config --- pytest_ruff/__init__.py | 11 +++++++++-- tests/assets/broken_config/empty.py | 1 + tests/assets/broken_config/ruff.toml | 1 + tests/test_plugin.py | 17 +++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/assets/broken_config/empty.py create mode 100644 tests/assets/broken_config/ruff.toml diff --git a/pytest_ruff/__init__.py b/pytest_ruff/__init__.py index 8ea9826..7709aca 100644 --- a/pytest_ruff/__init__.py +++ b/pytest_ruff/__init__.py @@ -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() @@ -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): diff --git a/tests/assets/broken_config/empty.py b/tests/assets/broken_config/empty.py new file mode 100644 index 0000000..fd12abc --- /dev/null +++ b/tests/assets/broken_config/empty.py @@ -0,0 +1 @@ +# Empty file to try to run ruff, but config in ruff.toml is broken. diff --git a/tests/assets/broken_config/ruff.toml b/tests/assets/broken_config/ruff.toml new file mode 100644 index 0000000..dcf7f74 --- /dev/null +++ b/tests/assets/broken_config/ruff.toml @@ -0,0 +1 @@ +broken = true diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 7775a22..fd1c8c2 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -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()