mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 05:00:25 +00:00
refactor: simplify test workflow helpers
This commit is contained in:
@@ -28,12 +28,8 @@ 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 = [];
|
||||
function countTestFiles(rootPath) {
|
||||
let total = 0;
|
||||
const stack = [rootPath];
|
||||
|
||||
while (stack.length > 0) {
|
||||
@@ -50,13 +46,13 @@ function collectTestFiles(rootPath) {
|
||||
stack.push(fullPath);
|
||||
continue;
|
||||
}
|
||||
if (entry.isFile() && isTestFile(fullPath)) {
|
||||
results.push(fullPath);
|
||||
if (entry.isFile() && (fullPath.endsWith(".test.ts") || fullPath.endsWith(".test.tsx"))) {
|
||||
total += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results.toSorted((left, right) => left.localeCompare(right));
|
||||
return total;
|
||||
}
|
||||
|
||||
function hasGitCommit(ref) {
|
||||
@@ -224,22 +220,22 @@ export function resolveExtensionTestPlan(params = {}) {
|
||||
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);
|
||||
}
|
||||
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)))
|
||||
.map((filePath) => normalizeRelative(path.relative(repoRoot, filePath)));
|
||||
const testFileCount = roots.reduce(
|
||||
(sum, root) => sum + countTestFiles(path.join(repoRoot, root)),
|
||||
0,
|
||||
);
|
||||
return {
|
||||
config,
|
||||
extensionDir: relativeExtensionDir,
|
||||
extensionId,
|
||||
hasTests: testFileCount > 0,
|
||||
roots,
|
||||
testFiles,
|
||||
testFileCount,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -247,7 +243,7 @@ async function runVitestBatch(params) {
|
||||
return await new Promise((resolve, reject) => {
|
||||
const child = spawn(
|
||||
pnpm,
|
||||
["exec", "vitest", "run", "--config", params.config, ...params.files, ...params.args],
|
||||
["exec", "vitest", "run", "--config", params.config, ...params.targets, ...params.args],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
stdio: "inherit",
|
||||
@@ -382,23 +378,23 @@ async function run() {
|
||||
console.log(`[test-extension] ${plan.extensionId}`);
|
||||
console.log(`config: ${plan.config}`);
|
||||
console.log(`roots: ${plan.roots.join(", ")}`);
|
||||
console.log(`tests: ${plan.testFiles.length}`);
|
||||
console.log(`tests: ${plan.testFileCount}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (plan.testFiles.length === 0) {
|
||||
if (!plan.hasTests) {
|
||||
process.exit(printNoTestsMessage(plan, requireTests));
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[test-extension] Running ${plan.testFiles.length} test files for ${plan.extensionId} with ${plan.config}`,
|
||||
`[test-extension] Running ${plan.testFileCount} test files for ${plan.extensionId} with ${plan.config}`,
|
||||
);
|
||||
const exitCode = await runVitestBatch({
|
||||
args: passthroughArgs,
|
||||
config: plan.config,
|
||||
env: process.env,
|
||||
files: plan.testFiles,
|
||||
targets: plan.roots,
|
||||
});
|
||||
process.exit(exitCode);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user