Skip to content

Commit 114b94d

Browse files
authored
fix(tui): reject unknown run options (#25)
1 parent b9dd1ed commit 114b94d

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/tui.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ async function runFile(args) {
277277
max = v;
278278
} else if (a === "--dry-run") {
279279
dryRun = true;
280+
} else if (a.startsWith("-") && !file) {
281+
console.log(err(`unknown option ${a}`));
282+
return;
280283
} else if (!file) {
281284
file = a;
282285
}

test/tui.test.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
import assert from "node:assert/strict";
2+
import { spawn } from "node:child_process";
23
import test from "node:test";
4+
import { fileURLToPath } from "node:url";
35

46
import { splitCommandLine } from "../src/tui.mjs";
57

8+
const BIN = fileURLToPath(new URL("../bin/moshcode.mjs", import.meta.url));
9+
10+
function runTui(input) {
11+
return new Promise((resolve, reject) => {
12+
const child = spawn(process.execPath, [BIN], { stdio: ["pipe", "pipe", "pipe"] });
13+
let stdout = "";
14+
let stderr = "";
15+
child.stdout.on("data", (chunk) => { stdout += chunk; });
16+
child.stderr.on("data", (chunk) => { stderr += chunk; });
17+
child.on("error", reject);
18+
child.on("close", (status, signal) => resolve({ status, signal, stdout, stderr }));
19+
child.stdin.end(input);
20+
});
21+
}
22+
623
test("TUI command parsing preserves quoted native CLI arguments", () => {
724
assert.deepEqual(
825
splitCommandLine('/coinpay card pay --description "Fix the build" --note \'ship it\''),
@@ -23,3 +40,11 @@ test("TUI command parsing rejects incomplete quoting", () => {
2340
assert.throws(() => splitCommandLine('/coinpay --description "unfinished'), /unterminated/);
2441
assert.throws(() => splitCommandLine("/ugig trailing\\"), /trailing escape/);
2542
});
43+
44+
test("TUI /run rejects unknown options before reading a script file", async () => {
45+
const result = await runTui("/run --dryrun\n/quit\n");
46+
47+
assert.equal(result.status, 0);
48+
assert.match(result.stdout, /unknown option --dryrun/);
49+
assert.doesNotMatch(result.stdout, /can't read --dryrun/);
50+
});

0 commit comments

Comments
 (0)