mirror of
https://github.com/nix-community/nix-direnv.git
synced 2024-09-19 07:17:10 +03:00
Merge pull request #425 from nix-community/more-ruff-lints
More ruff lints
This commit is contained in:
commit
fdbd3fc59f
@ -1,8 +1,32 @@
|
|||||||
[tool.ruff]
|
[tool.ruff]
|
||||||
|
target-version = "py311"
|
||||||
line-length = 88
|
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"]
|
# FIXME? Maybe we should enable these?
|
||||||
ignore = [ "E501" ]
|
"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]
|
[tool.mypy]
|
||||||
python_version = "3.10"
|
python_version = "3.10"
|
||||||
|
23
setup.cfg
23
setup.cfg
@ -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
0
tests/__init__.py
Normal file
@ -1,4 +1,4 @@
|
|||||||
pytest_plugins = [
|
pytest_plugins = [
|
||||||
"direnv_project",
|
"tests.direnv_project",
|
||||||
"root",
|
"tests.root",
|
||||||
]
|
]
|
||||||
|
@ -1,22 +1,23 @@
|
|||||||
import shutil
|
import shutil
|
||||||
import textwrap
|
import textwrap
|
||||||
|
from collections.abc import Iterator
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
from typing import Iterator
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from procs import run
|
|
||||||
|
from .procs import run
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class DirenvProject:
|
class DirenvProject:
|
||||||
dir: Path
|
directory: Path
|
||||||
nix_direnv: Path
|
nix_direnv: Path
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def envrc(self) -> Path:
|
def envrc(self) -> Path:
|
||||||
return self.dir / ".envrc"
|
return self.directory / ".envrc"
|
||||||
|
|
||||||
def setup_envrc(self, content: str, strict_env: bool) -> None:
|
def setup_envrc(self, content: str, strict_env: bool) -> None:
|
||||||
text = textwrap.dedent(
|
text = textwrap.dedent(
|
||||||
@ -27,23 +28,23 @@ class DirenvProject:
|
|||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
self.envrc.write_text(text)
|
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]:
|
def direnv_project(test_root: Path, project_root: Path) -> Iterator[DirenvProject]:
|
||||||
"""
|
"""
|
||||||
Setups a direnv test project
|
Setups a direnv test project
|
||||||
"""
|
"""
|
||||||
with TemporaryDirectory() as _dir:
|
with TemporaryDirectory() as _dir:
|
||||||
dir = Path(_dir) / "proj"
|
directory = Path(_dir) / "proj"
|
||||||
shutil.copytree(test_root / "testenv", dir)
|
shutil.copytree(test_root / "testenv", directory)
|
||||||
shutil.copyfile(project_root / "flake.nix", dir / "flake.nix")
|
shutil.copyfile(project_root / "flake.nix", directory / "flake.nix")
|
||||||
shutil.copyfile(project_root / "flake.lock", dir / "flake.lock")
|
shutil.copyfile(project_root / "flake.lock", directory / "flake.lock")
|
||||||
shutil.copyfile(project_root / "treefmt.nix", dir / "treefmt.nix")
|
shutil.copyfile(project_root / "treefmt.nix", directory / "treefmt.nix")
|
||||||
nix_direnv = project_root / "direnvrc"
|
nix_direnv = project_root / "direnvrc"
|
||||||
|
|
||||||
c = DirenvProject(Path(dir), nix_direnv)
|
c = DirenvProject(Path(directory), nix_direnv)
|
||||||
try:
|
try:
|
||||||
yield c
|
yield c
|
||||||
finally:
|
finally:
|
||||||
|
@ -1,23 +1,27 @@
|
|||||||
|
import logging
|
||||||
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import IO, Any, List, Optional, Union
|
from typing import IO, Any
|
||||||
|
|
||||||
_FILE = Union[None, int, IO[Any]]
|
_FILE = None | int | IO[Any]
|
||||||
_DIR = Union[None, Path, str]
|
_DIR = None | Path | str
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def run(
|
def run(
|
||||||
cmd: List[str],
|
cmd: list[str],
|
||||||
text: bool = True,
|
text: bool = True,
|
||||||
check: bool = True,
|
check: bool = True,
|
||||||
cwd: _DIR = None,
|
cwd: _DIR = None,
|
||||||
stderr: _FILE = None,
|
stderr: _FILE = None,
|
||||||
stdout: _FILE = None,
|
stdout: _FILE = None,
|
||||||
env: Optional[dict[str, str]] = None,
|
env: dict[str, str] | None = None,
|
||||||
) -> subprocess.CompletedProcess:
|
) -> subprocess.CompletedProcess:
|
||||||
if cwd is not None:
|
if cwd is not None:
|
||||||
print(f"cd {cwd}")
|
log.debug(f"cd {cwd}")
|
||||||
print("$ " + " ".join(cmd))
|
log.debug(f"$ {shlex.join(cmd)}")
|
||||||
return subprocess.run(
|
return subprocess.run(
|
||||||
cmd, text=text, check=check, cwd=cwd, stderr=stderr, stdout=stdout, env=env
|
cmd, text=text, check=check, cwd=cwd, stderr=stderr, stdout=stdout, env=env
|
||||||
)
|
)
|
||||||
|
@ -6,7 +6,7 @@ TEST_ROOT = Path(__file__).parent.resolve()
|
|||||||
PROJECT_ROOT = TEST_ROOT.parent
|
PROJECT_ROOT = TEST_ROOT.parent
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture()
|
||||||
def test_root() -> Path:
|
def test_root() -> Path:
|
||||||
"""
|
"""
|
||||||
Root directory of the tests
|
Root directory of the tests
|
||||||
@ -14,7 +14,7 @@ def test_root() -> Path:
|
|||||||
return TEST_ROOT
|
return TEST_ROOT
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture()
|
||||||
def project_root() -> Path:
|
def project_root() -> Path:
|
||||||
"""
|
"""
|
||||||
Root directory of the tests
|
Root directory of the tests
|
||||||
|
@ -1,22 +1,26 @@
|
|||||||
|
import logging
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import pytest
|
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:
|
def common_test(direnv_project: DirenvProject) -> None:
|
||||||
run(["nix-collect-garbage"])
|
run(["nix-collect-garbage"])
|
||||||
|
|
||||||
testenv = str(direnv_project.dir)
|
testenv = str(direnv_project.directory)
|
||||||
|
|
||||||
out1 = run(
|
out1 = run(
|
||||||
["direnv", "exec", testenv, "hello"],
|
["direnv", "exec", testenv, "hello"],
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
check=False,
|
check=False,
|
||||||
cwd=direnv_project.dir,
|
cwd=direnv_project.directory,
|
||||||
)
|
)
|
||||||
sys.stderr.write(out1.stderr)
|
sys.stderr.write(out1.stderr)
|
||||||
assert out1.returncode == 0
|
assert out1.returncode == 0
|
||||||
@ -29,7 +33,7 @@ def common_test(direnv_project: DirenvProject) -> None:
|
|||||||
["direnv", "exec", testenv, "hello"],
|
["direnv", "exec", testenv, "hello"],
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
check=False,
|
check=False,
|
||||||
cwd=direnv_project.dir,
|
cwd=direnv_project.directory,
|
||||||
)
|
)
|
||||||
sys.stderr.write(out2.stderr)
|
sys.stderr.write(out2.stderr)
|
||||||
assert out2.returncode == 0
|
assert out2.returncode == 0
|
||||||
@ -38,23 +42,25 @@ def common_test(direnv_project: DirenvProject) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def common_test_clean(direnv_project: DirenvProject) -> None:
|
def common_test_clean(direnv_project: DirenvProject) -> None:
|
||||||
testenv = str(direnv_project.dir)
|
testenv = str(direnv_project.directory)
|
||||||
|
|
||||||
out3 = run(
|
out3 = run(
|
||||||
["direnv", "exec", testenv, "hello"],
|
["direnv", "exec", testenv, "hello"],
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
check=False,
|
check=False,
|
||||||
cwd=direnv_project.dir,
|
cwd=direnv_project.directory,
|
||||||
)
|
)
|
||||||
sys.stderr.write(out3.stderr)
|
sys.stderr.write(out3.stderr)
|
||||||
|
|
||||||
files = [
|
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")]
|
rcs = [f for f in files if f.match("*.rc")]
|
||||||
profiles = [f for f in files if not f.match("*.rc")]
|
profiles = [f for f in files if not f.match("*.rc")]
|
||||||
if len(rcs) != 1 or len(profiles) != 1:
|
if len(rcs) != 1 or len(profiles) != 1:
|
||||||
print(files)
|
log.debug(files)
|
||||||
assert len(rcs) == 1
|
assert len(rcs) == 1
|
||||||
assert len(profiles) == 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:
|
def test_use_flake(direnv_project: DirenvProject, strict_env: bool) -> None:
|
||||||
direnv_project.setup_envrc("use flake", strict_env=strict_env)
|
direnv_project.setup_envrc("use flake", strict_env=strict_env)
|
||||||
common_test(direnv_project)
|
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
|
# should only contain our flake-utils flake
|
||||||
if len(inputs) != 4:
|
if len(inputs) != 4:
|
||||||
run(["nix", "flake", "archive", "--json"], cwd=direnv_project.dir)
|
run(["nix", "flake", "archive", "--json"], cwd=direnv_project.directory)
|
||||||
print(inputs)
|
log.debug(inputs)
|
||||||
assert len(inputs) == 4
|
assert len(inputs) == 4
|
||||||
for symlink in inputs:
|
for symlink in inputs:
|
||||||
assert symlink.is_dir()
|
assert symlink.is_dir()
|
||||||
|
@ -1,31 +1,35 @@
|
|||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
import pytest
|
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(
|
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:
|
) -> None:
|
||||||
args = ["direnv", "exec", str(direnv_project.dir), "sh", "-c", cmd]
|
args = ["direnv", "exec", str(direnv_project.directory), "sh", "-c", cmd]
|
||||||
print("$ " + " ".join(args))
|
log.debug(f"$ {shlex.join(args)}")
|
||||||
out = run(
|
out = run(
|
||||||
args,
|
args,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
check=False,
|
check=False,
|
||||||
cwd=direnv_project.dir,
|
cwd=direnv_project.directory,
|
||||||
env=env,
|
env=env,
|
||||||
)
|
)
|
||||||
sys.stdout.write(out.stdout)
|
sys.stdout.write(out.stdout)
|
||||||
sys.stderr.write(out.stderr)
|
sys.stderr.write(out.stderr)
|
||||||
assert out.returncode == 0
|
assert out.returncode == 0
|
||||||
assert "OK\n" == out.stdout
|
assert out.stdout == "OK\n"
|
||||||
assert "renewed cache" in out.stderr
|
assert "renewed cache" in out.stderr
|
||||||
|
|
||||||
|
|
||||||
@ -57,7 +61,7 @@ def test_no_files(direnv_project: DirenvProject, strict_env: bool) -> None:
|
|||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
check=False,
|
check=False,
|
||||||
cwd=direnv_project.dir,
|
cwd=direnv_project.directory,
|
||||||
)
|
)
|
||||||
assert out.returncode == 0
|
assert out.returncode == 0
|
||||||
assert 'Loaded watch: "."' not in out.stdout
|
assert 'Loaded watch: "."' not in out.stdout
|
||||||
|
Loading…
Reference in New Issue
Block a user