Repository navigation
fix: 清零 sourcery gate 16 项 issue #43
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,45 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """共享 fixture:黄金向量(tests/fixtures/crypto-vectors.json,禁手改)。""" | ||
| """共享 fixture:黄金向量按 ref+sha256 从 wop-specs 真源拉取(对齐 java-sdk 模式)。 | ||
|
|
||
| 副本不落库:collect 阶段拉取并校验,mismatch 即失败(fail-closed)。 | ||
| 升级流程见 CONTRIBUTING:先合 wop-specs PR,再同步 WOP_SPECS_REF 与下方 sha256。 | ||
| """ | ||
| import hashlib | ||
| import json | ||
| import os | ||
| import urllib.request | ||
|
|
||
| import pytest | ||
|
|
||
| HERE = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
| WOP_SPECS_REF = "a1f7feb5b4af5c9052149e2adfd2063caa102677" | ||
| WOP_SPECS_SHA256 = { | ||
| "crypto/crypto-vectors.json": "c7dd65740a1da9a5cfb804bca0c8b1bd7d4ab967adeb075297677a4bef148736", | ||
| "interop/v1/interop-cases.json": "c920ca1a93ccb3899a659f59fed6ec4652cf9e1b3b58bbdac23c45ac3ed2353e", | ||
| } | ||
|
|
||
|
|
||
| def _fetch_pinned_specs(): | ||
| fixtures_dir = os.path.join(HERE, "fixtures") | ||
| os.makedirs(fixtures_dir, exist_ok=True) # 副本出库后目录可能不存在 | ||
|
|
||
| for src, expected in WOP_SPECS_SHA256.items(): | ||
| url = f"https://raw.githubusercontent.com/wop-platform/wop-specs/{WOP_SPECS_REF}/{src}" | ||
| with urllib.request.urlopen(url, timeout=30) as resp: | ||
| data = resp.read() | ||
| digest = hashlib.sha256(data).hexdigest() | ||
| if digest != expected: | ||
| raise RuntimeError( | ||
| f"wop-specs {src} sha256 mismatch:期望 {expected},实得 {digest};" | ||
| "向量升级须先合 wop-specs PR 再同步钉版" | ||
| ) | ||
| with open(os.path.join(HERE, "fixtures", os.path.basename(src)), "wb") as f: | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · 并发运行时会读取到不完整的 fixture 当多个 pytest 进程同时使用同一份检出代码时,以 请先将每个 fixture 写入临时文件,写入完成后再以原子操作替换目标文件。 AI Agent 提示在 `tests/conftest.py` 第 34 行:
**并发运行时会读取到不完整的 fixture**
当多个 pytest 进程同时使用同一份检出代码时,以 `wb` 模式打开 fixture 会将其截断,而另一个进程可能正在读取该文件,因此 JSON 解析或 fixture 完整性测试会间歇性失败。
请先将每个 fixture 写入临时文件,写入完成后再以原子操作替换目标文件。Original comment in English🟡 Medium · Concurrent runs read incomplete fixtures When multiple pytest processes use the same checkout concurrently, opening the fixture with Write each fixture to a temporary file and atomically replace the destination after the write completes. Prompt for AI agentsIn `tests/conftest.py` at line 34:
**Concurrent runs read incomplete fixtures**
When multiple pytest processes use the same checkout concurrently, opening the fixture with `wb` truncates it while another process may be reading it, so JSON parsing or fixture-integrity tests fail intermittently.
Write each fixture to a temporary file and atomically replace the destination after the write completes. |
||
| f.write(data) | ||
|
|
||
|
|
||
| _fetch_pinned_specs() | ||
|
|
||
| _VECTORS_PATH = os.path.join(HERE, "fixtures", "crypto-vectors.json") | ||
| with open(_VECTORS_PATH, "r", encoding="utf-8") as _f: | ||
| VECTORS = json.load(_f) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · 离线环境无法收集测试
如果无法访问 GitHub,或测试环境没有网络,
urllib.request.urlopen()会引发异常,而模块级的_fetch_pinned_specs()调用会在导入 conftest 时传播该异常。即使本地已经存在有效的 fixture 文件,pytest 也无法收集测试。如果现有 fixture 可用,请使用经过哈希验证的本地 fixture,并避免在获取失败时中止测试收集。
另请参阅
tests/conftest.py:38。AI Agent 提示
Original comment in English
🟡 Medium · Offline tests cannot be collected
When gitHub is unreachable or the test environment has no network access,
urllib.request.urlopen()raises, and the module-level_fetch_pinned_specs()call propagates the exception during conftest import. Pytest cannot collect tests, even when valid fixture files are already present locally.Use existing hash-verified fixtures when available, and avoid aborting collection on fetch failure when those fixtures are usable.
Also at
tests/conftest.py:38.Prompt for AI agents