migrate: own the system standard-library modules and their baseline assertions #2
No files matched your search
@@ -1,14 +1,40 @@
|
||||
# marqdo-std-system
|
||||
|
||||
本仓负责时钟、UTC、环境、进程与随机。当前状态为 `planned`。
|
||||
本仓负责时钟、UTC、环境、进程与随机能力。当前处于**标准库源码归位迁移**阶段:源码与断言已迁入本仓,等待父仓候选组合验收(父仓工单 [marqdo#9](https://git.cflmy.cn/cflmy/marqdo/issues/9),本仓工单 [#1](https://git.cflmy.cn/cflmy/marqdo-std-system/issues/1))。
|
||||
|
||||
## 首项任务
|
||||
## 唯一公开入口(Bundle 根相对路径)
|
||||
|
||||
迁入时钟、UTC、环境、进程和随机能力。
|
||||
| 发布路径 | 公开导入 | 职责 |
|
||||
| --- | --- | --- |
|
||||
| `clock.mq.md` | `std:clock` | 项目模式 Unix 毫秒 |
|
||||
| `datetime.mq.md` | `std:datetime` | UTC ISO-8601 格式化/解析 |
|
||||
| `env.mq.md` | `std:env` | 仅授权名称的环境读取 |
|
||||
| `process.mq.md` | `std:process` | 显式授权程序、超时与输出上限 |
|
||||
| `random.mq.md` | `std:random` | OS 熵生成的字节/整数 |
|
||||
|
||||
`modules/` 保存将来 Bundle 根下的相对路径;Bundle 与 `stdlib.json` 由父仓唯一生产者生成,本仓不维护第二份官方 manifest。
|
||||
|
||||
## 目录
|
||||
|
||||
- [modules/](modules/README.md) 存放对外发布的模块源码。
|
||||
- [tests/](tests/README.md) 存放独立验收用的输入和断言。
|
||||
- `modules/`:对外发布的模块源码(Bundle 根相对路径)。
|
||||
- `tests/`:迁入的 `.mq.md` 自测源与 `tests/cases/` 期望(observation 终值、stdout、参数、环境)。
|
||||
- `scripts/`:独立验收入口。
|
||||
|
||||
## 独立验收
|
||||
|
||||
只接受显式传入的已验证制品,不读取兄弟仓物理路径或浮动分支:
|
||||
|
||||
```sh
|
||||
MARQDO_BIN=/path/to/verified/marqdo \
|
||||
MARQDO_STDLIB_DIR=/path/to/installed/bundle \
|
||||
scripts/test.sh
|
||||
```
|
||||
|
||||
- `MARQDO_BIN`:真实 CLI 二进制;`MARQDO_STDLIB_DIR`:已安装且通过 manifest digest 校验、包含本仓模块的 Bundle。
|
||||
- 期望值来自迁移前基线的真实执行结果;修改期望值等于声明公开行为变化,必须单独评审。
|
||||
|
||||
## 边界
|
||||
|
||||
不实现语言内核、Host 权限、资源原语或其他标准库;`clock`/`env`/`process`/`random` 只包装现行 Host 授权请求,不接管权限或 OS 监督;不新增 IANA 时区、进程监督器或密码学随机 API。
|
||||
|
||||
[生态迁移台账](https://git.cflmy.cn/cflmy/marqdo/src/branch/main/docs/MIGRATION.md) · [开发流程](https://git.cflmy.cn/cflmy/marqdo/src/branch/main/docs/DEVELOPMENT.md)
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
# @exec clock
|
||||
## 现在
|
||||
|
||||
`params()`
|
||||
|
||||
返回项目模式下的 UTC Unix 毫秒。pure/strict 返回
|
||||
permission.denied,与 resource.request 的其他操作一致。
|
||||
|
||||
`return resource.request("clock.now", [])`
|
||||
@@ -0,0 +1,340 @@
|
||||
|
||||
# @exec datetime
|
||||
## 失败
|
||||
|
||||
`params(消息)`
|
||||
|
||||
`return {ok: false, code: "datetime.invalid", message: 消息}`
|
||||
|
||||
## 闰年
|
||||
|
||||
`params(年)`
|
||||
|
||||
`return 年 % 4 == 0 and (年 % 100 != 0 or 年 % 400 == 0)`
|
||||
|
||||
## 每月天数
|
||||
|
||||
`params(年, 月)`
|
||||
|
||||
`if 月 == 2`
|
||||
|
||||
`if 闰年(年)`
|
||||
|
||||
`return 29`
|
||||
|
||||
`end`
|
||||
`return 28`
|
||||
|
||||
`else`
|
||||
`if 月 == 4 or 月 == 6 or 月 == 9 or 月 == 11`
|
||||
|
||||
`return 30`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`return 31`
|
||||
|
||||
## 补零2
|
||||
|
||||
`params(值)`
|
||||
|
||||
`if 值 < 10`
|
||||
|
||||
`return "0" + 文本(值)`
|
||||
|
||||
`end`
|
||||
`return 文本(值)`
|
||||
|
||||
## 补零3
|
||||
|
||||
`params(值)`
|
||||
|
||||
`if 值 < 10`
|
||||
|
||||
`return "00" + 文本(值)`
|
||||
|
||||
`else`
|
||||
`if 值 < 100`
|
||||
|
||||
`return "0" + 文本(值)`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`return 文本(值)`
|
||||
|
||||
## 补零4
|
||||
|
||||
`params(值)`
|
||||
|
||||
`if 值 < 10`
|
||||
|
||||
`return "000" + 文本(值)`
|
||||
|
||||
`else`
|
||||
`if 值 < 100`
|
||||
|
||||
`return "00" + 文本(值)`
|
||||
|
||||
`else`
|
||||
`if 值 < 1000`
|
||||
|
||||
`return "0" + 文本(值)`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
`return 文本(值)`
|
||||
|
||||
## 月份
|
||||
|
||||
`params(mp)`
|
||||
|
||||
`if mp < 10`
|
||||
|
||||
`return mp + 3`
|
||||
|
||||
`end`
|
||||
`return mp - 9`
|
||||
|
||||
## 年份
|
||||
|
||||
`params(年, 月)`
|
||||
|
||||
`if 月 <= 2`
|
||||
|
||||
`return 年 + 1`
|
||||
|
||||
`end`
|
||||
`return 年`
|
||||
|
||||
## 日期部件
|
||||
|
||||
`params(天)`
|
||||
|
||||
Howard Hinnant 的 civil-from-days 算法,输入为 1970-01-01 起的天数。
|
||||
|
||||
**z = 天 + 719468**
|
||||
|
||||
**era = z // 146097**
|
||||
|
||||
**doe = z - era * 146097**
|
||||
|
||||
**yoe = (doe - doe // 1460 + doe // 36524 - doe // 146096) // 365**
|
||||
|
||||
**年 = yoe + era * 400**
|
||||
|
||||
**doy = doe - (365 * yoe + yoe // 4 - yoe // 100)**
|
||||
|
||||
**mp = (5 * doy + 2) // 153**
|
||||
|
||||
**日 = doy - (153 * mp + 2) // 5 + 1**
|
||||
|
||||
**月 = 月份(mp)**
|
||||
|
||||
`return {年: 年份(年, 月), 月: 月, 日: 日}`
|
||||
|
||||
## 日期序号
|
||||
|
||||
`params(年, 月, 日)`
|
||||
|
||||
**y = 年**
|
||||
|
||||
`if 月 <= 2`
|
||||
|
||||
**y = y - 1**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**era = y // 400**
|
||||
|
||||
**yoe = y - era * 400**
|
||||
|
||||
**m = 月**
|
||||
|
||||
`if 月 > 2`
|
||||
|
||||
**m = 月 - 3**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**m = 月 + 9**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**doy = (153 * m + 2) // 5 + 日 - 1**
|
||||
|
||||
**doe = yoe * 365 + yoe // 4 - yoe // 100 + doy**
|
||||
|
||||
`return era * 146097 + doe - 719468`
|
||||
|
||||
## 是数字串
|
||||
|
||||
`params(文本)`
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
**结果 = 长度(点) > 0**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 长度(点) and 结果`
|
||||
|
||||
`if 点[下标] < 48 or 点[下标] > 57`
|
||||
|
||||
**结果 = false**
|
||||
|
||||
`else`
|
||||
`if true`
|
||||
|
||||
**忽略 = 0**
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 片段
|
||||
|
||||
`params(点, 起点, 终点)`
|
||||
|
||||
**结果 = ""**
|
||||
|
||||
**下标 = 起点**
|
||||
|
||||
`while 下标 < 终点`
|
||||
|
||||
**结果 = 结果 + text.from_codepoint(点[下标])**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return 结果`
|
||||
|
||||
## 格式化
|
||||
|
||||
`params(毫秒)`
|
||||
|
||||
输出 UTC ISO-8601:YYYY-MM-DDTHH:MM:SSZ;毫秒非零时追加
|
||||
.mmm。负数毫秒按 1970 前的 UTC 时间处理。
|
||||
|
||||
**秒 = 毫秒 // 1000**
|
||||
|
||||
**毫 = 毫秒 % 1000**
|
||||
|
||||
**天 = 秒 // 86400**
|
||||
|
||||
**日内 = 秒 % 86400**
|
||||
|
||||
**时 = 日内 // 3600**
|
||||
|
||||
**分 = 日内 % 3600 // 60**
|
||||
|
||||
**秒数 = 日内 % 60**
|
||||
|
||||
**日期 = 日期部件(天)**
|
||||
|
||||
**基 = 补零4(日期.年) + "-" + 补零2(日期.月) + "-" + 补零2(日期.日) + "T" + 补零2(时) + ":" + 补零2(分) + ":" + 补零2(秒数)**
|
||||
|
||||
`if 毫 == 0`
|
||||
|
||||
`return 基 + "Z"`
|
||||
|
||||
`end`
|
||||
`return 基 + "." + 补零3(毫) + "Z"`
|
||||
|
||||
## 解析
|
||||
|
||||
`params(文本)`
|
||||
|
||||
解析 YYYY-MM-DDTHH:MM:SSZ 或带 .mmm 的 UTC 文本,返回 Unix 毫秒。
|
||||
范围、闰年和格式错误都是可恢复 datetime.invalid。
|
||||
|
||||
**点 = text.codepoints(文本)**
|
||||
|
||||
`if 长度(点) < 20 or 长度(点) > 24`
|
||||
|
||||
`return 失败("长度不是 20–24 个码点")`
|
||||
|
||||
`else`
|
||||
`if 点[4] != 45 or 点[7] != 45 or 点[10] != 84 or 点[13] != 58 or 点[16] != 58`
|
||||
|
||||
`return 失败("分隔符不符合 YYYY-MM-DDTHH:MM:SS")`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**年文本 = 片段(点, 0, 4)**
|
||||
|
||||
**月文本 = 片段(点, 5, 7)**
|
||||
|
||||
**日文本 = 片段(点, 8, 10)**
|
||||
|
||||
**时文本 = 片段(点, 11, 13)**
|
||||
|
||||
**分文本 = 片段(点, 14, 16)**
|
||||
|
||||
**秒文本 = 片段(点, 17, 19)**
|
||||
|
||||
`if not 是数字串(年文本) or not 是数字串(月文本) or not 是数字串(日文本) or not 是数字串(时文本) or not 是数字串(分文本) or not 是数字串(秒文本)`
|
||||
|
||||
`return 失败("日期时间分量必须是数字")`
|
||||
|
||||
`end`
|
||||
**年 = 整数(年文本)**
|
||||
|
||||
**月 = 整数(月文本)**
|
||||
|
||||
**日 = 整数(日文本)**
|
||||
|
||||
**时 = 整数(时文本)**
|
||||
|
||||
**分 = 整数(分文本)**
|
||||
|
||||
**秒 = 整数(秒文本)**
|
||||
|
||||
**毫文本 = "0"**
|
||||
|
||||
**结束 = 20**
|
||||
|
||||
`if 长度(点) >= 24 and 点[19] == 46 and 点[23] == 90`
|
||||
|
||||
**毫文本 = 片段(点, 20, 23)**
|
||||
|
||||
**结束 = 24**
|
||||
|
||||
`else`
|
||||
`if 长度(点) != 结束 or 点[结束 - 1] != 90`
|
||||
|
||||
`return 失败("必须以 Z 结尾")`
|
||||
|
||||
`else`
|
||||
`if not 是数字串(毫文本)`
|
||||
|
||||
`return 失败("毫秒必须是数字")`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`end`
|
||||
**毫 = 整数(毫文本)**
|
||||
|
||||
`if 年 < 1970 or 月 < 1 or 月 > 12 or 日 < 1 or 日 > 每月天数(年, 月)`
|
||||
|
||||
`return 失败("日期超出范围")`
|
||||
|
||||
`else`
|
||||
`if 时 < 0 or 时 > 23 or 分 < 0 or 分 > 59 or 秒 < 0 or 秒 > 59`
|
||||
|
||||
`return 失败("时间超出范围")`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
**毫秒 = 日期序号(年, 月, 日) * 86400000 + 时 * 3600000 + 分 * 60000 + 秒 * 1000 + 毫**
|
||||
|
||||
`return {ok: true, value: 毫秒}`
|
||||
@@ -0,0 +1,30 @@
|
||||
|
||||
# @exec env
|
||||
## 读取
|
||||
|
||||
`params(名称)`
|
||||
|
||||
读取一个显式授权的环境变量。名称是精确匹配,不支持 glob;未授权是
|
||||
permission.denied,未设置是 env.not_found,非 UTF-8 是
|
||||
env.not_unicode。
|
||||
|
||||
`return resource.request("env.get", [名称])`
|
||||
|
||||
## 存在
|
||||
|
||||
`params(名称)`
|
||||
|
||||
**结果 = 读取(名称)**
|
||||
|
||||
`if 结果.ok`
|
||||
|
||||
`return {ok: true, value: true}`
|
||||
|
||||
`else`
|
||||
`if 结果.code == "env.not_found"`
|
||||
|
||||
`return {ok: true, value: false}`
|
||||
|
||||
`end`
|
||||
`end`
|
||||
`return 结果`
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
# @exec process
|
||||
## 运行
|
||||
|
||||
`params(程序, 参数列表)`
|
||||
|
||||
在项目模式下运行一个显式授权的可执行文件。程序路径必须与
|
||||
--allow-process PROGRAM 精确匹配;不经过 shell,清空环境变量,
|
||||
工作目录是项目根,stdin 为空。5 秒超时、1 MiB stdout/stderr 上限,
|
||||
结果形状为 {status, stdout, stderr}。
|
||||
|
||||
`return resource.request("process.run", [程序, 参数列表])`
|
||||
@@ -0,0 +1,42 @@
|
||||
|
||||
# @exec random
|
||||
## 字节
|
||||
|
||||
`params(数量)`
|
||||
|
||||
读取 数量 个 OS 熵字节,返回 List<Int>(每个 0–255)。仅项目模式,
|
||||
需要显式 --allow-random;pure/strict 拒绝。
|
||||
|
||||
`return resource.request("random.bytes", [数量])`
|
||||
|
||||
## 整数
|
||||
|
||||
`params(上限)`
|
||||
|
||||
返回 0 <= 值 < 上限 的整数。上限 必须大于 0;使用 8 个熵字节
|
||||
组合后取模(无偏拒绝采样尚未实现)。
|
||||
|
||||
`if 上限 <= 0`
|
||||
|
||||
`return {ok: false, code: "random.invalid", message: "上限必须大于 0"}`
|
||||
|
||||
`end`
|
||||
**字节结果 = 字节(8)**
|
||||
|
||||
`if not 字节结果.ok`
|
||||
|
||||
`return 字节结果`
|
||||
|
||||
`end`
|
||||
**值 = 0**
|
||||
|
||||
**下标 = 0**
|
||||
|
||||
`while 下标 < 8`
|
||||
|
||||
**值 = 值 * 256 + 字节结果.value[下标]**
|
||||
|
||||
**下标 = 下标 + 1**
|
||||
|
||||
`end`
|
||||
`return {ok: true, value: 值 % 上限}`
|
||||
Executable
+207
@@ -0,0 +1,207 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run this component's public `.mq.md` self-tests against a real CLI Bundle.
|
||||
|
||||
Inputs are explicit and never discovered from sibling checkouts:
|
||||
|
||||
* ``MARQDO_BIN`` verified ``marqdo`` executable;
|
||||
* ``MARQDO_STDLIB_DIR`` Bundle that already installs this component's modules at
|
||||
their frozen bundle paths (the Bundle identity is the caller's responsibility;
|
||||
the CLI itself verifies the manifest digest before loading a module).
|
||||
|
||||
Each ``tests/cases/*.json`` file pins one self-test: the source file, stdin, CLI
|
||||
flags, environment, the expected bundle module path and the expected observation
|
||||
terminal value / stdout. The expected values are the baseline captured from the
|
||||
pre-migration CLI Bundle; a change is a public behaviour change and must be
|
||||
reviewed as one.
|
||||
|
||||
Usage: scripts/test.sh [CASE_NAME...]
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
REPO = Path(__file__).resolve().parents[1]
|
||||
CASES = REPO / "tests" / "cases"
|
||||
SCHEMA = "marqdo.stdlib-test-case"
|
||||
SCHEMA_VERSION = 1
|
||||
|
||||
|
||||
class CaseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def _substitute(value, bin_version: str):
|
||||
if isinstance(value, dict):
|
||||
if set(value) == {"$bin_version"}:
|
||||
return bin_version
|
||||
return {key: _substitute(item, bin_version) for key, item in value.items()}
|
||||
if isinstance(value, list):
|
||||
return [_substitute(item, bin_version) for item in value]
|
||||
return value
|
||||
|
||||
|
||||
def _channel_text(events: list[dict], channel: str) -> str:
|
||||
chunks = []
|
||||
for event in events:
|
||||
if event.get("kind") != channel:
|
||||
continue
|
||||
payload = event["payload"]
|
||||
data = base64.b64decode(payload["data_base64"])
|
||||
if len(data) != payload["byte_count"]:
|
||||
raise CaseError(f"{channel} byte_count does not match decoded bytes")
|
||||
chunks.append(data)
|
||||
return b"".join(chunks).decode("utf-8")
|
||||
|
||||
|
||||
def _run_case(case: dict, case_path: Path, binary: str, bundle: Path) -> None:
|
||||
name = case["name"]
|
||||
module = case.get("require_module")
|
||||
if module and not (bundle / module).is_file():
|
||||
raise CaseError(f"{name}: Bundle {bundle} does not install {module}")
|
||||
source = REPO / "tests" / case["file"]
|
||||
if not source.is_file():
|
||||
raise CaseError(f"{name}: missing test source {source}")
|
||||
for relative in case.get("clean", []):
|
||||
target = (REPO / relative).resolve()
|
||||
if REPO not in target.parents:
|
||||
raise CaseError(f"{name}: clean path escapes the repository")
|
||||
if target.is_dir():
|
||||
shutil.rmtree(target)
|
||||
elif target.exists():
|
||||
target.unlink()
|
||||
env = os.environ.copy()
|
||||
env["MARQDO_STDLIB_DIR"] = str(bundle)
|
||||
for key, value in case.get("env", {}).items():
|
||||
env[key] = str(value).replace("$MARQDO_BIN", binary)
|
||||
version = subprocess.run(
|
||||
[binary, "--version"], capture_output=True, text=True, check=True
|
||||
).stdout
|
||||
args = [str(arg).replace("$MARQDO_BIN", binary) for arg in case.get("args", [])]
|
||||
command = [binary, "run", str(source), *args, "--output", "json"]
|
||||
result = subprocess.run(
|
||||
command,
|
||||
cwd=REPO,
|
||||
env=env,
|
||||
input=case.get("stdin", "").encode(),
|
||||
capture_output=True,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
raise CaseError(
|
||||
f"{name}: CLI exited {result.returncode}\nstderr:\n"
|
||||
f"{result.stderr.decode(errors='replace')}"
|
||||
)
|
||||
try:
|
||||
report = json.loads(result.stdout)
|
||||
except json.JSONDecodeError as error:
|
||||
raise CaseError(f"{name}: CLI did not emit a JSON report: {error}") from error
|
||||
if report.get("run", {}).get("status") != "ok":
|
||||
raise CaseError(f"{name}: run status is not ok: {report.get('run')}")
|
||||
events = report.get("events") or []
|
||||
if not events or events[-1].get("kind") not in (
|
||||
"run.completed",
|
||||
"run.failed",
|
||||
"run.rejected",
|
||||
):
|
||||
raise CaseError(f"{name}: report has no terminal event")
|
||||
terminal = events[-1]["payload"]
|
||||
if terminal.get("status") != "ok":
|
||||
raise CaseError(f"{name}: terminal status is not ok: {terminal.get('status')}")
|
||||
actual_value = terminal.get("value")
|
||||
expected_value = _substitute(case["expected_terminal"], version)
|
||||
if actual_value != expected_value:
|
||||
raise CaseError(
|
||||
f"{name}: terminal value changed\nexpected: "
|
||||
f"{json.dumps(expected_value, ensure_ascii=False, sort_keys=True)}\n"
|
||||
f"actual: {json.dumps(actual_value, ensure_ascii=False, sort_keys=True)}"
|
||||
)
|
||||
expected_stdout = case.get("expected_stdout", "")
|
||||
actual_stdout = _channel_text(events, "stdout.chunk")
|
||||
if actual_stdout != expected_stdout:
|
||||
raise CaseError(
|
||||
f"{name}: stdout changed: expected {expected_stdout!r}, got {actual_stdout!r}"
|
||||
)
|
||||
for check in case.get("event_checks", []):
|
||||
matching = [
|
||||
event
|
||||
for event in events
|
||||
if event.get("kind") == check["kind"]
|
||||
and all(
|
||||
_dig(event.get("payload"), pointer) == expected
|
||||
for pointer, expected in check.get("payload", {}).items()
|
||||
)
|
||||
]
|
||||
if not matching:
|
||||
raise CaseError(
|
||||
f"{name}: no {check['kind']} event matched {check.get('payload')}"
|
||||
)
|
||||
|
||||
|
||||
def _dig(value, pointer: str):
|
||||
current = value
|
||||
for part in pointer.split("."):
|
||||
if isinstance(current, list):
|
||||
current = current[int(part)]
|
||||
elif isinstance(current, dict):
|
||||
current = current[part]
|
||||
else:
|
||||
return object()
|
||||
return current
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("cases", nargs="*", help="case names to run")
|
||||
args = parser.parse_args()
|
||||
binary = os.environ.get("MARQDO_BIN")
|
||||
bundle = os.environ.get("MARQDO_STDLIB_DIR")
|
||||
if not binary or not shutil.which(binary):
|
||||
print("MARQDO_BIN must point at a verified marqdo executable", file=sys.stderr)
|
||||
return 2
|
||||
if not bundle or not Path(bundle).is_dir():
|
||||
print("MARQDO_STDLIB_DIR must point at an installed Bundle", file=sys.stderr)
|
||||
return 2
|
||||
bundle = Path(bundle).resolve()
|
||||
paths = sorted(CASES.glob("*.json"))
|
||||
if not paths:
|
||||
print("no test cases in tests/cases", file=sys.stderr)
|
||||
return 2
|
||||
selected = set(args.cases)
|
||||
failures = 0
|
||||
ran = 0
|
||||
for path in paths:
|
||||
try:
|
||||
case = json.loads(path.read_text(encoding="utf-8"))
|
||||
except json.JSONDecodeError as error:
|
||||
print(f"FAIL {path.name}: invalid JSON: {error}", file=sys.stderr)
|
||||
failures += 1
|
||||
continue
|
||||
if case.get("schema") != SCHEMA or case.get("schema_version") != SCHEMA_VERSION:
|
||||
print(f"FAIL {path.name}: unsupported case schema", file=sys.stderr)
|
||||
failures += 1
|
||||
continue
|
||||
if selected and case["name"] not in selected:
|
||||
continue
|
||||
ran += 1
|
||||
try:
|
||||
_run_case(case, path, binary, bundle)
|
||||
print(f"PASS {case['name']}")
|
||||
except CaseError as error:
|
||||
print(f"FAIL {error}", file=sys.stderr)
|
||||
failures += 1
|
||||
if failures:
|
||||
print(f"{failures} of {ran} cases failed", file=sys.stderr)
|
||||
return 1
|
||||
print(f"{ran} cases passed")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
# Explicit inputs only: a verified CLI binary and a Bundle that installs this
|
||||
# component's modules at their frozen paths. Never discovers sibling checkouts.
|
||||
set -eu
|
||||
: "${MARQDO_BIN:?set MARQDO_BIN to the verified marqdo executable}"
|
||||
: "${MARQDO_STDLIB_DIR:?set MARQDO_STDLIB_DIR to the installed Bundle}"
|
||||
exec python3 "$(dirname "$0")/run_cases.py" "$@"
|
||||
+5
-1
@@ -1,3 +1,7 @@
|
||||
# 验收测试
|
||||
|
||||
首个功能附带正例和失败用例,记录所用 CLI 提交或发行版本以及实际运行命令。
|
||||
`tests/*.mq.md` 是从迁移前 CLI 官方标准库自测迁入的真实测试源;`tests/cases/*.json` 固定每个测试的输入(stdin、CLI 参数、环境、所需 Bundle 模块)和期望结果(observation 终值、stdout、必要的资源事件)。
|
||||
|
||||
运行入口是 `scripts/test.sh`,它要求显式传入 `MARQDO_BIN` 与 `MARQDO_STDLIB_DIR`。缺少制品、Bundle 缺少模块或 digest 校验失败时必须失败,不得回退到其它目录。
|
||||
|
||||
期望值取自迁移前基线 Bundle(`sha256:5407d36...`)与已验证候选 CLI 的真实执行结果;任何期望值改动都必须作为公开行为变化单独评审并记录证据。
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"schema": "marqdo.stdlib-test-case",
|
||||
"schema_version": 1,
|
||||
"name": "clock",
|
||||
"file": "clock.mq.md",
|
||||
"require_module": "clock.mq.md",
|
||||
"args": [],
|
||||
"stdin": "",
|
||||
"env": {},
|
||||
"expected_stdout": "",
|
||||
"expected_terminal": {
|
||||
"items": [
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"value": "Int"
|
||||
},
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
}
|
||||
],
|
||||
"type": "list"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"schema": "marqdo.stdlib-test-case",
|
||||
"schema_version": 1,
|
||||
"name": "datetime",
|
||||
"file": "datetime.mq.md",
|
||||
"require_module": "datetime.mq.md",
|
||||
"args": [],
|
||||
"stdin": "",
|
||||
"env": {},
|
||||
"expected_stdout": "",
|
||||
"expected_terminal": {
|
||||
"items": [
|
||||
{
|
||||
"type": "text",
|
||||
"value": "1970-01-01T00:00:00Z"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"value": "2023-11-14T22:13:20.123Z"
|
||||
},
|
||||
{
|
||||
"decimal": "0",
|
||||
"type": "int"
|
||||
},
|
||||
{
|
||||
"decimal": "1795248000123",
|
||||
"type": "int"
|
||||
},
|
||||
{
|
||||
"type": "bool",
|
||||
"value": false
|
||||
},
|
||||
{
|
||||
"type": "bool",
|
||||
"value": false
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"value": "1969-12-31T23:59:59.999Z"
|
||||
}
|
||||
],
|
||||
"type": "list"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"schema": "marqdo.stdlib-test-case",
|
||||
"schema_version": 1,
|
||||
"name": "env",
|
||||
"file": "env.mq.md",
|
||||
"require_module": "env.mq.md",
|
||||
"args": [
|
||||
"--allow-env",
|
||||
"MARQDO_TEST_ENV",
|
||||
"--allow-env",
|
||||
"MARQDO_MISSING_ENV"
|
||||
],
|
||||
"stdin": "",
|
||||
"env": {
|
||||
"MARQDO_TEST_ENV": "env-value"
|
||||
},
|
||||
"expected_stdout": "",
|
||||
"expected_terminal": {
|
||||
"items": [
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
},
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
},
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"value": "env-value"
|
||||
},
|
||||
{
|
||||
"type": "bool",
|
||||
"value": false
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"value": "env.not_found"
|
||||
}
|
||||
],
|
||||
"type": "list"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"schema": "marqdo.stdlib-test-case",
|
||||
"schema_version": 1,
|
||||
"name": "process",
|
||||
"file": "process.mq.md",
|
||||
"require_module": "process.mq.md",
|
||||
"args": [
|
||||
"--allow-env",
|
||||
"MARQDO_TEST_PROCESS_PROGRAM",
|
||||
"--allow-process",
|
||||
"$MARQDO_BIN"
|
||||
],
|
||||
"stdin": "",
|
||||
"env": {
|
||||
"MARQDO_TEST_PROCESS_PROGRAM": "$MARQDO_BIN"
|
||||
},
|
||||
"expected_stdout": "",
|
||||
"expected_terminal": {
|
||||
"items": [
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
},
|
||||
{
|
||||
"decimal": "0",
|
||||
"type": "int"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"value": {
|
||||
"$bin_version": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"value": ""
|
||||
}
|
||||
],
|
||||
"type": "list"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"schema": "marqdo.stdlib-test-case",
|
||||
"schema_version": 1,
|
||||
"name": "random",
|
||||
"file": "random.mq.md",
|
||||
"require_module": "random.mq.md",
|
||||
"args": [
|
||||
"--allow-random"
|
||||
],
|
||||
"stdin": "",
|
||||
"env": {},
|
||||
"expected_stdout": "",
|
||||
"expected_terminal": {
|
||||
"items": [
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
},
|
||||
{
|
||||
"decimal": "16",
|
||||
"type": "int"
|
||||
},
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
},
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
},
|
||||
{
|
||||
"type": "bool",
|
||||
"value": true
|
||||
}
|
||||
],
|
||||
"type": "list"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
import:
|
||||
clock: "std:clock"
|
||||
---
|
||||
|
||||
# @exec clock
|
||||
|
||||
## main
|
||||
|
||||
**现在 = clock.现在()**
|
||||
|
||||
`return [现在.ok, 类型(现在.value), 现在.value > 1600000000000]`
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
import:
|
||||
datetime: "std:datetime"
|
||||
---
|
||||
|
||||
# @exec datetime
|
||||
|
||||
## main
|
||||
|
||||
`return [datetime.格式化(0), datetime.格式化(1700000000123), datetime.解析("1970-01-01T00:00:00Z").value, datetime.解析("2026-11-21T08:00:00.123Z").value, datetime.解析("2026-13-01T00:00:00Z").ok, datetime.解析("2026-02-29T00:00:00Z").ok, datetime.格式化(-1)]`
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
import:
|
||||
env: "std:env"
|
||||
---
|
||||
|
||||
# @exec env
|
||||
|
||||
## main
|
||||
|
||||
**存在 = env.存在("MARQDO_TEST_ENV")**
|
||||
|
||||
**读取 = env.读取("MARQDO_TEST_ENV")**
|
||||
|
||||
**缺失 = env.读取("MARQDO_MISSING_ENV")**
|
||||
|
||||
`return [存在.ok, 存在.value, 读取.ok, 读取.value, 缺失.ok, 缺失.code]`
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
import:
|
||||
env: "std:env"
|
||||
process: "std:process"
|
||||
---
|
||||
|
||||
# @exec process
|
||||
|
||||
## main
|
||||
|
||||
**程序 = env.读取("MARQDO_TEST_PROCESS_PROGRAM")**
|
||||
|
||||
**结果 = process.运行(程序.value, ["--version"])**
|
||||
|
||||
`return [结果.ok, 结果.value.status, 结果.value.stdout, 结果.value.stderr]`
|
||||
@@ -0,0 +1,14 @@
|
||||
---
|
||||
import:
|
||||
random: "std:random"
|
||||
---
|
||||
|
||||
# @exec random
|
||||
|
||||
## main
|
||||
|
||||
**字节结果 = random.字节(16)**
|
||||
|
||||
**整数结果 = random.整数(100)**
|
||||
|
||||
`return [字节结果.ok, 长度(字节结果.value), 整数结果.ok, 整数结果.value >= 0, 整数结果.value < 100]`
|
||||
Reference in new issue
Block a user