Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bin/moshcode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions test/run-options.test.mjs
Original file line number Diff line number Diff line change
@@ -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/);
});
Loading