|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Issue Form の種別(`type:`)と、 それを読む `issue-form.sh` を確かめる。 |
| 3 | +
|
| 4 | +**ネットワークを使わない。** `gh` を差し替え、 手元の Form を正本の代わりに |
| 5 | +返す。 |
| 6 | +
|
| 7 | +なぜ見るのか。 直近の Issue 71 件はすべて種別なしだった。 Form も `/issue` |
| 8 | +も種別を指定していなかったためで、 仕組みを足しても**値を入れる経路**が |
| 9 | +無ければ使われない。 種別を Form の `type:` 1 箇所に書き、 CLI の |
| 10 | +`/issue` もそこから読む。 どちらかが崩れると、 また種別なしの Issue に戻る。 |
| 11 | +""" |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import os |
| 15 | +import re |
| 16 | +import subprocess |
| 17 | +import tempfile |
| 18 | +import unittest |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +ROOT = Path(__file__).resolve().parents[2] |
| 22 | +FORMS = ROOT / ".github" / "ISSUE_TEMPLATE" |
| 23 | +SCRIPT = ROOT / ".claude" / "scripts" / "issue-form.sh" |
| 24 | + |
| 25 | +#: `gh api repos/<正本>/contents/<Form> --jq .content` の代わり。 |
| 26 | +#: 正本の API と同じく base64 で返す。 読む場所は FAKE_FORMS。 |
| 27 | +FAKE_GH = """#!/usr/bin/env bash |
| 28 | +for a in "$@"; do |
| 29 | + case "$a" in */ISSUE_TEMPLATE/*.yml) f="${a##*/}" ;; esac |
| 30 | +done |
| 31 | +base64 < "$FAKE_FORMS/$f" |
| 32 | +""" |
| 33 | + |
| 34 | + |
| 35 | +def _forms(): |
| 36 | + """種別を持つべき Form(`config.yml` は Form ではない)。""" |
| 37 | + return sorted(p for p in FORMS.glob("*.yml") if p.name != "config.yml") |
| 38 | + |
| 39 | + |
| 40 | +def _form_type(path: Path) -> "str | None": |
| 41 | + m = re.search(r"^type:\s*\"?([^\"\n]+?)\"?\s*$", |
| 42 | + path.read_text(encoding="utf-8"), re.M) |
| 43 | + return m.group(1) if m else None |
| 44 | + |
| 45 | + |
| 46 | +def _run(args, forms_dir=FORMS): |
| 47 | + """`issue-form.sh` を差し替えた gh で実行する。""" |
| 48 | + with tempfile.TemporaryDirectory() as d: |
| 49 | + gh = Path(d) / "gh" |
| 50 | + gh.write_text(FAKE_GH, encoding="utf-8") |
| 51 | + gh.chmod(0o755) |
| 52 | + env = dict(os.environ, PATH=f"{d}:{os.environ['PATH']}", |
| 53 | + FAKE_FORMS=str(forms_dir)) |
| 54 | + return subprocess.run(["bash", str(SCRIPT), *args], env=env, |
| 55 | + capture_output=True, text=True) |
| 56 | + |
| 57 | + |
| 58 | +def _filled_body(kind: str) -> str: |
| 59 | + """skeleton から、 必須項目と選択肢を埋めた本文を作る。""" |
| 60 | + r = _run(["skeleton", kind]) |
| 61 | + first = {} |
| 62 | + for ln in r.stderr.splitlines(): |
| 63 | + m = re.match(r"^ (.+?): (.+)$", ln) |
| 64 | + if m: |
| 65 | + first.setdefault(m.group(1), m.group(2)) |
| 66 | + out = [] |
| 67 | + for ln in r.stdout.splitlines(): |
| 68 | + if ln.startswith("### "): |
| 69 | + label = ln[4:] |
| 70 | + out += [ln, "", first.get(label, "x"), ""] |
| 71 | + return "\n".join(out) + "\n" |
| 72 | + |
| 73 | + |
| 74 | +class EveryFormHasATypeTest(unittest.TestCase): |
| 75 | + |
| 76 | + def test_every_form_names_its_issue_type(self) -> None: |
| 77 | + """**Form を足したときに `type:` を忘れると、 その Issue は種別なし。**""" |
| 78 | + self.assertTrue(_forms(), "Form が 1 件も見つかりません") |
| 79 | + for f in _forms(): |
| 80 | + with self.subTest(form=f.name): |
| 81 | + self.assertTrue(_form_type(f), f"{f.name} に type: が無い") |
| 82 | + |
| 83 | + |
| 84 | +class TheScriptReadsTheTypeTest(unittest.TestCase): |
| 85 | + """**種別を書き写さない。** Form の `type:` から読むこと。""" |
| 86 | + |
| 87 | + def test_the_skeleton_shows_the_type(self) -> None: |
| 88 | + for f in _forms(): |
| 89 | + kind, want = f.stem, _form_type(f) |
| 90 | + with self.subTest(form=f.name): |
| 91 | + r = _run(["skeleton", kind]) |
| 92 | + self.assertEqual(r.returncode, 0, r.stderr) |
| 93 | + self.assertIn(f"Issue の種別: {want}(gh issue create --type" |
| 94 | + f" {want})", r.stderr) |
| 95 | + self.assertNotIn("Issue の種別", r.stdout, |
| 96 | + "種別を本文(標準出力)に混ぜています") |
| 97 | + |
| 98 | + def test_the_check_names_the_type_right_before_creation(self) -> None: |
| 99 | + """check は作成の直前に実行する。 そこで `--type` を思い出させる。""" |
| 100 | + for f in _forms(): |
| 101 | + kind, want = f.stem, _form_type(f) |
| 102 | + with self.subTest(form=f.name), tempfile.NamedTemporaryFile( |
| 103 | + "w", suffix=".md", encoding="utf-8", delete=False) as b: |
| 104 | + b.write(_filled_body(kind)) |
| 105 | + try: |
| 106 | + r = _run(["check", kind, b.name]) |
| 107 | + self.assertEqual(r.returncode, 0, r.stderr) |
| 108 | + self.assertIn("OK:", r.stdout) |
| 109 | + self.assertIn(f"--type {want}", r.stdout) |
| 110 | + finally: |
| 111 | + os.unlink(b.name) |
| 112 | + |
| 113 | + def test_a_form_without_a_type_says_so(self) -> None: |
| 114 | + """**種別が無いのに空の `--type` を案内しないこと。**""" |
| 115 | + with tempfile.TemporaryDirectory() as d: |
| 116 | + src = (FORMS / "feature.yml").read_text(encoding="utf-8") |
| 117 | + (Path(d) / "feature.yml").write_text( |
| 118 | + re.sub(r"^type:.*\n", "", src, flags=re.M), encoding="utf-8") |
| 119 | + r = _run(["skeleton", "feature"], forms_dir=Path(d)) |
| 120 | + self.assertEqual(r.returncode, 0, r.stderr) |
| 121 | + self.assertIn("Issue の種別: なし", r.stderr) |
| 122 | + self.assertNotIn("--type (", r.stderr) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + unittest.main() |
0 commit comments