ci3: lint/mypy 第二阶段清理 + 覆盖率 76% -> 78%(门槛不动)
基底:#251 头 174b631(堆叠于 #242 feat-ci-expand 之上), 基线 origin/main aebc6238;本轨不改 #251 已定语义、不动 workflow 门槛、不删检查、不加 skip。 lint(ruff E,F@120):109 -> 10 - F401 43 -> 0:删未用导入;try/except 双绑定的名字出现 “打地鼠”(删 except 分支后 try 分支又被标出),两分支 均已核对正文无引用后清掉;MCP 三模块的 Error 名不删, 改走 importlib 属性绑定(见下),两路径绑定都保留。 - E702 40 -> 0:全在 host.py main() 的 argparse 段,纯分行, tokenize 按分号 token 拆(含 2377 行多行语句只断首个分号); 一次 5 空格缩进失误已回滚重做并以 ast + 全量 host 测试验证。 - E501 13 -> 0:host.py 5 项随 E702 分行连带清掉;mcp_server 3 项把描述拆成相邻字面量(AST 比对 24 条工具描述与原值 完全一致,注意 ruff 按东亚宽度计长,CJK 算 2);test_host 5 项断开 git commit 参数表。 - E402 8 -> 10:生产侧 2 项清掉(db.py _re、github_app _base64 移到顶部,之前无引用、无重名);剩余 10 项全是 tests/ 的 sys.path 前置 import(6 项旧 + 4 个新测试文件 沿用同一既定模式),移上去会破坏单文件直跑,有意保留。 - F841 4 -> 0:只去绑定、不删调用(run_task、DB 入队、 tarfile 上下文、git 提交),各有注释与原测试/新测试覆盖; host dispatch_from_branch 新增真实 git 仓库回归测试。 - F811 1 -> 0:server 本地 _row_get 与 peer_messages.row_get 逐行相同,删本地定义,全部调用点解析到同一 import; 新增 test_server_row_get(同一对象 + 三路行为)。 mypy(生产范围):24 -> 0,零新增 type: ignore - no-redef 9 -> 0:MCP 三模块 try/except 两次 from-import 改 importlib.import_module + 属性绑定(cloud 优先、flat copy 回落、内层 ImportError 同样回落,语义与原来一致; flat-copy 子进程测试证实回落后 __module__ 为顶层名); server _row_get 随 F811 消除。 - arg-type 10 -> 0:harness2_tasks shared 改 TypedDict 逐键精确类型;CodexProbeAdapter 补 no-op validate_tree (_adapter_validate 本就跳过缺失方法,行为一致)。 - var-annotated 3 -> 0:sub2api _SHARED_ADMIN/_login_slots、 verify-multiuser RESULTS 加精确注解。 - assignment/misc 2 -> 0:marker_tools ZoneInfo 先声明 Any 再 try import(草稿先验 mypy 通过)。 bandit:总数 286 不变,High 仍 4 项,全在 host.py,本轨不改 (属第 3 项白名单轨范围),只做 triage: - 1319 run_verify:无插值,整串验测命令,管道/&& 须 shell 语义(新增 test_host_run_verify 断言管道与 && 真实生效)。 - 1497 notify:模板来自算子配置,三处插值全部 shlex.quote, 证据为既有 test_notify_failure_quotes_shell_metacharacters (; touch 与 $(touch ) 不执行)。 - 2099/2103 goal verify:无插值,任务配置验测串,同 run_verify。 - 无新增 High;pip-audit 0 漏洞。 覆盖率(同口径三套件 coverage 计量):75.96% -> 77.87% (22392 行/缺 5385 -> 22437 行/缺 4963,显示 76% -> 78%) - client.py 0% -> 90%(pipe 假子进程 + main 分支) - run_parallel.py 0% -> 97%、migrate 包装 0% -> 95% - playwright_mcp 37% -> 62%、imagegen_mcp 47% -> 79%、 websearch_mcp 51% -> 86%、deploy.py 56% -> 77% - verify-multiuser 0% -> 27%(仅 check/req helper;main() 是主机侧真实多用户实弹验证,按门禁不建生产任务、 不以 mock 冒充,仍为缺口) - host.py 55% -> 57%(文件 2020 -> 2060 行系 E702 分行 的计量口径变化,非代码膨胀) - 未到 85%:门槛保持 75% 不动,不删低覆盖模块出统计口径; 缺口主力仍是 server.py / worker.py / host.py 大文件, 下一阶段须针对性深测,非本轨快赢可及。 验证:三套件 1576 -> 1629 项全过(tests 127、cloud/tests 1449 skipped=4、deploy/tests 53);ruff E9 通过;mypy 0; bandit High 4(同上);pip-audit 0 漏洞;试跑产物已删, 提交后工作树干净。
This commit is contained in:
1 parent
7ba5bfaf81
commit
f39ed3654e
51 files changed
+1477
-120
No files matched your search
@@ -0,0 +1,293 @@
|
||||
"""client.py 单测(CI 第二阶段覆盖率:基线 0%)。
|
||||
|
||||
不跑真实 codex 二进制:AppServer 只跟子进程的 stdin/stdout
|
||||
说话,本文件用真实 os.pipe 作 stdout 通道(select + os.read
|
||||
原样走)、BytesIO 作 stdin——JSON-RPC 成帧、审批自动回包、
|
||||
wait_for 事件分发、initialize/close 都是被测代码的真实逻辑,
|
||||
只把“子进程是 codex”换成管道。main() 的分支用一个记录型
|
||||
FakeAppServer 测(断言它发出的请求参数与分支选择)。
|
||||
"""
|
||||
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import types
|
||||
import unittest
|
||||
from contextlib import redirect_stdout, redirect_stderr
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(REPO_ROOT))
|
||||
|
||||
import client as client_mod
|
||||
|
||||
|
||||
class FakeProc:
|
||||
"""Popen 替身:stdout 是真实 pipe(可 select/os.read),
|
||||
stdin 是 BytesIO(可断言发出的每一行)。"""
|
||||
|
||||
def __init__(self, chunks, wait_exc=None):
|
||||
self.r, self.w = os.pipe()
|
||||
for chunk in chunks:
|
||||
os.write(self.w, chunk)
|
||||
os.close(self.w)
|
||||
self.stdin = io.BytesIO()
|
||||
self.stdout = types.SimpleNamespace(fileno=lambda: self.r)
|
||||
self._wait_exc = wait_exc
|
||||
self.waited = False
|
||||
self.killed = False
|
||||
|
||||
def poll(self):
|
||||
return None
|
||||
|
||||
def wait(self, timeout=None):
|
||||
self.waited = True
|
||||
if self._wait_exc is not None:
|
||||
raise self._wait_exc
|
||||
return 0
|
||||
|
||||
def kill(self):
|
||||
self.killed = True
|
||||
|
||||
def close_fds(self):
|
||||
try:
|
||||
os.close(self.r)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def _line(obj):
|
||||
return (json.dumps(obj) + "\n").encode()
|
||||
|
||||
|
||||
class AppServerPipeTest(unittest.TestCase):
|
||||
def _server(self, chunks, codex_home=None, wait_exc=None):
|
||||
proc = FakeProc(chunks, wait_exc=wait_exc)
|
||||
self.addCleanup(proc.close_fds)
|
||||
with mock.patch.object(client_mod.subprocess, "Popen",
|
||||
return_value=proc):
|
||||
srv = client_mod.AppServer(codex_home=codex_home)
|
||||
srv._proc = proc
|
||||
return srv, proc
|
||||
|
||||
def test_request_and_notify_framing(self):
|
||||
srv, proc = self._server([])
|
||||
rid = srv.request("thread/list", {"a": 1})
|
||||
self.assertEqual(rid, 1)
|
||||
srv.notify("initialized", {})
|
||||
self.assertEqual(srv.next_id, 2)
|
||||
lines = proc.stdin.getvalue().decode().splitlines()
|
||||
self.assertEqual(json.loads(lines[0]),
|
||||
{"jsonrpc": "2.0", "id": 1, "method": "thread/list",
|
||||
"params": {"a": 1}})
|
||||
self.assertEqual(json.loads(lines[1]),
|
||||
{"jsonrpc": "2.0", "method": "initialized",
|
||||
"params": {}})
|
||||
|
||||
def test_read_msg_answers_approval_then_returns_response(self):
|
||||
chunks = [_line({"jsonrpc": "2.0", "id": 9,
|
||||
"method": "approval/request",
|
||||
"params": {"x": 1}}),
|
||||
_line({"jsonrpc": "2.0", "id": 1, "result": {"ok": True}})]
|
||||
srv, proc = self._server(chunks)
|
||||
srv.approval_policy = "deny"
|
||||
msg = srv._read_msg(5)
|
||||
self.assertEqual(msg, {"jsonrpc": "2.0", "id": 1,
|
||||
"result": {"ok": True}})
|
||||
sent = proc.stdin.getvalue().decode().splitlines()
|
||||
self.assertEqual(json.loads(sent[0]),
|
||||
{"jsonrpc": "2.0", "id": 9,
|
||||
"result": {"decision": "deny"}})
|
||||
|
||||
def test_read_msg_approval_auto(self):
|
||||
chunks = [_line({"jsonrpc": "2.0", "id": 7,
|
||||
"method": "permission/request", "params": {}}),
|
||||
_line({"jsonrpc": "2.0", "id": 1, "result": {}})]
|
||||
srv, proc = self._server(chunks)
|
||||
srv.approval_policy = "auto"
|
||||
with redirect_stderr(io.StringIO()):
|
||||
msg = srv._read_msg(5)
|
||||
self.assertEqual(msg["id"], 1)
|
||||
sent = proc.stdin.getvalue().decode().splitlines()
|
||||
self.assertEqual(json.loads(sent[0])["result"],
|
||||
{"decision": "approve"})
|
||||
|
||||
def test_wait_for_dispatches_events(self):
|
||||
chunks = [_line({"jsonrpc": "2.0", "method": "agent/message/delta",
|
||||
"params": {"item": {"type": "agentMessage",
|
||||
"text": "hi"}}}),
|
||||
_line({"jsonrpc": "2.0", "id": 3, "result": {"done": 1}})]
|
||||
srv, _ = self._server(chunks)
|
||||
seen = []
|
||||
msg = srv.wait_for(3, timeout=5, on_event=seen.append)
|
||||
self.assertEqual(msg["result"], {"done": 1})
|
||||
self.assertEqual(len(seen), 1)
|
||||
self.assertEqual(seen[0]["method"], "agent/message/delta")
|
||||
|
||||
def test_wait_for_eof_returns_none(self):
|
||||
srv, _ = self._server([])
|
||||
self.assertIsNone(srv.wait_for(1, timeout=5))
|
||||
|
||||
def test_initialize_success_and_error(self):
|
||||
chunks = [_line({"jsonrpc": "2.0", "id": 1,
|
||||
"result": {"codexHome": "/tmp/ch",
|
||||
"platformOs": "linux"}})]
|
||||
srv, proc = self._server(chunks)
|
||||
info = srv.initialize()
|
||||
self.assertEqual(info["platformOs"], "linux")
|
||||
sent = proc.stdin.getvalue().decode().splitlines()
|
||||
self.assertEqual(json.loads(sent[0])["method"], "initialize")
|
||||
self.assertEqual(json.loads(sent[1])["method"], "initialized")
|
||||
|
||||
srv2, _ = self._server(
|
||||
[_line({"jsonrpc": "2.0", "id": 1,
|
||||
"error": {"code": -1, "message": "boom"}})])
|
||||
with self.assertRaises(RuntimeError):
|
||||
srv2.initialize()
|
||||
|
||||
def test_codex_home_created(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
home = os.path.join(tmp, "new-home")
|
||||
srv, proc = self._server([], codex_home=home)
|
||||
self.assertTrue(os.path.isdir(home))
|
||||
self.assertEqual(proc.stdin.getvalue(), b"")
|
||||
|
||||
def test_close_waits_and_kills_on_timeout(self):
|
||||
srv, proc = self._server([])
|
||||
srv.close()
|
||||
self.assertTrue(proc.waited)
|
||||
self.assertFalse(proc.killed)
|
||||
import subprocess as _sp
|
||||
srv2, proc2 = self._server(
|
||||
[], wait_exc=_sp.TimeoutExpired("codex", 5))
|
||||
srv2.close()
|
||||
self.assertTrue(proc2.killed)
|
||||
|
||||
|
||||
class HandleEventTest(unittest.TestCase):
|
||||
def test_agent_delta_printed(self):
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out):
|
||||
client_mod.handle_event(
|
||||
{"method": "agent/message/delta",
|
||||
"params": {"item": {"type": "agentMessage",
|
||||
"text": "hello"}}})
|
||||
self.assertEqual(out.getvalue(), "hello")
|
||||
|
||||
def test_turn_completed_and_error_paths(self):
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with redirect_stdout(out), redirect_stderr(err):
|
||||
client_mod.handle_event(
|
||||
{"method": "turn/completed",
|
||||
"params": {"status": "completed"}})
|
||||
# 首分支元组外、含 started 的方法走 stderr 分支。
|
||||
client_mod.handle_event(
|
||||
{"method": "session/started", "params": {}})
|
||||
client_mod.handle_event(
|
||||
{"method": "something", "params": {"error": "x"}})
|
||||
self.assertIn("[turn/completed]", out.getvalue())
|
||||
self.assertIn("[session/started]", err.getvalue())
|
||||
self.assertIn("[something]", err.getvalue())
|
||||
|
||||
|
||||
class FakeAppServer:
|
||||
def __init__(self, codex_home=None, extra_args=None):
|
||||
self.codex_home = codex_home
|
||||
self.approval_policy = "deny"
|
||||
self.requests = []
|
||||
self.notifies = []
|
||||
self.responses = {}
|
||||
self.read_msgs = []
|
||||
self.closed = False
|
||||
|
||||
def request(self, method, params=None):
|
||||
rid = len(self.requests) + 1
|
||||
self.requests.append((method, params))
|
||||
return rid
|
||||
|
||||
def notify(self, method, params=None):
|
||||
self.notifies.append((method, params))
|
||||
|
||||
def wait_for(self, rid, timeout=60, on_event=None):
|
||||
return self.responses.get(rid)
|
||||
|
||||
def _read_msg(self, timeout):
|
||||
return self.read_msgs.pop(0) if self.read_msgs else None
|
||||
|
||||
def initialize(self):
|
||||
return {"codexHome": "/tmp/ch", "platformOs": "linux"}
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
|
||||
class ClientMainTest(unittest.TestCase):
|
||||
def _run(self, argv, setup):
|
||||
fake = FakeAppServer()
|
||||
setup(fake)
|
||||
err = io.StringIO()
|
||||
with mock.patch.object(client_mod, "AppServer",
|
||||
return_value=fake), \
|
||||
mock.patch.object(sys, "argv", argv), \
|
||||
redirect_stderr(err):
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out):
|
||||
client_mod.main()
|
||||
return fake, out.getvalue(), err.getvalue()
|
||||
|
||||
def test_list_threads(self):
|
||||
def setup(fake):
|
||||
fake.responses[1] = {"result": {"threads": []}}
|
||||
fake, out, _ = self._run(
|
||||
["client.py", "--list-threads", "--cwd", "/tmp"],
|
||||
setup)
|
||||
self.assertEqual(fake.requests[0][0], "thread/list")
|
||||
self.assertIn("threads", out)
|
||||
self.assertTrue(fake.closed)
|
||||
|
||||
def test_start_and_turn_completed(self):
|
||||
def setup(fake):
|
||||
fake.responses[1] = {"result": {"thread": {"id": "th-9"},
|
||||
"model": "m"}}
|
||||
fake.read_msgs.append(
|
||||
{"method": "turn/completed", "params": {"ok": True}})
|
||||
fake, out, _ = self._run(
|
||||
["client.py", "--prompt", "fix it", "--cwd", "/tmp",
|
||||
"--model", "gpt-x", "--sandbox", "read-only",
|
||||
"--approval-policy", "never"],
|
||||
setup)
|
||||
start = fake.requests[0]
|
||||
self.assertEqual(start[0], "thread/start")
|
||||
self.assertEqual(start[1]["approvalPolicy"], "never")
|
||||
self.assertEqual(start[1]["sandbox"], "read-only")
|
||||
self.assertEqual(start[1]["model"], "gpt-x")
|
||||
turn = fake.requests[1]
|
||||
self.assertEqual(turn[0], "turn/start")
|
||||
self.assertEqual(turn[1]["threadId"], "th-9")
|
||||
self.assertEqual(turn[1]["input"][0]["text"], "fix it")
|
||||
self.assertIn("[done]", out)
|
||||
self.assertTrue(fake.closed)
|
||||
|
||||
def test_resume_failure_prints_and_returns(self):
|
||||
def setup(fake):
|
||||
fake.responses[1] = {"error": {"message": "gone"}}
|
||||
fake, out, err = self._run(
|
||||
["client.py", "--thread-id", "th-1", "--cwd", "/tmp"],
|
||||
setup)
|
||||
self.assertEqual(fake.requests[0][0], "thread/resume")
|
||||
self.assertIn("thread/resume", err)
|
||||
self.assertTrue(fake.closed)
|
||||
|
||||
def test_no_prompt_prints_thread_id(self):
|
||||
def setup(fake):
|
||||
fake.responses[1] = {"result": {"thread": {"id": "th-2"}}}
|
||||
_, out, _ = self._run(
|
||||
["client.py", "--cwd", "/tmp"], setup)
|
||||
self.assertIn("thread_id=th-2", out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
+10
-5
@@ -126,7 +126,8 @@ class MergeVerifyTest(unittest.TestCase):
|
||||
" self.assertEqual(f(), 1)\n")
|
||||
(tests_dir / "test_mod.py").write_text(body, encoding="utf-8")
|
||||
subprocess.run(["git", "add", "-A"], cwd=tmp, check=True)
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com", "commit", "-qm", "initial"], cwd=tmp,
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com",
|
||||
"commit", "-qm", "initial"], cwd=tmp,
|
||||
check=True)
|
||||
# worktree 手动建:模拟 host 的 worktree/分支
|
||||
subprocess.run(["git", "worktree", "add", os.path.join(tmp, "wt"),
|
||||
@@ -175,7 +176,8 @@ class MergeVerifyTest(unittest.TestCase):
|
||||
Path(tmp, "mod.py").write_text("def f():\n return 1\n",
|
||||
encoding="utf-8")
|
||||
subprocess.run(["git", "add", "-A"], cwd=tmp, check=True)
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com", "commit", "-qm", "initial"], cwd=tmp,
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com",
|
||||
"commit", "-qm", "initial"], cwd=tmp,
|
||||
check=True)
|
||||
# 分支1:加 add2
|
||||
subprocess.run(["git", "worktree", "add",
|
||||
@@ -643,7 +645,8 @@ class GithubPRTest(unittest.TestCase):
|
||||
_init_git_repo(tmp)
|
||||
Path(tmp, "f.txt").write_text("x\n")
|
||||
subprocess.run(["git", "add", "-A"], cwd=tmp, check=True)
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com", "commit", "-qm", "initial"],
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com",
|
||||
"commit", "-qm", "initial"],
|
||||
cwd=tmp, check=True)
|
||||
task = {"name": "t", "branch": "host/t", "status": "completed",
|
||||
"prompt": "do it"}
|
||||
@@ -658,12 +661,14 @@ class GithubPRTest(unittest.TestCase):
|
||||
_init_git_repo(repo)
|
||||
Path(repo, "f.txt").write_text("x\n")
|
||||
subprocess.run(["git", "add", "-A"], cwd=repo, check=True)
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com", "commit", "-qm", "initial"],
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com",
|
||||
"commit", "-qm", "initial"],
|
||||
cwd=repo, check=True)
|
||||
subprocess.run(["git", "checkout", "-qb", "host/t1"],
|
||||
cwd=repo, check=True)
|
||||
Path(repo, "f.txt").write_text("y\n")
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com", "commit", "-qam", "change"],
|
||||
subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com",
|
||||
"commit", "-qam", "change"],
|
||||
cwd=repo, check=True)
|
||||
remote = os.path.join(tmp, "remote.git")
|
||||
subprocess.run(["git", "init", "--bare", "-q", remote],
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
"""dispatch_from_branch 回归测试(CI 第二阶段 F841 证据)。
|
||||
|
||||
背景:host.py 的 dispatch_from_branch 里 `git commit` 的返回值
|
||||
曾以 `c = git(...)` 绑定却从未读取(F841)。本轨只去绑定、
|
||||
不删调用——提交本身是依赖派发的前置:依赖 worktree 的改动
|
||||
必须先落到依赖分支,新 worktree 才从该分支建出、带血缘。
|
||||
本测试用真实 git 仓库断言这条链:
|
||||
|
||||
- 依赖 worktree 里未提交的改动被提交到依赖分支;
|
||||
- 新 worktree 的分支 host/<name> 由依赖分支 HEAD 派生
|
||||
(含该提交),且任务记录进 state。
|
||||
"""
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(REPO_ROOT))
|
||||
|
||||
import host
|
||||
|
||||
|
||||
def _git(args, cwd, check=True):
|
||||
return subprocess.run(["git"] + args, cwd=cwd, check=check,
|
||||
capture_output=True, text=True)
|
||||
|
||||
|
||||
class FakeServer:
|
||||
def create_thread(self, path, model=None, sandbox=None,
|
||||
approval_policy=None):
|
||||
self.created = (path, model, sandbox, approval_policy)
|
||||
return "th-1"
|
||||
|
||||
|
||||
class DispatchFromBranchTest(unittest.TestCase):
|
||||
def test_commit_then_worktree_from_dep_branch(self):
|
||||
name = "dep-" + uuid.uuid4().hex[:8]
|
||||
server = FakeServer()
|
||||
state = {"tasks": {}}
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
repo = os.path.join(tmp, "repo")
|
||||
os.makedirs(repo)
|
||||
_git(["init", "-q", "-b", "main"], repo)
|
||||
_git(["config", "user.name", "Test"], repo)
|
||||
_git(["config", "user.email", "test@example.com"], repo)
|
||||
Path(repo, "f.txt").write_text("base\n", encoding="utf-8")
|
||||
_git(["add", "-A"], repo)
|
||||
_git(["commit", "-qm", "initial"], repo)
|
||||
_git(["branch", "dep-branch"], repo)
|
||||
dep_wt = os.path.join(tmp, "dep-wt")
|
||||
_git(["worktree", "add", "-q", dep_wt, "dep-branch"], repo)
|
||||
Path(dep_wt, "f.txt").write_text("dep change\n",
|
||||
encoding="utf-8")
|
||||
try:
|
||||
task, err = host.dispatch_from_branch(
|
||||
server, state, name, "do it", repo, "dep-branch",
|
||||
dep_wt, "model-x", "medium", "workspace-write", "never")
|
||||
self.assertEqual(err, "")
|
||||
self.assertIsNotNone(task)
|
||||
self.assertEqual(task["branch"], "host/" + name)
|
||||
self.assertEqual(task["thread_id"], "th-1")
|
||||
# 依赖分支 HEAD 含本次提交,且新分支由它派生。
|
||||
dep_head = _git(["rev-parse", "dep-branch"],
|
||||
repo).stdout.strip()
|
||||
new_head = _git(["rev-parse", "host/" + name],
|
||||
repo).stdout.strip()
|
||||
self.assertEqual(new_head, dep_head)
|
||||
msg = _git(["log", "-1", "--format=%s", "dep-branch"],
|
||||
repo).stdout.strip()
|
||||
self.assertIn(name, msg)
|
||||
self.assertEqual(
|
||||
Path(task["cwd"], "f.txt").read_text(encoding="utf-8"),
|
||||
"dep change\n")
|
||||
self.assertIs(state["tasks"][name], task)
|
||||
finally:
|
||||
# 清理 HERE/work 下建出的 worktree(work/ 已
|
||||
# gitignore,但不留给下一轮测试;须在临时仓
|
||||
# 删除前用该仓的 git 登记移除)。
|
||||
path = os.path.join(host.HERE, "work",
|
||||
"host-worktrees", name)
|
||||
if os.path.exists(path):
|
||||
_git(["worktree", "remove", "--force", path], repo,
|
||||
check=False)
|
||||
shutil.rmtree(path, ignore_errors=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,53 @@
|
||||
"""host.run_verify / goal verify 的 shell 语义证据(CI 第二阶段
|
||||
bandit High triage,第 3 项白名单轨范围外、本轨只做 triage)。
|
||||
|
||||
背景:bandit High 4 项全是 host.py 的 shell=True:
|
||||
run_verify(1319)、notify(1497)、goal verify 两处
|
||||
(2099/2103)。本轨不改它们,理由须有测试证据:
|
||||
|
||||
- run_verify 的命令是整串 shell 字符串(配置/默认验测命令),
|
||||
管道与复合命令必须经 shell 解释:列表式 exec 形跑不通。
|
||||
本文件断言管道与 && 真实生效——改 shell=False 会立刻失败。
|
||||
- notify 的插值已全部 shlex.quote:证据在 tests/test_host.py
|
||||
的 test_notify_failure_quotes_shell_metacharacters(`; touch`
|
||||
与 `$(touch )` 不执行、只落日志原文),本文件不再重复。
|
||||
"""
|
||||
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(REPO_ROOT))
|
||||
|
||||
import host
|
||||
|
||||
|
||||
class RunVerifyShellTest(unittest.TestCase):
|
||||
def test_pipe_requires_shell(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
rc, out = host.run_verify("echo hello | tr a-z A-Z", tmp)
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(out, "HELLO")
|
||||
|
||||
def test_compound_command_requires_shell(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
rc, out = host.run_verify("echo a && echo b", tmp)
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(out, "a\nb")
|
||||
|
||||
def test_empty_command_is_noop(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
self.assertEqual(host.run_verify("", tmp), (0, ""))
|
||||
self.assertEqual(host.run_verify(None, tmp), (0, ""))
|
||||
|
||||
def test_failing_command_returns_rc_and_output(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
rc, out = host.run_verify("echo oops; exit 3", tmp)
|
||||
self.assertEqual(rc, 3)
|
||||
self.assertEqual(out, "oops")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -13,11 +13,9 @@ import unittest
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
import host
|
||||
from host import Server, TRACE_MAX_LINES, _summarize_trace_item
|
||||
except ImportError:
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
import host
|
||||
from host import Server, TRACE_MAX_LINES, _summarize_trace_item
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
"""run_parallel.py 单测(CI 第二阶段覆盖率:基线 0%)。
|
||||
|
||||
不跑真实 client.py/codex:只断言 main() 拼出的子进程命令
|
||||
(可执行文件、client.py 路径、cwd 解析、sandbox 默认值、
|
||||
approval-policy、codex-home 透传)与成功计数/返回码——
|
||||
命令构造与汇总正是本文件的全部逻辑。
|
||||
"""
|
||||
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(REPO_ROOT))
|
||||
|
||||
import run_parallel as rp_mod
|
||||
|
||||
|
||||
class FakePopen:
|
||||
seen = []
|
||||
|
||||
def __init__(self, cmd):
|
||||
self.cmd = cmd
|
||||
FakePopen.seen.append(cmd)
|
||||
self._rc = 0
|
||||
|
||||
def wait(self):
|
||||
return self._rc
|
||||
|
||||
|
||||
class RunParallelTest(unittest.TestCase):
|
||||
def _run(self, jobs, extra_args):
|
||||
FakePopen.seen = []
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
jobs_path = os.path.join(tmp, "jobs.json")
|
||||
with open(jobs_path, "w", encoding="utf-8") as f:
|
||||
json.dump(jobs, f)
|
||||
argv = ["run_parallel.py", "--jobs", jobs_path] + extra_args
|
||||
with mock.patch.object(rp_mod.subprocess, "Popen", FakePopen), \
|
||||
mock.patch.object(sys, "argv", argv):
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out):
|
||||
rc = rp_mod.main()
|
||||
return rc, list(FakePopen.seen), out.getvalue()
|
||||
|
||||
def test_commands_and_all_success(self):
|
||||
jobs = [{"cwd": "job-a", "prompt": "review a"},
|
||||
{"cwd": "/tmp/abs-b", "prompt": "review b",
|
||||
"sandbox": "workspace-write"}]
|
||||
rc, seen, out = self._run(jobs, ["--codex-home", "/tmp/ch"])
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(len(seen), 2)
|
||||
client = os.path.join(rp_mod.HERE, "client.py")
|
||||
self.assertEqual(seen[0], [sys.executable, client,
|
||||
"--prompt", "review a",
|
||||
"--cwd", os.path.join(rp_mod.HERE, "job-a"),
|
||||
"--sandbox", "read-only",
|
||||
"--approval-policy", "never",
|
||||
"--codex-home", "/tmp/ch"])
|
||||
self.assertEqual(seen[1][5], "/tmp/abs-b")
|
||||
self.assertEqual(seen[1][7], "workspace-write")
|
||||
self.assertIn("2/2", out)
|
||||
|
||||
def test_one_failure_returns_1(self):
|
||||
class FailingPopen(FakePopen):
|
||||
def __init__(self, cmd):
|
||||
super().__init__(cmd)
|
||||
self._idx = len(FakePopen.seen)
|
||||
|
||||
def wait(self):
|
||||
return 1 if self._idx == 2 else 0
|
||||
|
||||
jobs = [{"cwd": "/tmp/a", "prompt": "x"},
|
||||
{"cwd": "/tmp/b", "prompt": "y"}]
|
||||
FakePopen.seen = []
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
jobs_path = os.path.join(tmp, "jobs.json")
|
||||
with open(jobs_path, "w", encoding="utf-8") as f:
|
||||
json.dump(jobs, f)
|
||||
with mock.patch.object(rp_mod.subprocess, "Popen",
|
||||
FailingPopen), \
|
||||
mock.patch.object(sys, "argv",
|
||||
["run_parallel.py", "--jobs",
|
||||
jobs_path]):
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out):
|
||||
rc = rp_mod.main()
|
||||
self.assertEqual(rc, 1)
|
||||
self.assertIn("1/2", out.getvalue())
|
||||
|
||||
def test_empty_jobs_returns_0(self):
|
||||
rc, seen, out = self._run([], [])
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(seen, [])
|
||||
self.assertIn("0/0", out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user