Skip to content

Commit 88756ae

Browse files
authored
fix: validate run command options (#9)
1 parent 1800d12 commit 88756ae

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

bin/moshcode.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ async function main() {
107107
catch (e) { console.error(String(e.message || e)); process.exit(1); }
108108
}
109109
else if (a === "--dry-run") dryRun = true;
110+
else if (a.startsWith("-")) {
111+
console.error(`moshcode run: unknown option ${a}`);
112+
process.exit(1);
113+
}
114+
else if (file) {
115+
console.error(`moshcode run: expected one script file, got ${JSON.stringify(file)} and ${JSON.stringify(a)}`);
116+
process.exit(1);
117+
}
110118
else file = a;
111119
}
112120
const src = file ? readScript(file) : (fs.existsSync(EXAMPLE) ? fs.readFileSync(EXAMPLE, "utf8") : DEFAULT_SCRIPT);

test/run-options.test.mjs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import assert from "node:assert/strict";
2+
import { mkdtempSync, writeFileSync } from "node:fs";
3+
import { fileURLToPath } from "node:url";
4+
import { tmpdir } from "node:os";
5+
import { join } from "node:path";
6+
import { spawnSync } from "node:child_process";
7+
import test from "node:test";
8+
9+
const BIN = fileURLToPath(new URL("../bin/moshcode.mjs", import.meta.url));
10+
11+
function run(args) {
12+
return spawnSync(process.execPath, [BIN, "run", ...args], {
13+
encoding: "utf8",
14+
});
15+
}
16+
17+
test("run rejects unknown options before treating them as files", () => {
18+
const result = run(["--dryrun"]);
19+
20+
assert.equal(result.status, 1);
21+
assert.match(result.stderr, /moshcode run: unknown option --dryrun/);
22+
});
23+
24+
test("run rejects multiple script files", () => {
25+
const dir = mkdtempSync(join(tmpdir(), "moshcode-run-"));
26+
const first = join(dir, "first.mosh");
27+
const second = join(dir, "second.mosh");
28+
writeFileSync(first, 'say("one");\n');
29+
writeFileSync(second, 'say("two");\n');
30+
31+
const result = run([first, second, "--dry-run"]);
32+
33+
assert.equal(result.status, 1);
34+
assert.match(result.stderr, /moshcode run: expected one script file/);
35+
});

0 commit comments

Comments
 (0)