Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
tests/tmp/
plugin/

# CPython writes these when a suite imports the hook's Python modules
# (e.g. the activation probe); untracked but unignored, they dirty the
# checkout after a green run (issue #897).
__pycache__/
*.pyc

# opencode: @opencode-ai/plugin is a devDependency for typings only (issue
# #319); node_modules/ is never imported at runtime and never ships (see
# tests/build-plugin.sh, which copies only opencode/{index.js,package.json}).
Expand Down
6 changes: 5 additions & 1 deletion tests/fixtures/test_rewrite_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ def prefix_fixture(self, body):
spec = self.root / "prefix-fixture.json"
spec.write_text(json.dumps({"profile": profile, "records": str(records.root)}))
native = self.native.replace("'printf fixture'", shlex.quote(issued["command"]))
return [sys.executable, "-I", str(Path(__file__).resolve()), "--prefix-fixture", str(spec), native]
# -I ignores PYTHONDONTWRITEBYTECODE (it implies -E), and this
# reinvocation re-imports RUNTIME at module load before any guard in
# __main__ runs, so -B is required here to keep it from writing
# agentkit/hooks/lib/__pycache__ into the shipped tree (issue #897).
return [sys.executable, "-I", "-B", str(Path(__file__).resolve()), "--prefix-fixture", str(spec), native]

def test_public_prefix_executes_once_with_native_result_and_refuses_replay(self):
command = self.prefix_fixture('printf "%s|%s|%s\\n" "$PWD" "$1" "$2"\nprintf stderr >&2\nexit 37\n')
Expand Down
6 changes: 6 additions & 0 deletions tests/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ pin_locale() {
}
pin_locale

# Suites that import Python modules (e.g. the activation probe) write
# __pycache__/ into the shipped tree unless bytecode writes are disabled.
# Export it here so it reaches every suite subprocess and the probe stays
# read-only (issue #897).
export PYTHONDONTWRITEBYTECODE=1

here=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
root=$(dirname -- "$here")
# The plugin root holds both the skills and the hook dispatchers. The hooks are
Expand Down
29 changes: 29 additions & 0 deletions tests/test-clean-tree-after-probe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
# Pins issue #897: a suite that imports the hook's Python modules must never
# leave __pycache__/ behind in the shipped tree. test-rewrite-runtime.sh's
# --prefix-fixture reinvocation runs with `-I`, which implies `-E` and so
# ignores PYTHONDONTWRITEBYTECODE; without an explicit -B on that reinvocation
# it re-imports agentkit/hooks/lib/rewrite_runtime.py and writes
# agentkit/hooks/lib/__pycache__/, dirtying an otherwise-clean checkout.
set -uo pipefail

TEST_NAME='clean tree after probe'
here=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
root=$(dirname -- "$here")
# shellcheck source=lib/assert.sh
source "$here/lib/assert.sh"

plugin="$root/agentkit"

before=$(find "$plugin" -name '__pycache__' 2>/dev/null | LC_ALL=C sort)
assert_eq '' "$before" 'no __pycache__ under agentkit/ before the suite runs'

unset PYTHONDONTWRITEBYTECODE
(cd -- "$here" && python3 -B fixtures/test_rewrite_runtime.py) > /dev/null 2>&1
rc=$?
assert_eq 0 "$rc" 'test_rewrite_runtime.py exits clean'

after=$(find "$plugin" -name '__pycache__' 2>/dev/null | LC_ALL=C sort)
assert_eq '' "$after" 'no __pycache__ under agentkit/ after running the rewrite-runtime fixture'

finish
Loading