Skip to content

Commit

Permalink
Merge pull request #5 from businho/ruff-format
Browse files Browse the repository at this point in the history
Ruff format
  • Loading branch information
iurisilvio authored Oct 30, 2023
2 parents 16004c5 + db94be4 commit 812fe41
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ pip install pytest-ruff
## Usage

```shell
pytest --ruff
pytest --ruff --ruff-format
```

The plugin will run one ruff check test per file and fail if code is not ok for ruff.

Format command only checks for format and fails for formatting errors.

## Configuration

You can override ruff configuration options by placing a `pyproject.toml` or `ruff.toml` file in your project directory, like when using standalone ruff.
Expand Down
39 changes: 30 additions & 9 deletions pytest_ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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")


def pytest_configure(config):
Expand Down Expand Up @@ -54,10 +55,34 @@ class RuffError(Exception):

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


def check_file(path):
ruff = find_ruff_bin()
command = [ruff, "check", path, '--quiet', '--show-source']
child = Popen(command, stdout=PIPE, stderr=PIPE)
stdout, _ = child.communicate()
if stdout:
raise RuffError(stdout.decode())


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

if child.returncode == 1:
raise RuffError("File would be reformatted")


class RuffItem(pytest.Item):
handler = check_file

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

def runtest(self):
check_file(self.fspath)
self.handler(self.fspath)

ruffmtimes = self.config.stash.get(_MTIMES_STASH_KEY, None)
if ruffmtimes:
ruffmtimes[str(self.fspath)] = self._ruffmtime
Expand All @@ -79,10 +105,5 @@ def reportinfo(self):
return (self.fspath, None, "")


def check_file(path):
ruff = find_ruff_bin()
command = [ruff, path, '--quiet', '--show-source']
child = Popen(command, stdout=PIPE, stderr=PIPE)
stdout, _ = child.communicate()
if stdout:
raise RuffError(stdout.decode())
class RuffFormatItem(RuffItem):
handler = format_file
File renamed without changes.
1 change: 1 addition & 0 deletions tests/assets/format_broken.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def foo(): pass
7 changes: 6 additions & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ 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("tests/assets/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("tests/assets/format_broken.py")

0 comments on commit 812fe41

Please sign in to comment.