|
| 1 | +/* |
| 2 | + * launcher-arguments.test.ts |
| 3 | + * |
| 4 | + * The Windows launcher quarto.cmd must pass its arguments to Deno unchanged, |
| 5 | + * including "!" and "^", which delayed expansion would otherwise consume, and |
| 6 | + * must short-circuit --version, -v and --paths when the caller quotes them. |
| 7 | + * Covered for both the dev layout (package/dist/bin/quarto.cmd) and the |
| 8 | + * bundled layout (bin/quarto.cmd next to quarto.js). |
| 9 | + * |
| 10 | + * Copyright (C) 2026 Posit Software, PBC |
| 11 | + */ |
| 12 | + |
| 13 | +import { unitTest } from "../test.ts"; |
| 14 | +import { assert, assertEquals } from "../asserts.ts"; |
| 15 | +import { isWindows } from "../../src/deno_ral/platform.ts"; |
| 16 | +import { dirname, fromFileUrl, join } from "../../src/deno_ral/path.ts"; |
| 17 | +import { |
| 18 | + copySync, |
| 19 | + ensureDirSync, |
| 20 | + removeSync, |
| 21 | + writeTextFileSync, |
| 22 | +} from "../../src/deno_ral/fs.ts"; |
| 23 | +import { execProcess } from "../../src/core/process.ts"; |
| 24 | +import { withTempDir } from "../utils.ts"; |
| 25 | + |
| 26 | +const repoRoot = join(dirname(fromFileUrl(import.meta.url)), "..", ".."); |
| 27 | + |
| 28 | +// Prints the arguments and, when Deno got a --location option, its URL. |
| 29 | +const probe = 'import process from "node:process";\n' + |
| 30 | + 'console.log("ARGS=" + JSON.stringify(process.argv.slice(2)));\n' + |
| 31 | + 'let loc = "";\n' + |
| 32 | + "try { loc = globalThis.location.href; } catch { /* no --location */ }\n" + |
| 33 | + 'console.log("LOC=" + loc);\n'; |
| 34 | + |
| 35 | +// A Deno option whose value holds characters cmd would otherwise interpret. |
| 36 | +const location = "http://example.com/?a=1&b=^c!d"; |
| 37 | + |
| 38 | +const specialArgs = [ |
| 39 | + "C:\\work\\notes!", |
| 40 | + "C:\\x!OS!y", |
| 41 | + "c^d", |
| 42 | + "a & b", |
| 43 | + "100%", |
| 44 | + "%OS%", |
| 45 | + "p (1)", |
| 46 | + "sp ace", |
| 47 | +]; |
| 48 | + |
| 49 | +interface Launcher { |
| 50 | + cmd: string; |
| 51 | + env: Record<string, string>; |
| 52 | +} |
| 53 | + |
| 54 | +async function launch(launcher: Launcher, args: string[], cwd: string) { |
| 55 | + const result = await execProcess({ |
| 56 | + cmd: launcher.cmd, |
| 57 | + args, |
| 58 | + cwd, |
| 59 | + stdout: "piped", |
| 60 | + stderr: "piped", |
| 61 | + env: launcher.env, |
| 62 | + }); |
| 63 | + const stdout = result.stdout ?? ""; |
| 64 | + const details = `(exit ${result.code})\nstdout:\n${stdout}\nstderr:\n${ |
| 65 | + result.stderr ?? "" |
| 66 | + }`; |
| 67 | + return { result, stdout, details }; |
| 68 | +} |
| 69 | + |
| 70 | +async function assertArgumentsReachDeno( |
| 71 | + launcher: Launcher, |
| 72 | + args: string[], |
| 73 | + cwd: string, |
| 74 | +) { |
| 75 | + const { result, stdout, details } = await launch(launcher, args, cwd); |
| 76 | + const argsLine = stdout.split(/\r?\n/).find((line) => |
| 77 | + line.startsWith("ARGS=") |
| 78 | + ); |
| 79 | + assertEquals( |
| 80 | + argsLine, |
| 81 | + "ARGS=" + JSON.stringify(args), |
| 82 | + `quarto.cmd changed its arguments ${details}`, |
| 83 | + ); |
| 84 | + assert(result.success, `quarto.cmd exited with an error ${details}`); |
| 85 | +} |
| 86 | + |
| 87 | +// Runs quarto.cmd through cmd with an unquoted command line, as typed in a |
| 88 | +// terminal, where cmd itself sees the ")" in "report(1).qmd". |
| 89 | +async function assertTypedArgumentsReachDeno(launcher: Launcher, cwd: string) { |
| 90 | + const result = await execProcess({ |
| 91 | + cmd: "cmd", |
| 92 | + args: ["/d", "/s", "/c", `""${launcher.cmd}" run report(1).qmd C:\\notes!"`], |
| 93 | + windowsRawArguments: true, |
| 94 | + cwd, |
| 95 | + stdout: "piped", |
| 96 | + stderr: "piped", |
| 97 | + env: launcher.env, |
| 98 | + }); |
| 99 | + const stdout = result.stdout ?? ""; |
| 100 | + const details = `(exit ${result.code})\nstdout:\n${stdout}\nstderr:\n${ |
| 101 | + result.stderr ?? "" |
| 102 | + }`; |
| 103 | + const argsLine = stdout.split(/\r?\n/).find((line) => |
| 104 | + line.startsWith("ARGS=") |
| 105 | + ); |
| 106 | + assertEquals( |
| 107 | + argsLine, |
| 108 | + 'ARGS=["run","report(1).qmd","C:\\\\notes!"]', |
| 109 | + `quarto.cmd changed a typed command line ${details}`, |
| 110 | + ); |
| 111 | + assert(result.success, `quarto.cmd exited with an error ${details}`); |
| 112 | +} |
| 113 | + |
| 114 | +async function assertOptionsReachDeno(launcher: Launcher, cwd: string) { |
| 115 | + const { result, stdout, details } = await launch({ |
| 116 | + cmd: launcher.cmd, |
| 117 | + env: { |
| 118 | + ...launcher.env, |
| 119 | + QUARTO_DENO_EXTRA_OPTIONS: `--location=${location}`, |
| 120 | + }, |
| 121 | + }, ["run"], cwd); |
| 122 | + const locLine = stdout.split(/\r?\n/).find((line) => line.startsWith("LOC=")); |
| 123 | + assertEquals( |
| 124 | + locLine, |
| 125 | + `LOC=${location}`, |
| 126 | + `quarto.cmd changed QUARTO_DENO_EXTRA_OPTIONS ${details}`, |
| 127 | + ); |
| 128 | + assert(result.success, `quarto.cmd exited with an error ${details}`); |
| 129 | +} |
| 130 | + |
| 131 | +async function assertShortCircuits(launcher: Launcher, cwd: string) { |
| 132 | + for (const flag of ["--version", "-v"]) { |
| 133 | + const { stdout, details } = await launch(launcher, [flag], cwd); |
| 134 | + assertEquals( |
| 135 | + stdout.trim(), |
| 136 | + "9.8.7", |
| 137 | + `quarto.cmd ${flag} did not print the forced version ${details}`, |
| 138 | + ); |
| 139 | + } |
| 140 | + const { stdout, details } = await launch(launcher, ["--paths"], cwd); |
| 141 | + assert( |
| 142 | + !stdout.includes("ARGS=") && stdout.trim().split(/\r?\n/).length === 2, |
| 143 | + `quarto.cmd --paths did not print the bin and share paths ${details}`, |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +unitTest( |
| 148 | + "launcher-arguments - bundled quarto.cmd passes arguments unchanged", |
| 149 | + async () => { |
| 150 | + await withTempDir(async (dir) => { |
| 151 | + // Minimal bundled install layout, as in |
| 152 | + // bundled-launcher-path-with-space.test.ts. |
| 153 | + const bin = join(dir, "bin"); |
| 154 | + ensureDirSync(bin); |
| 155 | + copySync( |
| 156 | + join(repoRoot, "package", "scripts", "windows", "quarto.cmd"), |
| 157 | + join(bin, "quarto.cmd"), |
| 158 | + ); |
| 159 | + writeTextFileSync(join(bin, "quarto.js"), probe); |
| 160 | + const tools = join(bin, "tools"); |
| 161 | + const mklink = await execProcess({ |
| 162 | + cmd: "cmd", |
| 163 | + args: [ |
| 164 | + "/c", |
| 165 | + "mklink", |
| 166 | + "/J", |
| 167 | + tools, |
| 168 | + join(repoRoot, "package", "dist", "bin", "tools"), |
| 169 | + ], |
| 170 | + stdout: "piped", |
| 171 | + stderr: "piped", |
| 172 | + }); |
| 173 | + assert(mklink.success, `creating the junction failed: ${mklink.stderr}`); |
| 174 | + try { |
| 175 | + const launcher = { |
| 176 | + cmd: join(bin, "quarto.cmd"), |
| 177 | + env: { QUARTO_FORCE_VERSION: "9.8.7" }, |
| 178 | + }; |
| 179 | + await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir); |
| 180 | + await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir); |
| 181 | + await assertOptionsReachDeno(launcher, dir); |
| 182 | + await assertTypedArgumentsReachDeno(launcher, dir); |
| 183 | + await assertShortCircuits(launcher, dir); |
| 184 | + } finally { |
| 185 | + // Remove the junction itself (not its target) before the temp dir goes. |
| 186 | + removeSync(tools); |
| 187 | + } |
| 188 | + }); |
| 189 | + }, |
| 190 | + { ignore: !isWindows }, |
| 191 | +); |
| 192 | + |
| 193 | +unitTest( |
| 194 | + "launcher-arguments - dev quarto.cmd passes arguments unchanged", |
| 195 | + async () => { |
| 196 | + await withTempDir(async (dir) => { |
| 197 | + const target = join(dir, "probe.ts"); |
| 198 | + writeTextFileSync(target, probe); |
| 199 | + const launcher = { |
| 200 | + cmd: join(repoRoot, "package", "dist", "bin", "quarto.cmd"), |
| 201 | + env: { |
| 202 | + QUARTO_TARGET: target, |
| 203 | + QUARTO_NO_TYPECHECK: "true", |
| 204 | + QUARTO_FORCE_VERSION: "9.8.7", |
| 205 | + // See dev-launcher-path-with-space.test.ts: keeps a failed launch |
| 206 | + // from running configure.cmd in the checkout under test. |
| 207 | + NoDefaultCurrentDirectoryInExePath: "1", |
| 208 | + }, |
| 209 | + }; |
| 210 | + await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir); |
| 211 | + await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir); |
| 212 | + await assertOptionsReachDeno(launcher, dir); |
| 213 | + await assertTypedArgumentsReachDeno(launcher, dir); |
| 214 | + await assertShortCircuits(launcher, dir); |
| 215 | + }); |
| 216 | + }, |
| 217 | + { ignore: !isWindows, requiresDevQuarto: true }, |
| 218 | +); |
0 commit comments