Skip to content

Commit 85fe618

Browse files
committed
🔧 build(lint): bump simplint to 0.10.3
Use the server API to verify startup and cleanup, removing lint exceptions made unnecessary by those paths.
1 parent 859ba77 commit 85fe618

7 files changed

Lines changed: 119 additions & 64 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ repos:
4040
priority: 3
4141
args: ["--fix", "--unsafe-fixes", "--exit-non-zero-on-fix"]
4242
- repo: https://github.com/gaborbernat/simplint-pre-commit
43-
rev: c8719dca99c13381570bc69f8955c9448fc271b7 # frozen: v0.7.1
43+
rev: 073bd9250c7a4d15b71eef71f2c95e4af5c76283 # frozen: v0.10.3
4444
hooks:
4545
- id: simplint
4646
priority: 4
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Report an error when devpi exits before it is ready, and clean up the child process if startup fails.

‎pyproject.toml‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,23 @@ lint.select = [
130130
"ALL",
131131
]
132132
lint.ignore = [
133-
"any-type", # allow Any as type annotation
134133
"CPY", # No copyright statements
135134
"DOC", # not supported
136-
"hardcoded-bind-all-interfaces", # Possible binding to all interface
137-
"incorrect-blank-line-before-class", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible
135+
"incorrect-blank-line-before-class", # Conflicts with `blank-line-before-class`
138136
"missing-trailing-comma", # Conflict with formatter
139-
"multi-line-summary-first-line", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
140-
"non-empty-init-module", # `__init__` module should only contain docstrings and re-exports
137+
"multi-line-summary-first-line", # Conflicts with `multi-line-summary-second-line`
141138
"single-line-implicit-string-concatenation", # Conflict with formatter
142-
"undocumented-magic-method", # undocumented-public-api (simplint SMP903) covers the public API instead
143-
"undocumented-public-class", # undocumented-public-api (simplint SMP903) covers the public API instead
144-
"undocumented-public-function", # undocumented-public-api (simplint SMP903) covers the public API instead
145-
"undocumented-public-init", # undocumented-public-api (simplint SMP903) covers the public API instead
146-
"undocumented-public-method", # undocumented-public-api (simplint SMP903) covers the public API instead
147-
"undocumented-public-nested-class", # undocumented-public-api (simplint SMP903) covers the public API instead
139+
"undocumented-magic-method", # function-docstring (simplint) owns internal docstrings
140+
"undocumented-public-class", # undocumented-public-api (simplint) owns the public API
141+
"undocumented-public-function", # undocumented-public-api (simplint) owns the public API
142+
"undocumented-public-init", # undocumented-public-api (simplint) owns the public API
143+
"undocumented-public-method", # undocumented-public-api (simplint) owns the public API
144+
"undocumented-public-nested-class", # undocumented-public-api (simplint) owns the public API
148145
]
149146
lint.per-file-ignores."tests/**/*.py" = [
150-
"assert", # asserts allowed in tests
151-
"D", # don't care about documentation in tests
152-
"FBT", # don't care about booleans as positional arguments in tests
153-
"implicit-namespace-package", # no implicit namespace
154-
"magic-value-comparison", # Magic value used in comparison, consider replacing with a constant variable
155-
"subprocess-without-shell-equals-true", # `subprocess` call: check for execution of untrusted input
147+
"assert", # asserts allowed in tests
148+
"D", # test names carry the intent
149+
"implicit-namespace-package", # test directories are not packages
156150
]
157151
lint.isort = { known-first-party = [
158152
"devpi_process",

‎src/devpi_process/_index.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,22 @@ def url(self) -> str:
3535

3636
def use(self) -> None:
3737
"""Use this index server."""
38-
_check_call([*self._client_cmd_base, "use", f"{self.user}/{self.name}"])
38+
check_call([*self._client_cmd_base, "use", f"{self.user}/{self.name}"])
3939

4040
def upload(self, *files: Path) -> None:
4141
"""
4242
Upload packages to the index.
4343
4444
:param files: the files to upload
4545
"""
46-
_check_call(self._client_cmd_base + ["upload", "--index", self.name] + [str(i) for i in files])
46+
check_call(self._client_cmd_base + ["upload", "--index", self.name] + [str(i) for i in files])
4747

4848

49-
def _check_call(cmd: list[str]) -> None:
49+
def check_call(cmd: list[str]) -> None:
5050
run(cmd, check=True, capture_output=True) # ruff:ignore[subprocess-without-shell-equals-true] # no shell, argument list only
5151

5252

5353
__all__ = [
5454
"Index",
55+
"check_call",
5556
]

‎src/devpi_process/_server.py‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from __future__ import annotations
22

3-
import random
3+
import secrets
44
import socket
55
import string
66
import sys
77
import sysconfig
88
from contextlib import closing
9+
from logging import getLogger
910
from pathlib import Path
1011
from subprocess import PIPE, Popen # ruff:ignore[suspicious-subprocess-import] # no shell, argument lists only
1112
from threading import Thread
1213
from typing import IO, TYPE_CHECKING, cast
1314

14-
from ._index import Index, _check_call
15+
from ._index import Index, check_call
1516

1617
if TYPE_CHECKING:
1718
from collections.abc import Iterator, Sequence
@@ -30,7 +31,8 @@ class IndexServer:
3031
def __init__(
3132
self,
3233
path: Path,
33-
with_root_pypi: bool = False, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument] # public parameter, changing it breaks callers
34+
# Preserve the public positional argument.
35+
with_root_pypi: bool = False, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument]
3436
start_args: Sequence[str] | None = None,
3537
) -> None:
3638
"""
@@ -46,14 +48,14 @@ def __init__(
4648

4749
self.host = "localhost"
4850
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as socket_handler:
49-
socket_handler.bind(("", 0))
51+
socket_handler.bind(("", 0)) # devpi can bind to any IPv4 interface via start_args
5052
self.port = cast("int", socket_handler.getsockname()[1])
51-
self._passwd = "".join(random.choices(string.ascii_letters, k=8)) # ruff:ignore[suspicious-non-cryptographic-random-usage] # throwaway password of a test server
53+
self._passwd = "".join(secrets.choice(string.ascii_letters) for _ in range(8))
5254

5355
scripts_dir = sysconfig.get_path("scripts")
5456
if scripts_dir is None:
55-
msg = "could not get scripts folder of host interpreter" # pragma: no cover # typing guard
56-
raise RuntimeError(msg) # pragma: no cover # typing guard
57+
msg = "could not get scripts folder of host interpreter"
58+
raise RuntimeError(msg)
5759

5860
def _exe(name: str) -> str:
5961
return str(Path(scripts_dir) / f"{name}{'.exe' if sys.platform == 'win32' else ''}")
@@ -71,8 +73,12 @@ def _exe(name: str) -> str:
7173

7274
def __enter__(self) -> Self:
7375
"""Start the index server."""
74-
self._create_and_start_server()
75-
self._setup_client()
76+
try:
77+
self._create_and_start_server()
78+
self._setup_client()
79+
except BaseException:
80+
self.__exit__(None, None, None)
81+
raise
7682
return self
7783

7884
def _create_and_start_server(self) -> None:
@@ -83,13 +89,13 @@ def _create_and_start_server(self) -> None:
8389
cmd.extend(("--role", "standalone", "--root-passwd", self._passwd))
8490
if self._with_root_pypi is False:
8591
cmd.append("--no-root-pypi")
86-
_check_call(cmd)
92+
check_call(cmd)
8793
# 2. start the server
8894
cmd = [self._server, "--serverdir", server_at, "--port", str(self.port)]
8995
cmd.extend(self._start_args)
9096
self._process = Popen(cmd, stdout=PIPE, universal_newlines=True) # ruff:ignore[subprocess-without-shell-equals-true] # no shell, argument list only
9197
stdout = self._drain_stdout()
92-
for line in stdout: # pragma: no branch # will always loop at least once
98+
for line in stdout:
9399
if "serving at url" in line:
94100

95101
def _keep_draining() -> None:
@@ -100,21 +106,24 @@ def _keep_draining() -> None:
100106
self._stdout_drain = Thread(target=_keep_draining, name="tox-test-stdout-drain")
101107
self._stdout_drain.start()
102108
break
109+
else:
110+
msg = f"devpi server at {self._server_dir} exited before it was ready"
111+
raise RuntimeError(msg)
103112

104113
def _drain_stdout(self) -> Iterator[str]:
105114
process = cast("Popen[str]", self._process)
106115
stdout = cast("IO[str]", process.stdout)
107116
while True:
108-
if process.poll() is not None: # pragma: no cover # a dead server is a failure path the tests do not drive
109-
print(f"devpi server with pid {process.pid} at {self._server_dir} died") # ruff:ignore[print] # reports the dead server in the test output
117+
if process.poll() is not None:
118+
getLogger(__name__).error("devpi server with pid %s at %s died", process.pid, self._server_dir)
110119
break
111120
yield stdout.readline()
112121

113122
def _setup_client(self) -> None:
114123
self._client_dir.mkdir(exist_ok=True)
115124
base = ["--clientdir", str(self._client_dir)]
116-
_check_call([self._client, "use", *base, self.url])
117-
_check_call([self._client, "login", *base, self.user, "--password", self._passwd])
125+
check_call([self._client, "use", *base, self.url])
126+
check_call([self._client, "login", *base, self.user, "--password", self._passwd])
118127

119128
@property
120129
def url(self) -> str:
@@ -139,9 +148,9 @@ def __exit__(
139148
:param exc_val: the exception leaving the block, or None
140149
:param exc_tb: traceback of that exception, or None
141150
"""
142-
if self._process is not None: # pragma: no cover # defend against devpi startup fail
151+
if self._process is not None:
143152
self._process.terminate()
144-
if self._stdout_drain is not None and self._stdout_drain.is_alive(): # pragma: no cover # devpi startup fail
153+
if self._stdout_drain is not None and self._stdout_drain.is_alive():
145154
self._stdout_drain.join()
146155

147156
def __repr__(self) -> str:
@@ -157,11 +166,11 @@ def create_index(self, name: str, *args: str) -> Index:
157166
:returns: the created index
158167
:raises ValueError: when an index with this name already exists
159168
"""
160-
if name in self._indexes: # pragma: no cover # a duplicate name is a caller error the tests do not drive
169+
if name in self._indexes:
161170
msg = f"index {name} already exists"
162171
raise ValueError(msg)
163172
base = [self._client, "--clientdir", str(self._client_dir)]
164-
_check_call([*base, "index", "-c", name, *args])
173+
check_call([*base, "index", "-c", name, *args])
165174
index = Index(f"{self.url}/{self.user}", name, self.user, base)
166175
self._indexes[name] = index
167176
return index

‎tests/demo_pkg_inline/build.py‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,24 @@
5151

5252
def build_wheel(
5353
wheel_directory: str,
54-
metadata_directory: str | None = None, # ruff:ignore[unused-function-argument] # PEP 517 hook signature, the backend must accept the argument
55-
config_settings: None = None, # ruff:ignore[unused-function-argument] # PEP 517 hook signature, the backend must accept the argument
54+
metadata_directory: str | None = None, # ruff:ignore[unused-function-argument] # PEP 517 signature
55+
config_settings: None = None, # ruff:ignore[unused-function-argument] # PEP 517 signature
5656
) -> str:
5757
base_name = f"{_NAME}-{_VERSION}-py{sys.version_info[0]}-none-any.whl"
5858
with ZipFile(str(Path(wheel_directory) / base_name), "w") as zip_file_handler:
59-
for arc_name, data in _CONTENT.items(): # pragma: no branch # the content table always has entries
59+
for arc_name, data in _CONTENT.items(): # pragma: no branch # _CONTENT is nonempty
6060
zip_file_handler.writestr(arc_name, dedent(data).strip())
6161
return base_name
6262

6363

64-
def get_requires_for_build_wheel(
65-
config_settings: None = None, # ruff:ignore[unused-function-argument] # PEP 517 hook signature, the backend must accept the argument
66-
) -> list[str]:
67-
return [] # pragma: no cover # only executed in non-host pythons
64+
# PEP 517 requires the unused argument.
65+
def get_requires_for_build_wheel(config_settings: None = None) -> list[str]: # ruff:ignore[unused-function-argument]
66+
return []
6867

6968

7069
def build_sdist(
7170
sdist_directory: str,
72-
config_settings: None = None, # ruff:ignore[unused-function-argument] # PEP 517 hook signature, the backend must accept the argument
71+
config_settings: None = None, # ruff:ignore[unused-function-argument] # PEP 517 signature
7372
) -> str:
7473
result = f"{_NAME}-{_VERSION}.tar.gz"
7574
with tarfile.open(str(Path(sdist_directory) / result), "w:gz") as tar:
@@ -85,7 +84,6 @@ def build_sdist(
8584
return result
8685

8786

88-
def get_requires_for_build_sdist(
89-
config_settings: None = None, # ruff:ignore[unused-function-argument] # PEP 517 hook signature, the backend must accept the argument
90-
) -> list[str]:
91-
return [] # pragma: no cover # only executed in non-host pythons
87+
# PEP 517 requires the unused argument.
88+
def get_requires_for_build_sdist(config_settings: None = None) -> list[str]: # ruff:ignore[unused-function-argument]
89+
return []

‎tests/test_devpi_process.py‎

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
from __future__ import annotations
22

3+
import socket
4+
import sysconfig
5+
from contextlib import ExitStack
6+
from http import HTTPStatus
37
from importlib.util import module_from_spec, spec_from_file_location
48
from pathlib import Path
59
from typing import TYPE_CHECKING
610

711
import pytest
812
from httpx import get
913

10-
from devpi_process import IndexServer
14+
from devpi_process import IndexServer, __version__
1115

1216
if TYPE_CHECKING:
13-
from _pytest.tmpdir import TempPathFactory
17+
from types import ModuleType
1418

1519

1620
def test_version() -> None:
17-
import devpi_process # ruff:ignore[import-outside-top-level] # the test checks the package import itself
18-
19-
assert devpi_process.__version__ is not None
21+
assert __version__ is not None
2022

2123

2224
@pytest.fixture(scope="session")
23-
def demo_artifacts(tmp_path_factory: TempPathFactory) -> tuple[Path, Path]:
25+
def demo_backend() -> ModuleType:
2426
spec = spec_from_file_location("demo_pkg_inline.build", str(Path(__file__).parent / "demo_pkg_inline" / "build.py"))
2527
assert spec is not None
2628
build = module_from_spec(spec)
2729
loader = spec.loader
2830
assert loader is not None
2931
loader.exec_module(build)
32+
return build
33+
34+
35+
@pytest.fixture(scope="session")
36+
def demo_artifacts(tmp_path_factory: pytest.TempPathFactory, demo_backend: ModuleType) -> tuple[Path, Path]:
3037
base = tmp_path_factory.mktemp("wheel")
31-
return base / build.build_wheel(str(base)), base / build.build_sdist(str(base))
38+
return base / demo_backend.build_wheel(str(base)), base / demo_backend.build_sdist(str(base))
39+
40+
41+
def test_demo_backend_requires_no_extra_build_dependencies(demo_backend: ModuleType) -> None:
42+
assert demo_backend.get_requires_for_build_wheel() == []
43+
assert demo_backend.get_requires_for_build_sdist() == []
3244

3345

3446
def test_create_server(tmp_path: Path, demo_artifacts: tuple[Path, Path]) -> None:
@@ -65,8 +77,48 @@ def test_create_server_with_pypi(tmp_path: Path) -> None:
6577

6678

6779
def test_create_server_start_args(tmp_path: Path) -> None:
68-
with IndexServer(tmp_path, start_args=["--offline-mode"]) as server:
69-
assert server._process is not None # ruff:ignore[private-member-access] # the test inspects the spawned command
70-
args = server._process.args # ruff:ignore[private-member-access] # the test inspects the spawned command
71-
assert isinstance(args, list)
72-
assert args[-1] == "--offline-mode"
80+
with IndexServer(tmp_path, start_args=["--host", "127.0.0.1"]) as server:
81+
response = get(f"{server.url}/+status", headers={"Accept": "application/json"})
82+
assert response.status_code == HTTPStatus.OK
83+
assert response.json()["result"]["host"] == "127.0.0.1"
84+
85+
86+
def test_server_probes_all_ipv4_interfaces(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
87+
bound_addresses: list[socket._Address] = []
88+
89+
class ProbeSocket(socket.socket):
90+
def bind(self, address: socket._Address) -> None:
91+
bound_addresses.append(address)
92+
super().bind(address)
93+
94+
monkeypatch.setattr(socket, "socket", ProbeSocket)
95+
96+
IndexServer(tmp_path)
97+
98+
assert bound_addresses == [("", 0)]
99+
100+
101+
def test_create_server_rejects_duplicate_index(tmp_path: Path) -> None:
102+
with IndexServer(tmp_path) as server:
103+
server.create_index("base")
104+
with pytest.raises(ValueError, match="index base already exists"):
105+
server.create_index("base")
106+
107+
108+
def test_create_server_rejects_missing_scripts(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
109+
monkeypatch.setattr(sysconfig, "get_path", lambda _name: None)
110+
111+
with pytest.raises(RuntimeError, match="could not get scripts folder of host interpreter"):
112+
IndexServer(tmp_path)
113+
114+
115+
def test_create_server_reports_missing_executable(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
116+
monkeypatch.setattr(sysconfig, "get_path", lambda _name: str(tmp_path / "missing"))
117+
118+
with ExitStack() as stack, pytest.raises(FileNotFoundError, match="devpi-init"):
119+
stack.enter_context(IndexServer(tmp_path))
120+
121+
122+
def test_create_server_reports_startup_failure(tmp_path: Path) -> None:
123+
with ExitStack() as stack, pytest.raises(RuntimeError, match="exited before it was ready"):
124+
stack.enter_context(IndexServer(tmp_path, start_args=["--definitely-invalid"]))

0 commit comments

Comments
 (0)