unity-mcp/tests/test_logging_stdout.py

69 lines
2.5 KiB
Python
Raw Normal View History

Claude‑friendly edit tools + framed transport + live Unity NL test framework (#243) * CI: gate desktop-parity on Anthropic key; pass anthropic_api_key like NL suite * Add quickprobe prompt and CI workflow (mcp-quickprobe.md, unity-mcp-quickprobe.yml) * strictier tool use to prevent subagent spawning and force mcp tools * update workflow filesto reduce likelihood of subagent spawning * improve permissions for claude agent, fix mcpbridge timeout/token issue * increase max turns to 10 * ci: align NL suite to new permissions schema; prevent subagent drift * ci: NL suite -> mini prompt for e2e; add full NL/T prompt; server: ctx optional + project_root fallback; workflows: set UNITY_PROJECT_ROOT for CI * ci: add checks:write; revert local project hardcodes (manifest, ProjectVersion.txt) * tools: text-edit routing fixes (anchor_insert via text, CRLF calc); prompts: mini NL/T clarifications * ci: use absolute UNITY_PROJECT_ROOT; prompts target TestProjects; server: accept relative UNITY_PROJECT_ROOT and bare spec URI * ci: ignore Unity test project's packages-lock.json; remove from repo to avoid absolute paths * CI: start persistent Unity Editor for MCP (guarded by license) + allow batch-mode bridge via UNITY_MCP_ALLOW_BATCH * CI: hide license and pass via env to docker; fix invalid ref format * CI: readiness probe uses handshake on Unity MCP port (deterministic) * CI: fix YAML; use TCP handshake readiness probe (FRAMING=1) * CI: prime Unity license via game-ci; mount ULF into container; extend readiness timeout * CI: use ULF write + mount for Unity licensing; remove serial/email/pass from container * CI: entitlement activation (UNITY_SERIAL=''); verify host ULF cache; keep mount * CI: write ULF from secret and verify; drop entitlement activation step * CI: detect any licensing path; GameCI prime; status dir env; log+probe readiness; fix YAML * CI: add GameCI license prime; conditional ULF write; one-shot license validation; explicit status dir + license env * CI: fix YAML (inline python), add Anthropic key detect via GITHUB_ENV; ready to run happy path * CI: mount Unity token/ulf/cache dirs into container to share host license; create dirs before start * CI: fix YAML indentation; write ULF on host; activate in container with shared mounts; mount .config and .cache too * CI: gate Claude via outputs; mount all Unity license dirs; fix inline probe python; stabilize licensing flow * CI: normalize detect to step outputs; ensure license dirs mounted and validated; fix indentation * Bridge: honor UNITY_MCP_STATUS_DIR for heartbeat/status file (CI-friendly) * CI: guard project path for activation/start; align tool allowlist; run MCP server with python; tighten secret scoping * CI: finalize Unity licensing mounts + status dir; mode-detect (ULF/EBL); readiness logs+probe; Claude gating via outputs * CI: fix YAML probe (inline python -c) and finalize happy-path Unity licensing and MCP/Claude wiring * CI: inline python probe; unify Unity image and cache mounts; ready to test * CI: fix docker run IMAGE placement; ignore cache find perms; keep same editor image * CI: pass -manualLicenseFile to persistent Editor; keep mounts and single image * CI: mount full GameCI cache to /root in persistent Unity; set HOME=/root; add optional license check * CI: make -manualLicenseFile conditional; keep full /root mount and license check * CI: set HOME=/github/home; mount GameCI cache there; adjust manualLicenseFile path; expand license check * CI: EBL sign-in for persistent Unity (email/password/serial); revert HOME=/root and full /root mount; keep conditional manualLicenseFile and improved readiness * CI: run full NL/T suite prompt (nl-unity-suite-full.md) instead of mini * NL/T: require unified diffs + explicit verdicts in JUnit; CI: remove short sanity step, publish JUnit, upload artifacts * NL/T prompt: require CDATA wrapping for JUnit XML fields; guidance for splitting embedded ]]>; keep VERDICT in CDATA only * CI: remove in-container license check step; keep readiness and full suite * NL/T prompt: add version header, stricter JUnit schema, hashing/normalization, anchors, statuses, atomic semantics, tool logging * CI: increase Claude NL/T suite timeout to 30 minutes * CI: pre-create reports dir and result files to avoid tool approval prompts * CI: skip wait if container not running; skip Editor start if project missing; broaden MCP deps detection; expand allowed tools * fixies to harden ManageScript * CI: sanitize NL/T markdown report to avoid NUL/encoding issues * revert breaking yyaml changes * CI: prime license, robust Unity start/wait, sanitize markdown via heredoc * Resolve merge: accept upstream renames/installer (fix/installer-cleanup-v2) and keep local framing/script-editing - Restored upstream server.py, EditorWindow, uv.lock\n- Preserved ManageScript editing/validation; switched to atomic write + debounced refresh\n- Updated tools/__init__.py to keep script_edits/resources and adopt new logger name\n- All Python tests via uv: 7 passed, 6 skipped, 9 xpassed; Unity compile OK * Fix Claude Desktop config path and atomic write issues - Fix macOS path for Claude Desktop config: use ~/Library/Application Support/Claude/ instead of ~/.config/Claude/ - Improve atomic write pattern with backup/restore safety - Replace File.Replace() with File.Move() for better macOS compatibility - Add proper error handling and cleanup for file operations - Resolves issue where installer couldn't find Claude Desktop config on macOS * Editor: use macConfigPath on macOS for MCP client config writes (Claude Desktop, etc.). Fallback to linuxConfigPath only if mac path missing. * Models: add macConfigPath to McpClient for macOS config path selection (fixes CS1061 in editor window). * Editor: on macOS, prefer macConfigPath in ManualConfigEditorWindow (fallback to linux path); Linux/Windows unchanged. * Fix McpClient: align with upstream/main, prep for framing split * NL suite: shard workflow; tighten bridge readiness; add MCP preflight; use env-based shard vars * NL suite: fix shard step indentation; move shard vars to env; remove invalid inputs * MCP clients: split VSCode Copilot config paths into macConfigPath and linuxConfigPath * Unity bridge: clean stale status; bind host; robust wait probe with IPv4/IPv6 + diagnostics * CI: use MCPForUnity.Editor.MCPForUnityBridge.StartAutoConnect as executeMethod * Action wiring: inline mcpServers in settings for all shards; remove redundant .claude/mcp.json step * CI: embed mcpServers in settings for all shards; fix startup sanity step; lint clean * CI: pin claude-code-base-action to e6f32c8; use claude_args --mcp-config; switch to allowed_tools; ensure MCP config per step * CI: unpin claude-code-base-action to @beta (commit ref not found) * CI: align with claude-code-base-action @beta; pass MCP via claude_args and allowedTools * Editor: Fix apply_text_edits heuristic when edits shift positions; recompute method span on candidate text with fallback delta adjustment * CI: unify MCP wiring across workflows; write .claude/mcp.json; switch to claude_args with --mcp-config/--allowedTools; remove unsupported inputs * CI: collapse NL suite shards into a single run to avoid repeated test execution * CI: minimize allowedTools for NL suite to essential Unity MCP + Bash("git:*") + Write * CI: mkdir -p reports before run; remove unsupported --timeout-minutes from claude_args * CI: broaden allowedTools to include find_in_file and mcp__unity__* * CI: enable use_node_cache and switch NL suite model to claude-3-7-haiku-20250219 * CI: disable use_node_cache to avoid setup-node lockfile error * CI: set NL suite model to claude-3-haiku-20240307 * CI: cap Haiku output with --max-tokens 2048 for NL suite * CI: switch to claude-3-7-sonnet-latest and remove unsupported --max-tokens * CI: update allowedTools to Bash(*) and explicit Unity MCP tool list * CI: update NL suite workflow (latest tweaks) * Tests: tighten NL suite prompt for logging, hash discipline, stale retry, evidence windows, diff cap, and VERDICT line * Add disallowed tools to NL suite workflow * docs: clarify stale write retry * Add fallback JUnit report and adjust publisher * Indent fallback JUnit XML in workflow * fix: correct fallback JUnit report generation * Update mcp-quickprobe.md Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Update mcp-quickprobe.md Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Update Response.cs Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Update MCPForUnityBridge.cs Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * fix: correct McpTypes reference * Add directory existence checks for symlink and XDG paths * fix: only set installation flag after successful server install * Update resource_tools.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * fix: respect mac config paths * Use File.Replace for atomic config write * Remove unused imports in manage_script * bump server version * Tests: update NL suite prompt and workflows; remove deprecated smoke/desktop-parity; quickprobe tidy * Editor: atomic config write via File.Replace fallback; remove redundant backups and racey exists checks * CI: harden NL suite - idempotent docker, gate on unity_ok, safer port probe, least-priv perms * Editor: make atomic config write restoration safe (flag writeDone; copy-overwrite restore; cleanup in finally) * CI: fix fallback JUnit heredoc by using printf lines (no EOF delimiter issues) * CI: switch NL suite to mini prompt; mini prompt honors / and NL discipline * CI: replace claude_args with allowed_tools/model/mcp_config per action schema * CI: expand UNITY_PROJECT_ROOT via in MCP config heredoc * EditorWindow: add cross-platform fallback for File.Replace; macOS-insensitive PathsEqual; safer uv resolve; honor macConfigPath * CI: strengthen JUnit publishing for NL mini suite (normalize, debug list, publish both, fail_on_parse_error) * CI: set job-wide JUNIT_OUT/MD_OUT; normalization uses env; publish references env and ungroup reports * CI: publish a single normalized JUnit (reports/junit-for-actions.xml); fallback writes same; avoid checkName/reportPaths mismatch * CI: align mini prompt report filenames; redact Unity log tail in diagnostics * chore: sync workflow and mini prompt; redacted logs; JUnit normalization/publish tweaks * CI: redact sensitive tokens in Stop Unity; docs: CI usage + edit tools * prompts: update nl-unity-suite-full (mini-style setup + reporting discipline); remove obsolete prompts * CI: harden NL workflows (timeout_minutes, robust normalization); prompts: unify JUnit suite name and reporting discipline * prompts: add guarded write pattern (LF hash, stale_file retry) to full suite * prompts: enforce continue-on-failure, driver flow, and status handling in full suite * Make test list more explicit in prompt. Get rid of old test prompts for hygeine. * prompts: add stale fast-retry (server hash) + in-memory buf guidance * CI: standardize JUNIT_OUT to reports/junit-nl-suite.xml; fix artifact upload indentation; prompt copy cleanups * prompts: reporting discipline — append-only fragments, batch writes, no model round-trip * prompts: stale fast-retry preference, buffer/sha carry, snapshot revert, essential logging * workflows(nl-suite): precreate report skeletons, assemble junit, synthesize markdown; restrict allowed_tools to append-only Bash + MCP tools * thsis too * Update README-DEV.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .github/workflows/claude-nl-suite-mini.yml Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .github/workflows/claude-nl-suite.yml Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * workflows(nl-mini): fix YAML indentation/trailing spaces under with: and cleanup heredoc spacing * workflows(nl-suite): fix indentation on docker logs redaction line (YAML lint) * Add write to allowlist * nl-suite: harden reporting discipline (fragment-only writes, forbid alt paths); workflow: clean stray junit-*updated*.xml * nl-suite: enforce end-of-suite single Write (no bash redirection); workflow: restrict allowed_tools to Write+MCP only * prompts(nl-full): end-of-suite results must be valid XML with single <cases> root and only <testcase> children; no raw text outside CDATA * workflows(nl-suite): make Claude step non-fatal; tolerant normalizer extracts <testcase> via regex on bad fragments * nl-suite: fix stale classname to UnityMCP.NL-T in mini fallback; prompt: require re-read after every revert; correct PLAN/PROGRESS to 15 * nl-suite: fix fallback JUnit classname to UnityMCP.NL-T; prompt: forbid create_script and env/mkdir checks, enforce single baseline-byte revert flow and post-revert re-read; add corruption-handling guidance * prompts(nl-full): after each write re-read raw bytes to refresh pre_sha; prefer script_apply_edits for anchors; avoid header/using changes * prompts(nl-full): canonicalize outputs to /; allow small fragment appends via Write or Bash(printf/echo); forbid wrappers and full-file round-trips * prompts(nl-full): finalize markdown formatting for guarded write, execution order, specs, status * workflows(nl-suite, mini): header/lint fixes and constrained Bash append path; align allowed_tools * prompts(nl-full): format Fast Restore, Guarded Write, Execution, Specs, Status as proper markdown lists and code fences * workflows(nl-suite): keep header tidy and append-path alignment with prompt * minor fix * workflows(nl-suite): fix indentation and dispatch; align allowed_tools and revert helper * prompts(nl-full): switch to read_resource for buf/sha; re-read only when needed; convert 'Print this once' to heading; note snapshot helper creates parent dirs * workflows(nl-suite): normalize step removes bootstrap when real testcases present; recompute tests/failures * workflows(nl-suite): enrich Markdown summary by extracting per-test <system-out> blocks (truncated) * clarify prompt resilience instructions * ci(nl-suite): revert prompt and workflow to known-good e0f8a72 for green run; remove extra MD details * ci(nl-suite): minimal fixes — no-mkdir guard in prompt; drop bootstrap and recompute JUnit counts * ci(nl-suite): richer JUnit→Markdown report (per-test system-out) * Small guard to incorret asset read call. * ci(nl-suite): refine MD builder — unescape XML entities, safe code fences, PASS/FAIL badges * Update UnityMcpBridge/UnityMcpServer~/src/tools/resource_tools.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update UnityMcpBridge/UnityMcpServer~/src/unity_connection.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update UnityMcpBridge/UnityMcpServer~/src/tools/manage_script_edits.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .github/scripts/mark_skipped.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .github/scripts/mark_skipped.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .github/scripts/mark_skipped.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * server(manage_script): robust URI handling — percent-decode file://, normalize, strip host/leading slashes, return Assets-relative if present * Update .claude/prompts/nl-unity-suite-full.md Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * tests(framing): reduce handshake poll window, nonblocking peek to avoid disconnect race; still enforce pre-handshake data drop * tests(manage_script): add _split_uri tests for unity://path, file:// URLs (decoded/Assets-relative), and plain paths * server+tests: fix handshake syntax error; robust file:// URI normalization in manage_script; add _split_uri tests; adjust stdout scan to ignore venv/site-packages * bridge(framing): accept zero-length frames (treat as empty keepalive) * tests(logging): use errors='replace' on decode fallback to avoid silent drops * resources(list): restrict to Assets/, resolve symlinks, enforce .cs; add traversal/outside-path tests * Update .claude/prompts/nl-unity-suite-full.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update UnityMcpBridge/UnityMcpServer~/src/unity_connection.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * misc: framing keepalive (zero-length), regex preview consistency, resource.list hardening, URI parsing, legacy update routing, test cleanups * docs(tools): richer MCP tool descriptions; tests accept decorator kwargs; resource URI parsing hardened * Update .claude/prompts/nl-unity-suite-full.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update UnityMcpBridge/UnityMcpServer~/src/tools/resource_tools.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update UnityMcpBridge/UnityMcpServer~/src/unity_connection.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * net+docs: hard-reject zero-length frames; TCP_NODELAY on connect; Assets detection case-insensitive; NL prompt statuses aligned * prompt(nl-suite): constrain Write destinations under reports/, forbid traversal * prompt+net: harden Write path rules; use monotonic deadline and plain-text advisory for non-framed peers * unity_connection: restore recv timeout via try/finally; make global connection getter thread-safe with module lock and double-checked init * NL/T prompt: pin structured edit ops for T-D/T-E; add schema-error guarded write behavior; keep existing path/URI and revert rules * unity_connection: add FRAMED_MAX; use ValueError for framed length violations; lower framed receive log to debug; serialize connect() with per-instance lock * ManageScript: use UTF8Encoding(without BOM) for atomic writes in ApplyTextEdits/EditScript to align with Create/Update and avoid BOM-related diffs/hash mismatches * NL/T prompt: make helper deletion regex multiline-safe ((?ms) so ^ anchors line starts) * ManageScript: emit structured overlap status {status:"overlap"} for overlapping edit ranges in apply_text_edits and edit paths * NL/T prompt: clarify fallback vs failure — fallback only for unsupported/missing_field; treat bad_request as failure; note unsupported after fallback as failure * NL/T prompt: pin deterministic overlap probe (apply_text_edits two ranges from same snapshot); gate too_large behind RUN_TOO_LARGE env hint * TB update * NL/T prompt: harden Output Rules — constrain Bash(printf|echo) to stdout-only; forbid redirection/here-docs/tee; only scripts/nlt-revert.sh may mutate FS * Prompt: enumerate allowed script_apply_edits ops; add manage_editor/read_console guidance; fix T‑F atomic batch to single script_apply_edits. ManageScript: regex timeout for diagnostics; symlink ancestor guard; complete allowed-modes list. * Fixes * ManageScript: add rich overlap diagnostics (conflicts + hint) for both text range and structured batch paths * ManageScript: return structured {status:"validation_failed"} diagnostics in create/update/edits and validate before commit * ManageScript: echo canonical uri in responses (create/read/update/apply_text_edits/structured edits) to reinforce resource identity * improve clarity of capabilities message * Framing: allow zero-length frames on both ends (C# bridge, Python server). Prompt: harden T-F to single text-range apply_text_edits batch (descending order, one snapshot). URI: normalize file:// outside Assets by stripping leading slash. * ManageScript: include new sha256 in success payload for apply_text_edits; harden TryResolveUnderAssets by rejecting symlinked ancestors up to Assets/. * remove claudetest dir * manage_script_edits: normalize method-anchored anchor_insert to insert_method (map text->replacement); improves CI compatibility for T‑A/T‑E without changing Editor behavior. * tighten testing protocol around mkdir * manage_script: validate create_script inputs (Assets/.cs/name/no traversal); add Assets/ guard to delete_script; validate level+Assets in validate_script; make legacy manage_script optional params; harden legacy update routing with base64 reuse and payload size preflight. * Tighten prompt for testing * Update .claude/prompts/nl-unity-suite-full.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .claude/prompts/nl-unity-suite-full.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update UnityMcpBridge/Editor/Tools/ManageScript.cs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * manage_script_edits: honor ignore_case on anchor_insert and regex_replace in both direct and text-conversion paths (MULTILINE|IGNORECASE). * remove extra file * workflow: use python3 for inline scripts and port detection on ubuntu-latest. * Tighten prompt + manage_script * Update UnityMcpBridge/UnityMcpServer~/src/tools/manage_script_edits.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update UnityMcpBridge/UnityMcpServer~/src/tools/manage_script_edits.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .claude/prompts/nl-unity-suite-full.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update UnityMcpBridge/Editor/Tools/ManageScript.cs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update .claude/prompts/nl-unity-suite-full.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * manage_script: improve file:// UNC handling; preserve POSIX absolute semantics internally; keep test-expected slash stripping for non-Assets paths. * ManageScript.cs: add TimeSpan timeouts to all Regex uses (IsMatch/Match/new Regex) and keep CultureInvariant/Multiline options; reduces risk of catastrophic backtracking stalls. * workflow: ensure reports/ exists in markdown build step to avoid FileNotFoundError when writing MD_OUT. * fix brace * manage_script_edits: expand backrefs for regex_replace in preview->text conversion and translate to \g<n> in local apply; keeps previews and actual edits consistent. * anchor_insert: default to position=after, normalize surrounding newlines in Python conversion paths; C# path ensures trailing newline and skips duplicate insertion within class. * feat(mcp): add get_sha tool; apply_text_edits normalization+overlap preflight+strict; no-op evidence in C#; update NL suite prompt; add unit tests * feat(frames): accept zero-length heartbeat frames in client; add heartbeat test * feat(edits): guard destructive regex_replace with structural preflight; add robust tests; prompt uses delete_method for temp helper * feat(frames): bound heartbeat loop with timeout/threshold; align zero-length response with C#; update test * SDK hardening: atomic multi-span text edits; stop forcing sequential for structured ops; forward options on apply_text_edits; add validate=relaxed support and scoped checks; update NL/T prompt; add tests for options forwarding, relaxed mode, and atomic batches * Router: default applyMode=atomic for multi-span apply_text_edits; add tests * CI prompt: pass options.validate=relaxed for T-B/C; options.applyMode=atomic for T-F; emphasize always writing testcase and restoring on errors * Validation & DX: add validate=syntax (scoped), standardize evidence windows; early regex compile with hints; debug_preview for apply_text_edits * Update UnityMcpBridge/Editor/Windows/MCPForUnityEditorWindow.cs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * NL/T suite-driven edits: LongUnityScriptClaudeTest, bridge helpers, server_version; prepare framing tests * Fix duplicate macConfigPath field in McpClient to resolve CS0102 * Editor threading: run EnsureServerInstalled on main thread; marshal EditorPrefs/DeleteKey + logging via delayCall * Docs(apply_text_edits): strengthen guidance on 1-based positions, verify-before-edit, and recommend anchors/structured edits * Docs(script_apply_edits): add safety guidance (anchors, method ops, validators) and recommended practices * Framed VerifyBridgePing in editor window; docs hardening for apply_text_edits and script_apply_edits --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-08-31 00:55:38 +08:00
import ast
from pathlib import Path
import pytest
# locate server src dynamically to avoid hardcoded layout assumptions
ROOT = Path(__file__).resolve().parents[1]
candidates = [
ROOT / "UnityMcpBridge" / "UnityMcpServer~" / "src",
ROOT / "UnityMcpServer~" / "src",
]
SRC = next((p for p in candidates if p.exists()), None)
if SRC is None:
searched = "\n".join(str(p) for p in candidates)
pytest.skip(
"Unity MCP server source not found. Tried:\n" + searched,
allow_module_level=True,
)
@pytest.mark.skip(reason="TODO: ensure server logs only to stderr and rotating file")
def test_no_stdout_output_from_tools():
pass
def test_no_print_statements_in_codebase():
"""Ensure no stray print/sys.stdout writes remain in server source."""
offenders = []
syntax_errors = []
for py_file in SRC.rglob("*.py"):
# Skip virtual envs and third-party packages if they exist under SRC
parts = set(py_file.parts)
if ".venv" in parts or "site-packages" in parts:
continue
try:
text = py_file.read_text(encoding="utf-8", errors="strict")
except UnicodeDecodeError:
# Be tolerant of encoding edge cases in source tree without silently dropping bytes
text = py_file.read_text(encoding="utf-8", errors="replace")
try:
tree = ast.parse(text, filename=str(py_file))
except SyntaxError:
syntax_errors.append(py_file.relative_to(SRC))
continue
class StdoutVisitor(ast.NodeVisitor):
def __init__(self):
self.hit = False
def visit_Call(self, node: ast.Call):
# print(...)
if isinstance(node.func, ast.Name) and node.func.id == "print":
self.hit = True
# sys.stdout.write(...)
if isinstance(node.func, ast.Attribute) and node.func.attr == "write":
val = node.func.value
if isinstance(val, ast.Attribute) and val.attr == "stdout":
if isinstance(val.value, ast.Name) and val.value.id == "sys":
self.hit = True
self.generic_visit(node)
v = StdoutVisitor()
v.visit(tree)
if v.hit:
offenders.append(py_file.relative_to(SRC))
assert not syntax_errors, "syntax errors in: " + ", ".join(str(e) for e in syntax_errors)
assert not offenders, "stdout writes found in: " + ", ".join(str(o) for o in offenders)