lintStagedConfig.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { readFile, rm } from "node:fs/promises";
  2. import { dirname, resolve } from "node:path";
  3. import { tmpdir } from "node:os";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import config from "../../../lint-staged.config.mjs";
  6. const manifests: string[] = [];
  7. afterEach(async () => {
  8. await Promise.all(manifests.splice(0).map((path) => rm(path, { force: true })));
  9. });
  10. async function manifestFor(files: string[]) {
  11. const callbacks = Object.values(config);
  12. expect(callbacks).toHaveLength(1);
  13. const command = await callbacks[0]!(files);
  14. expect(typeof command).toBe("string");
  15. const path = String(command)
  16. .match(/--files-manifest\s+(.+)$/u)![1]!
  17. .replace(/^['"]|['"]$/gu, "");
  18. manifests.push(path);
  19. return { command, path, manifest: JSON.parse(await readFile(path, "utf8")) };
  20. }
  21. describe("lint-staged affected 入口", () => {
  22. it("源码、测试和重复输入进入同一清单,由选择器统一去重执行", async () => {
  23. const files = ["packages/ui/src/a.ts", "packages/ui/test/a.test.ts", "packages/ui/src/a.ts"];
  24. const { manifest, path } = await manifestFor(files);
  25. expect(manifest).toEqual({ mode: "affected", files: files.slice(0, 2) });
  26. expect(dirname(path)).toBe(resolve(tmpdir()));
  27. });
  28. it("文件名中的 Windows shell 字符只存在于 JSON,不拼进命令", async () => {
  29. const file = "packages/ui/src/foo%PATH% caret^ amp& paren(x).ts";
  30. const { command, manifest } = await manifestFor([file]);
  31. expect(command).not.toContain(file);
  32. expect(manifest.files).toEqual([file]);
  33. });
  34. it("空清单和 E2E-only 不启动根单测", async () => {
  35. const callback = Object.values(config)[0]!;
  36. expect(await callback([])).toEqual([]);
  37. expect(await callback(["packages\\desktop\\test\\e2e\\example.test.ts"])).toEqual([]);
  38. });
  39. });