Guard SDK sim-config staging paths

This commit is contained in:
2026-06-13 20:12:15 +08:00
parent ea2537e4e3
commit 7fae830726
3 changed files with 133 additions and 2 deletions

View File

@@ -4511,18 +4511,49 @@
generatedToolTablePath(toolTable.wasmPath, expectedWasmPath);
}
function assertSimConfigStagingPlan(machineRel, plan, expectedWasmDir) {
if (plan.wasmDir !== expectedWasmDir) {
throw new Error(`${machineRel}: browser sim staging root drift`);
}
if (!plan.iniPath.startsWith(`${expectedWasmDir}/`)) {
throw new Error(`${machineRel}: browser sim INI staging path drift`);
}
if (plan.programPath !== null && !plan.programPath.startsWith(`${expectedWasmDir}/`)) {
throw new Error(`${machineRel}: browser sim program staging path drift`);
}
const paths = [];
for (const file of plan.files) {
if (file.path !== file.wasmPath) {
throw new Error(`${file.sourceRel}: browser sim path/wasmPath drift`);
}
if (!file.wasmPath.startsWith(`${expectedWasmDir}/`)) {
throw new Error(`${file.sourceRel}: browser sim file staging root drift`);
}
if (!file.sourceRel.startsWith("configs/sim/")) {
throw new Error(`${file.sourceRel}: browser sim source root drift`);
}
if (paths.includes(file.wasmPath)) {
throw new Error(`${machineRel}: duplicate browser sim staging path ${file.wasmPath}`);
}
paths.push(file.wasmPath);
}
}
async function loadSimMachineFiles(machineRel, iniFile, programFile = null) {
const iniText = await fetchText(
`../../vendor/linuxcnc/configs/sim/${machineRel}/${iniFile}`,
);
const expectedWasmDir = `/work/browser-sim/${machineRel}`;
const plan = planSimConfigStaging({
manifestText: await fetchText("../../tools/source-manifest.txt"),
machineRel,
iniFile,
iniText,
programFile,
wasmDir: `/work/browser-sim/${machineRel}`,
wasmDir: expectedWasmDir,
});
assertSimConfigStagingPlan(machineRel, plan, expectedWasmDir);
return {
...plan,

View File

@@ -187,14 +187,16 @@ function simMachine(machineRel, iniFile, programFile, nativeToolTableRel) {
resolve(vendorRoot, `configs/sim/${machineRel}/${iniFile}`),
"utf8",
);
const expectedWasmDir = `/work/sim-inventory/${machineRel}`;
const plan = planSimConfigStaging({
manifestText,
machineRel,
iniFile,
iniText,
programFile,
wasmDir: `/work/sim-inventory/${machineRel}`,
wasmDir: expectedWasmDir,
});
assertSimConfigStagingPlan(machineRel, plan, expectedWasmDir);
const machine = {
...plan,
files: plan.files.map((file) => ({
@@ -207,6 +209,40 @@ function simMachine(machineRel, iniFile, programFile, nativeToolTableRel) {
return withNativeToolTableFallback(machine, iniText, nativeToolTableRel);
}
function assertSimConfigStagingPlan(machineRel, plan, expectedWasmDir) {
assert.equal(plan.wasmDir, expectedWasmDir, `${machineRel}: sim staging root drift`);
assert.ok(
plan.iniPath.startsWith(`${expectedWasmDir}/`),
`${machineRel}: sim INI staging path drift`,
);
if (plan.programPath !== null) {
assert.ok(
plan.programPath.startsWith(`${expectedWasmDir}/`),
`${machineRel}: sim program staging path drift`,
);
}
const paths = [];
for (const file of plan.files) {
assert.equal(file.path, file.wasmPath, `${file.sourceRel}: path/wasmPath drift`);
assert.ok(
file.wasmPath.startsWith(`${expectedWasmDir}/`),
`${file.sourceRel}: sim file staging root drift`,
);
assert.ok(
file.sourceRel.startsWith("configs/sim/"),
`${file.sourceRel}: sim source root drift`,
);
paths.push(file.wasmPath);
}
assert.deepEqual(
paths.filter((path, index) => paths.indexOf(path) !== index),
[],
`${machineRel}: duplicate sim staging paths`,
);
}
function firstIniValue(iniText, section, key) {
let activeSection = "";
for (const rawLine of iniText.split("\n")) {