Merge pull request #425 from nix-community/more-ruff-lints

More ruff lints
This commit is contained in:
Jörg Thalheim 2023-11-29 13:25:18 +01:00 committed by GitHub
commit fdbd3fc59f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 85 additions and 69 deletions

View File

@ -1,8 +1,32 @@
[tool.ruff]
target-version = "py311"
line-length = 88
select = ["ALL"]
ignore = [
# pydocstyle
"D",
# Missing type annotation for `self` in method
"ANN101",
# Trailing comma missing
"COM812",
# Unnecessary `dict` call (rewrite as a literal)
"C408",
# Boolean-typed positional argument in function definition
"FBT001",
# Logging statement uses f-string
"G004",
# disabled on ruff's recommendation as causes problems with the formatter
"ISC001",
# Use of `assert` detected
"S101",
# `subprocess` call: check for execution of untrusted input
"S603",
select = ["E", "F", "I"]
ignore = [ "E501" ]
# FIXME? Maybe we should enable these?
"PLR0913", # Too many arguments in function definition (7 > 5)
"PLR2004", # Magic value used in comparison, consider replacing 4 with a constant variable
"FBT002", # Boolean default positional argument in function definition
]
[tool.mypy]
python_version = "3.10"

View File

@ -1,23 +0,0 @@
[wheel]
universal = 1
[pycodestyle]
max-line-length = 88
ignore = E501,E741,W503
[flake8]
max-line-length = 88
ignore = E501,E741,W503
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist
[mypy]
warn_redundant_casts = true
disallow_untyped_calls = true
disallow_untyped_defs = true
no_implicit_optional = true
[mypy-pytest.*]
ignore_missing_imports = True
[isort]
profile = black

0
tests/__init__.py Normal file
View File

View File

@ -1,4 +1,4 @@
pytest_plugins = [
"direnv_project",
"root",
"tests.direnv_project",
"tests.root",
]

View File

@ -1,22 +1,23 @@
import shutil
import textwrap
from collections.abc import Iterator
from dataclasses import dataclass
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Iterator
import pytest
from procs import run
from .procs import run
@dataclass
class DirenvProject:
dir: Path
directory: Path
nix_direnv: Path
@property
def envrc(self) -> Path:
return self.dir / ".envrc"
return self.directory / ".envrc"
def setup_envrc(self, content: str, strict_env: bool) -> None:
text = textwrap.dedent(
@ -27,23 +28,23 @@ class DirenvProject:
"""
)
self.envrc.write_text(text)
run(["direnv", "allow"], cwd=self.dir)
run(["direnv", "allow"], cwd=self.directory)
@pytest.fixture
@pytest.fixture()
def direnv_project(test_root: Path, project_root: Path) -> Iterator[DirenvProject]:
"""
Setups a direnv test project
"""
with TemporaryDirectory() as _dir:
dir = Path(_dir) / "proj"
shutil.copytree(test_root / "testenv", dir)
shutil.copyfile(project_root / "flake.nix", dir / "flake.nix")
shutil.copyfile(project_root / "flake.lock", dir / "flake.lock")
shutil.copyfile(project_root / "treefmt.nix", dir / "treefmt.nix")
directory = Path(_dir) / "proj"
shutil.copytree(test_root / "testenv", directory)
shutil.copyfile(project_root / "flake.nix", directory / "flake.nix")
shutil.copyfile(project_root / "flake.lock", directory / "flake.lock")
shutil.copyfile(project_root / "treefmt.nix", directory / "treefmt.nix")
nix_direnv = project_root / "direnvrc"
c = DirenvProject(Path(dir), nix_direnv)
c = DirenvProject(Path(directory), nix_direnv)
try:
yield c
finally:

View File

@ -1,23 +1,27 @@
import logging
import shlex
import subprocess
from pathlib import Path
from typing import IO, Any, List, Optional, Union
from typing import IO, Any
_FILE = Union[None, int, IO[Any]]
_DIR = Union[None, Path, str]
_FILE = None | int | IO[Any]
_DIR = None | Path | str
log = logging.getLogger(__name__)
def run(
cmd: List[str],
cmd: list[str],
text: bool = True,
check: bool = True,
cwd: _DIR = None,
stderr: _FILE = None,
stdout: _FILE = None,
env: Optional[dict[str, str]] = None,
env: dict[str, str] | None = None,
) -> subprocess.CompletedProcess:
if cwd is not None:
print(f"cd {cwd}")
print("$ " + " ".join(cmd))
log.debug(f"cd {cwd}")
log.debug(f"$ {shlex.join(cmd)}")
return subprocess.run(
cmd, text=text, check=check, cwd=cwd, stderr=stderr, stdout=stdout, env=env
)

View File

@ -6,7 +6,7 @@ TEST_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TEST_ROOT.parent
@pytest.fixture
@pytest.fixture()
def test_root() -> Path:
"""
Root directory of the tests
@ -14,7 +14,7 @@ def test_root() -> Path:
return TEST_ROOT
@pytest.fixture
@pytest.fixture()
def project_root() -> Path:
"""
Root directory of the tests

View File

@ -1,22 +1,26 @@
import logging
import subprocess
import sys
import unittest
import pytest
from direnv_project import DirenvProject
from procs import run
from .direnv_project import DirenvProject
from .procs import run
log = logging.getLogger(__name__)
def common_test(direnv_project: DirenvProject) -> None:
run(["nix-collect-garbage"])
testenv = str(direnv_project.dir)
testenv = str(direnv_project.directory)
out1 = run(
["direnv", "exec", testenv, "hello"],
stderr=subprocess.PIPE,
check=False,
cwd=direnv_project.dir,
cwd=direnv_project.directory,
)
sys.stderr.write(out1.stderr)
assert out1.returncode == 0
@ -29,7 +33,7 @@ def common_test(direnv_project: DirenvProject) -> None:
["direnv", "exec", testenv, "hello"],
stderr=subprocess.PIPE,
check=False,
cwd=direnv_project.dir,
cwd=direnv_project.directory,
)
sys.stderr.write(out2.stderr)
assert out2.returncode == 0
@ -38,23 +42,25 @@ def common_test(direnv_project: DirenvProject) -> None:
def common_test_clean(direnv_project: DirenvProject) -> None:
testenv = str(direnv_project.dir)
testenv = str(direnv_project.directory)
out3 = run(
["direnv", "exec", testenv, "hello"],
stderr=subprocess.PIPE,
check=False,
cwd=direnv_project.dir,
cwd=direnv_project.directory,
)
sys.stderr.write(out3.stderr)
files = [
path for path in (direnv_project.dir / ".direnv").iterdir() if path.is_file()
path
for path in (direnv_project.directory / ".direnv").iterdir()
if path.is_file()
]
rcs = [f for f in files if f.match("*.rc")]
profiles = [f for f in files if not f.match("*.rc")]
if len(rcs) != 1 or len(profiles) != 1:
print(files)
log.debug(files)
assert len(rcs) == 1
assert len(profiles) == 1
@ -75,11 +81,11 @@ def test_use_nix(direnv_project: DirenvProject, strict_env: bool) -> None:
def test_use_flake(direnv_project: DirenvProject, strict_env: bool) -> None:
direnv_project.setup_envrc("use flake", strict_env=strict_env)
common_test(direnv_project)
inputs = list((direnv_project.dir / ".direnv/flake-inputs").iterdir())
inputs = list((direnv_project.directory / ".direnv/flake-inputs").iterdir())
# should only contain our flake-utils flake
if len(inputs) != 4:
run(["nix", "flake", "archive", "--json"], cwd=direnv_project.dir)
print(inputs)
run(["nix", "flake", "archive", "--json"], cwd=direnv_project.directory)
log.debug(inputs)
assert len(inputs) == 4
for symlink in inputs:
assert symlink.is_dir()

View File

@ -1,31 +1,35 @@
import logging
import os
import shlex
import subprocess
import sys
import unittest
from typing import Optional
import pytest
from direnv_project import DirenvProject
from procs import run
from .direnv_project import DirenvProject
from .procs import run
log = logging.getLogger(__name__)
def direnv_exec(
direnv_project: DirenvProject, cmd: str, env: Optional[dict[str, str]] = None
direnv_project: DirenvProject, cmd: str, env: dict[str, str] | None = None
) -> None:
args = ["direnv", "exec", str(direnv_project.dir), "sh", "-c", cmd]
print("$ " + " ".join(args))
args = ["direnv", "exec", str(direnv_project.directory), "sh", "-c", cmd]
log.debug(f"$ {shlex.join(args)}")
out = run(
args,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
check=False,
cwd=direnv_project.dir,
cwd=direnv_project.directory,
env=env,
)
sys.stdout.write(out.stdout)
sys.stderr.write(out.stderr)
assert out.returncode == 0
assert "OK\n" == out.stdout
assert out.stdout == "OK\n"
assert "renewed cache" in out.stderr
@ -57,7 +61,7 @@ def test_no_files(direnv_project: DirenvProject, strict_env: bool) -> None:
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
check=False,
cwd=direnv_project.dir,
cwd=direnv_project.directory,
)
assert out.returncode == 0
assert 'Loaded watch: "."' not in out.stdout