Skip to content

Commit 6348397

Browse files
fix(run): accept - as the stdin script argument (#39)
`moshcode run - < script.mosh` is documented in README.md and handled explicitly by readScript(), but the option parser treated the bare `-` as an unknown option and exited 1 before it could be used. Exclude `-` from the leading-dash check so it falls through to the positional argument, where readScript() already reads stdin. Unknown options such as --bogus still exit 1 as before. Co-authored-by: clawedassistant26 <307253840+clawedassistant26@users.noreply.github.com>
1 parent 775a7e4 commit 6348397

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

bin/moshcode.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ async function main() {
289289
catch (e) { console.error(String(e.message || e)); process.exit(1); }
290290
}
291291
else if (a === "--dry-run") dryRun = true;
292-
else if (a.startsWith("-") && positional.length === 0) {
292+
else if (a !== "-" && a.startsWith("-") && positional.length === 0) {
293293
console.error(`moshcode run: unknown option ${a}`);
294294
process.exit(1);
295295
}

test/run-options.test.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,39 @@ test("run rejects unknown options before treating them as files", async () => {
4242
assert.match(result.stderr, /moshcode run: unknown option --dryrun/);
4343
});
4444

45+
function runWithStdin(args, input) {
46+
return new Promise((resolve, reject) => {
47+
const dir = mkdtempSync(join(tmpdir(), "moshcode-run-stdin-"));
48+
const stdinFile = join(dir, "stdin");
49+
const stdoutFile = join(dir, "stdout");
50+
const stderrFile = join(dir, "stderr");
51+
writeFileSync(stdinFile, input);
52+
const stdin = openSync(stdinFile, "r");
53+
const stdout = openSync(stdoutFile, "w");
54+
const stderr = openSync(stderrFile, "w");
55+
const child = spawn(process.execPath, [BIN, "run", ...args], {
56+
stdio: [stdin, stdout, stderr],
57+
});
58+
let failed = false;
59+
child.on("error", (error) => { failed = true; reject(error); });
60+
child.on("close", (status) => {
61+
for (const fd of [stdin, stdout, stderr]) closeSync(fd);
62+
if (!failed) resolve({
63+
status,
64+
stdout: readFileSync(stdoutFile, "utf8"),
65+
stderr: readFileSync(stderrFile, "utf8"),
66+
});
67+
});
68+
});
69+
}
70+
71+
test("run - reads the script from stdin", async () => {
72+
const result = await runWithStdin(["-"], 'say("from stdin");\n');
73+
74+
assert.equal(result.status, 0);
75+
assert.match(result.stdout, /from stdin/);
76+
});
77+
4578
test("run accepts equals-form max option", async () => {
4679
const result = await run(["--max=1", "--dry-run"]);
4780

0 commit comments

Comments
 (0)