
























@@ -1,11 +1,49 @@
1+import { spawnSync } from "node:child_process";
12import fs from "node:fs";
23import path from "node:path";
3-import { describe, expect, it } from "vitest";
4+import { describe, expect, it, vi } from "vitest";
4556const repoRoot = path.resolve(import.meta.dirname, "../../..");
67const extensionsRoot = path.join(repoRoot, "extensions");
789+function isProductionSourcePath(filePath: string): boolean {
10+const normalized = filePath.split(path.sep).join("/");
11+if (!/\.(?:ts|tsx|js|mjs|cjs)$/.test(normalized)) {
12+return false;
13+}
14+if (/(^|\/)(?:node_modules|dist|\.[^/]+)(?:\/|$)/.test(normalized)) {
15+return false;
16+}
17+return !/(\.test\.|\.spec\.|\/__tests__\/|\/test-support\/)/.test(normalized);
18+}
19+20+function listGitProductionSourceFiles(root: string): string[] | null {
21+const relativeRoot = path.relative(repoRoot, root).split(path.sep).join("/");
22+if (!relativeRoot || relativeRoot.startsWith("..") || path.isAbsolute(relativeRoot)) {
23+return null;
24+}
25+const result = spawnSync("git", ["ls-files", "--", relativeRoot], {
26+cwd: repoRoot,
27+encoding: "utf8",
28+stdio: ["ignore", "pipe", "ignore"],
29+});
30+if (result.status !== 0) {
31+return null;
32+}
33+return result.stdout
34+.split("\n")
35+.map((line) => line.trim().replaceAll("\\", "/"))
36+.filter((line) => line.length > 0 && isProductionSourcePath(line))
37+.map((line) => path.join(repoRoot, ...line.split("/")))
38+.toSorted();
39+}
40+841function walkProductionSourceFiles(dir: string): string[] {
42+const gitFiles = listGitProductionSourceFiles(dir);
43+if (gitFiles) {
44+return gitFiles;
45+}
46+947const files: string[] = [];
1048for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
1149if (entry.name === "node_modules" || entry.name === "dist" || entry.name.startsWith(".")) {
@@ -16,11 +54,7 @@ function walkProductionSourceFiles(dir: string): string[] {
1654files.push(...walkProductionSourceFiles(entryPath));
1755continue;
1856}
19-const normalized = entryPath.split(path.sep).join("/");
20-if (!/\.(?:ts|tsx|js|mjs|cjs)$/.test(normalized)) {
21-continue;
22-}
23-if (/(\.test\.|\.spec\.|\/__tests__\/|\/test-support\/)/.test(normalized)) {
57+if (!isProductionSourcePath(entryPath)) {
2458continue;
2559}
2660files.push(entryPath);
@@ -108,6 +142,22 @@ function lineNumberFor(source: string, offset: number): number {
108142}
109143110144describe("bundled provider catalog deprecation guard", () => {
145+it("lists production extension sources from git without walking extension roots", () => {
146+const readDir = vi.spyOn(fs, "readdirSync");
147+try {
148+const files = walkProductionSourceFiles(extensionsRoot);
149+150+expect(files.length).toBeGreaterThan(0);
151+expect(files.every((file) => path.relative(repoRoot, file).startsWith("extensions"))).toBe(
152+true,
153+);
154+expect(files.some((file) => file.endsWith(".test.ts"))).toBe(false);
155+expect(readDir).not.toHaveBeenCalled();
156+} finally {
157+readDir.mockRestore();
158+}
159+});
160+111161it("keeps bundled provider plugins off the deprecated discovery hook", () => {
112162const offenders: string[] = [];
113163for (const filePath of walkProductionSourceFiles(extensionsRoot)) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。