Skip to content

Commit 4b940f6

Browse files
committed
fix(test): avoid scanning live shard roots
1 parent 41b4eb9 commit 4b940f6

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

scripts/test-live-shard.mjs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
import { spawnSync } from "node:child_process";
23
import fs from "node:fs";
34
import path from "node:path";
45
import { fileURLToPath } from "node:url";
@@ -64,13 +65,83 @@ function walkFiles(rootDir) {
6465
}
6566

6667
export function collectAllLiveTestFiles(repoRoot = process.cwd()) {
68+
const externalFiles = listExternalLiveTestFiles(repoRoot);
69+
if (externalFiles) {
70+
return externalFiles;
71+
}
6772
return ["src", "test", "extensions"]
6873
.flatMap((dir) => walkFiles(path.join(repoRoot, dir)))
6974
.map((file) => path.relative(repoRoot, file).split(path.sep).join("/"))
7075
.filter((file) => file.endsWith(LIVE_TEST_SUFFIX))
7176
.toSorted((a, b) => a.localeCompare(b));
7277
}
7378

79+
function listExternalLiveTestFiles(repoRoot) {
80+
return listGitLiveTestFiles(repoRoot) ?? listFindLiveTestFiles(repoRoot);
81+
}
82+
83+
function listGitLiveTestFiles(repoRoot) {
84+
const result = spawnSync("git", ["ls-files", "--", "src", "test", "extensions"], {
85+
cwd: repoRoot,
86+
encoding: "utf8",
87+
maxBuffer: 1024 * 1024 * 4,
88+
stdio: ["ignore", "pipe", "ignore"],
89+
});
90+
if (result.status !== 0) {
91+
return null;
92+
}
93+
return result.stdout
94+
.split("\n")
95+
.map((line) => line.trim())
96+
.filter((file) => file.endsWith(LIVE_TEST_SUFFIX))
97+
.toSorted((a, b) => a.localeCompare(b));
98+
}
99+
100+
function listFindLiveTestFiles(repoRoot) {
101+
const roots = ["src", "test", "extensions"].map((dir) => path.join(repoRoot, dir));
102+
const result = spawnSync(
103+
"find",
104+
[
105+
...roots,
106+
"(",
107+
"-name",
108+
"node_modules",
109+
"-o",
110+
"-name",
111+
"dist",
112+
"-o",
113+
"-name",
114+
"vendor",
115+
"-o",
116+
"-name",
117+
"fixtures",
118+
")",
119+
"-prune",
120+
"-o",
121+
"-type",
122+
"f",
123+
"-name",
124+
`*${LIVE_TEST_SUFFIX}`,
125+
"-print",
126+
],
127+
{
128+
cwd: repoRoot,
129+
encoding: "utf8",
130+
maxBuffer: 1024 * 1024 * 4,
131+
stdio: ["ignore", "pipe", "ignore"],
132+
},
133+
);
134+
if (result.status !== 0) {
135+
return null;
136+
}
137+
return result.stdout
138+
.split("\n")
139+
.map((line) => line.trim())
140+
.filter((file) => file.length > 0)
141+
.map((file) => path.relative(repoRoot, file).split(path.sep).join("/"))
142+
.toSorted((a, b) => a.localeCompare(b));
143+
}
144+
74145
function extensionKey(file) {
75146
const relative = file.slice("extensions/".length);
76147
return relative.split("/", 1)[0]?.toLowerCase() ?? "";

test/scripts/test-live-shard.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { readFileSync } from "node:fs";
2-
import { describe, expect, it } from "vitest";
1+
import fs, { readFileSync } from "node:fs";
2+
import { describe, expect, it, vi } from "vitest";
33
import {
44
LIVE_TEST_SHARDS,
55
RELEASE_LIVE_TEST_SHARDS,
@@ -10,6 +10,19 @@ import {
1010
describe("scripts/test-live-shard", () => {
1111
const allFiles = collectAllLiveTestFiles();
1212

13+
it("discovers live tests without scanning source roots in-process", () => {
14+
const readDir = vi.spyOn(fs, "readdirSync");
15+
try {
16+
const files = collectAllLiveTestFiles();
17+
18+
expect(files.length).toBeGreaterThan(0);
19+
expect(files.every((file) => file.endsWith(".live.test.ts"))).toBe(true);
20+
expect(readDir).not.toHaveBeenCalled();
21+
} finally {
22+
readDir.mockRestore();
23+
}
24+
});
25+
1326
it("covers every native live test and tracks provider-filtered release fanout", () => {
1427
const selected = RELEASE_LIVE_TEST_SHARDS.flatMap((shard) =>
1528
selectLiveShardFiles(shard, allFiles).map((file) => ({ file, shard })),

0 commit comments

Comments
 (0)