Skip to content

Commit 70d7fb5

Browse files
authored
refactor(tooling): remove test-only production helper exports (#128770)
1 parent 3dec4e3 commit 70d7fb5

7 files changed

Lines changed: 29 additions & 70 deletions

‎scripts/check-deadcode-unused-files.mts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import { fileURLToPath } from "node:url";
44
import {
55
isLikelyRepoFilePath,
6-
KNIP_MAX_BUFFER_BYTES,
76
runKnip,
87
type KnipRunResult,
98
uniqueSorted,
109
} from "./deadcode-knip-runner.mts";
1110

12-
export { KNIP_MAX_BUFFER_BYTES };
13-
1411
const KNIP_COMMON_ARGS = ["--no-progress", "--reporter", "compact", "--files", "--no-config-hints"];
1512

1613
const KNIP_SCANS = [
@@ -53,14 +50,6 @@ export function parseKnipCompactUnusedFiles(output: string) {
5350
return uniqueSorted(files);
5451
}
5552

56-
/** Runs Knip and returns parsed unused-file results. */
57-
export async function runKnipUnusedFiles(params: NonNullable<Parameters<typeof runKnip>[1]> = {}) {
58-
return await runKnip([...KNIP_SCANS[0].args, ...KNIP_COMMON_ARGS], {
59-
...params,
60-
scanName: KNIP_SCANS[0].name,
61-
});
62-
}
63-
6453
/** Rejects every unused file reported by Knip. */
6554
export function checkUnusedFiles(output: string) {
6655
const files = parseKnipCompactUnusedFiles(output);

‎scripts/lib/extension-test-plan.mts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,6 @@ function listFilesystemTestFiles(rootPath: string) {
240240
return files.toSorted((left, right) => left.localeCompare(right));
241241
}
242242

243-
/** List tracked or filesystem-discovered test files for extension roots. */
244-
export function listTrackedTestFilesForRoots(roots: string[]) {
245-
const files = [];
246-
for (const root of roots) {
247-
const rootPath = path.join(repoRoot, root);
248-
const trackedFiles = listTrackedTestFiles(rootPath) ?? listFilesystemTestFiles(rootPath);
249-
files.push(...trackedFiles);
250-
}
251-
return [...new Set(files)].toSorted((left, right) => left.localeCompare(right));
252-
}
253-
254243
/** List working-tree test files for extension roots, including new untracked tests. */
255244
export function listExtensionTestFilesForRoots(roots: string[]) {
256245
const files = roots.flatMap((root) => listFilesystemTestFiles(path.join(repoRoot, root)));

‎scripts/test-report-utils.mts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ export function readJsonFile(filePath: fs.PathOrFileDescriptor) {
3232
return parsed;
3333
}
3434

35-
/**
36-
* Reads a JSON file or returns the provided fallback on failure.
37-
*/
38-
export function tryReadJsonFile(filePath: fs.PathOrFileDescriptor, fallback: unknown) {
39-
try {
40-
return readJsonFile(filePath);
41-
} catch {
42-
return fallback;
43-
}
44-
}
45-
4635
function validateVitestJsonReport(reportPath: fs.PathLike) {
4736
const displayPath = typeof reportPath === "string" ? reportPath : reportPath.toString();
4837
if (!fs.existsSync(reportPath)) {

‎test/scripts/check-deadcode-unused-files.test.ts‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import { describe, expect, it } from "vitest";
99
import {
1010
checkKnipUnusedFileScanResult,
1111
checkUnusedFiles,
12-
KNIP_MAX_BUFFER_BYTES,
1312
parseKnipCompactUnusedFiles,
14-
runKnipUnusedFiles,
1513
} from "../../scripts/check-deadcode-unused-files.mts";
14+
import { KNIP_MAX_BUFFER_BYTES, runKnip } from "../../scripts/deadcode-knip-runner.mts";
1615
import { killPidIfAlive } from "../../src/test-utils/process-tree.js";
1716
import {
1817
isProcessAlive,
@@ -22,6 +21,18 @@ import {
2221
waitForPidFile,
2322
} from "../helpers/process-wait.js";
2423

24+
const KNIP_UNUSED_FILE_ARGS = [
25+
"--config",
26+
"config/knip.config.ts",
27+
"--production",
28+
"--no-progress",
29+
"--reporter",
30+
"compact",
31+
"--files",
32+
"--no-config-hints",
33+
];
34+
const KNIP_UNUSED_FILE_SCAN_NAME = "production unused-file scan";
35+
2536
class FakeKnipProcess extends EventEmitter {
2637
readonly stderr = new EventEmitter();
2738
readonly stdout = new EventEmitter();
@@ -150,7 +161,7 @@ Delete the files or model their real entrypoints in Knip.`,
150161
writeFileSync(pnpmExecPath, "console.log('pnpm');\n", "utf8");
151162

152163
try {
153-
const resultPromise = runKnipUnusedFiles({
164+
const resultPromise = runKnip(KNIP_UNUSED_FILE_ARGS, {
154165
nodeExecPath: "/test-node",
155166
npmExecPath: pnpmExecPath,
156167
spawnCommand(command: string, args: string[], options: unknown) {
@@ -208,7 +219,7 @@ Delete the files or model their real entrypoints in Knip.`,
208219
it("falls back to bare pnpm when no managed pnpm runner is available", async () => {
209220
const calls: unknown[] = [];
210221

211-
const resultPromise = runKnipUnusedFiles({
222+
const resultPromise = runKnip(KNIP_UNUSED_FILE_ARGS, {
212223
env: { PATH: "" },
213224
npmExecPath: "",
214225
platform: "linux",
@@ -266,10 +277,11 @@ Delete the files or model their real entrypoints in Knip.`,
266277
return originalKill(pid, signal as NodeJS.Signals);
267278
}) as typeof process.kill;
268279
try {
269-
const result = await runKnipUnusedFiles({
280+
const result = await runKnip(KNIP_UNUSED_FILE_ARGS, {
270281
heartbeatMs: 1,
271282
killGraceMs: 50,
272283
maxBufferBytes: KNIP_MAX_BUFFER_BYTES,
284+
scanName: KNIP_UNUSED_FILE_SCAN_NAME,
273285
spawnCommand: () => child,
274286
timeoutMs: 5,
275287
writeStatus: (message: string) => statuses.push(message),
@@ -311,7 +323,7 @@ Delete the files or model their real entrypoints in Knip.`,
311323
"setInterval(() => {}, 1000);",
312324
].join("");
313325

314-
const resultPromise = runKnipUnusedFiles({
326+
const resultPromise = runKnip(KNIP_UNUSED_FILE_ARGS, {
315327
env: { ...process.env, OPENCLAW_TEST_CHILD_PID: childPidPath },
316328
killGraceMs: 50,
317329
spawnCommand(_command: string, _args: string[], options: unknown) {
@@ -345,7 +357,7 @@ Delete the files or model their real entrypoints in Knip.`,
345357
const root = mkdtempSync(path.join(os.tmpdir(), "openclaw-knip-parent-signal-"));
346358
const childPidPath = path.join(root, "child.pid");
347359
const readyPath = path.join(root, "child.ready");
348-
const scriptUrl = pathToFileURL(path.resolve("scripts/check-deadcode-unused-files.mts")).href;
360+
const scriptUrl = pathToFileURL(path.resolve("scripts/deadcode-knip-runner.mts")).href;
349361
let childPid = 0;
350362
let runner: ReturnType<typeof spawn> | undefined;
351363

@@ -365,8 +377,8 @@ Delete the files or model their real entrypoints in Knip.`,
365377
].join("");
366378
const runnerScript = [
367379
"import { spawn } from 'node:child_process';",
368-
`import { runKnipUnusedFiles } from ${JSON.stringify(scriptUrl)};`,
369-
"await runKnipUnusedFiles({",
380+
`import { runKnip } from ${JSON.stringify(scriptUrl)};`,
381+
`await runKnip(${JSON.stringify(KNIP_UNUSED_FILE_ARGS)}, {`,
370382
" spawnCommand(_command, _args, options) {",
371383
` return spawn(process.execPath, ['-e', ${JSON.stringify(parentScript)}], options);`,
372384
" },",
@@ -403,7 +415,7 @@ Delete the files or model their real entrypoints in Knip.`,
403415

404416
it("keeps output delivered after process exit but before stdio close", async () => {
405417
const child = new FakeKnipProcess();
406-
const resultPromise = runKnipUnusedFiles({
418+
const resultPromise = runKnip(KNIP_UNUSED_FILE_ARGS, {
407419
spawnCommand: () => child,
408420
writeStatus: () => {},
409421
});
@@ -436,9 +448,10 @@ Delete the files or model their real entrypoints in Knip.`,
436448
return originalKill(pid, signal as NodeJS.Signals);
437449
}) as typeof process.kill;
438450
try {
439-
const resultPromise = runKnipUnusedFiles({
451+
const resultPromise = runKnip(KNIP_UNUSED_FILE_ARGS, {
440452
killGraceMs: 50,
441453
maxBufferBytes: 4,
454+
scanName: KNIP_UNUSED_FILE_SCAN_NAME,
442455
spawnCommand: () => child,
443456
timeoutMs: 1000,
444457
writeStatus: () => {},
@@ -458,7 +471,7 @@ Delete the files or model their real entrypoints in Knip.`,
458471
});
459472

460473
it("reports spawn errors", async () => {
461-
const resultPromise = runKnipUnusedFiles({
474+
const resultPromise = runKnip(KNIP_UNUSED_FILE_ARGS, {
462475
spawnCommand: () => {
463476
const child = new FakeKnipProcess();
464477
queueMicrotask(() =>

‎test/scripts/plugin-prerelease-telegram-shards.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { parse } from "yaml";
77
import {
88
DEFAULT_EXTENSION_TEST_SHARD_COUNT,
99
createExtensionTestShards,
10-
listTrackedTestFilesForRoots,
10+
listExtensionTestFilesForRoots,
1111
splitExtensionTestJobTargets,
1212
} from "../../scripts/lib/extension-test-plan.mts";
1313
import { createVitestRunSpecs } from "../../scripts/test-projects.test-support.mts";
@@ -137,7 +137,7 @@ describe("plugin prerelease Telegram extension shards", () => {
137137
const matrix = runPluginPrereleaseManifest();
138138
const genericRows = matrix.include.filter((row) => row.task === "extensions-batch");
139139
const telegramRows = matrix.include.filter((row) => row.task === "extension-file-shard");
140-
const trackedTelegramTestFiles = listTrackedTestFilesForRoots(["extensions/telegram"]);
140+
const allTelegramTestFiles = listExtensionTestFilesForRoots(["extensions/telegram"]);
141141
const runnableTelegramTestFiles = listTelegramRunnableTestFiles();
142142

143143
expect(genericRows).toHaveLength(DEFAULT_EXTENSION_TEST_SHARD_COUNT);
@@ -173,7 +173,7 @@ describe("plugin prerelease Telegram extension shards", () => {
173173
expect(telegramPartitions.every((partition) => partition.length <= 10)).toBe(true);
174174
expect(Math.max(...telegramPartitions.map((partition) => partition.length))).toBe(10);
175175
expect(
176-
trackedTelegramTestFiles.filter((file) => !runnableTelegramTestFiles.includes(file)).length,
176+
allTelegramTestFiles.filter((file) => !runnableTelegramTestFiles.includes(file)).length,
177177
).toBeGreaterThan(0);
178178

179179
const tempDir = mkdtempSync(join(tmpdir(), "openclaw-plugin-prerelease-telegram-specs-"));

‎test/scripts/test-extension.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
createExtensionTestProcessTargetChunks,
1818
createExtensionTestShards,
1919
listExtensionTestFilesForRoots,
20-
listTrackedTestFilesForRoots,
2120
resolveExtensionBatchPlan,
2221
resolveExtensionTestConfig,
2322
resolveExtensionTestPlan,
@@ -94,7 +93,7 @@ function findExtensionWithoutTests() {
9493
}
9594

9695
function listExtensionTestFiles(extensionId: string): string[] {
97-
return listTrackedTestFilesForRoots([bundledPluginRoot(extensionId)]);
96+
return listExtensionTestFilesForRoots([bundledPluginRoot(extensionId)]);
9897
}
9998

10099
function expectedMatrixTestProcessCount() {

‎test/scripts/test-report-utils.test.ts‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
collectVitestAssertionDurations,
99
collectVitestFileDurations,
1010
normalizeTrackedRepoPath,
11-
tryReadJsonFile,
1211
} from "../../scripts/test-report-utils.mts";
1312

1413
const { spawnSyncMock } = vi.hoisted(() => ({
@@ -91,25 +90,6 @@ describe("scripts/test-report-utils collectVitestAssertionDurations", () => {
9190
});
9291
});
9392

94-
describe("scripts/test-report-utils tryReadJsonFile", () => {
95-
it("returns the fallback when the file is missing", () => {
96-
const missingPath = path.join(os.tmpdir(), `openclaw-missing-${Date.now()}.json`);
97-
98-
expect(tryReadJsonFile(missingPath, { ok: true })).toEqual({ ok: true });
99-
});
100-
101-
it("reads valid JSON files", () => {
102-
const tempPath = path.join(os.tmpdir(), `openclaw-json-${Date.now()}.json`);
103-
fs.writeFileSync(tempPath, JSON.stringify({ ok: true }));
104-
105-
try {
106-
expect(tryReadJsonFile(tempPath, null)).toEqual({ ok: true });
107-
} finally {
108-
fs.unlinkSync(tempPath);
109-
}
110-
});
111-
});
112-
11393
describe("scripts/test-report-utils runVitestJsonReport", () => {
11494
beforeEach(() => {
11595
vi.resetModules();

0 commit comments

Comments
 (0)