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
@@ -46,7 +46,8 @@ class TestDeployPure(unittest.TestCase):
|
||||
with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete=False) as f:
|
||||
path = f.name
|
||||
try:
|
||||
with tarfile.open(path, "w:gz") as tf:
|
||||
# 空 tarball 文件本身是测试前置(tarball_commit 应拒绝):上下文照跑,只去 as 绑定。
|
||||
with tarfile.open(path, "w:gz"):
|
||||
pass
|
||||
with self.assertRaises(SystemExit):
|
||||
deploy_mod.tarball_commit(path)
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
"""deploy.py 门禁编排单测(CI 第二阶段覆盖率补点)。
|
||||
|
||||
只测编排层:run() 的 check 开关、check_clean_tree 的
|
||||
拒绝/告警/放行、deploy_via_git 的 dry-run 与各门禁拒绝、
|
||||
main() 的 git 分发与 tar 路径前置检查。真实 SSH/传输仍由
|
||||
test_deploy.py 的 transfer/remote_sha 测试覆盖,本文件不
|
||||
碰网络与主机。
|
||||
"""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
from unittest import mock
|
||||
|
||||
REPO_ROOT = os.path.join(os.path.dirname(__file__), "..", "..")
|
||||
sys.path.insert(0, os.path.join(REPO_ROOT, "deploy"))
|
||||
import deploy as deploy_mod # noqa: E402
|
||||
|
||||
|
||||
def _args(**over):
|
||||
base = dict(tarball=None, build_only=False, host=None, user="root",
|
||||
dry_run=True, repo="git@github.com:openqj/codex-app-server",
|
||||
git_key="/tmp/k", allow_dirty=False, skip_tests=False,
|
||||
ref="HEAD", commit=None, source="git",
|
||||
out_dir="/tmp/out")
|
||||
base.update(over)
|
||||
return SimpleNamespace(**base)
|
||||
|
||||
|
||||
class RunAndCleanTreeTest(unittest.TestCase):
|
||||
def test_run_check_exits_on_failure(self):
|
||||
bad = subprocess.CompletedProcess(args=[], returncode=1,
|
||||
stdout="", stderr="boom")
|
||||
with mock.patch.object(deploy_mod.subprocess, "run",
|
||||
return_value=bad):
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
deploy_mod.run(["false"])
|
||||
self.assertEqual(ctx.exception.code, 1)
|
||||
|
||||
def test_run_no_check_returns_result(self):
|
||||
bad = subprocess.CompletedProcess(args=[], returncode=1,
|
||||
stdout="o", stderr="e")
|
||||
with mock.patch.object(deploy_mod.subprocess, "run",
|
||||
return_value=bad):
|
||||
self.assertIs(deploy_mod.run(["false"], check=False), bad)
|
||||
|
||||
def test_clean_tree_dirty_refuses_and_warns(self):
|
||||
dirty = SimpleNamespace(returncode=1)
|
||||
with mock.patch.object(deploy_mod, "run", return_value=dirty):
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
deploy_mod.check_clean_tree(False)
|
||||
self.assertEqual(ctx.exception.code, 2)
|
||||
deploy_mod.check_clean_tree(True) # 只告警,不退出
|
||||
|
||||
def test_clean_tree_clean_passes(self):
|
||||
clean = SimpleNamespace(returncode=0)
|
||||
with mock.patch.object(deploy_mod, "run", return_value=clean):
|
||||
deploy_mod.check_clean_tree(False)
|
||||
|
||||
|
||||
class DeployViaGitTest(unittest.TestCase):
|
||||
def _patched(self, commit="a" * 40, on_main=True):
|
||||
return (mock.patch.object(deploy_mod, "check_clean_tree"),
|
||||
mock.patch.object(deploy_mod, "run_tests"),
|
||||
mock.patch.object(deploy_mod, "git_commit",
|
||||
return_value=commit),
|
||||
mock.patch.object(deploy_mod, "commit_on_origin_main",
|
||||
return_value=on_main))
|
||||
|
||||
def test_dry_run_success(self):
|
||||
patches = self._patched()
|
||||
for p in patches:
|
||||
p.start()
|
||||
try:
|
||||
self.assertEqual(deploy_mod.deploy_via_git(_args()), 0)
|
||||
finally:
|
||||
for p in patches:
|
||||
p.stop()
|
||||
|
||||
def test_gates(self):
|
||||
cases = [
|
||||
(_args(tarball="/tmp/x.tgz"), "tarball 冲突"),
|
||||
(_args(repo="https://github.com/o/r"), "非 SSH 仓库"),
|
||||
]
|
||||
for args, _why in cases:
|
||||
patches = self._patched()
|
||||
for p in patches:
|
||||
p.start()
|
||||
try:
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
deploy_mod.deploy_via_git(args)
|
||||
self.assertEqual(ctx.exception.code, 2)
|
||||
finally:
|
||||
for p in patches:
|
||||
p.stop()
|
||||
patches = self._patched(commit="abc")
|
||||
for p in patches:
|
||||
p.start()
|
||||
try:
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
deploy_mod.deploy_via_git(_args())
|
||||
self.assertEqual(ctx.exception.code, 2)
|
||||
finally:
|
||||
for p in patches:
|
||||
p.stop()
|
||||
patches = self._patched(on_main=False)
|
||||
for p in patches:
|
||||
p.start()
|
||||
try:
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
deploy_mod.deploy_via_git(_args())
|
||||
self.assertEqual(ctx.exception.code, 2)
|
||||
finally:
|
||||
for p in patches:
|
||||
p.stop()
|
||||
|
||||
|
||||
class MainDispatchTest(unittest.TestCase):
|
||||
def test_main_dispatches_git(self):
|
||||
seen = []
|
||||
with mock.patch.object(
|
||||
deploy_mod, "deploy_via_git",
|
||||
side_effect=lambda a: seen.append(a) or 0):
|
||||
rc = deploy_mod.main(["h", "root", "--source", "git",
|
||||
"--dry-run"])
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(seen[0].source, "git")
|
||||
self.assertTrue(seen[0].dry_run)
|
||||
|
||||
def test_main_tar_needs_host_or_mode(self):
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
deploy_mod.main([])
|
||||
self.assertEqual(ctx.exception.code, 2)
|
||||
|
||||
def test_main_build_only_tar_path(self):
|
||||
with mock.patch.object(deploy_mod, "check_clean_tree"), \
|
||||
mock.patch.object(deploy_mod, "run_tests"), \
|
||||
mock.patch.object(
|
||||
deploy_mod, "build_release",
|
||||
return_value=("tb.tgz", "sha", "c" * 40)):
|
||||
rc = deploy_mod.main(["myhost", "--build-only"])
|
||||
self.assertEqual(rc, 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -6,7 +6,6 @@ host_deploy.sh 代替真实换目录脚本:假脚本只验 tar sha 与
|
||||
DEPLOYED_COMMIT,打印 RELEASE_OK,不碰 systemctl、不动 live。
|
||||
host_git_fetch.sh 的 CODEX_GIT_* 环境变量把工作区指到临时目录。
|
||||
"""
|
||||
import hashlib
|
||||
import os
|
||||
import subprocess
|
||||
import tarfile
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
"""deploy/verify-multiuser.py 纯 helper 单测(CI 第二阶段覆盖率:
|
||||
基线 0%)。只测 check() 与 req():
|
||||
|
||||
- check() 写 RESULTS 台账并打印 PASS/FAIL;
|
||||
- req() 对本地回环 fake HTTP 成功/HTTPError/非 JSON 体/
|
||||
连接拒绝四路的返回值与请求头。
|
||||
|
||||
main() 是主机侧对真实控制面的多用户实弹验证(建
|
||||
multiuser-test-* 用户/项目/任务、调 merge),按任务门禁
|
||||
“不要建生产任务”,本轨不以 mock 冒充它——main() 仍为
|
||||
未覆盖,如实留在缺口里。
|
||||
"""
|
||||
|
||||
import http.server
|
||||
import importlib.util
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import threading
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from pathlib import Path
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[2]
|
||||
_SPEC = importlib.util.spec_from_file_location(
|
||||
"verify_multiuser",
|
||||
os.path.join(REPO_ROOT, "deploy", "verify-multiuser.py"))
|
||||
vm = importlib.util.module_from_spec(_SPEC)
|
||||
_SPEC.loader.exec_module(vm)
|
||||
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
seen = []
|
||||
|
||||
def _send(self, code, body, ctype="application/json"):
|
||||
data = body if isinstance(body, bytes) else body.encode()
|
||||
self.send_response(code)
|
||||
self.send_header("Content-Type", ctype)
|
||||
self.send_header("Content-Length", str(len(data)))
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
|
||||
def do_GET(self):
|
||||
Handler.seen.append((self.path, dict(self.headers)))
|
||||
if self.path == "/ok":
|
||||
self._send(200, json.dumps({"ok": True}))
|
||||
elif self.path == "/nf":
|
||||
self._send(404, json.dumps({"error": "nope"}))
|
||||
else:
|
||||
self._send(404, "not json", "text/plain")
|
||||
|
||||
def do_POST(self):
|
||||
length = int(self.headers.get("Content-Length") or 0)
|
||||
Handler.seen.append(
|
||||
(self.path, dict(self.headers), self.rfile.read(length)))
|
||||
self._send(201, json.dumps({"created": True}))
|
||||
|
||||
def log_message(self, *args):
|
||||
pass
|
||||
|
||||
|
||||
class ReqTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.httpd = http.server.ThreadingHTTPServer(
|
||||
("127.0.0.1", 0), Handler)
|
||||
cls.port = cls.httpd.server_address[1]
|
||||
cls.thread = threading.Thread(target=cls.httpd.serve_forever,
|
||||
daemon=True)
|
||||
cls.thread.start()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.httpd.shutdown()
|
||||
cls.httpd.server_close()
|
||||
|
||||
def setUp(self):
|
||||
Handler.seen = []
|
||||
|
||||
def base(self):
|
||||
return "http://127.0.0.1:%d" % self.port
|
||||
|
||||
def test_get_ok(self):
|
||||
code, body = vm.req(self.base(), "GET", "/ok")
|
||||
self.assertEqual((code, body), (200, {"ok": True}))
|
||||
|
||||
def test_http_error_json_and_non_json(self):
|
||||
code, body = vm.req(self.base(), "GET", "/nf")
|
||||
self.assertEqual((code, body), (404, {"error": "nope"}))
|
||||
code, body = vm.req(self.base(), "GET", "/bad")
|
||||
self.assertEqual((code, body), (404, {}))
|
||||
|
||||
def test_post_headers_and_body(self):
|
||||
code, body = vm.req(self.base(), "POST", "/ok",
|
||||
key="k-123", body={"a": 1})
|
||||
self.assertEqual(code, 201)
|
||||
self.assertEqual(body, {"created": True})
|
||||
path, headers, raw = Handler.seen[0]
|
||||
self.assertEqual(headers.get("X-Api-Key"), "k-123")
|
||||
self.assertEqual(headers.get("Content-Type"), "application/json")
|
||||
self.assertEqual(json.loads(raw), {"a": 1})
|
||||
|
||||
def test_connection_refused_returns_none(self):
|
||||
sock = socket.socket()
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
dead_port = sock.getsockname()[1]
|
||||
sock.close()
|
||||
code, body = vm.req("http://127.0.0.1:%d" % dead_port,
|
||||
"GET", "/ok")
|
||||
self.assertIsNone(code)
|
||||
self.assertIn("error", body)
|
||||
|
||||
|
||||
class CheckTest(unittest.TestCase):
|
||||
def test_check_appends_and_prints(self):
|
||||
vm.RESULTS.clear()
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out):
|
||||
vm.check("alpha", True)
|
||||
vm.check("beta", 0)
|
||||
self.assertEqual(vm.RESULTS, [("alpha", True), ("beta", False)])
|
||||
self.assertIn("PASS alpha", out.getvalue())
|
||||
self.assertIn("FAIL beta", out.getvalue())
|
||||
vm.RESULTS.clear()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user