Repository navigation
fix(test): isolate the test suite from a live B12 install #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0945ee5
fix(test): isolate the test suite from a live B12 install
dorukardahan 064d842
fix(install): pass custom runtime paths into the launchd daemon job
dorukardahan 97b68b3
test: keep the isolation self-check collectable without pwd
dorukardahan ecc0934
test: keep the installed venv visible to interpreter-only tests
dorukardahan 7a0835d
test: isolate USERPROFILE too so Windows expanduser stays in the temp…
dorukardahan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| """Keep the whole test suite away from a developer's live B12 install. | ||
|
|
||
| The memory DB (~/Library/Application Support/mcp-memory), ~/.B12 and | ||
| ~/Library/LaunchAgents all derive from $HOME, while the launchd job domain, | ||
| the daemon sockets and PID files under /tmp and the user crontab are shared by | ||
| the whole account. Running the suite on a machine with B12 installed once | ||
| rebound the live com.b12.mcp.daemon to a pytest temp plist, stopped the live | ||
| embed daemon and wrote a session summary into the live database. | ||
|
|
||
| pytest_configure runs before any test module is imported, so module-level | ||
| paths (DB_PATH, socket paths) already see the isolated values: | ||
|
|
||
| - HOME, the embed runtime dir and the MCP daemon socket point into a private | ||
| temp tree; inherited B12_* settings are dropped. | ||
| - No-op `launchctl` and `crontab` stubs come first on PATH, so an installer run | ||
| can never load, unload or schedule anything for the real account. | ||
| - The Hugging Face cache keeps its real location so model files are not | ||
| downloaded again, and B12_TEST_VENV_PYTHON points at the installed venv | ||
| interpreter for tests that only need to run it. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import pathlib | ||
| import shutil | ||
| import tempfile | ||
|
|
||
| _LAUNCHCTL = """#!/bin/sh | ||
| # Test isolation stub: never touch the real launchd domain. | ||
| echo "launchctl $*" >> "{log}" | ||
| # `launchctl list <label>` reports "not loaded" so callers never reload a job. | ||
| if [ "$1" = list ] && [ -n "$2" ]; then exit 113; fi | ||
| exit 0 | ||
| """ | ||
|
|
||
| _CRONTAB = """#!/bin/sh | ||
| # Test isolation stub: never read or replace the real user crontab. | ||
| echo "crontab $*" >> "{log}" | ||
| if [ "$1" = - ]; then cat >/dev/null; fi | ||
| exit 0 | ||
| """ | ||
|
|
||
|
|
||
| _ISOLATION_BASE: pathlib.Path | None = None | ||
|
|
||
|
|
||
| def pytest_configure(config): | ||
| global _ISOLATION_BASE | ||
| real_home = os.path.expanduser("~") | ||
| # Short base under /tmp keeps AF_UNIX socket paths below the 104-byte limit. | ||
| base = pathlib.Path(tempfile.mkdtemp(prefix="b12t-", dir="/tmp" if os.path.isdir("/tmp") else None)) | ||
| _ISOLATION_BASE = base | ||
| home, runtime, stubs = base / "home", base / "run", base / "bin" | ||
| for path in (home, runtime, stubs): | ||
| path.mkdir() | ||
|
|
||
| log = base / "stub-calls.log" | ||
| for name, body in (("launchctl", _LAUNCHCTL), ("crontab", _CRONTAB)): | ||
| stub = stubs / name | ||
| stub.write_text(body.format(log=log)) | ||
| stub.chmod(0o755) | ||
|
|
||
| real_venv_python = os.path.join(real_home, ".local", "b12-venv", "bin", "python3") | ||
| hf_cache = os.path.join(real_home, ".cache", "huggingface") | ||
| if "HF_HOME" not in os.environ and os.path.isdir(hf_cache): | ||
| os.environ["HF_HOME"] = hf_cache | ||
|
|
||
| for key in [k for k in os.environ if k.startswith("B12_")]: | ||
| del os.environ[key] | ||
| os.environ["HOME"] = str(home) | ||
|
dorukardahan marked this conversation as resolved.
|
||
| # On native Windows expanduser() reads USERPROFILE, not HOME. | ||
| os.environ["USERPROFILE"] = str(home) | ||
| os.environ["B12_EMBED_RUNTIME_DIR"] = str(runtime) | ||
| os.environ["B12_MCP_DAEMON_SOCK"] = str(runtime / "b12-mcp.sock") | ||
| os.environ["B12_TEST_STUB_LOG"] = str(log) | ||
| # Tests that need the installed interpreter (not its data) can still find it. | ||
| if os.path.exists(real_venv_python): | ||
| os.environ["B12_TEST_VENV_PYTHON"] = real_venv_python | ||
| os.environ["PATH"] = f"{stubs}{os.pathsep}{os.environ.get('PATH', '')}" | ||
|
|
||
|
|
||
| def pytest_unconfigure(config): | ||
| if _ISOLATION_BASE is not None: | ||
| shutil.rmtree(_ISOLATION_BASE, ignore_errors=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.