Skip to content

Commit efabae2

Browse files
committed
fix(test): avoid walking unit-fast candidate roots
1 parent a1e208e commit efabae2

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

test/vitest-unit-fast-config.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { spawnSync } from "node:child_process";
12
import { describe, expect, it } from "vitest";
23
import { createCommandsLightVitestConfig } from "./vitest/vitest.commands-light.config.ts";
34
import { createPluginSdkLightVitestConfig } from "./vitest/vitest.plugin-sdk-light.config.ts";
@@ -49,6 +50,31 @@ function collectUnroutedForcedFiles(
4950
}
5051

5152
describe("unit-fast vitest lane", () => {
53+
it("loads the config without recursively walking repo roots", () => {
54+
const script = `
55+
import fs from "node:fs";
56+
let readdirSyncCalls = 0;
57+
const originalReaddirSync = fs.readdirSync;
58+
fs.readdirSync = function patchedReaddirSync(...args) {
59+
readdirSyncCalls += 1;
60+
return originalReaddirSync.apply(this, args);
61+
};
62+
await import("./test/vitest/vitest.unit-fast.config.ts?io-probe=" + Date.now());
63+
console.log(readdirSyncCalls);
64+
`;
65+
const result = spawnSync(
66+
process.execPath,
67+
["--import", "tsx", "--input-type=module", "-e", script],
68+
{
69+
cwd: process.cwd(),
70+
encoding: "utf8",
71+
},
72+
);
73+
74+
expect(result.status, result.stderr).toBe(0);
75+
expect(Number(result.stdout.trim())).toBeLessThan(20);
76+
});
77+
5278
it("runs cache-friendly tests without the reset-heavy runner or runtime setup", () => {
5379
const config = createUnitFastVitestConfig({});
5480
const testConfig = requireTestConfig(config);

test/vitest/vitest.unit-fast-paths.mjs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { spawnSync } from "node:child_process";
12
import fs from "node:fs";
23
import path from "node:path";
34
import {
@@ -309,15 +310,32 @@ function walkFiles(directory, files = []) {
309310

310311
const walkedTestFilesByCwd = new Map();
311312

313+
function collectRepoTestFilesFromGit(cwd) {
314+
const result = spawnSync("git", ["ls-files", "--", "src", "packages", "test"], {
315+
cwd,
316+
encoding: "utf8",
317+
stdio: ["ignore", "pipe", "ignore"],
318+
});
319+
if (result.status !== 0) {
320+
return null;
321+
}
322+
return result.stdout
323+
.split("\n")
324+
.map((file) => normalizeRepoPath(file.trim()))
325+
.filter((file) => file.endsWith(".test.ts"));
326+
}
327+
312328
function collectRepoTestFiles(cwd) {
313329
const normalizedCwd = normalizeRepoPath(cwd);
314330
const cached = walkedTestFilesByCwd.get(normalizedCwd);
315331
if (cached) {
316332
return cached;
317333
}
318-
const files = ["src", "packages", "test"]
319-
.flatMap((directory) => walkFiles(path.join(cwd, directory)))
320-
.map((file) => normalizeRepoPath(path.relative(cwd, file)));
334+
const files =
335+
collectRepoTestFilesFromGit(cwd) ??
336+
["src", "packages", "test"]
337+
.flatMap((directory) => walkFiles(path.join(cwd, directory)))
338+
.map((file) => normalizeRepoPath(path.relative(cwd, file)));
321339
walkedTestFilesByCwd.set(normalizedCwd, files);
322340
return files;
323341
}

0 commit comments

Comments
 (0)