mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-17 13:00:48 +00:00
186 lines
5.3 KiB
JavaScript
186 lines
5.3 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { spawn } from "node:child_process";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
import { channelTestRoots } from "../vitest.channel-paths.mjs";
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const repoRoot = path.resolve(__dirname, "..");
|
|
const pnpm = "pnpm";
|
|
|
|
function normalizeRelative(inputPath) {
|
|
return inputPath.split(path.sep).join("/");
|
|
}
|
|
|
|
function isTestFile(filePath) {
|
|
return filePath.endsWith(".test.ts") || filePath.endsWith(".test.tsx");
|
|
}
|
|
|
|
function collectTestFiles(rootPath) {
|
|
const results = [];
|
|
const stack = [rootPath];
|
|
|
|
while (stack.length > 0) {
|
|
const current = stack.pop();
|
|
if (!current || !fs.existsSync(current)) {
|
|
continue;
|
|
}
|
|
for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
|
|
const fullPath = path.join(current, entry.name);
|
|
if (entry.isDirectory()) {
|
|
if (entry.name === "node_modules" || entry.name === "dist") {
|
|
continue;
|
|
}
|
|
stack.push(fullPath);
|
|
continue;
|
|
}
|
|
if (entry.isFile() && isTestFile(fullPath)) {
|
|
results.push(fullPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
return results.toSorted((left, right) => left.localeCompare(right));
|
|
}
|
|
|
|
function resolveExtensionDirectory(targetArg, cwd = process.cwd()) {
|
|
if (targetArg) {
|
|
const asGiven = path.resolve(cwd, targetArg);
|
|
if (fs.existsSync(path.join(asGiven, "package.json"))) {
|
|
return asGiven;
|
|
}
|
|
|
|
const byName = path.join(repoRoot, "extensions", targetArg);
|
|
if (fs.existsSync(path.join(byName, "package.json"))) {
|
|
return byName;
|
|
}
|
|
|
|
throw new Error(
|
|
`Unknown extension target "${targetArg}". Use an extension name like "slack" or a path under extensions/.`,
|
|
);
|
|
}
|
|
|
|
let current = cwd;
|
|
while (true) {
|
|
if (
|
|
normalizeRelative(path.relative(repoRoot, current)).startsWith("extensions/") &&
|
|
fs.existsSync(path.join(current, "package.json"))
|
|
) {
|
|
return current;
|
|
}
|
|
const parent = path.dirname(current);
|
|
if (parent === current) {
|
|
break;
|
|
}
|
|
current = parent;
|
|
}
|
|
|
|
throw new Error(
|
|
"No extension target provided, and current working directory is not inside extensions/.",
|
|
);
|
|
}
|
|
|
|
export function resolveExtensionTestPlan(params = {}) {
|
|
const cwd = params.cwd ?? process.cwd();
|
|
const targetArg = params.targetArg;
|
|
const extensionDir = resolveExtensionDirectory(targetArg, cwd);
|
|
const extensionId = path.basename(extensionDir);
|
|
const relativeExtensionDir = normalizeRelative(path.relative(repoRoot, extensionDir));
|
|
|
|
const roots = [relativeExtensionDir];
|
|
const pairedCoreRoot = path.join(repoRoot, "src", extensionId);
|
|
if (fs.existsSync(pairedCoreRoot)) {
|
|
const pairedRelativeRoot = normalizeRelative(path.relative(repoRoot, pairedCoreRoot));
|
|
if (collectTestFiles(pairedCoreRoot).length > 0) {
|
|
roots.push(pairedRelativeRoot);
|
|
}
|
|
}
|
|
|
|
const usesChannelConfig = roots.some((root) => channelTestRoots.includes(root));
|
|
const config = usesChannelConfig ? "vitest.channels.config.ts" : "vitest.extensions.config.ts";
|
|
const testFiles = roots.flatMap((root) => collectTestFiles(path.join(repoRoot, root)));
|
|
|
|
return {
|
|
config,
|
|
extensionDir: relativeExtensionDir,
|
|
extensionId,
|
|
roots,
|
|
testFiles: testFiles.map((filePath) => normalizeRelative(path.relative(repoRoot, filePath))),
|
|
};
|
|
}
|
|
|
|
function printUsage() {
|
|
console.error("Usage: pnpm test:extension <extension-name|path> [vitest args...]");
|
|
console.error(" node scripts/test-extension.mjs [extension-name|path] [vitest args...]");
|
|
}
|
|
|
|
async function run() {
|
|
const rawArgs = process.argv.slice(2);
|
|
const dryRun = rawArgs.includes("--dry-run");
|
|
const json = rawArgs.includes("--json");
|
|
const args = rawArgs.filter((arg) => arg !== "--" && arg !== "--dry-run" && arg !== "--json");
|
|
|
|
let targetArg;
|
|
if (args[0] && !args[0].startsWith("-")) {
|
|
targetArg = args.shift();
|
|
}
|
|
|
|
let plan;
|
|
try {
|
|
plan = resolveExtensionTestPlan({ cwd: process.cwd(), targetArg });
|
|
} catch (error) {
|
|
printUsage();
|
|
console.error(error instanceof Error ? error.message : String(error));
|
|
process.exit(1);
|
|
}
|
|
|
|
if (plan.testFiles.length === 0) {
|
|
console.error(`No tests found for ${plan.extensionDir}.`);
|
|
process.exit(1);
|
|
}
|
|
|
|
if (dryRun) {
|
|
if (json) {
|
|
process.stdout.write(`${JSON.stringify(plan, null, 2)}\n`);
|
|
} else {
|
|
console.log(`[test-extension] ${plan.extensionId}`);
|
|
console.log(`config: ${plan.config}`);
|
|
console.log(`roots: ${plan.roots.join(", ")}`);
|
|
console.log(`tests: ${plan.testFiles.length}`);
|
|
}
|
|
return;
|
|
}
|
|
|
|
console.log(
|
|
`[test-extension] Running ${plan.testFiles.length} test files for ${plan.extensionId} with ${plan.config}`,
|
|
);
|
|
|
|
const child = spawn(
|
|
pnpm,
|
|
["exec", "vitest", "run", "--config", plan.config, ...plan.testFiles, ...args],
|
|
{
|
|
cwd: repoRoot,
|
|
stdio: "inherit",
|
|
shell: process.platform === "win32",
|
|
env: process.env,
|
|
},
|
|
);
|
|
|
|
child.on("exit", (code, signal) => {
|
|
if (signal) {
|
|
process.kill(process.pid, signal);
|
|
return;
|
|
}
|
|
process.exit(code ?? 1);
|
|
});
|
|
}
|
|
|
|
const entryHref = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : "";
|
|
|
|
if (import.meta.url === entryHref) {
|
|
await run();
|
|
}
|