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: pytest --ruff and --ruff-format don't work at the command line #8

Merged
merged 4 commits into from
Oct 31, 2023
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
32 changes: 21 additions & 11 deletions pytest_ruff.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from subprocess import Popen, PIPE

# Python<=3.8 don't support typing with builtin dict.
from typing import Dict

Expand All @@ -12,7 +13,9 @@
def pytest_addoption(parser):
group = parser.getgroup("general")
group.addoption("--ruff", action="store_true", help="enable checking with ruff")
group.addoption("--ruff-format", action="store_true", help="enable format checking with ruff")
group.addoption(

Check warning on line 16 in pytest_ruff.py

View check run for this annotation

Codecov / codecov/patch

pytest_ruff.py#L16

Added line #L16 was not covered by tests
"--ruff-format", action="store_true", help="enable format checking with ruff"
)


def pytest_configure(config):
Expand Down Expand Up @@ -55,15 +58,16 @@

class RuffFile(pytest.File):
def collect(self):
return [
RuffItem.from_parent(self, name="ruff"),
RuffFormatItem.from_parent(self, name="ruff::format"),
]

collection = []
if self.config.option.ruff:
collection.append(RuffItem)
if self.config.option.ruff_format:
collection.append(RuffFormatItem.from_parent(self, name="ruff::format"))

Check warning on line 65 in pytest_ruff.py

View check run for this annotation

Codecov / codecov/patch

pytest_ruff.py#L65

Added line #L65 was not covered by tests
return [Item.from_parent(self, name=Item.name) for Item in collection]

def check_file(path):
ruff = find_ruff_bin()
command = [ruff, "check", path, '--quiet', '--show-source', 'force-exclude']
command = [ruff, "check", path, "--quiet", "--show-source", "--force-exclude"]
child = Popen(command, stdout=PIPE, stderr=PIPE)
stdout, _ = child.communicate()
if stdout:
Expand All @@ -72,7 +76,7 @@

def format_file(path):
ruff = find_ruff_bin()
command = [ruff, "format", path, '--quiet', '--check', '--force-exclude']
command = [ruff, "format", path, "--quiet", "--check", "--force-exclude"]

Check warning on line 79 in pytest_ruff.py

View check run for this annotation

Codecov / codecov/patch

pytest_ruff.py#L79

Added line #L79 was not covered by tests
with Popen(command) as child:
pass

Expand All @@ -81,7 +85,7 @@


class RuffItem(pytest.Item):
handler = check_file
name = "ruff"

Check warning on line 88 in pytest_ruff.py

View check run for this annotation

Codecov / codecov/patch

pytest_ruff.py#L88

Added line #L88 was not covered by tests

def __init__(self, *k, **kwargs):
super().__init__(*k, **kwargs)
Expand All @@ -95,7 +99,7 @@
pytest.skip("file previously passed ruff checks")

def runtest(self):
self.handler(self.fspath)
self.handler(path=self.fspath)

ruffmtimes = self.config.stash.get(_MTIMES_STASH_KEY, None)
if ruffmtimes:
Expand All @@ -104,6 +108,12 @@
def reportinfo(self):
return (self.fspath, None, "")

def handler(self, path):

Check warning on line 111 in pytest_ruff.py

View check run for this annotation

Codecov / codecov/patch

pytest_ruff.py#L111

Added line #L111 was not covered by tests
return check_file(path)


class RuffFormatItem(RuffItem):
handler = format_file
name = "ruff::format"

Check warning on line 116 in pytest_ruff.py

View check run for this annotation

Codecov / codecov/patch

pytest_ruff.py#L116

Added line #L116 was not covered by tests

def handler(self, path):
return format_file(path)

Check warning on line 119 in pytest_ruff.py

View check run for this annotation

Codecov / codecov/patch

pytest_ruff.py#L118-L119

Added lines #L118 - L119 were not covered by tests
44 changes: 44 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import subprocess
import sys

import pytest

import pytest_ruff
Expand Down Expand Up @@ -33,3 +36,44 @@ def test_check_file():
def test_format_file():
with pytest.raises(pytest_ruff.RuffError, match=r"File would be reformatted"):
pytest_ruff.format_file("tests/assets/format_broken.py")


def test_pytest_ruff():
out, err = subprocess.Popen(
[sys.executable, "-m", "pytest", "--ruff", "tests/assets/check_broken.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).communicate()
out_utf8 = out.decode("utf-8")
assert "`os` imported but unused" in out_utf8


def test_pytest_ruff_format():
out, err = subprocess.Popen(
[
sys.executable,
"-m",
"pytest",
"--ruff",
"--ruff-format",
"tests/assets/format_broken.py",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).communicate()
assert "File would be reformatted" in out.decode("utf-8")


def test_pytest_ruff_noformat():
out, err = subprocess.Popen(
[
sys.executable,
"-m",
"pytest",
"--ruff",
"tests/assets/format_broken.py",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).communicate()
assert "File would be reformatted" not in out.decode("utf-8")
Loading