smoke.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env bash
  2. # Smoke test for the ast-grep skill on POSIX (macOS / Linux / WSL / Git Bash).
  3. #
  4. # Tests:
  5. # 1. helper --version
  6. # 2. helper langs (lists 25 languages)
  7. # 3. helper validate '\w+' --lang ts (must exit 2 with hint)
  8. # 4. helper validate 'console.log($MSG)' (must exit 0 plausible)
  9. # 5. helper validate 'def $F($$$):' --lang py (must exit 2 - trailing colon)
  10. # 6. helper validate 'function $N' --lang ts (must exit 2 - incomplete)
  11. # 7. helper validate 'foo|bar' --lang ts (must exit 2 - alternation)
  12. # 8. helper doctor (informational; tolerates no-binary)
  13. # 9. helper search w/o binary => exit 3 with install hint
  14. # 10. install.sh --help (parses)
  15. # 11. SKILL.md frontmatter shape (canonical)
  16. #
  17. # Runs against the helper using ONLY stdlib python3 - no ast-grep needed.
  18. set -euo pipefail
  19. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  20. SKILL_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  21. HELPER="python3 $SKILL_DIR/scripts/ast_grep_helper.py"
  22. OUTPUT_DIR="$(mktemp -d -t ast-grep-skill-smoke-XXXXXX)"
  23. trap 'rm -rf "$OUTPUT_DIR"' EXIT
  24. fail() { echo "FAIL: $*" >&2; exit 1; }
  25. pass() { echo "PASS: $*"; }
  26. omo_runtime_slug() {
  27. case "$(uname -s)" in
  28. Darwin) local os_slug="darwin" ;;
  29. MINGW*|MSYS*|CYGWIN*) local os_slug="win32" ;;
  30. *) local os_slug="linux" ;;
  31. esac
  32. case "$(uname -m | tr '[:upper:]' '[:lower:]')" in
  33. arm64|aarch64) local arch_slug="arm64" ;;
  34. *) local arch_slug="x64" ;;
  35. esac
  36. printf '%s-%s' "$os_slug" "$arch_slug"
  37. }
  38. fake_sg() {
  39. local target="$1"
  40. mkdir -p "$(dirname "$target")"
  41. cat > "$target" <<'SH'
  42. #!/usr/bin/env bash
  43. if [ "${1:-}" = "--version" ]; then
  44. printf 'ast-grep 0.45.0 fake\n'
  45. else
  46. printf 'fake ast-grep\n'
  47. fi
  48. SH
  49. chmod +x "$target"
  50. }
  51. # 1. --version
  52. $HELPER --version | grep -q "ast-grep-helper" || fail "--version output missing"
  53. pass "--version"
  54. # 2. langs (must list 25)
  55. LANG_LINES=$($HELPER langs | grep -E '^ [a-z]' | wc -l | tr -d ' ')
  56. [ "$LANG_LINES" -ge 25 ] || fail "langs listed only $LANG_LINES (expected >=25)"
  57. pass "langs lists at least 25 languages"
  58. # 3. regex misuse: \w+
  59. set +e
  60. $HELPER validate '\w+' --lang ts > "$OUTPUT_DIR/v1.out" 2>&1
  61. RC=$?
  62. set -e
  63. [ $RC -eq 2 ] || fail "validate '\\w+' should exit 2, got $RC (output: $(cat $OUTPUT_DIR/v1.out))"
  64. grep -qi 'regex' "$OUTPUT_DIR/v1.out" || fail "validate '\\w+' should mention regex (output: $(cat $OUTPUT_DIR/v1.out))"
  65. pass "validate detects \\w regex misuse"
  66. # 4. valid pattern
  67. set +e
  68. $HELPER validate 'console.log($MSG)' --lang ts > "$OUTPUT_DIR/v2.out" 2>&1
  69. RC=$?
  70. set -e
  71. [ $RC -eq 0 ] || fail "validate 'console.log(\$MSG)' should exit 0, got $RC (output: $(cat $OUTPUT_DIR/v2.out))"
  72. pass "validate accepts plausible pattern"
  73. # 5. Python trailing colon
  74. set +e
  75. $HELPER validate 'def $F($$$):' --lang py > "$OUTPUT_DIR/v3.out" 2>&1
  76. RC=$?
  77. set -e
  78. [ $RC -eq 2 ] || fail "validate 'def \$F(\$\$\$):' should exit 2, got $RC"
  79. grep -qi 'colon\|trailing' "$OUTPUT_DIR/v3.out" || fail "validate should mention trailing colon"
  80. pass "validate detects Python trailing colon"
  81. # 6. Incomplete TS function
  82. set +e
  83. $HELPER validate 'function $N' --lang ts > "$OUTPUT_DIR/v4.out" 2>&1
  84. RC=$?
  85. set -e
  86. [ $RC -eq 2 ] || fail "validate 'function \$N' should exit 2, got $RC"
  87. grep -qi 'incomplete\|params\|body' "$OUTPUT_DIR/v4.out" || fail "validate should hint about params/body"
  88. pass "validate detects incomplete TS function"
  89. # 7. Alternation pipe
  90. set +e
  91. $HELPER validate 'foo|bar' --lang ts > "$OUTPUT_DIR/v5.out" 2>&1
  92. RC=$?
  93. set -e
  94. [ $RC -eq 2 ] || fail "validate 'foo|bar' should exit 2, got $RC"
  95. grep -qi 'alternation\|regex' "$OUTPUT_DIR/v5.out" || fail "validate should mention alternation"
  96. pass "validate detects literal | alternation"
  97. # 8. doctor (informational)
  98. $HELPER doctor > "$OUTPUT_DIR/doc.out" 2>&1 || true
  99. grep -q "ast-grep-helper" "$OUTPUT_DIR/doc.out" || fail "doctor missing helper version line"
  100. pass "doctor produces output"
  101. # Given OMO_AST_GREP_SG_PATH points at a fake executable.
  102. # When doctor resolves ast-grep, then it reports that exact path.
  103. FAKE_ENV_SG="$OUTPUT_DIR/fake-env/sg"
  104. fake_sg "$FAKE_ENV_SG"
  105. OMO_AST_GREP_SG_PATH="$FAKE_ENV_SG" $HELPER doctor > "$OUTPUT_DIR/omo-env.out" 2>&1
  106. grep -Fq "ast-grep binary: $FAKE_ENV_SG" "$OUTPUT_DIR/omo-env.out" || fail "OMO_AST_GREP_SG_PATH was not preferred: $(cat "$OUTPUT_DIR/omo-env.out")"
  107. pass "OMO_AST_GREP_SG_PATH resolves first"
  108. # Given HOME has an OMO runtime sg executable.
  109. # When doctor resolves ast-grep, then it reports the HOME runtime path.
  110. RUNTIME_HOME="$OUTPUT_DIR/home"
  111. RUNTIME_SLUG="$(omo_runtime_slug)"
  112. RUNTIME_BIN="sg"
  113. case "$RUNTIME_SLUG" in
  114. win32-*) RUNTIME_BIN="sg.exe" ;;
  115. esac
  116. FAKE_RUNTIME_SG="$RUNTIME_HOME/.omo/runtime/ast-grep/$RUNTIME_SLUG/$RUNTIME_BIN"
  117. fake_sg "$FAKE_RUNTIME_SG"
  118. HOME="$RUNTIME_HOME" CODEX_HOME= OMO_AST_GREP_SG_PATH= $HELPER doctor > "$OUTPUT_DIR/omo-runtime.out" 2>&1
  119. grep -Fq "ast-grep binary: $FAKE_RUNTIME_SG" "$OUTPUT_DIR/omo-runtime.out" || fail "OMO HOME runtime was not resolved: $(cat "$OUTPUT_DIR/omo-runtime.out")"
  120. pass "OMO HOME runtime resolves before standalone fallback"
  121. # 9. search w/o binary => either runs successfully (binary found) OR exits 3 with hint
  122. # The CI may or may not have ast-grep installed; both are valid.
  123. set +e
  124. $HELPER -q search 'foo()' --lang ts /nonexistent-path-xyzzy > "$OUTPUT_DIR/s1.out" 2>&1
  125. RC=$?
  126. set -e
  127. case "$RC" in
  128. 0|1|4)
  129. # Binary present but no matches OR sg returned non-fatal error - both fine
  130. pass "search runs (rc=$RC, ast-grep available)"
  131. ;;
  132. 3)
  133. grep -qi 'install' "$OUTPUT_DIR/s1.out" || fail "search rc=3 should print install hint"
  134. pass "search without binary prints install hint"
  135. ;;
  136. *)
  137. fail "search returned unexpected rc=$RC: $(cat $OUTPUT_DIR/s1.out)"
  138. ;;
  139. esac
  140. # 10. install.sh syntax check + --help
  141. bash -n "$SKILL_DIR/install.sh" || fail "install.sh has syntax errors"
  142. $SKILL_DIR/install.sh --help > "$OUTPUT_DIR/inst.out" 2>&1 || true
  143. grep -qi 'install' "$OUTPUT_DIR/inst.out" || fail "install.sh --help missing keyword 'install'"
  144. pass "install.sh syntax + --help"
  145. # 11. SKILL.md frontmatter
  146. python3 - <<PY
  147. import re, sys
  148. src = open("$SKILL_DIR/SKILL.md").read()
  149. assert src.startswith("---\n"), "SKILL.md must start with YAML frontmatter"
  150. end = src.find("\n---\n", 4)
  151. assert end > 0, "SKILL.md missing closing ---"
  152. fm = src[4:end]
  153. assert re.search(r"^name:\s*ast-grep\s*$", fm, re.M), "frontmatter missing name: ast-grep"
  154. assert re.search(r"^description:", fm, re.M), "frontmatter missing description"
  155. PY
  156. pass "SKILL.md frontmatter shape"
  157. # 12. All required reference files exist
  158. for f in references/install.md references/patterns.md references/pitfalls.md \
  159. references/recipes.md references/cli.md references/yaml-rules.md \
  160. references/sgconfig.md; do
  161. test -f "$SKILL_DIR/$f" || fail "missing $f"
  162. done
  163. pass "all references present"
  164. python3 - "$SKILL_DIR" <<'PY' || fail "Korean characters found in skill content"
  165. import pathlib, re, sys
  166. root = pathlib.Path(sys.argv[1])
  167. hangul = re.compile(r"[\uac00-\ud7a3]")
  168. targets = [root / "SKILL.md", root / "README.md"]
  169. for d in ("references", "scripts", "tests", ".github"):
  170. targets.extend((root / d).rglob("*"))
  171. targets.extend([root / "install.sh", root / "install.ps1"])
  172. hits = 0
  173. for p in targets:
  174. if not p.is_file():
  175. continue
  176. try:
  177. text = p.read_text(encoding="utf-8")
  178. except (UnicodeDecodeError, OSError):
  179. continue
  180. for n, line in enumerate(text.splitlines(), 1):
  181. if hangul.search(line):
  182. print(f"{p}:{n}: {line.rstrip()}")
  183. hits += 1
  184. sys.exit(1 if hits else 0)
  185. PY
  186. pass "no Korean in skill content"
  187. echo ""
  188. echo "all smoke tests passed"