Skip to content

Commit 9eb685d

Browse files
committed
fix(omniroute): verify windows wrapper startup
1 parent 0dad25d commit 9eb685d

2 files changed

Lines changed: 46 additions & 20 deletions

File tree

packages/omniroute/scripts/verify-startup.mjs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { spawn } from "node:child_process"
44
import { access, mkdtemp, readFile, readdir, rm } from "node:fs/promises"
55
import os from "node:os"
66
import path from "node:path"
7-
import { fileURLToPath } from "node:url"
7+
import { fileURLToPath, pathToFileURL } from "node:url"
88

99
import { getManifestBinEntries, getNativeSmokeWrapperFile, getWrapperDefinitions, normalizeTargetPlatform, resolveReleasePath } from "./wrappers.mjs"
1010

@@ -14,10 +14,12 @@ const packageRoot = path.resolve(__dirname, "..")
1414
const root = path.resolve(packageRoot, "../..")
1515
const downloadedDir = path.resolve(root, process.env.ARTIFACTS_DOWNLOAD_DIR || path.join("downloaded", "omniroute"))
1616

17-
main().catch((error) => {
18-
console.error(error instanceof Error ? error.stack || error.message : String(error))
19-
process.exitCode = 1
20-
})
17+
if (isMainModule()) {
18+
main().catch((error) => {
19+
console.error(error instanceof Error ? error.stack || error.message : String(error))
20+
process.exitCode = 1
21+
})
22+
}
2123

2224
async function main() {
2325
const metadataPath = await findFile(downloadedDir, (entryPath) => path.basename(entryPath) === "metadata.json")
@@ -139,17 +141,6 @@ async function runNativeWrapperVersion(releaseRoot, binEntries, targetPlatform,
139141
const wrapperFile = getNativeSmokeWrapperFile(binEntries, targetPlatform)
140142
const wrapperPath = resolveReleasePath(releaseRoot, wrapperFile)
141143

142-
if (targetPlatform === "windows") {
143-
return runAndCapture(
144-
"cmd.exe",
145-
["/d", "/s", "/c", `"${wrapperPath}" --version`],
146-
{
147-
cwd: releaseRoot,
148-
env,
149-
},
150-
)
151-
}
152-
153144
return runAndCapture(wrapperPath, ["--version"], {
154145
cwd: releaseRoot,
155146
env,
@@ -192,9 +183,22 @@ async function exists(targetPath) {
192183
}
193184
}
194185

186+
export function resolveSpawnInvocation(command, args, hostPlatform = process.platform) {
187+
if (hostPlatform === "win32" && /\.(cmd|bat)$/i.test(command)) {
188+
return {
189+
command: process.env.ComSpec || "C:\\Windows\\System32\\cmd.exe",
190+
args: ["/d", "/s", "/c", command, ...args],
191+
}
192+
}
193+
194+
return { command, args }
195+
}
196+
195197
function run(command, args, options = {}) {
198+
const invocation = resolveSpawnInvocation(command, args)
199+
196200
return new Promise((resolve, reject) => {
197-
const child = spawn(command, args, {
201+
const child = spawn(invocation.command, invocation.args, {
198202
cwd: options.cwd || root,
199203
env: options.env || process.env,
200204
stdio: "inherit",
@@ -206,14 +210,16 @@ function run(command, args, options = {}) {
206210
resolve()
207211
return
208212
}
209-
reject(new Error(`${command} ${args.join(" ")} exited with code ${code}`))
213+
reject(new Error(`${invocation.command} ${invocation.args.join(" ")} exited with code ${code}`))
210214
})
211215
})
212216
}
213217

214218
function runAndCapture(command, args, options = {}) {
219+
const invocation = resolveSpawnInvocation(command, args)
220+
215221
return new Promise((resolve, reject) => {
216-
const child = spawn(command, args, {
222+
const child = spawn(invocation.command, invocation.args, {
217223
cwd: options.cwd || root,
218224
env: options.env || process.env,
219225
stdio: ["ignore", "pipe", "inherit"],
@@ -230,11 +236,15 @@ function runAndCapture(command, args, options = {}) {
230236
resolve(output)
231237
return
232238
}
233-
reject(new Error(`${command} ${args.join(" ")} exited with code ${code}`))
239+
reject(new Error(`${invocation.command} ${invocation.args.join(" ")} exited with code ${code}`))
234240
})
235241
})
236242
}
237243

238244
function escapePowerShell(value) {
239245
return value.replaceAll("'", "''")
240246
}
247+
248+
function isMainModule() {
249+
return process.argv[1] != null && import.meta.url === pathToFileURL(process.argv[1]).href
250+
}

packages/omniroute/scripts/verify-startup.test.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from "node:test"
22
import assert from "node:assert/strict"
33

4+
import { resolveSpawnInvocation } from "./verify-startup.mjs"
45
import { getManifestBinEntries, getNativeSmokeWrapperFile, getWrapperDefinitions } from "./wrappers.mjs"
56

67
test("verification wrapper expectations stay aligned with the manifest command surface", () => {
@@ -29,3 +30,18 @@ test("verification wrapper expectations stay aligned with the manifest command s
2930
assert.equal(getNativeSmokeWrapperFile(binEntries, "windows"), "omniroute.cmd")
3031
assert.equal(getNativeSmokeWrapperFile(binEntries, "macos"), "omniroute.sh")
3132
})
33+
34+
35+
test("resolveSpawnInvocation routes Windows script wrappers through cmd.exe", () => {
36+
const invocation = resolveSpawnInvocation("C:\\temp\\omniroute.cmd", ["--version"], "win32")
37+
38+
assert.equal(invocation.command, process.env.ComSpec || "C:\\Windows\\System32\\cmd.exe")
39+
assert.deepEqual(invocation.args, ["/d", "/s", "/c", "C:\\temp\\omniroute.cmd", "--version"])
40+
})
41+
42+
test("resolveSpawnInvocation keeps non-wrapper commands unchanged", () => {
43+
const invocation = resolveSpawnInvocation(process.execPath, ["bin/omniroute.mjs", "--version"], "win32")
44+
45+
assert.equal(invocation.command, process.execPath)
46+
assert.deepEqual(invocation.args, ["bin/omniroute.mjs", "--version"])
47+
})

0 commit comments

Comments
 (0)