architecture-upstream-admission.test.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import assert from "node:assert/strict";
  2. import { execFile } from "node:child_process";
  3. import { mkdtemp, mkdir, writeFile, rm, rename, chmod } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { promisify } from "node:util";
  7. import { test } from "node:test";
  8. import { addedFilesFromGit, checkArchitecture, loadPolicy } from "../architecture/index.mjs";
  9. const exec = promisify(execFile);
  10. const original = "export const value = 1;\n";
  11. const file = "packages/legacy/imported.ts";
  12. async function git(cwd, ...args) {
  13. return (await exec("git", args, { cwd })).stdout.trim();
  14. }
  15. async function fixture(run) {
  16. const cwd = await mkdtemp(join(tmpdir(), "architecture-upstream-"));
  17. try {
  18. await git(cwd, "init", "-b", "main");
  19. await git(cwd, "config", "user.name", "Architecture Test");
  20. await git(cwd, "config", "user.email", "architecture@example.invalid");
  21. await git(cwd, "config", "commit.gpgsign", "false");
  22. await git(cwd, "config", "core.filemode", "true");
  23. await git(cwd, "config", "core.autocrlf", "false");
  24. const commit = async () => {
  25. await git(cwd, "add", ".");
  26. await git(cwd, "-c", "core.hooksPath=", "commit", "--allow-empty", "-m", "fixture");
  27. return git(cwd, "rev-parse", "HEAD");
  28. };
  29. const previousHead = await commit();
  30. await mkdir(join(cwd, "packages/legacy"), { recursive: true });
  31. await writeFile(join(cwd, file), original);
  32. const upstream = await commit();
  33. const acceptedUpstreamCommits = [
  34. { commit: upstream, previousHead, reason: "fixture admission" },
  35. ];
  36. const collect = (base = previousHead) =>
  37. addedFilesFromGit(cwd, { base, acceptedUpstreamCommits });
  38. await run({ cwd, previousHead, upstream, commit, collect, acceptedUpstreamCommits });
  39. } finally {
  40. await rm(cwd, { recursive: true, force: true });
  41. }
  42. }
  43. test("原样上游存量可接纳,本地新增仍受管", async () => {
  44. await fixture(async ({ cwd, collect }) => {
  45. await writeFile(join(cwd, "packages/legacy/local.ts"), original);
  46. assert.deepEqual(await collect(), ["packages/legacy/local.ts"]);
  47. });
  48. });
  49. for (const state of ["committed", "staged", "unstaged", "staged-then-restored"]) {
  50. test(`${state}:改过的上游文件仍须登记`, async () => {
  51. await fixture(async ({ cwd, collect, commit }) => {
  52. await writeFile(join(cwd, file), "export const value = 2;\n");
  53. if (state === "committed") await commit();
  54. if (state.startsWith("staged")) await git(cwd, "add", file);
  55. if (state === "staged-then-restored") await writeFile(join(cwd, file), original);
  56. assert.deepEqual(await collect(), [file]);
  57. });
  58. });
  59. }
  60. test("首次推送后的后续修改仍要求模块登记", async () => {
  61. await fixture(async ({ cwd, upstream, collect }) => {
  62. await writeFile(join(cwd, file), "export const value = 2;\n");
  63. assert.deepEqual(await collect(upstream), [file]);
  64. });
  65. });
  66. test("再次合并指定上游原样更新的存量文件,仍接纳原样但拒绝本地修改", async () => {
  67. await fixture(async ({ cwd, upstream, collect, commit, acceptedUpstreamCommits }) => {
  68. await writeFile(join(cwd, file), "export const value = 2;\n");
  69. const updated = await commit();
  70. acceptedUpstreamCommits.push({
  71. commit: updated,
  72. previousHead: upstream,
  73. reason: "upstream update",
  74. });
  75. assert.deepEqual(await collect(upstream), []);
  76. await writeFile(join(cwd, file), "export const value = 3;\n");
  77. assert.deepEqual(await collect(upstream), [file]);
  78. });
  79. });
  80. test("同内容的重命名与重新生成的未跟踪路径不享受准入", async () => {
  81. await fixture(async ({ cwd, collect }) => {
  82. const moved = "packages/legacy/renamed.ts";
  83. await rename(join(cwd, file), join(cwd, moved));
  84. await git(cwd, "add", ".");
  85. assert.deepEqual(await collect(), [moved]);
  86. await writeFile(join(cwd, file), original);
  87. assert.deepEqual((await collect()).sort(), [file, moved].sort());
  88. });
  89. });
  90. test("文件模式变化不能当作原样上游", { skip: process.platform === "win32" }, async () => {
  91. await fixture(async ({ cwd, collect }) => {
  92. await chmod(join(cwd, file), 0o755);
  93. assert.deepEqual(await collect(), [file]);
  94. });
  95. });
  96. test("不允许用未合入或不存在的提交接纳文件", async () => {
  97. await fixture(async ({ cwd, previousHead, upstream, collect, acceptedUpstreamCommits }) => {
  98. await git(cwd, "checkout", "--detach", previousHead);
  99. await assert.rejects(collect(), /ancestor|祖先|合入/);
  100. await git(cwd, "checkout", "--detach", upstream);
  101. acceptedUpstreamCommits[0].commit = "f".repeat(40);
  102. await assert.rejects(collect(), /commit|提交/);
  103. });
  104. });
  105. test("未提交的当前合并可先校验,修改上游文件仍不能接纳", async () => {
  106. await fixture(async ({ cwd, previousHead, upstream, collect }) => {
  107. await git(cwd, "checkout", "--detach", previousHead);
  108. await git(cwd, "merge", "--no-commit", "--no-ff", upstream);
  109. assert.deepEqual(await collect(), []);
  110. await writeFile(join(cwd, file), "export const value = 2;\n");
  111. assert.deepEqual(await collect(), [file]);
  112. await git(cwd, "restore", file);
  113. await git(cwd, "merge", "--abort");
  114. await assert.rejects(collect(), /ancestor|祖先|合入/);
  115. });
  116. });
  117. test("准入不豁免 managed 模块的结构规则", async () => {
  118. await fixture(async ({ cwd, collect }) => {
  119. await writeFile(
  120. join(cwd, "architecture-policy.yaml"),
  121. `version: 1
  122. modules:
  123. - id: legacy
  124. roots: [packages/legacy]
  125. managed: true
  126. requires: []
  127. global:
  128. maxFileLines: 1
  129. `,
  130. );
  131. const result = await checkArchitecture({ cwd, addedFiles: await collect() });
  132. assert.ok(result.newViolations.some((item) => item.rule === "max-file-lines"));
  133. assert.ok(result.newViolations.some((item) => item.rule === "missing-module-artifact"));
  134. });
  135. });
  136. test("策略只接受完整固定 SHA 和非空理由", async () => {
  137. await fixture(async ({ cwd, acceptedUpstreamCommits }) => {
  138. for (const patch of [{ commit: "main" }, { previousHead: "HEAD" }, { reason: " " }]) {
  139. await writeFile(
  140. join(cwd, "architecture-policy.yaml"),
  141. JSON.stringify({
  142. version: 1,
  143. modules: [],
  144. global: { acceptedUpstreamCommits: [{ ...acceptedUpstreamCommits[0], ...patch }] },
  145. }),
  146. );
  147. await assert.rejects(loadPolicy(cwd), /acceptedUpstreamCommits/);
  148. }
  149. });
  150. });