Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ venv/
# Mac
.DS_Store
.idea/

# 黄金向量按钉版拉取生成(conftest),不落库
tests/fixtures/crypto-vectors.json
tests/fixtures/interop-cases.json
35 changes: 34 additions & 1 deletion tests/conftest.py
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:

Copy link
Copy Markdown
Contributor

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 提示
在 `tests/conftest.py` 第 26 行:

**离线环境无法收集测试**

如果无法访问 GitHub,或测试环境没有网络,`urllib.request.urlopen()` 会引发异常,而模块级的 `_fetch_pinned_specs()` 调用会在导入 conftest 时传播该异常。即使本地已经存在有效的 fixture 文件,pytest 也无法收集测试。

如果现有 fixture 可用,请使用经过哈希验证的本地 fixture,并避免在获取失败时中止测试收集。

另请参阅 `tests/conftest.py:38`。
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
In `tests/conftest.py` at line 26:

**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`.

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:
Comment thread
sourcery-ai[bot] marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · 并发运行时会读取到不完整的 fixture

当多个 pytest 进程同时使用同一份检出代码时,以 wb 模式打开 fixture 会将其截断,而另一个进程可能正在读取该文件,因此 JSON 解析或 fixture 完整性测试会间歇性失败。

请先将每个 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 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.

Prompt for AI agents
In `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)
Expand Down
Loading
Loading