check-document-python.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { execFile } from "node:child_process";
  2. import { promisify } from "node:util";
  3. import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join, resolve } from "node:path";
  6. import { describe, expect, it } from "vitest";
  7. const exec = promisify(execFile);
  8. const checker = resolve("scripts/check-document-python.mjs");
  9. const selection = '"$(command -v zcode-python || command -v python3)"';
  10. describe("document Python command gate", () => {
  11. it("checks all distributed plugin commands", async () => {
  12. await expect(exec(process.execPath, [checker])).resolves.toMatchObject({ stderr: "" });
  13. });
  14. it("accepts interpreter selection and prose but rejects a newly added bare command", async () => {
  15. const root = await mkdtemp(join(tmpdir(), "document-python-gate-"));
  16. try {
  17. for (const plugin of ["documents", "pdf", "presentations", "spreadsheets"]) {
  18. await mkdir(join(root, `${plugin}-plugin`, "skills"), { recursive: true });
  19. await mkdir(join(root, `${plugin}-plugin`, "scripts"));
  20. }
  21. const doc = join(root, "documents-plugin/skills/SKILL.md");
  22. await writeFile(
  23. doc,
  24. `Use python3 and python-docx.\npython (default): Output format.\n\n\`\`\`python\nprint('hello')\n\`\`\`\n\n\`${selection} task.py\`\nUse the class from \`"$DOCX_SCRIPTS/document.py"\`.\n`,
  25. );
  26. await writeFile(
  27. join(root, "documents-plugin/skills/table.md"),
  28. '| Command |\n| --- |\n| `"$(command -v zcode-python \\|\\| command -v python3)" task.py` |\n',
  29. );
  30. await writeFile(
  31. join(root, "documents-plugin/scripts/setup.ps1"),
  32. "$Python = Get-Command zcode-python -ErrorAction SilentlyContinue\n",
  33. );
  34. await writeFile(
  35. join(root, "documents-plugin/scripts/helper.py"),
  36. '#!/usr/bin/env python3\nimport subprocess, sys\nsubprocess.run([sys.executable, "-m", "pip", "install", "example"])\n',
  37. );
  38. await expect(exec(process.execPath, [checker, root])).resolves.toMatchObject({ stderr: "" });
  39. await writeFile(doc, "```bash\npython3 task.py\n```\n");
  40. await expect(exec(process.execPath, [checker, root])).rejects.toMatchObject({
  41. code: 1,
  42. stderr: expect.stringContaining("SKILL.md:2"),
  43. });
  44. } finally {
  45. await rm(root, { recursive: true, force: true });
  46. }
  47. });
  48. it.each([
  49. ["example.md", "Run `python helper.py`."],
  50. ["example.sh", "result=$(python3 -c 'print(1)')"],
  51. ["example.sh", '"python3" -c "print(1)"'],
  52. ["example.py", 'print("Install: pip install example")'],
  53. ["example.py", 'subprocess.run(["python3", "helper.py"])'],
  54. // 直接执行脚本靠 shebang 落到系统 python3,同样绕过所选解释器。
  55. ["example.md", 'Run `"$DOCX_SCRIPTS/add_toc_placeholders.py" --auto` after generation'],
  56. ["table.md", '| Pivot | `"$XLSX_SKILL_DIR/xlsx.py" pivot` |'],
  57. ["example.sh", 'out=$("$SCRIPT_DIR/helper.py" --json)'],
  58. ])("rejects an unselected command in %s", async (name, content) => {
  59. const root = await mkdtemp(join(tmpdir(), "document-python-gate-"));
  60. try {
  61. for (const plugin of ["documents", "pdf", "presentations", "spreadsheets"]) {
  62. await mkdir(join(root, `${plugin}-plugin`, "skills"), { recursive: true });
  63. await mkdir(join(root, `${plugin}-plugin`, "scripts"));
  64. }
  65. await writeFile(join(root, "documents-plugin/scripts", name), content);
  66. await expect(exec(process.execPath, [checker, root])).rejects.toHaveProperty("code", 1);
  67. } finally {
  68. await rm(root, { recursive: true, force: true });
  69. }
  70. });
  71. });