static-php-py 0.1.5__tar.gz → 0.1.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. {static_php_py-0.1.5 → static_php_py-0.1.6}/PKG-INFO +1 -1
  2. {static_php_py-0.1.5 → static_php_py-0.1.6}/docs/design-api.md +4 -4
  3. {static_php_py-0.1.5 → static_php_py-0.1.6}/docs/design-modules.md +4 -3
  4. {static_php_py-0.1.5 → static_php_py-0.1.6}/docs/impl.md +1 -0
  5. {static_php_py-0.1.5 → static_php_py-0.1.6}/pyproject.toml +1 -1
  6. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/_packaged.py +9 -2
  7. static_php_py-0.1.6/src/static_php_py/py.typed +0 -0
  8. static_php_py-0.1.6/tests/_sources/conftest.py +104 -0
  9. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/_sources/test_discovery.py +0 -13
  10. static_php_py-0.1.6/tests/_sources/test_entrypoints.py +63 -0
  11. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/_sources/test_integrity.py +10 -4
  12. static_php_py-0.1.6/tests/_sources/test_packaged.py +291 -0
  13. static_php_py-0.1.6/tests/_sources/test_packaged_selection.py +195 -0
  14. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/_sources/test_path.py +2 -59
  15. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/_sources/test_remote_archive.py +15 -44
  16. static_php_py-0.1.6/tests/_sources/test_remote_checksum.py +157 -0
  17. static_php_py-0.1.6/tests/_sources/test_remote_containment.py +151 -0
  18. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/_sources/test_remote_download.py +58 -131
  19. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/test_exceptions.py +16 -0
  20. static_php_py-0.1.6/tests/test_init.py +48 -0
  21. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/test_integration_exec.py +2 -42
  22. static_php_py-0.1.6/tests/test_integration_factory.py +50 -0
  23. static_php_py-0.1.6/tests/test_manifest.py +117 -0
  24. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/test_php_exec.py +47 -0
  25. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/test_php_factory.py +12 -0
  26. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/test_php_require.py +29 -1
  27. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/test_types.py +19 -14
  28. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/test_version_fallback.py +16 -1
  29. static_php_py-0.1.5/tests/_sources/test_entrypoints.py +0 -25
  30. static_php_py-0.1.5/tests/_sources/test_packaged.py +0 -294
  31. static_php_py-0.1.5/tests/_sources/test_remote_containment.py +0 -82
  32. static_php_py-0.1.5/tests/test_integration_factory.py +0 -116
  33. static_php_py-0.1.5/tests/test_manifest.py +0 -45
  34. {static_php_py-0.1.5 → static_php_py-0.1.6}/.gitignore +0 -0
  35. {static_php_py-0.1.5 → static_php_py-0.1.6}/LICENSE +0 -0
  36. {static_php_py-0.1.5 → static_php_py-0.1.6}/README.md +0 -0
  37. {static_php_py-0.1.5 → static_php_py-0.1.6}/docs/concept.md +0 -0
  38. {static_php_py-0.1.5 → static_php_py-0.1.6}/docs/design-discovery.md +0 -0
  39. {static_php_py-0.1.5 → static_php_py-0.1.6}/docs/design-extraction.md +0 -0
  40. {static_php_py-0.1.5 → static_php_py-0.1.6}/docs/design-packaged.md +0 -0
  41. {static_php_py-0.1.5 → static_php_py-0.1.6}/docs/design-providers.md +0 -0
  42. {static_php_py-0.1.5 → static_php_py-0.1.6}/docs/index.md +0 -0
  43. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/__init__.py +0 -0
  44. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_exceptions.py +0 -0
  45. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_execution.py +0 -0
  46. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_manifest.py +0 -0
  47. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_php.py +0 -0
  48. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/__init__.py +0 -0
  49. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/_cache.py +0 -0
  50. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/_discovery.py +0 -0
  51. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/_entrypoints.py +0 -0
  52. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/_extraction.py +0 -0
  53. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/_integrity.py +0 -0
  54. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/_path.py +0 -0
  55. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/_publishing.py +0 -0
  56. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_sources/_remote.py +0 -0
  57. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_types.py +0 -0
  58. {static_php_py-0.1.5 → static_php_py-0.1.6}/src/static_php_py/_version.py +0 -0
  59. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/_sources/test_cache.py +0 -0
  60. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/_sources/test_remote_search.py +0 -0
  61. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/conftest.py +0 -0
  62. {static_php_py-0.1.5 → static_php_py-0.1.6}/tests/test_php_inspect.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: static-php-py
3
- Version: 0.1.5
3
+ Version: 0.1.6
4
4
  Summary: Python wrapper library for static PHP binaries; code-only execution library
5
5
  Project-URL: Homepage, https://github.com/samhsu-dev/static-php-py
6
6
  Project-URL: Repository, https://github.com/samhsu-dev/static-php-py
@@ -95,17 +95,17 @@ php = PHP.remote("https://example.com/php-8.4.tar.gz", checksum="sha256:9f86d08.
95
95
  - **Behavior**: Execute PHP script file. `ini` entries render as `-d key=value` options placed before the script path (after the script they would land in `$argv`); `input` is delivered as the process's standard-input text.
96
96
  - **Input**: `script` - Path to PHP script, `args` - Script arguments, `input` - Standard-input text (None inherits the parent's stdin), `ini` - Per-invocation ini overrides, `timeout` - Execution timeout.
97
97
  - **Output**: ExecutionResult with execution outcome.
98
- - **Raises**: `FileNotFoundError` if script not found.
98
+ - **Raises**: `FileNotFoundError` if script not found. `ExecutionError` on execution timeout or when the binary cannot be launched.
99
99
  - **[eval(code: str, *, input: str | None = None, ini: Mapping[str, str] | None = None, timeout: float | None = None) -> ExecutionResult]**
100
100
  - **Behavior**: Execute PHP code string via php -r. `ini` entries render as `-d key=value` options placed before `-r`; `input` is delivered as the process's standard-input text.
101
101
  - **Input**: `code` - PHP code to execute, `input` - Standard-input text (None inherits the parent's stdin), `ini` - Per-invocation ini overrides, `timeout` - Execution timeout.
102
102
  - **Output**: ExecutionResult with execution outcome.
103
- - **Raises**: None
103
+ - **Raises**: `ExecutionError` on execution timeout or when the binary cannot be launched.
104
104
  - **[version() -> str]**
105
- - **Behavior**: Get PHP version string.
105
+ - **Behavior**: Get PHP version string, parsed from the binary's `--version` output in either reported format (`PHP X.Y.Z` or `static-php-cli X.Y.Z`).
106
106
  - **Input**: None
107
107
  - **Output**: Version string (e.g., "8.3.0").
108
- - **Raises**: None
108
+ - **Raises**: `ExecutionError` when the version query fails or reports an unparseable format.
109
109
  - **[path -> Path]** (Property)
110
110
  - **Behavior**: Path to the PHP executable.
111
111
  - **Output**: Path to PHP executable.
@@ -58,12 +58,13 @@ discovery is by group name, so the module path is companion-internal.
58
58
  `tests/` mirrors modules as `test_<module>.py`, split by responsibility as
59
59
  `test_<module>_<responsibility>.py` when oversized:
60
60
 
61
+ - `__init__.py` → `test_init.py`
61
62
  - `_php.py` → `test_php_factory.py`, `test_php_exec.py`, `test_php_inspect.py`, `test_php_require.py`
62
63
  - `_execution.py` → exercised through `PHP` delegation in `test_php_exec.py`, `test_php_inspect.py`
63
64
  - `_sources/_path.py` → `_sources/test_path.py`
64
- - `_sources/_remote.py` → `_sources/test_remote_download.py`
65
- - `_sources/_packaged.py` → `_sources/test_packaged.py`; `_sources/_publishing.py` → exercised through `_sources/test_packaged.py`, `_sources/test_remote_download.py`
66
- - `_sources/_extraction.py` + `_RemoteProvider` archive flow → `_sources/test_remote_archive.py`, `_sources/test_remote_search.py`, `_sources/test_remote_containment.py`
65
+ - `_sources/_remote.py` → `_sources/test_remote_download.py`, `_sources/test_remote_checksum.py`
66
+ - `_sources/_packaged.py` → `_sources/test_packaged.py`, `_sources/test_packaged_selection.py`; `_sources/_publishing.py` → exercised through `_sources/test_packaged.py`, `_sources/test_remote_download.py`
67
+ - `_sources/_extraction.py` full-tree model + `_RemoteProvider` archive flow → `_sources/test_remote_archive.py`, `_sources/test_remote_search.py`, `_sources/test_remote_containment.py`; single-member model → exercised through `_sources/test_packaged.py`
67
68
  - `_sources/_discovery.py` → `_sources/test_discovery.py`; `_sources/_integrity.py` → `_sources/test_integrity.py`; `_sources/_cache.py` → `_sources/test_cache.py`; `_sources/_entrypoints.py` → `_sources/test_entrypoints.py`
68
69
  - `_manifest.py` → `test_manifest.py`; `_types.py` → `test_types.py`; `_exceptions.py` → `test_exceptions.py`; `_version.py` → `test_version_fallback.py`
69
70
  - Companion: `_php.py` → `test_php.py`; `_exceptions.py` → `test_exceptions.py`
@@ -5,6 +5,7 @@
5
5
  - **[stdlib]** `subprocess.run([php, "-m"], capture_output=True, text=True)` — lists loaded extensions (`PHP.extensions`).
6
6
  - **[stdlib]** `subprocess.run(cmd, input="héllo", capture_output=True, encoding="utf-8", errors="replace")` — `input` takes str when `encoding` is set and reaches PHP's STDIN as UTF-8; `input=None` inherits the parent's stdin while `input=""` delivers an EOF-only stdin (distinct behaviors, verified with the 8.4 binary).
7
7
  - **[php-cli]** `php -d key=value` — must precede the script path or `-r`; placed after the script path it lands in `$argv` instead of applying as ini. Multiple `-d` options compose. Script args after the script path and stdin are readable simultaneously (all verified with the 8.4 binary).
8
+ - **[php-cli]** `php --version` output — standard binaries report `PHP X.Y.Z (cli) ...`; static-php-cli builds can report `static-php-cli X.Y.Z`; version parsing tries both patterns and returns the bare `X.Y.Z`.
8
9
  - **[stdlib]** `php -m` output parse — drop the `[PHP Modules]` / `[Zend Modules]` header lines and blank lines; lowercase each remaining name (output is mixed case, e.g. `Core`, `tokenizer`, `SPL`).
9
10
  - **[stdlib]** `importlib.metadata.entry_points(group="static_php_py.binary")` — `PHP.bundled` discovers the companion's binary resolver; empty group → companion not installed → `BinaryNotFoundError` naming the `[bundled]` extra; for the multiple-registration error message use `EntryPoint.name` + `.value` — `.dist` is 3.10+ only (absent on 3.9, verified) and the package supports 3.9.
10
11
  - **[stdlib]** `importlib.resources.files(pkg)` returns a `Traversable`, not a `Path`; binaries must be exec'd from a real path, so use `with as_file(files("static_php_py_binary") / name) as p:` — the package name is the companion package, not a bare `"resources"`.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "static-php-py"
3
- version = "0.1.5"
3
+ version = "0.1.6"
4
4
  description = "Python wrapper library for static PHP binaries; code-only execution library"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.9"
@@ -14,7 +14,7 @@ import os
14
14
  import platform
15
15
  import re
16
16
  import sys
17
- from pathlib import Path
17
+ from pathlib import Path, PureWindowsPath
18
18
 
19
19
  from .._exceptions import BinaryNotFoundError
20
20
  from ._extraction import _extract_php_member
@@ -152,7 +152,14 @@ def _validate_directory(directory: str) -> None:
152
152
  """Reject directory values that leave the package."""
153
153
  if not directory:
154
154
  raise ValueError("Empty archive directory: pass a package-relative path.")
155
- if "\\" in directory or "" in directory.split("/") or ".." in directory.split("/"):
155
+ # PureWindowsPath.drive catches drive-letter absolutes ("C:/x") that
156
+ # carry no backslash and no empty segment on any host platform.
157
+ if (
158
+ "\\" in directory
159
+ or "" in directory.split("/")
160
+ or ".." in directory.split("/")
161
+ or PureWindowsPath(directory).drive
162
+ ):
156
163
  raise ValueError(
157
164
  f"Archive directory {directory!r} must be a package-relative path "
158
165
  "with '/' separators and no '..' segments."
File without changes
@@ -0,0 +1,104 @@
1
+ """Shared fixtures for _sources provider tests.
2
+
3
+ Fixtures:
4
+ - fake_response -- factory building context-manager mocks mimicking urlopen's response.
5
+ - packaged_stub -- stub binary bytes the packaged archive builders embed by default.
6
+ - make_packaged_targz -- builder writing a one-member tar.gz stub archive (test_packaged*.py).
7
+ - make_packaged_zip -- builder writing a one-member zip stub archive (test_packaged*.py).
8
+ - patch_packaged_host -- context-manager factory patching the packaged module's host probes.
9
+ - resolve_packaged -- resolver running PackagedBinary against a directory on a patched host.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import tarfile
15
+ import zipfile
16
+ from collections.abc import Callable, Iterator
17
+ from contextlib import AbstractContextManager, contextmanager
18
+ from pathlib import Path
19
+ from unittest.mock import MagicMock, Mock, patch
20
+
21
+ import pytest
22
+ from static_php_py._sources._packaged import PackagedBinary
23
+
24
+ _PACKAGED_MODULE = "static_php_py._sources._packaged"
25
+ _PACKAGED_STUB = b"#!/bin/sh\n"
26
+
27
+
28
+ @pytest.fixture
29
+ def fake_response() -> Callable[[bytes], MagicMock]:
30
+ """Return a factory building context-manager mocks mimicking urlopen's response."""
31
+
32
+ def build(body: bytes) -> MagicMock:
33
+ response = MagicMock()
34
+ response.read.return_value = body
35
+ response.__enter__ = Mock(return_value=response)
36
+ response.__exit__ = Mock(return_value=None)
37
+ return response
38
+
39
+ return build
40
+
41
+
42
+ @pytest.fixture
43
+ def packaged_stub() -> bytes:
44
+ """Stub binary bytes the packaged archive builders embed by default."""
45
+ return _PACKAGED_STUB
46
+
47
+
48
+ @pytest.fixture
49
+ def make_packaged_targz() -> Callable[..., Path]:
50
+ """Return a builder writing a one-member tar.gz stub archive."""
51
+
52
+ def _make(path: Path, member: str, content: bytes = _PACKAGED_STUB) -> Path:
53
+ stub = path.parent / f".stub-{member}"
54
+ stub.write_bytes(content)
55
+ with tarfile.open(path, "w:gz") as archive:
56
+ archive.add(stub, arcname=member)
57
+ stub.unlink()
58
+ return path
59
+
60
+ return _make
61
+
62
+
63
+ @pytest.fixture
64
+ def make_packaged_zip() -> Callable[..., Path]:
65
+ """Return a builder writing a one-member zip stub archive."""
66
+
67
+ def _make(path: Path, member: str, content: bytes = _PACKAGED_STUB) -> Path:
68
+ with zipfile.ZipFile(path, "w") as archive:
69
+ archive.writestr(member, content)
70
+ return path
71
+
72
+ return _make
73
+
74
+
75
+ @pytest.fixture
76
+ def patch_packaged_host() -> Callable[[str, str], AbstractContextManager[None]]:
77
+ """Return a context-manager factory patching the packaged module's host probes."""
78
+
79
+ @contextmanager
80
+ def _patched(system: str, machine: str) -> Iterator[None]:
81
+ with (
82
+ patch(f"{_PACKAGED_MODULE}.sys.platform", system),
83
+ patch(f"{_PACKAGED_MODULE}.platform.machine", return_value=machine),
84
+ ):
85
+ yield
86
+
87
+ return _patched
88
+
89
+
90
+ @pytest.fixture
91
+ def resolve_packaged(
92
+ patch_packaged_host: Callable[[str, str], AbstractContextManager[None]],
93
+ ) -> Callable[..., Path]:
94
+ """Return a resolver running PackagedBinary against a real archive directory."""
95
+
96
+ def _resolve(directory: Path, host: tuple[str, str], series: str | None = None) -> Path:
97
+ binary = PackagedBinary(package="my_lib", series=series)
98
+ with (
99
+ patch(f"{_PACKAGED_MODULE}._resource_directory", return_value=directory),
100
+ patch_packaged_host(*host),
101
+ ):
102
+ return binary.resolve()
103
+
104
+ return _resolve
@@ -4,7 +4,6 @@ Test cases:
4
4
  - TestBundledDiscovery::test_resolve_returns_companion_path -- resolve() returns the path from the single entry point's resolver.
5
5
  - TestBundledDiscovery::test_resolve_raises_when_group_empty -- resolve() raises BinaryNotFoundError naming the [bundled] extra when the group is empty.
6
6
  - TestBundledDiscovery::test_resolve_raises_on_multiple_registrations -- resolve() raises plain PHPError (not BinaryNotFoundError) naming every registration when the group holds more than one entry point.
7
- - TestBundledDiscovery::test_resolve_propagates_resolver_failure -- resolve() propagates BinaryNotFoundError when the companion resolver cannot locate its binary.
8
7
  - TestBundledDiscovery::test_resolve_translates_companion_binary_not_found -- a companion-local BinaryNotFoundError is translated to the execution library's PHPError subclass, chained to the original.
9
8
  - TestBundledDiscovery::test_resolve_translates_companion_invalid_archive -- a companion-local InvalidArchiveError is translated to the execution library's PHPError subclass, chained to the original.
10
9
  - TestBundledDiscovery::test_resolve_propagates_unmirrored_resolver_error -- an exception whose class name is not mirrored propagates untranslated.
@@ -85,18 +84,6 @@ class TestBundledDiscovery:
85
84
  assert "'bundled' -> static_php_py_binary._resource:_RESOLVER" in message
86
85
  assert "'rogue' -> rogue_pkg._impl:_RESOLVER" in message
87
86
 
88
- @patch("static_php_py._sources._discovery.entries_for_group")
89
- def test_resolve_propagates_resolver_failure(self, mock_eps: Mock) -> None:
90
- """resolve() propagates BinaryNotFoundError raised by the companion resolver."""
91
- resolver = Mock()
92
- resolver.resolve.side_effect = BinaryNotFoundError("companion binary missing")
93
- entry = Mock()
94
- entry.load.return_value = resolver
95
- mock_eps.return_value = [entry]
96
-
97
- with pytest.raises(BinaryNotFoundError, match="companion binary missing"):
98
- _BundledDiscovery().resolve()
99
-
100
87
  @patch("static_php_py._sources._discovery.entries_for_group")
101
88
  def test_resolve_translates_companion_binary_not_found(self, mock_eps: Mock) -> None:
102
89
  """A companion-local BinaryNotFoundError becomes this library's PHPError subclass."""
@@ -0,0 +1,63 @@
1
+ """Tests for _entrypoints — version-tolerant entry-point group queries.
2
+
3
+ Regression guard for the Python 3.9 crash: ``importlib.metadata.entry_points``
4
+ accepts no ``group`` keyword before 3.10, so the query must go through
5
+ ``entries_for_group``. The first two tests use the real ``importlib.metadata``
6
+ API of the running interpreter; the dict-API tests pin the module to the
7
+ 3.9 shape by patching its version gate and ``entry_points`` callable.
8
+
9
+ Test cases:
10
+ - test_entries_for_group_unknown_group_returns_empty -- an unregistered group yields an empty list on the running interpreter.
11
+ - test_entries_for_group_known_group_returns_entries -- the companion's static_php_py.binary group lists its resolver entry.
12
+ - test_entries_for_group_dict_api_returns_group_entries -- the Python 3.9 dict-shaped entry_points() result yields the group's entries.
13
+ - test_entries_for_group_dict_api_missing_group_returns_empty -- the Python 3.9 dict-shaped result yields an empty list for a missing group.
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import importlib.metadata
19
+ from collections.abc import Iterator
20
+ from contextlib import contextmanager
21
+ from unittest.mock import patch
22
+
23
+ from static_php_py._sources._entrypoints import entries_for_group
24
+
25
+ _MODULE = "static_php_py._sources._entrypoints"
26
+
27
+
28
+ @contextmanager
29
+ def _dict_shaped_entry_points(
30
+ groups: dict[str, list[importlib.metadata.EntryPoint]],
31
+ ) -> Iterator[None]:
32
+ """Pin the module to the Python 3.9 dict-returning entry_points API."""
33
+ with (
34
+ patch(f"{_MODULE}.sys.version_info", (3, 9, 18, "final", 0)),
35
+ patch(f"{_MODULE}.importlib.metadata.entry_points", return_value=groups),
36
+ ):
37
+ yield
38
+
39
+
40
+ def test_entries_for_group_unknown_group_returns_empty() -> None:
41
+ """An unregistered group yields an empty list, not an error."""
42
+ assert entries_for_group("static_php_py.no_such_group") == []
43
+
44
+
45
+ def test_entries_for_group_known_group_returns_entries() -> None:
46
+ """The companion's registered resolver is listed for its group."""
47
+ assert [entry.name for entry in entries_for_group("static_php_py.binary")] == ["resolver"]
48
+
49
+
50
+ def test_entries_for_group_dict_api_returns_group_entries() -> None:
51
+ """The 3.9 dict-shaped entry_points() result yields the group's entries."""
52
+ entry = importlib.metadata.EntryPoint("resolver", "pkg.mod:obj", "demo.group")
53
+
54
+ with _dict_shaped_entry_points({"demo.group": [entry]}):
55
+ assert entries_for_group("demo.group") == [entry]
56
+
57
+
58
+ def test_entries_for_group_dict_api_missing_group_returns_empty() -> None:
59
+ """The 3.9 dict-shaped result yields an empty list for a missing group."""
60
+ entry = importlib.metadata.EntryPoint("resolver", "pkg.mod:obj", "demo.group")
61
+
62
+ with _dict_shaped_entry_points({"demo.group": [entry]}):
63
+ assert entries_for_group("absent.group") == []
@@ -9,7 +9,7 @@ Test cases:
9
9
  - test_parse_wrong_digest_length_raises_value_error -- a digest of the wrong length is rejected.
10
10
  - test_parse_non_hex_digest_raises_value_error -- non-hex characters in the digest are rejected.
11
11
  - test_verify_matching_file_passes -- a file matching the expected digest verifies silently.
12
- - test_verify_mismatching_file_raises_download_error -- a mismatch raises with expected and actual digests.
12
+ - test_verify_mismatching_file_raises_download_error -- a mismatch raises with the path, algorithm, expected, and actual digests.
13
13
  """
14
14
 
15
15
  from __future__ import annotations
@@ -79,12 +79,18 @@ def test_verify_matching_file_passes(tmp_path: Path) -> None:
79
79
 
80
80
 
81
81
  def test_verify_mismatching_file_raises_download_error(tmp_path: Path) -> None:
82
- """A mismatch raises with expected and actual digests."""
82
+ """A mismatch raises with the path, algorithm, expected, and actual digests.
83
+
84
+ The error carries all four fields (design-providers.md _verify_file).
85
+ """
83
86
  artifact = tmp_path / "downloaded"
84
87
  artifact.write_bytes(b"tampered content")
85
88
  actual = hashlib.sha256(b"tampered content").hexdigest()
86
89
 
87
90
  with pytest.raises(DownloadError) as exc_info:
88
91
  _verify_file(artifact, "sha256", _SHA256_DIGEST)
89
- assert _SHA256_DIGEST in str(exc_info.value)
90
- assert actual in str(exc_info.value)
92
+ message = str(exc_info.value)
93
+ assert str(artifact) in message
94
+ assert "sha256" in message
95
+ assert _SHA256_DIGEST in message
96
+ assert actual in message
@@ -0,0 +1,291 @@
1
+ """Tests for PackagedBinary — resolve, extraction, and publishing.
2
+
3
+ Archive selection heuristics (slug mapping, name/token matching, series
4
+ narrowing, candidate composition): test_packaged_selection.py. Shared
5
+ archive builders and host patching: conftest.py.
6
+
7
+ Test cases:
8
+ - test_init_with_empty_package_raises -- an empty package name raises ValueError.
9
+ - test_init_with_empty_series_raises -- an empty series string raises ValueError.
10
+ - test_init_rejects_escaping_directory -- empty, absolute, backslashed, or ..-carrying directories raise ValueError.
11
+ - test_resolve_extracts_from_targz -- the binary is extracted from a matching tar.gz beside it under the archive stem.
12
+ - test_resolve_extracts_from_tgz -- the binary is extracted from a matching .tgz archive.
13
+ - test_resolve_extracts_from_zip -- the binary is extracted from a matching zip archive.
14
+ - test_resolve_windows_zip_targets_exe -- on Windows the extracted binary carries an .exe suffix.
15
+ - test_resolve_reuses_extracted_binary -- a second resolve returns the binary without re-reading the archive.
16
+ - test_resolve_sets_execute_bit_on_pre_extracted -- a pre-extracted non-executable binary gets the execute bit without extraction.
17
+ - test_resolve_reads_real_package_directory -- a real importable package's multi-level archive directory resolves without patching the resource boundary.
18
+ - test_two_series_binaries_coexist -- resolving two series in one directory publishes two distinct binaries under their archive stems.
19
+ - test_resolve_raises_on_unsupported_platform -- an unsupported platform raises BinaryNotFoundError.
20
+ - test_resolve_raises_when_package_not_importable -- an unimportable package raises BinaryNotFoundError.
21
+ - test_resolve_raises_when_directory_missing -- a missing archive directory raises BinaryNotFoundError.
22
+ - test_targz_without_php_member_raises -- a tar.gz without a php member raises InvalidArchiveError.
23
+ - test_zip_without_php_member_raises -- a zip without a php member raises InvalidArchiveError.
24
+ - test_targz_symlink_php_member_raises -- a tar whose php-named member is a symlink is skipped, raising InvalidArchiveError.
25
+ - test_zip_directory_php_member_raises -- a zip whose php-named entry is a directory is skipped, raising InvalidArchiveError.
26
+ - test_corrupt_archive_raises_invalid_archive -- an unreadable tar.gz raises InvalidArchiveError, not a raw tarfile error.
27
+ - test_corrupt_zip_raises_invalid_archive -- an unreadable zip raises InvalidArchiveError, not a raw zipfile error.
28
+ - test_failed_publish_leaves_no_binary -- an interrupted extraction publish leaves no partial binary or temp file.
29
+ """
30
+
31
+ from __future__ import annotations
32
+
33
+ import os
34
+ import tarfile
35
+ import zipfile
36
+ from collections.abc import Callable
37
+ from contextlib import AbstractContextManager
38
+ from pathlib import Path
39
+ from unittest.mock import patch
40
+
41
+ import pytest
42
+ from static_php_py._exceptions import BinaryNotFoundError, InvalidArchiveError
43
+ from static_php_py._sources._packaged import PackagedBinary
44
+
45
+
46
+ def test_init_with_empty_package_raises() -> None:
47
+ with pytest.raises(ValueError, match="[Ee]mpty package"):
48
+ PackagedBinary(package="", series="8.4")
49
+
50
+
51
+ def test_init_with_empty_series_raises() -> None:
52
+ with pytest.raises(ValueError, match="[Ee]mpty PHP series"):
53
+ PackagedBinary(package="my_lib", series="")
54
+
55
+
56
+ @pytest.mark.parametrize("directory", ["", "/abs", "a\\b", "a/../b", "a//b", "a/"])
57
+ def test_init_rejects_escaping_directory(directory: str) -> None:
58
+ with pytest.raises(ValueError, match="directory"):
59
+ PackagedBinary(package="my_lib", directory=directory)
60
+
61
+
62
+ def test_resolve_extracts_from_targz(
63
+ tmp_path: Path,
64
+ make_packaged_targz: Callable[..., Path],
65
+ resolve_packaged: Callable[..., Path],
66
+ packaged_stub: bytes,
67
+ ) -> None:
68
+ make_packaged_targz(tmp_path / "php-8.4.11-cli-linux-x86_64.tar.gz", "php")
69
+
70
+ resolved = resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
71
+
72
+ assert resolved == tmp_path / "php-8.4.11-cli-linux-x86_64"
73
+ assert resolved.read_bytes() == packaged_stub
74
+
75
+
76
+ def test_resolve_extracts_from_tgz(
77
+ tmp_path: Path,
78
+ make_packaged_targz: Callable[..., Path],
79
+ resolve_packaged: Callable[..., Path],
80
+ packaged_stub: bytes,
81
+ ) -> None:
82
+ make_packaged_targz(tmp_path / "php-cli-8.4-linux-x86_64.tgz", "php")
83
+
84
+ resolved = resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
85
+
86
+ assert resolved == tmp_path / "php-cli-8.4-linux-x86_64"
87
+ assert resolved.read_bytes() == packaged_stub
88
+
89
+
90
+ def test_resolve_extracts_from_zip(
91
+ tmp_path: Path,
92
+ make_packaged_zip: Callable[..., Path],
93
+ resolve_packaged: Callable[..., Path],
94
+ packaged_stub: bytes,
95
+ ) -> None:
96
+ make_packaged_zip(tmp_path / "php-8.4-linux-x86_64.zip", "php")
97
+
98
+ resolved = resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
99
+
100
+ assert resolved.read_bytes() == packaged_stub
101
+
102
+
103
+ def test_resolve_windows_zip_targets_exe(
104
+ tmp_path: Path,
105
+ make_packaged_zip: Callable[..., Path],
106
+ resolve_packaged: Callable[..., Path],
107
+ ) -> None:
108
+ make_packaged_zip(tmp_path / "php-8.4.20-cli-win.zip", "php.exe")
109
+
110
+ resolved = resolve_packaged(tmp_path, ("win32", "AMD64"), series="8.4")
111
+
112
+ assert resolved == tmp_path / "php-8.4.20-cli-win.exe"
113
+
114
+
115
+ def test_resolve_reuses_extracted_binary(
116
+ tmp_path: Path,
117
+ make_packaged_targz: Callable[..., Path],
118
+ resolve_packaged: Callable[..., Path],
119
+ ) -> None:
120
+ """A second resolve returns the binary without re-reading the archive."""
121
+ archive = make_packaged_targz(tmp_path / "php-cli-8.4-linux-x86_64.tar.gz", "php")
122
+
123
+ first = resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
124
+ archive.write_bytes(b"not a tar.gz anymore")
125
+ second = resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
126
+
127
+ assert second == first
128
+
129
+
130
+ def test_resolve_sets_execute_bit_on_pre_extracted(
131
+ tmp_path: Path,
132
+ resolve_packaged: Callable[..., Path],
133
+ packaged_stub: bytes,
134
+ ) -> None:
135
+ (tmp_path / "php-cli-8.4-linux-x86_64.tar.gz").write_bytes(b"unreadable")
136
+ binary = tmp_path / "php-cli-8.4-linux-x86_64"
137
+ binary.write_bytes(packaged_stub)
138
+ binary.chmod(0o644)
139
+
140
+ resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
141
+
142
+ assert os.access(binary, os.X_OK)
143
+
144
+
145
+ def test_resolve_reads_real_package_directory(
146
+ tmp_path: Path,
147
+ monkeypatch: pytest.MonkeyPatch,
148
+ make_packaged_targz: Callable[..., Path],
149
+ patch_packaged_host: Callable[[str, str], AbstractContextManager[None]],
150
+ packaged_stub: bytes,
151
+ ) -> None:
152
+ """A real importable package's multi-level directory resolves unpatched."""
153
+ package_dir = tmp_path / "packaged_probe_pkg"
154
+ archive_dir = package_dir / "assets" / "php"
155
+ archive_dir.mkdir(parents=True)
156
+ (package_dir / "__init__.py").write_text("", encoding="utf-8")
157
+ make_packaged_targz(archive_dir / "php-cli-8.4-linux-x86_64.tar.gz", "php")
158
+ monkeypatch.syspath_prepend(str(tmp_path))
159
+ binary = PackagedBinary(package="packaged_probe_pkg", series="8.4", directory="assets/php")
160
+
161
+ with patch_packaged_host("linux", "x86_64"):
162
+ resolved = binary.resolve()
163
+
164
+ assert resolved == archive_dir / "php-cli-8.4-linux-x86_64"
165
+ assert resolved.read_bytes() == packaged_stub
166
+
167
+
168
+ def test_two_series_binaries_coexist(
169
+ tmp_path: Path,
170
+ make_packaged_targz: Callable[..., Path],
171
+ resolve_packaged: Callable[..., Path],
172
+ ) -> None:
173
+ """Binaries for two series publish under their archive stems side by side."""
174
+ make_packaged_targz(tmp_path / "php-cli-8.3-linux-x86_64.tar.gz", "php", content=b"83")
175
+ make_packaged_targz(tmp_path / "php-cli-8.4-linux-x86_64.tar.gz", "php", content=b"84")
176
+
177
+ first = resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.3")
178
+ second = resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
179
+
180
+ assert first == tmp_path / "php-cli-8.3-linux-x86_64"
181
+ assert second == tmp_path / "php-cli-8.4-linux-x86_64"
182
+ assert first.read_bytes() == b"83"
183
+ assert second.read_bytes() == b"84"
184
+
185
+
186
+ def test_resolve_raises_on_unsupported_platform(
187
+ tmp_path: Path, resolve_packaged: Callable[..., Path]
188
+ ) -> None:
189
+ with pytest.raises(BinaryNotFoundError, match="Unsupported platform"):
190
+ resolve_packaged(tmp_path, ("sunos", "sparc"), series="8.4")
191
+
192
+
193
+ def test_resolve_raises_when_package_not_importable(
194
+ patch_packaged_host: Callable[[str, str], AbstractContextManager[None]],
195
+ ) -> None:
196
+ binary = PackagedBinary(package="no_such_package_xyz", series="8.4")
197
+ with (
198
+ patch_packaged_host("linux", "x86_64"),
199
+ pytest.raises(BinaryNotFoundError, match="Cannot import package"),
200
+ ):
201
+ binary.resolve()
202
+
203
+
204
+ def test_resolve_raises_when_directory_missing(
205
+ tmp_path: Path, resolve_packaged: Callable[..., Path]
206
+ ) -> None:
207
+ with pytest.raises(BinaryNotFoundError, match="No archive directory"):
208
+ resolve_packaged(tmp_path / "absent", ("linux", "x86_64"), series="8.4")
209
+
210
+
211
+ def test_targz_without_php_member_raises(
212
+ tmp_path: Path,
213
+ make_packaged_targz: Callable[..., Path],
214
+ resolve_packaged: Callable[..., Path],
215
+ ) -> None:
216
+ make_packaged_targz(tmp_path / "php-cli-8.4-linux-x86_64.tar.gz", "readme.txt")
217
+
218
+ with pytest.raises(InvalidArchiveError, match="No PHP executable"):
219
+ resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
220
+
221
+
222
+ def test_zip_without_php_member_raises(
223
+ tmp_path: Path,
224
+ make_packaged_zip: Callable[..., Path],
225
+ resolve_packaged: Callable[..., Path],
226
+ ) -> None:
227
+ make_packaged_zip(tmp_path / "php-8.4-cli-win.zip", "readme.txt")
228
+
229
+ with pytest.raises(InvalidArchiveError, match="No PHP executable"):
230
+ resolve_packaged(tmp_path, ("win32", "AMD64"), series="8.4")
231
+
232
+
233
+ def test_targz_symlink_php_member_raises(
234
+ tmp_path: Path, resolve_packaged: Callable[..., Path]
235
+ ) -> None:
236
+ """A non-regular php-named tar member is skipped, not extracted."""
237
+ link = tarfile.TarInfo("php")
238
+ link.type = tarfile.SYMTYPE
239
+ link.linkname = "real-php"
240
+ with tarfile.open(tmp_path / "php-cli-8.4-linux-x86_64.tar.gz", "w:gz") as archive:
241
+ archive.addfile(link)
242
+
243
+ with pytest.raises(InvalidArchiveError, match="No PHP executable"):
244
+ resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
245
+
246
+
247
+ def test_zip_directory_php_member_raises(
248
+ tmp_path: Path, resolve_packaged: Callable[..., Path]
249
+ ) -> None:
250
+ """A php-named zip directory entry is skipped, not extracted."""
251
+ with zipfile.ZipFile(tmp_path / "php-8.4-cli-win.zip", "w") as archive:
252
+ archive.writestr("php/", b"")
253
+
254
+ with pytest.raises(InvalidArchiveError, match="No PHP executable"):
255
+ resolve_packaged(tmp_path, ("win32", "AMD64"), series="8.4")
256
+
257
+
258
+ def test_corrupt_archive_raises_invalid_archive(
259
+ tmp_path: Path, resolve_packaged: Callable[..., Path]
260
+ ) -> None:
261
+ (tmp_path / "php-cli-8.4-linux-x86_64.tar.gz").write_bytes(b"not a tar.gz")
262
+
263
+ with pytest.raises(InvalidArchiveError, match="Failed to extract"):
264
+ resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
265
+
266
+
267
+ def test_corrupt_zip_raises_invalid_archive(
268
+ tmp_path: Path, resolve_packaged: Callable[..., Path]
269
+ ) -> None:
270
+ (tmp_path / "php-8.4-linux-x86_64.zip").write_bytes(b"not a zip")
271
+
272
+ with pytest.raises(InvalidArchiveError, match="Failed to extract"):
273
+ resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
274
+
275
+
276
+ def test_failed_publish_leaves_no_binary(
277
+ tmp_path: Path,
278
+ make_packaged_targz: Callable[..., Path],
279
+ resolve_packaged: Callable[..., Path],
280
+ ) -> None:
281
+ """An interrupted extraction publish leaves no partial binary or temp file."""
282
+ make_packaged_targz(tmp_path / "php-cli-8.4-linux-x86_64.tar.gz", "php")
283
+
284
+ with (
285
+ patch("static_php_py._sources._publishing.os.replace", side_effect=OSError("disk full")),
286
+ pytest.raises(OSError, match="disk full"),
287
+ ):
288
+ resolve_packaged(tmp_path, ("linux", "x86_64"), series="8.4")
289
+
290
+ assert not (tmp_path / "php-cli-8.4-linux-x86_64").exists()
291
+ assert list(tmp_path.glob("*.tmp")) == []