workflow-git-world-read-real.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * 真 git 冒烟:用**本仓库**的实际输出跑一遍 porcelain-v2 / 路径列表 / log 解析器。
  3. *
  4. * 为什么值得单独有这么一个用例:fake 端口能证明"给定这段文本,解析出这个结构",但证明不了
  5. * "git 真的会输出这段文本"。这份实现里有三条假设**只能**由真 git falsify,而且每一条都已经
  6. * 在写代码的过程中被它纠正过一次:
  7. *
  8. * 1. `-z` 下 porcelain 的 rename 项占**两段**(NUL 分隔,不是 TAB)。
  9. * 2. `-z` 让 `status --porcelain=v2` 输出**仓库根相对**路径——不加 `-z` 时它是 cwd 相对的,
  10. * 而且没有任何 flag/config 能在 `-z` 下把它改回来。这一条直接决定了"基准统一在线上取
  11. * 根相对、由我们剥一次前缀"这个设计。
  12. * 3. `ls-files --others` 默认是 **cwd 相对**,所以必须补 `--full-name` 才能与上面两条对齐。
  13. *
  14. * 断言只钉**形状**与**一致性**,不钉内容:工作树的干净程度、分支名、commit 数都随环境变化。
  15. * 形状足够了——偏移错了路径就不再是路径(会带上 hash 前缀或整段错位),基准错了三条命令之间
  16. * 就会不一致(这正是第 2、3 条要防的那个 bug)。
  17. *
  18. * 不在 git 仓库里(或没有 git)时跳过而不失败:这是一条环境依赖的补充证据,不是门禁。
  19. */
  20. import { execFileSync } from "node:child_process";
  21. import { describe, expect, it } from "vitest";
  22. import {
  23. GIT_LOG_PRETTY,
  24. GIT_SHOW_PREFIX_ARGV,
  25. GIT_STATUS_ARGV,
  26. GIT_UNTRACKED_ARGV,
  27. gitChangedFilesPlan,
  28. parseGitLog,
  29. parseGitPathList,
  30. parseGitShowPrefix,
  31. parseGitStatusPorcelainV2,
  32. } from "../src/app/workflow-git-world-read.js";
  33. /** 跑一条 git;不可用(无 git / 非仓库)时返回 undefined 让用例跳过。 */
  34. function git(args: readonly string[]): string | undefined {
  35. try {
  36. return execFileSync("git", [...args], {
  37. cwd: process.cwd(),
  38. encoding: "utf8",
  39. stdio: ["ignore", "pipe", "ignore"],
  40. maxBuffer: 32 * 1024 * 1024,
  41. });
  42. } catch {
  43. return undefined;
  44. }
  45. }
  46. const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}:\d{2})$/;
  47. /** 交出去的路径必须是**工作区相对**的:不越界、不带仓库根前缀的残留。 */
  48. function expectWorkspaceRelative(path: string): void {
  49. expect(path.length).toBeGreaterThan(0);
  50. // 越界的路径是这套收束要防的那件事——脚本会拿它去 files.read。
  51. expect(path.startsWith("../")).toBe(false);
  52. expect(path.startsWith("/")).toBe(false);
  53. expect(path.split("/").includes("..")).toBe(false);
  54. // 路径不该以定长字段的残留开头:XY 位、mode、或一段 40 位 hash。
  55. expect(path).not.toMatch(/^[.MADRCU]{2}\s/);
  56. expect(path).not.toMatch(/^\d{6}\s/);
  57. expect(path).not.toMatch(/^[0-9a-f]{40}\s/);
  58. }
  59. describe("real git output — parser smoke", () => {
  60. it("parses this repository's actual porcelain-v2 -z status", () => {
  61. const prefixRaw = git(GIT_SHOW_PREFIX_ARGV);
  62. const raw = git(GIT_STATUS_ARGV);
  63. if (prefixRaw === undefined || raw === undefined) return;
  64. const prefix = parseGitShowPrefix(prefixRaw);
  65. const status = parseGitStatusPorcelainV2(raw, prefix);
  66. // 分支名要么缺席(detached),要么是一个非空且不含空格的名字。偏移读错时这里会拿到
  67. // 一段带 oid 的残句。
  68. if (status.branch !== undefined) {
  69. expect(status.branch.length).toBeGreaterThan(0);
  70. expect(status.branch).not.toContain(" ");
  71. }
  72. for (const bucket of [status.staged, status.unstaged, status.untracked]) {
  73. expect(Array.isArray(bucket)).toBe(true);
  74. for (const path of bucket) expectWorkspaceRelative(path);
  75. // 每个桶内部去重且已排序(journal 存的是这个值,必须只依赖内容)。
  76. expect(bucket).toEqual([...new Set(bucket)].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)));
  77. }
  78. // clean 与三个桶必须一致——它是从桶算出来的,这条断言防的是有人改成别的来源。
  79. const empty =
  80. status.staged.length === 0 && status.unstaged.length === 0 && status.untracked.length === 0;
  81. expect(status.clean).toBe(empty);
  82. });
  83. it("agrees on one path base across all three path-producing commands", () => {
  84. // 这是第 2、3 条假设的回归防线,也是这份冒烟最有价值的一条:三条命令的 git 默认基准各不
  85. // 相同(status -z 根相对、diff 根相对、ls-files cwd 相对),所以只要有一条的 argv 少了
  86. // 对齐用的 flag,它交出的路径就会与另外两条不在同一个基准上。这里断言它们的**并集**
  87. // 整体都是工作区相对的——基准混搭时,走错基准的那一批会立刻违反它。
  88. const prefixRaw = git(GIT_SHOW_PREFIX_ARGV);
  89. if (prefixRaw === undefined) return;
  90. const prefix = parseGitShowPrefix(prefixRaw);
  91. const plans = gitChangedFilesPlan(undefined);
  92. // 无 base 时是"已跟踪 diff + 未跟踪 ls-files"两条命令,且第二条恰是那份固定 argv。
  93. expect(plans).toHaveLength(2);
  94. expect(plans[1]?.argv).toEqual(GIT_UNTRACKED_ARGV);
  95. for (const plan of plans) {
  96. const raw = git(plan.argv);
  97. if (raw === undefined) return;
  98. for (const path of parseGitPathList(raw, prefix)) expectWorkspaceRelative(path);
  99. }
  100. });
  101. it("parses this repository's actual log records", () => {
  102. const raw = git(["log", "-n3", GIT_LOG_PRETTY]);
  103. if (raw === undefined) return;
  104. const commits = parseGitLog(raw);
  105. expect(commits.length).toBeGreaterThan(0);
  106. for (const commit of commits) {
  107. // 全长 hash(`%H`),不是缩写——脚本可能拿它去比较。
  108. expect(commit.hash).toMatch(/^[0-9a-f]{40}$/);
  109. expect(commit.subject.length).toBeGreaterThan(0);
  110. // subject 里绝不该出现换行或 NUL:前者会破坏按行切分,后者会破坏按字段切分。
  111. // `%s` 按定义是单行,这也是 log 这一条不需要 `-z` 的理由。
  112. expect(commit.subject).not.toContain("\n");
  113. expect(commit.subject).not.toContain("�");
  114. expect(commit.author.length).toBeGreaterThan(0);
  115. // `%aI` 是严格 ISO 8601。日期落到 author 位(偏移错一格)会在这里失败。
  116. expect(commit.date).toMatch(ISO_8601);
  117. }
  118. });
  119. });