Skip to content

Commit

Permalink
Merge pull request #8 from skellys/fix/fix_v0_2
Browse files Browse the repository at this point in the history
fix: pytest --ruff and --ruff-format don't work at the command line
  • Loading branch information
iurisilvio committed Oct 31, 2023
2 parents df22c01 + 3e0cfef commit 2098829
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
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(
"--ruff-format", action="store_true", help="enable format checking with ruff"
)


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

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"))
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 check_file(path):

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

Expand All @@ -81,7 +85,7 @@ def format_file(path):


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

def __init__(self, *k, **kwargs):
super().__init__(*k, **kwargs)
Expand All @@ -95,7 +99,7 @@ def setup(self):
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 runtest(self):
def reportinfo(self):
return (self.fspath, None, "")

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


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

def handler(self, path):
return format_file(path)
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")

0 comments on commit 2098829

Please sign in to comment.