Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iurisilvio committed Oct 31, 2023
1 parent 55fe6ee commit 3e0cfef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
27 changes: 17 additions & 10 deletions pytest_ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ class RuffError(Exception):

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


def check_file(self, path):
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"]
child = Popen(command, stdout=PIPE, stderr=PIPE)
Expand All @@ -73,7 +74,7 @@ def check_file(self, path):
raise RuffError(stdout.decode())


def format_file(self, path):
def format_file(path):
ruff = find_ruff_bin()
command = [ruff, "format", path, "--quiet", "--check", "--force-exclude"]
with Popen(command) as child:
Expand All @@ -84,7 +85,7 @@ def format_file(self, path):


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

def __init__(self, *k, **kwargs):
super().__init__(*k, **kwargs)
Expand All @@ -107,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)
7 changes: 2 additions & 5 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def test_configure_without_ruff(mocker):

def test_check_file():
with pytest.raises(pytest_ruff.RuffError, match=r"`os` imported but unused"):
pytest_ruff.check_file(None, path="tests/assets/check_broken.py")
pytest_ruff.check_file("tests/assets/check_broken.py")


def test_format_file():
with pytest.raises(pytest_ruff.RuffError, match=r"File would be reformatted"):
pytest_ruff.format_file(None, path="tests/assets/format_broken.py")
pytest_ruff.format_file("tests/assets/format_broken.py")


def test_pytest_ruff():
Expand All @@ -46,9 +46,6 @@ def test_pytest_ruff():
).communicate()
out_utf8 = out.decode("utf-8")
assert "`os` imported but unused" in out_utf8
assert (
"force-exclude:1:1: E902 No such file or directory (os error 2)" not in out_utf8
)


def test_pytest_ruff_format():
Expand Down

0 comments on commit 3e0cfef

Please sign in to comment.