From aba6b60934a1e25162a09022e4caf953bde20e6b Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 27 Nov 2026 00:08:16 +0800 Subject: [PATCH] migrate: own the gzip domain module as an explicit package Move the pre-migration compress.mq.md content into this repository's package entry. The module leaves the default Bundle and is consumed through the explicit @compress package import; the baseline gzip/gunzip behaviour and the original test terminal value are preserved, and the CLI host ops stay put. Refs cflmy/marqdo#9, cflmy/marqdo-ext-archive#1 --- README.md | 40 ++++++++-- index.mq.md | 19 +++++ marqdo.yaml | 5 ++ scripts/run_cases.py | 153 +++++++++++++++++++++++++++++++++++++++ scripts/test.sh | 7 ++ tests/README.md | 6 +- tests/cases/archive.json | 32 ++++++++ tests/compress.mq.md | 14 ++++ 8 files changed, 270 insertions(+), 6 deletions(-) create mode 100644 index.mq.md create mode 100644 marqdo.yaml create mode 100755 scripts/run_cases.py create mode 100755 scripts/test.sh create mode 100644 tests/cases/archive.json create mode 100644 tests/compress.mq.md diff --git a/README.md b/README.md index 681aea1..ad0a30d 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,44 @@ # marqdo-ext-archive -本仓负责压缩、安全归档和解包。当前状态为 `planned`。 +本仓负责gzip 压缩/解压(显式 Package)。当前处于**标准库源码归位迁移**阶段:领域模块已迁入本仓作为显式 Package,等待父仓候选组合验收(父仓工单 [marqdo#9](https://git.cflmy.cn/cflmy/marqdo/issues/9),本仓工单 [#1](https://git.cflmy.cn/cflmy/marqdo-ext-archive/issues/1))。 -## 首项任务 +## 现行形态:显式 Package,不进入默认 Bundle -实现归档与安全解包,为 CLI 移除旧压缩入口提供可验证的替代。 +- 入口:`index.mq.md`(`marqdo.yaml` 声明 package)。 +- 消费者声明依赖并用公开 `@alias` 导入: + +```yaml +# consumer/marqdo.yaml +dependencies: + compress: + git: https://git.cflmy.cn/cflmy/marqdo-ext-archive + rev: <精确提交> +``` + +```markdown +--- +import: + compress: "@compress" +--- + +## @exec main + +`return compress.<函数>(...)` +``` + +- 不再以 `std:compress` 提供,不保留隐藏 fallback;默认标准库 Bundle 不包含本模块。 +- 测试:`scripts/test.sh` 只接受 `MARQDO_BIN` + `MARQDO_STDLIB_DIR`,用临时消费者工程经 `marqdo lock` 与 `@alias` 公开导入执行。 + +`index.mq.md`:原 `compress.mq.md` 的 gzip/gunzip 文档函数,公开为 Package 入口(消费者通过 `@compress` 导入) + +## 边界 + +不进入默认 Bundle,不提供 `std:compress`;CLI 既有 `compress.gzip/gunzip` Host 请求与 flate2 依赖保留;不开发新的 ZIP/TAR 后端。保留 Text 与字节列表形状、8 MiB 限制、损坏输入与解压上限行为。 ## 目录 -- [modules/](modules/README.md) 存放对外发布的模块源码。 -- [tests/](tests/README.md) 存放独立验收用的输入和断言。 +- `index.mq.md`:Package 入口与唯一公开入口。 +- `tests/`:迁入的领域测试源与期望。 +- `scripts/`:独立验收入口。 [生态迁移台账](https://git.cflmy.cn/cflmy/marqdo/src/branch/main/docs/MIGRATION.md) · [开发流程](https://git.cflmy.cn/cflmy/marqdo/src/branch/main/docs/DEVELOPMENT.md) diff --git a/index.mq.md b/index.mq.md new file mode 100644 index 0000000..1cc0f84 --- /dev/null +++ b/index.mq.md @@ -0,0 +1,19 @@ + +# @exec compress +## 压缩 + +`params(文本)` + +把 UTF-8 文本压缩为 gzip 字节列表(List,0–255)。输入上限 +8 MiB;结果确定(不写入时间戳/文件名)。 + +`return resource.request("compress.gzip", [文本])` + +## 解压 + +`params(字节列表)` + +解压 gzip 字节列表并校验 UTF-8。输出上限 8 MiB;坏 gzip 返回 +compress.invalid,非 UTF-8 返回 compress.not_utf8。 + +`return resource.request("compress.gunzip", [字节列表])` diff --git a/marqdo.yaml b/marqdo.yaml new file mode 100644 index 0000000..1b6b5f5 --- /dev/null +++ b/marqdo.yaml @@ -0,0 +1,5 @@ +project: + name: marqdo-ext-archive + version: 0.1.0 + marqdo: ">=3.0.0-beta.1,<4.0.0" +dependencies: {} diff --git a/scripts/run_cases.py b/scripts/run_cases.py new file mode 100755 index 0000000..f2f05c1 --- /dev/null +++ b/scripts/run_cases.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python3 +"""Run an explicit-package extension repository's tests with real CLI inputs. + +Each case builds a throwaway consumer project whose `dependencies.` path +points at a copy of this repository's package (`marqdo.yaml` + `index.mq.md`), +runs `marqdo lock` once, then executes the migrated `.mq.md` test through the +public `@alias` import. Inputs are explicit: `MARQDO_BIN` (verified CLI) and +`MARQDO_STDLIB_DIR` (installed Bundle for the remaining `std:` imports, if any). +Nothing reads a sibling repository or a floating branch. +""" +from __future__ import annotations + +import base64 +import json +import os +import shutil +import subprocess +import sys +import tempfile +from pathlib import Path + +REPO = Path(__file__).resolve().parents[1] +CASES = REPO / "tests" / "cases" +SCHEMA = "marqdo.package-test-case" +SCHEMA_VERSION = 1 + + +class CaseError(Exception): + pass + + +def substitute(value, binary: str, package_root: Path): + if isinstance(value, dict): + if set(value) == {"$bin_version"}: + return subprocess.run([binary, "--version"], capture_output=True, text=True).stdout + if set(value) == {"$package_root"}: + return str(package_root) + return {k: substitute(v, binary, package_root) for k, v in value.items()} + if isinstance(value, list): + return [substitute(v, binary, package_root) for v in value] + return value + + +def channel_text(events, channel): + parts = [] + 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 mismatch") + parts.append(data) + return b"".join(parts).decode() + + +def consumer_manifest(alias: str) -> str: + return ( + "project:\n" + " name: package-test-consumer\n" + " version: 0.0.0\n" + ' marqdo: ">=3.0.0-beta.1,<4.0.0"\n' + "dependencies:\n" + f" {alias}:\n" + f" path: ./deps/{alias}\n" + ) + + +def run_case(case: dict, binary: str, bundle: Path) -> None: + name = case["name"] + alias = case["dep_alias"] + 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) + source = REPO / "tests" / case["file"] + if not source.is_file(): + raise CaseError(f"{name}: missing test source {source}") + with tempfile.TemporaryDirectory(prefix="mq-package-test-") as tmp: + consumer = Path(tmp) / "consumer" + package = consumer / "deps" / alias + package.mkdir(parents=True) + shutil.copy2(REPO / "marqdo.yaml", package / "marqdo.yaml") + shutil.copy2(REPO / "index.mq.md", package / "index.mq.md") + (consumer / "marqdo.yaml").write_text(consumer_manifest(alias)) + (consumer / "tests").mkdir() + shutil.copy2(source, consumer / "tests" / case["file"]) + lock = subprocess.run([binary, "lock"], cwd=consumer, env=env, capture_output=True) + if lock.returncode != 0: + raise CaseError(f"{name}: marqdo lock failed: {lock.stderr.decode(errors='replace')}") + args = [str(arg).replace("$MARQDO_BIN", binary) for arg in case.get("args", [])] + result = subprocess.run( + [binary, "run", str(consumer / "tests" / case["file"]), *args, "--output", "json"], + cwd=consumer, env=env, input=case.get("stdin", "").encode(), capture_output=True, + ) + if result.returncode != 0: + raise CaseError(f"{name}: CLI exited {result.returncode}:\n{result.stderr.decode(errors='replace')}") + report = json.loads(result.stdout) + if report.get("run", {}).get("status") != "ok": + raise CaseError(f"{name}: run status not ok") + terminal = report["events"][-1]["payload"] + if terminal.get("status") != "ok": + raise CaseError(f"{name}: terminal status not ok") + expected = substitute(case["expected_terminal"], binary, REPO) + if terminal.get("value") != expected: + raise CaseError( + f"{name}: terminal value changed\nexpected: " + f"{json.dumps(expected, ensure_ascii=False, sort_keys=True)}\nactual: " + f"{json.dumps(terminal.get('value'), ensure_ascii=False, sort_keys=True)}" + ) + stdout = channel_text(report["events"], "stdout.chunk") + if stdout != case.get("expected_stdout", ""): + raise CaseError(f"{name}: stdout changed: {stdout!r}") + + +def main() -> int: + 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", file=sys.stderr) + return 2 + failures = 0 + ran = 0 + for path in paths: + case = json.loads(path.read_text()) + if case.get("schema") != SCHEMA or case.get("schema_version") != SCHEMA_VERSION: + print(f"FAIL {path.name}: unsupported schema", file=sys.stderr) + failures += 1 + continue + ran += 1 + try: + run_case(case, binary, bundle) + print(f"PASS {case['name']}") + except (CaseError, json.JSONDecodeError) 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()) diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..380174f --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# Explicit inputs only: a verified CLI binary and the installed Bundle that +# still provides the std: dependencies (if any). Never reads 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" "$@" diff --git a/tests/README.md b/tests/README.md index 7d9f050..d3a4cc2 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,3 +1,7 @@ # 验收测试 -首个功能附带正例和失败用例,记录所用 CLI 提交或发行版本以及实际运行命令。 +`tests/*.mq.md` 是从迁移前 CLI 官方标准库自测迁入的真实测试源,导入已改为公开 Package 别名(`@alias`);`tests/cases/*.json` 固定输入(stdin、CLI 参数、环境、依赖别名)与期望结果(observation 终值、stdout)。 + +运行入口是 `scripts/test.sh`,要求显式传入 `MARQDO_BIN` 与 `MARQDO_STDLIB_DIR`。运行器为每个 case 建立临时消费者工程(`dependencies..path` 指向本仓 Package 副本),先 `marqdo lock` 再通过公开 `@alias` 导入执行,不读取兄弟仓路径或浮动分支。 + +期望值取自迁移前基线 Bundle 的真实执行结果;修改期望值等于声明公开行为变化,必须单独评审。 diff --git a/tests/cases/archive.json b/tests/cases/archive.json new file mode 100644 index 0000000..f68c3cb --- /dev/null +++ b/tests/cases/archive.json @@ -0,0 +1,32 @@ +{ + "schema": "marqdo.package-test-case", + "schema_version": 1, + "name": "archive", + "file": "compress.mq.md", + "dep_alias": "compress", + "args": [], + "stdin": "", + "env": {}, + "expected_stdout": "", + "expected_terminal": { + "items": [ + { + "type": "bool", + "value": true + }, + { + "type": "bool", + "value": true + }, + { + "type": "bool", + "value": true + }, + { + "type": "text", + "value": "hello 世界" + } + ], + "type": "list" + } +} diff --git a/tests/compress.mq.md b/tests/compress.mq.md new file mode 100644 index 0000000..4c5ff19 --- /dev/null +++ b/tests/compress.mq.md @@ -0,0 +1,14 @@ +--- +import: + compress: "@compress" +--- + +# @exec compress + +## main + +**压缩结果 = compress.压缩("hello 世界")** + +**解压结果 = compress.解压(压缩结果.value)** + +`return [压缩结果.ok, 长度(压缩结果.value) > 0, 解压结果.ok, 解压结果.value]` -- 2.54.1