方向反转:此前 SKILL.md 是「模板渲染产物」、sap-cli 是源;现 sap-cli 归档, sap-cli-skill 承接开发与分发,SKILL.md 回归手工维护的正本。 迁移(来自 sap-cli,共 104 文件): - tests/ 692 例测试(15 个文件的内联 sys.path 改指 assets/) - openspec/ SDD 规格与归档变更(42 文件) - docs/ 开发文档与 ADT 原理(含 dev/CLAUDE.md、AGENTS.md) - .claude/ rules 副本 + settings.json(供 Claude Code) - .github/ .hermes/ .pre-commit-config.yaml .editorconfig CLAUDE.md - scripts/ 保持仅 setup.py(pack_skill.py 已随旧仓归档,不迁) 修复(迁移暴露的真实缺陷): - assets/pyproject.toml 的 build-backend 写作 `setuptools.backends._legacy:_Backend`, 该模块在 setuptools 中不存在 → `pip install -e` 从来装不上。改为 build_meta。 实测:临时 venv 安装成功,sap-cli --help 正常列出 31 个命令 - pyproject readme 指向不存在的 assets/README.md(editable 安装会失败)→ 改内联文本 - pyproject urls 改指 sap-cli-skill 机制调整: - .github/workflows/ci.yml 适配 assets/ 布局;顶部注明该工作流仅 GitHub 执行, 本仓在 Gitee 不会自动跑 - pre-commit 增本地测试门禁(Gitee 上真正生效的那道) - .gitignore 合并旧仓完整规则(保留 log/ 下 md 知识库入库,只忽略运行日志) - 大文件上限 100KB→1MB(架构图 512KB) 守卫测试 tests/unit/test_repo_guards.py(10 → 18 例): - SKILL.md 须记录 parser 全部 CLI 命令 / 铁律 1-5 须为真实小节标题 / 示例不得违反铁律 5 - references/ 规则齐备;.claude/rules 与 references 必须一致(实测抓到一次真实漂移) - VERSION == sapcli.__version__ == README 版本 - 仓内不得再出现 pack_skill.py / skill-src(防废弃流程回潮) 698 tests OK;editable 安装与 CLI 入口经临时 venv 实测通过。 docs/RELEASING.md 重写为单源开发流程。
140 lines
4.9 KiB
Python
140 lines
4.9 KiB
Python
"""commands/history.py 单元测试 — cmd_history + _format_date。
|
|
|
|
运行: python tests/unit/test_history.py
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "assets"))
|
|
|
|
|
|
def _args(**kwargs):
|
|
defaults = {"type": "class", "name": "ZCL_TEST"}
|
|
defaults.update(kwargs)
|
|
return argparse.Namespace(**defaults)
|
|
|
|
|
|
# ═══════════════════════════════════════════
|
|
# _format_date
|
|
# ═══════════════════════════════════════════
|
|
|
|
class TestFormatDate(unittest.TestCase):
|
|
"""_format_date 将 ISO 日期转为可读格式。"""
|
|
|
|
def test_empty_returns_empty(self):
|
|
from sapcli.commands.history import _format_date
|
|
self.assertEqual(_format_date(""), "")
|
|
|
|
def test_iso_datetime(self):
|
|
from sapcli.commands.history import _format_date
|
|
self.assertEqual(_format_date("2026-06-15T10:30:00"), "2026-06-15 10:30:00")
|
|
|
|
def test_iso_with_z_suffix(self):
|
|
from sapcli.commands.history import _format_date
|
|
self.assertEqual(_format_date("2026-06-15T10:30:00Z"), "2026-06-15 10:30:00")
|
|
|
|
def test_none_value(self):
|
|
from sapcli.commands.history import _format_date
|
|
self.assertEqual(_format_date(None), "")
|
|
|
|
|
|
# ═══════════════════════════════════════════
|
|
# cmd_history
|
|
# ═══════════════════════════════════════════
|
|
|
|
class TestCmdHistory(unittest.TestCase):
|
|
"""cmd_history 查询对象版本历史。"""
|
|
|
|
def test_object_not_found_raises(self):
|
|
from sapcli.commands.history import cmd_history
|
|
from sapcli.exceptions import ObjectNotFoundError
|
|
|
|
client = MagicMock()
|
|
client.object_exists.return_value = False
|
|
|
|
with self.assertRaises(ObjectNotFoundError) as ctx:
|
|
cmd_history(_args(), client)
|
|
self.assertIn("ZCL_TEST", str(ctx.exception))
|
|
|
|
def test_api_error_prints_failure_and_returns(self):
|
|
"""get_object_versions 抛异常时打印失败并返回。"""
|
|
from sapcli.commands.history import cmd_history
|
|
|
|
client = MagicMock()
|
|
client.object_exists.return_value = True
|
|
client.get_object_versions.side_effect = Exception("server down")
|
|
|
|
with patch("builtins.print") as mock_print:
|
|
cmd_history(_args(), client)
|
|
|
|
printed = " ".join(str(c) for c, _ in mock_print.call_args_list)
|
|
self.assertIn("失败", printed)
|
|
|
|
def test_no_versions(self):
|
|
"""无版本历史时打印提示。"""
|
|
from sapcli.commands.history import cmd_history
|
|
|
|
client = MagicMock()
|
|
client.object_exists.return_value = True
|
|
client.get_object_versions.return_value = []
|
|
|
|
with patch("builtins.print") as mock_print:
|
|
cmd_history(_args(), client)
|
|
|
|
printed = " ".join(str(c) for c, _ in mock_print.call_args_list)
|
|
self.assertIn("未找到版本历史", printed)
|
|
|
|
def test_with_versions_prints_table(self):
|
|
"""有版本时打印版本表格。"""
|
|
from sapcli.commands.history import cmd_history
|
|
|
|
client = MagicMock()
|
|
client.object_exists.return_value = True
|
|
client.get_object_versions.return_value = [
|
|
{
|
|
"version": "0001", "author": "ALICE",
|
|
"date": "2026-06-15T10:30:00", "versionTitle": "initial import",
|
|
},
|
|
{
|
|
"version": "", "author": "BOB",
|
|
"date": "", "versionTitle": "current",
|
|
},
|
|
]
|
|
|
|
with patch("builtins.print") as mock_print:
|
|
cmd_history(_args(), client)
|
|
|
|
printed = " ".join(str(c) for c, _ in mock_print.call_args_list)
|
|
self.assertIn("0001", printed)
|
|
self.assertIn("ALICE", printed)
|
|
self.assertIn("initial import", printed)
|
|
# version 为空时回退到 "active"
|
|
self.assertIn("active", printed)
|
|
self.assertIn("2", printed) # 找到 2 个版本
|
|
|
|
def test_versions_missing_version_key(self):
|
|
"""版本记录缺 version 键时回退到 active。"""
|
|
from sapcli.commands.history import cmd_history
|
|
|
|
client = MagicMock()
|
|
client.object_exists.return_value = True
|
|
client.get_object_versions.return_value = [
|
|
{"author": "CAROL", "date": "2026-01-01T00:00:00", "versionTitle": "v1"},
|
|
]
|
|
|
|
with patch("builtins.print") as mock_print:
|
|
cmd_history(_args(), client)
|
|
|
|
printed = " ".join(str(c) for c, _ in mock_print.call_args_list)
|
|
self.assertIn("active", printed)
|
|
self.assertIn("CAROL", printed)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|