Files
cleanup-release/tests/lib/jq_mock.py
T
JefferyandClaude Opus 4.8 6cad836ce3 test(清理成品): 新增 tag 清理測試並抽出共用 jq mock
新增 entrypoint_tag_cleanup.sh 驗證孤兒 tag 刪除、保留指定 release 的 tag;分頁測試改為端點感知並共用 jq mock。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 16:14:56 +08:00

99 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Minimal jq stand-in for entrypoint.sh tests.
Only the jq expressions used by entrypoint.sh are implemented. Unknown
expressions fail loudly so new jq usage is noticed instead of silently
producing wrong results.
"""
import json
import re
import sys
expr = ""
flags = set()
named_args = {}
args = sys.argv[1:]
while args:
current = args.pop(0)
if current in {"-e", "-c", "-r", "-s"}:
flags.add(current)
continue
if current == "--arg":
key = args.pop(0)
value = args.pop(0)
named_args[key] = value
continue
if current == "--":
if not args:
raise SystemExit("missing jq expression")
expr = args.pop(0)
break
if current.startswith("-"):
flags.add(current)
continue
expr = current
break
raw = sys.stdin.read()
def dump(value):
if "-r" in flags and isinstance(value, (str, int, float)) and not isinstance(value, bool):
sys.stdout.write(str(value))
else:
sys.stdout.write(json.dumps(value, separators=(",", ":")))
if expr == "length":
data = json.loads(raw or "null")
print(len(data))
raise SystemExit(0)
if expr == "add":
arrays = [json.loads(line) for line in raw.splitlines() if line.strip()]
merged = []
for item in arrays:
merged.extend(item)
dump(merged)
raise SystemExit(0)
if expr == "sort_by(.created_at) | reverse":
data = json.loads(raw or "[]")
dump(sorted(data, key=lambda item: item["created_at"], reverse=True))
raise SystemExit(0)
if expr == "[.[].tag_name]":
data = json.loads(raw or "[]")
dump([item["tag_name"] for item in data])
raise SystemExit(0)
match = re.fullmatch(r"any\(\.\[\]; \. == \$(\w+)\)", expr)
if match:
data = json.loads(raw or "[]")
needle = named_args[match.group(1)]
sys.stdout.write("true" if any(item == needle for item in data) else "false")
raise SystemExit(0)
match = re.fullmatch(r"\.\[(\d+):\]", expr)
if match:
data = json.loads(raw or "[]")
dump(data[int(match.group(1)):])
raise SystemExit(0)
if expr == ".[]":
data = json.loads(raw or "[]")
for item in data:
dump(item)
sys.stdout.write("\n")
raise SystemExit(0)
match = re.fullmatch(r"\.(id|tag_name|name)", expr)
if match:
data = json.loads(raw or "null")
value = data.get(match.group(1))
dump(value)
raise SystemExit(0)
raise SystemExit(f"unsupported jq expression: {expr}")