Skip to content

Commit

Permalink
Address some ruff errors (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Nov 18, 2024
1 parent 52c5540 commit b7acaeb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 36 deletions.
14 changes: 1 addition & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,7 @@ ignore = [
"INP001", # "is part of an implicit namespace package", all false positives
"ISC001", # conflicts with COM812 on format
"PLW2901", # PLW2901: Redefined loop variable
"RET504", # Unnecessary variable assignment before `return` statement
# temporary disabled until we fix them:
"ANN",
"ARG001",
"PTH109",
"PTH112",
"PTH113",
"PTH123",
"S603",
"S605",
"S607",
"SIM117",
"UP022"
"RET504" # Unnecessary variable assignment before `return` statement
]
select = ["ALL"]

Expand Down
8 changes: 3 additions & 5 deletions src/tox_extra/bindep.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import logging
import os
import subprocess
import sys
from functools import cache
Expand All @@ -21,17 +20,16 @@ def check_bindep(path: Path, profiles: Iterable[str] | None = None) -> None:
"""Check bindeps requirements or exit."""
if profiles is None: # pragma: no cover
profiles = []
if os.path.isfile(path / "bindep.txt"):
if (path / "bindep.txt").is_file():
# as 'bindep --profiles' does not show user defined profiles like 'test'
# it makes no sense to list them.
cmd = [sys.executable, "-m", "bindep", "-b", *sorted(profiles)]

result = subprocess.run(
result = subprocess.run( # noqa: S603
cmd,
check=False,
text=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
capture_output=True,
cwd=path,
)
if result.returncode:
Expand Down
20 changes: 11 additions & 9 deletions src/tox_extra/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import os
import pathlib
import shutil
import sys
from typing import TYPE_CHECKING, Any

Expand All @@ -30,17 +31,18 @@

def is_git_dirty(path: str) -> bool:
"""Reports if the git repository at the given path is dirty."""
if os.path.isdir(f"{path}/.git"):
git_path = shutil.which("git")
if pathlib.Path(f"{path}/.git").is_dir():
_environ = dict(os.environ)
try:
repo = git.Repo(os.getcwd())
repo = git.Repo(pathlib.Path.cwd())
if repo.is_dirty(untracked_files=True):
os.system("git status -s")
os.system(f"{git_path} status -s") # noqa: S605
# We want to display long diff only on non-interactive shells,
# like CI/CD pipelines because on local shell, the user can
# decide to run it himself if the status line was not enogh.
if not os.isatty(sys.stdout.fileno()):
os.system("git --no-pager diff -U0 --minimal")
os.system(f"{git_path} --no-pager diff -U0 --minimal") # noqa: S605
return True
finally:
os.environ.clear()
Expand All @@ -63,9 +65,9 @@ def tox_add_option(parser: ArgumentParser) -> None:
# pylint: disable=unused-argument
def tox_on_install(
tox_env: ToxEnv,
arguments: Any,
section: str,
of_type: str,
arguments: Any, # noqa: ARG001,ANN401
section: str, # noqa: ARG001
of_type: str, # noqa: ARG001
) -> None:
"""Runs just before installing package."""
if os.environ.get("TOX_EXTRA_BINDEP", "1") != "0":
Expand All @@ -87,8 +89,8 @@ def tox_on_install(
# pylint: disable=unused-argument
def tox_after_run_commands(
tox_env: ToxEnv,
exit_code: int,
outcomes: list[Outcome],
exit_code: int, # noqa: ARG001
outcomes: list[Outcome], # noqa: ARG001
) -> None:
"""Hook that runs after test commands."""
allow_dirty = getattr(tox_env.options, "allow_dirty", False)
Expand Down
8 changes: 5 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import functools
import os
from pathlib import Path
from typing import Any, Callable


def preserve_cwd(function):
def preserve_cwd(function: Callable) -> Callable:
"""Decorator for restoring cwd."""

@functools.wraps(function)
def decorator(*args, **kwargs):
cwd = os.getcwd()
def decorator(*args: Any, **kwargs: Any): # noqa: ANN202, ANN401
cwd = Path.cwd()
try:
return function(*args, **kwargs)
finally:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_bindep.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
def test_bindep(folder: str, expected_rc: int) -> None:
"""Tests that running tox with a bindep file that is missing deps fails."""
os.chdir(folder)
with patch("sys.argv", ["tox"]):
with pytest.raises(SystemExit) as exc:
run_module("tox", run_name="__main__")
with patch("sys.argv", ["tox"]), pytest.raises(SystemExit) as exc:
run_module("tox", run_name="__main__")
assert exc.type is SystemExit
assert exc.value.code == expected_rc
6 changes: 3 additions & 3 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


@preserve_cwd
def test_fail_if_dirty(tmp_path, monkeypatch: pytest.MonkeyPatch) -> None:
def test_fail_if_dirty(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Validated that it fails when drity."""
# We need to mock sys.argv because run_module will look at it and fail
# as the argv received of pytest would leak into our tox module call.
Expand All @@ -26,7 +26,7 @@ def test_fail_if_dirty(tmp_path, monkeypatch: pytest.MonkeyPatch) -> None:
os.chdir(path)

# add tox.ini and .gitignore files (untracked)
with open(Path(tmp_path) / "tox.ini", "w", encoding="utf-8") as file:
with (Path(tmp_path) / "tox.ini").open("w", encoding="utf-8") as file:
file.write(TOX_SAMPLE)

# check running tox w/o git repo is passing
Expand All @@ -47,7 +47,7 @@ def test_fail_if_dirty(tmp_path, monkeypatch: pytest.MonkeyPatch) -> None:
output = check_output("git status --porcelain", shell=True, universal_newlines=True)
assert output == ""

with open(Path(tmp_path) / ".gitignore", "w", encoding="utf-8") as file:
with (Path(tmp_path) / ".gitignore").open("w", encoding="utf-8") as file:
file.write(".tox\n")

result = run(
Expand Down

0 comments on commit b7acaeb

Please sign in to comment.