feat(FN-4605): complete Step 3 — add workspace-root bootstrap and pretest hooks
Fusion-Task-Id: FN-4605 Fusion-Task-Lineage: 472b0ccb-d015-4ff7-bdf2-1d5d05ae09e9
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import {
|
||||
detectMissingArtifacts,
|
||||
detectMissingOrStaleArtifacts,
|
||||
@@ -23,6 +26,32 @@ test("ensureTestArtifacts skips build when nothing is missing", () => {
|
||||
assert.deepEqual(built, []);
|
||||
});
|
||||
|
||||
test("ensureTestArtifacts resolves workspace root from nested cwd", () => {
|
||||
const originalCwd = process.cwd();
|
||||
const workspaceRoot = mkdtempSync(path.join(tmpdir(), "fn-4605-workspace-"));
|
||||
const nestedPkg = path.join(workspaceRoot, "packages", "dashboard");
|
||||
|
||||
let capturedCwd = null;
|
||||
try {
|
||||
writeFileSync(path.join(workspaceRoot, "pnpm-workspace.yaml"), "packages:\n - 'packages/*'\n");
|
||||
mkdirSync(nestedPkg, { recursive: true });
|
||||
process.chdir(nestedPkg);
|
||||
|
||||
ensureTestArtifacts(
|
||||
undefined,
|
||||
(_cmd, _args, cwd) => {
|
||||
capturedCwd = cwd;
|
||||
},
|
||||
() => false,
|
||||
);
|
||||
} finally {
|
||||
process.chdir(originalCwd);
|
||||
rmSync(workspaceRoot, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
assert.equal(path.basename(capturedCwd), path.basename(workspaceRoot));
|
||||
});
|
||||
|
||||
test("ensureTestArtifacts builds only missing packages", () => {
|
||||
const calls = [];
|
||||
const built = ensureTestArtifacts(
|
||||
|
||||
@@ -188,21 +188,43 @@ function run(
|
||||
}
|
||||
}
|
||||
|
||||
function resolveWorkspaceRoot(explicitRootDir) {
|
||||
if (process.env.FUSION_PROJECT_DIR) {
|
||||
return path.resolve(process.env.FUSION_PROJECT_DIR);
|
||||
}
|
||||
if (explicitRootDir) {
|
||||
return path.resolve(explicitRootDir);
|
||||
}
|
||||
|
||||
let current = path.resolve(process.cwd());
|
||||
while (true) {
|
||||
if (existsSync(path.join(current, "pnpm-workspace.yaml"))) {
|
||||
return current;
|
||||
}
|
||||
const parent = path.dirname(current);
|
||||
if (parent === current) break;
|
||||
current = parent;
|
||||
}
|
||||
|
||||
return path.resolve(process.cwd());
|
||||
}
|
||||
|
||||
export function ensureTestArtifacts(
|
||||
rootDir = process.cwd(),
|
||||
rootDir,
|
||||
runFn = run,
|
||||
existsFn = existsSync,
|
||||
statFn = statSync,
|
||||
readdirFn = readdirSync,
|
||||
runOptions = {},
|
||||
) {
|
||||
const missingOrStale = detectMissingOrStaleArtifacts(rootDir, existsFn, statFn, readdirFn);
|
||||
const resolvedRootDir = resolveWorkspaceRoot(rootDir);
|
||||
const missingOrStale = detectMissingOrStaleArtifacts(resolvedRootDir, existsFn, statFn, readdirFn);
|
||||
if (missingOrStale.length === 0) return [];
|
||||
|
||||
const names = missingOrStale.map((pkg) => pkg.name);
|
||||
console.log(`[test-bootstrap] rebuilding workspace dist artifacts (missing or stale): ${names.join(", ")}`);
|
||||
if (runFn === run) {
|
||||
runFn("pnpm", [...names.flatMap((name) => ["--filter", name]), "build"], rootDir, {
|
||||
runFn("pnpm", [...names.flatMap((name) => ["--filter", name]), "build"], resolvedRootDir, {
|
||||
...runOptions,
|
||||
pkgEntries: missingOrStale,
|
||||
existsFn,
|
||||
@@ -210,7 +232,7 @@ export function ensureTestArtifacts(
|
||||
readdirFn,
|
||||
});
|
||||
} else {
|
||||
runFn("pnpm", [...names.flatMap((name) => ["--filter", name]), "build"], rootDir);
|
||||
runFn("pnpm", [...names.flatMap((name) => ["--filter", name]), "build"], resolvedRootDir);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user