From a568a1e5e86b65d009c18d9689c12186c55e74f3 Mon Sep 17 00:00:00 2001 From: aiirvizionz Date: Sat, 11 Jul 2026 23:16:03 -0600 Subject: [PATCH] fix: validate run command options --- bin/moshcode.mjs | 8 ++++++++ test/run-options.test.mjs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 test/run-options.test.mjs diff --git a/bin/moshcode.mjs b/bin/moshcode.mjs index aa58a4b..839900c 100755 --- a/bin/moshcode.mjs +++ b/bin/moshcode.mjs @@ -107,6 +107,14 @@ async function main() { catch (e) { console.error(String(e.message || e)); process.exit(1); } } else if (a === "--dry-run") dryRun = true; + else if (a.startsWith("-")) { + console.error(`moshcode run: unknown option ${a}`); + process.exit(1); + } + else if (file) { + console.error(`moshcode run: expected one script file, got ${JSON.stringify(file)} and ${JSON.stringify(a)}`); + process.exit(1); + } else file = a; } const src = file ? readScript(file) : (fs.existsSync(EXAMPLE) ? fs.readFileSync(EXAMPLE, "utf8") : DEFAULT_SCRIPT); diff --git a/test/run-options.test.mjs b/test/run-options.test.mjs new file mode 100644 index 0000000..43ac876 --- /dev/null +++ b/test/run-options.test.mjs @@ -0,0 +1,35 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, writeFileSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { spawnSync } from "node:child_process"; +import test from "node:test"; + +const BIN = fileURLToPath(new URL("../bin/moshcode.mjs", import.meta.url)); + +function run(args) { + return spawnSync(process.execPath, [BIN, "run", ...args], { + encoding: "utf8", + }); +} + +test("run rejects unknown options before treating them as files", () => { + const result = run(["--dryrun"]); + + assert.equal(result.status, 1); + assert.match(result.stderr, /moshcode run: unknown option --dryrun/); +}); + +test("run rejects multiple script files", () => { + const dir = mkdtempSync(join(tmpdir(), "moshcode-run-")); + const first = join(dir, "first.mosh"); + const second = join(dir, "second.mosh"); + writeFileSync(first, 'say("one");\n'); + writeFileSync(second, 'say("two");\n'); + + const result = run([first, second, "--dry-run"]); + + assert.equal(result.status, 1); + assert.match(result.stderr, /moshcode run: expected one script file/); +});