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
2 changes: 1 addition & 1 deletion bin/moshcode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ 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("-") && positional.length === 0) {
else if (a !== "-" && a.startsWith("-") && positional.length === 0) {
console.error(`moshcode run: unknown option ${a}`);
process.exit(1);
}
Expand Down
33 changes: 33 additions & 0 deletions test/run-options.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ test("run rejects unknown options before treating them as files", async () => {
assert.match(result.stderr, /moshcode run: unknown option --dryrun/);
});

function runWithStdin(args, input) {
return new Promise((resolve, reject) => {
const dir = mkdtempSync(join(tmpdir(), "moshcode-run-stdin-"));
const stdinFile = join(dir, "stdin");
const stdoutFile = join(dir, "stdout");
const stderrFile = join(dir, "stderr");
writeFileSync(stdinFile, input);
const stdin = openSync(stdinFile, "r");
const stdout = openSync(stdoutFile, "w");
const stderr = openSync(stderrFile, "w");
const child = spawn(process.execPath, [BIN, "run", ...args], {
stdio: [stdin, stdout, stderr],
});
let failed = false;
child.on("error", (error) => { failed = true; reject(error); });
child.on("close", (status) => {
for (const fd of [stdin, stdout, stderr]) closeSync(fd);
if (!failed) resolve({
status,
stdout: readFileSync(stdoutFile, "utf8"),
stderr: readFileSync(stderrFile, "utf8"),
});
});
});
}

test("run - reads the script from stdin", async () => {
const result = await runWithStdin(["-"], 'say("from stdin");\n');

assert.equal(result.status, 0);
assert.match(result.stdout, /from stdin/);
});

test("run accepts equals-form max option", async () => {
const result = await run(["--max=1", "--dry-run"]);

Expand Down
Loading