Skip to content

The agent that grows with you

refactor(dev): drop the activation demo; keep only the invariant tests

ethernet1ec55baea8d8
diff --git a/scripts/_activation.py b/scripts/_activation.py
index 689aa4f2d12..4795880d087 100644
--- a/scripts/_activation.py
+++ b/scripts/_activation.py
@@ -31,8 +31,7 @@ same file::
The kernel appends the invoking script as the last argument, and ``bash -c``
binds it to ``$0`` — so the prologue finds the target relative to itself and
the shebang stays independent of the cwd. ``$BASH`` for the second hop keeps it
-free of both the exec bit and a ``PATH`` lookup. ``scripts/_activation_demo.py``
-is a runnable example.
+free of both the exec bit and a ``PATH`` lookup.
Activation is not re-run when the inherited environment is still current: the
sentinel's value is the installed-state file the environment was composed
diff --git a/scripts/_activation_demo.py b/scripts/_activation_demo.py
deleted file mode 100755
index 99a34de02d8..00000000000
--- a/scripts/_activation_demo.py
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/env -S bash -c 'exec "$BASH" "$(dirname "$0")/_hermes-python" "$0" "$@"'
-"""Demo: a POSIX script that activates the Hermes environment for itself.
-
-Run it from any cwd in any shell: the shebang hands the file to
-``scripts/_hermes-python``, which sources ``activate`` and execs the
-interpreter on this same file. Delete this file once you have copied the
-header, or keep it as the template.
-"""
-
-import os
-import sys
-
-from _activation import require_activation
-
-require_activation()
-
-print("activated sentinel:", os.environ.get("__HERMES_ACTIVATED"))
-print("interpreter :", sys.executable)
-print("args :", sys.argv[1:])
diff --git a/tests/scripts/test_activation_guard.py b/tests/scripts/test_activation_guard.py
index ab4c28faf7b..2cd9638de79 100644
--- a/tests/scripts/test_activation_guard.py
+++ b/tests/scripts/test_activation_guard.py
@@ -1,64 +1,36 @@
"""The guard repo scripts call to require an activated shell.
``scripts/_activation.py`` is stdlib-only and importable before the environment
-it checks for exists; these tests pin its contract: it passes under activation,
-and it exits naming the exact command for the caller's shell otherwise.
+it checks for exists. Only one part of it cannot be eyeballed from a single
+host: ``activation_command()`` names a different command per shell, so a POSIX
+run cannot see the Windows string and vice versa.
"""
from __future__ import annotations
-import os
-import subprocess
-import sys
-from pathlib import Path
-
import pytest
from scripts._activation import ACTIVATION_ENV_VAR, activation_command, require_activation
-REPO_ROOT = Path(__file__).resolve().parents[2]
-
-def test_require_activation_passes_when_the_sentinel_is_set(monkeypatch):
- monkeypatch.setenv(ACTIVATION_ENV_VAR, "1")
+def test_returns_when_activated_and_exits_naming_the_command_when_not(monkeypatch, capsys):
+ monkeypatch.setenv(ACTIVATION_ENV_VAR, "<installed-state path>")
require_activation() # must return, not raise
-
-def test_require_activation_exits_naming_the_activation_command(monkeypatch, capsys):
- monkeypatch.delenv(ACTIVATION_ENV_VAR, raising=False)
+ monkeypatch.delenv(ACTIVATION_ENV_VAR)
with pytest.raises(SystemExit) as excinfo:
require_activation()
- assert excinfo.value.code not in (0, None)
+ assert excinfo.value.code == 1
assert activation_command() in capsys.readouterr().err
@pytest.mark.platforms("posix")
-def test_activation_command_is_the_posix_source_line():
+def test_command_is_the_posix_source_line():
assert activation_command() == "source ./activate"
@pytest.mark.platforms("windows")
-def test_activation_command_is_powershell_on_a_native_windows_shell(monkeypatch):
+def test_command_is_powershell_on_a_native_windows_shell(monkeypatch):
monkeypatch.delenv("MSYSTEM", raising=False)
monkeypatch.delenv("SHELL", raising=False)
- assert activation_command() == ". .\\activate.ps1"
-
-
-def test_a_script_refuses_to_run_without_activation(tmp_path):
- """End-to-end: a real script importing the guard stops before its body."""
- script = tmp_path / "probe.py"
- script.write_text(
- "import sys\n"
- f"sys.path.insert(0, {str(REPO_ROOT / 'scripts')!r})\n"
- "from _activation import require_activation\n"
- "require_activation()\n"
- "print('ran')\n",
- encoding="utf-8",
- )
- env = {key: value for key, value in os.environ.items() if key != ACTIVATION_ENV_VAR}
- result = subprocess.run(
- [sys.executable, str(script)], capture_output=True, text=True, env=env
- )
- assert result.returncode != 0
- assert "ran" not in result.stdout
- assert activation_command() in result.stderr
\ No newline at end of file
+ assert activation_command() == ". .\\activate.ps1"
\ No newline at end of file
diff --git a/tests/scripts/test_hermes_python_prologue.py b/tests/scripts/test_hermes_python_prologue.py
index c34f0d1d664..982a9108d47 100644
--- a/tests/scripts/test_hermes_python_prologue.py
+++ b/tests/scripts/test_hermes_python_prologue.py
@@ -1,14 +1,14 @@
"""The self-activating shebang prologue: when does it re-activate?
-``scripts/_hermes-python`` is the POSIX shebang target. Its one interesting
-decision is staleness: ``__HERMES_ACTIVATED`` holds the installed-state file the
+``scripts/_hermes-python`` is the POSIX shebang target. Its one decision worth
+pinning is staleness: ``__HERMES_ACTIVATED`` holds the installed-state file the
environment was built against, so any of ``uv.lock`` / ``pyproject.toml`` /
``pm/lock.json`` being newer than that file means the inherited environment
-predates its inputs.
+predates its inputs. Invert that either way and the cost is invisible — a
+re-sync on every run, or a stale environment that looks fine.
-These drive the real prologue with controlled mtimes. A ``python3`` shim is put
-on PATH because the prologue execs ``python3`` by name — on some hosts (stock
-Windows) that name does not exist, and the subject here is the staleness
+A ``python3`` shim goes on PATH because the prologue execs ``python3`` by name,
+which does not exist on stock Windows; the subject here is the staleness
branch, not interpreter resolution.
"""
@@ -25,12 +25,11 @@ import pytest
REPO_ROOT = Path(__file__).resolve().parents[2]
PROLOGUE = REPO_ROOT / "scripts" / "_hermes-python"
-DEMO = REPO_ROOT / "scripts" / "_activation_demo.py"
-GUARD = REPO_ROOT / "scripts" / "_activation.py"
LONG_AGO = "2019-01-01 00:00:00"
STAMP_TIME = "2020-06-01 00:00:00"
JUST_AFTER = "2021-01-01 00:00:00"
+INPUTS = ("uv.lock", "pyproject.toml", "pm/lock.json")
def _posix(path: Path) -> str:
@@ -49,28 +48,35 @@ def _bash() -> str:
return found or "bash"
+def _set_mtime(path: Path, stamp: str) -> None:
+ when = datetime.datetime.strptime(stamp, "%Y-%m-%d %H:%M:%S").timestamp()
+ os.utime(path, (when, when))
+
+
@pytest.fixture
def checkout(tmp_path: Path) -> Path:
- """A repo-shaped tree with a stub activate that announces each sourcing."""
+ """A repo-shaped tree whose stub activate announces each sourcing."""
root = tmp_path / "checkout"
(root / "scripts").mkdir(parents=True)
(root / "pm").mkdir()
(root / "shim").mkdir()
- for src in (PROLOGUE, DEMO, GUARD):
- shutil.copy2(src, root / "scripts" / src.name)
+ shutil.copy2(PROLOGUE, root / "scripts" / PROLOGUE.name)
for name in ("uv.lock", "pyproject.toml"):
(root / name).touch()
(root / "pm" / "lock.json").touch()
(root / "stamp").touch()
+ # Quote in posix form: a /bin/sh script treats backslashes in an unquoted
+ # word as escapes (same pattern as tests/pm/test_activate_scripts.py).
shim = root / "shim" / "python3"
- # Quote in posix form: the shim is a /bin/sh script, where backslashes in
- # an unquoted word are escape characters (repo pattern: _fake_store).
shim.write_text("#!/bin/sh\nexec '%s' \"$@\"\n" % _posix(Path(sys.executable)), encoding="utf-8")
shim.chmod(0o755)
- # Stands in for the real activate: announces itself and exports the stamp
- # path, exactly as activation_environment() now supplies it.
+ (root / "scripts" / "probe.py").write_text(
+ "import os, sys\n"
+ "print('target ran; sentinel =', os.environ.get('__HERMES_ACTIVATED'))\n",
+ encoding="utf-8",
+ )
(root / "activate").write_text(
"echo 'ACTIVATED' >&2\n"
"export __HERMES_ACTIVATED=\"%s/stamp\"\n"
@@ -80,58 +86,43 @@ def checkout(tmp_path: Path) -> Path:
return root
-def _touch_at(path: Path, stamp: str) -> None:
- """Set a file's mtime without shelling out (the test runner scrubs PATH,
- so `touch` is not reliably available)."""
- when = datetime.datetime.strptime(stamp, "%Y-%m-%d %H:%M:%S").timestamp()
- os.utime(path, (when, when))
-
-
-def run_prologue(root: Path, sentinel: str | None) -> tuple[int, str]:
+def _run(root: Path, sentinel: str | None) -> str:
+ """Drive the prologue as the kernel would; return stderr, assert it ran."""
env = {**os.environ, "PATH": f"{_posix(root / 'shim')}{os.pathsep}{os.environ.get('PATH', '')}"}
env.pop("__HERMES_ACTIVATED", None)
if sentinel is not None:
env["__HERMES_ACTIVATED"] = sentinel.replace("{root}", _posix(root))
result = subprocess.run(
- [_bash(), _posix(PROLOGUE), _posix(root / "scripts" / DEMO.name)],
+ [_bash(), _posix(root / "scripts" / PROLOGUE.name), _posix(root / "scripts" / "probe.py")],
capture_output=True, text=True, cwd=_posix(root), env=env, timeout=60,
)
- return result.returncode, result.stderr
-
-
-def test_no_sentinel_activates(checkout: Path):
- code, err = run_prologue(checkout, None)
- assert code == 0, err
- assert "ACTIVATED" in err
+ assert result.returncode == 0, result.stderr
+ assert "target ran" in result.stdout, result.stderr
+ return result.stderr
def test_current_environment_is_left_alone(checkout: Path):
- """Inputs older than the stamp: the inherited env is current, so the
- prologue must not pay for a re-sync on every run."""
- _touch_at(checkout / "stamp", STAMP_TIME)
- for name in ("uv.lock", "pyproject.toml", "pm/lock.json"):
- _touch_at(checkout / name, LONG_AGO)
- code, err = run_prologue(checkout, "{root}/stamp")
- assert code == 0, err
- assert "ACTIVATED" not in err
+ """Inputs older than the stamp: re-syncing on every run is the cost of
+ getting this wrong, so the prologue must stay out of the way."""
+ _set_mtime(checkout / "stamp", STAMP_TIME)
+ for name in INPUTS:
+ _set_mtime(checkout / name, LONG_AGO)
+ assert "ACTIVATED" not in _run(checkout, "{root}/stamp")
[email protected]("input_name", ["uv.lock", "pyproject.toml", "pm/lock.json"])
[email protected]("input_name", INPUTS)
def test_input_newer_than_stamp_reactivates(checkout: Path, input_name: str):
- _touch_at(checkout / "stamp", STAMP_TIME)
- for name in ("uv.lock", "pyproject.toml", "pm/lock.json"):
- _touch_at(checkout / name, LONG_AGO)
- _touch_at(checkout / input_name, JUST_AFTER)
- code, err = run_prologue(checkout, "{root}/stamp")
- assert code == 0, err
- assert "ACTIVATED" in err, f"{input_name} newer than the stamp must re-activate"
-
-
[email protected]("sentinel", ["{root}/gone", "1"])
-def test_unusable_sentinel_activates(checkout: Path, sentinel: str):
- """A missing stamp path forces activation; the literal ``1`` written by an
- older activate must keep working rather than silently reading as current."""
- _touch_at(checkout / "stamp", STAMP_TIME)
- code, err = run_prologue(checkout, sentinel)
- assert code == 0, err
- assert "ACTIVATED" in err
\ No newline at end of file
+ _set_mtime(checkout / "stamp", STAMP_TIME)
+ for name in INPUTS:
+ _set_mtime(checkout / name, LONG_AGO)
+ _set_mtime(checkout / input_name, JUST_AFTER)
+ assert "ACTIVATED" in _run(checkout, "{root}/stamp")
+
+
[email protected]("sentinel", [None, "{root}/gone", "1"])
+def test_unusable_sentinel_activates(checkout: Path, sentinel: str | None):
+ """Cold, dangling stamp, or the bare ``1`` an older activate exported —
+ each must activate rather than read as current."""
+ for name in INPUTS:
+ _set_mtime(checkout / name, LONG_AGO)
+ assert "ACTIVATED" in _run(checkout, sentinel)
\ No newline at end of file