Skip to content

Commit

Permalink
fix: not finding a command should error in the shell (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Mar 30, 2024
1 parent 786faaf commit 1d6ebdd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
16 changes: 10 additions & 6 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ Deno.test("should throw when exit code is non-zero", async () => {
);
});

Deno.test("should error in the shell when the command can't be found", async () => {
const output = await $`nonexistentcommanddaxtest`.noThrow().stderr("piped");
assertEquals(output.code, 127);
assertEquals(output.stderr, "dax: nonexistentcommanddaxtest: command not found\n");
});

Deno.test("throws when providing an object that doesn't override toString", async () => {
{
const obj1 = {};
Expand Down Expand Up @@ -490,11 +496,9 @@ Deno.test("should not allow invalid command names", () => {

Deno.test("should unregister commands", async () => {
const builder = new CommandBuilder().unregisterCommand("export").noThrow();
await assertRejects(
async () => await builder.command("export somewhere"),
Error,
"Command not found: export",
);
const output = await builder.command("export somewhere").stderr("piped");
assertEquals(output.code, 127);
assertEquals(output.stderr, "dax: export: command not found\n");
});

Deno.test("sleep command", async () => {
Expand Down Expand Up @@ -1438,7 +1442,7 @@ Deno.test("shebang support", async (t) => {
.text();
},
Error,
"Command not found: deno run",
"Exited with code: 127",
);
});

Expand Down
10 changes: 7 additions & 3 deletions src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,10 @@ async function executeUnresolvedCommand(
context: Context,
): Promise<ExecuteResult> {
const resolvedCommand = await resolveCommand(unresolvedCommand, context);
if (resolvedCommand === false) {
context.stderr.writeLine(`dax: ${unresolvedCommand.name}: command not found`);
return { code: 127 };
}
if (resolvedCommand.kind === "shebang") {
return executeUnresolvedCommand(resolvedCommand.command, [...resolvedCommand.args, ...commandArgs], context);
}
Expand Down Expand Up @@ -1113,7 +1117,7 @@ function pipeCommandPipeReaderToWriterSync(
}
}

type ResolvedCommand = ResolvedPathCommand | ResolvedShebangCommand;
type ResolvedCommand = ResolvedPathCommand | ResolvedShebangCommand | false;

interface ResolvedPathCommand {
kind: "path";
Expand Down Expand Up @@ -1141,7 +1145,7 @@ async function resolveCommand(unresolvedCommand: UnresolvedCommand, context: Con
// won't have a script with a shebang in it on Windows
const result = await getExecutableShebangFromPath(commandPath);
if (result === false) {
throw new Error(`Command not found: ${unresolvedCommand.name}`);
return false;
} else if (result != null) {
const args = await parseShebangArgs(result, context);
const name = args.shift()!;
Expand Down Expand Up @@ -1180,7 +1184,7 @@ async function resolveCommand(unresolvedCommand: UnresolvedCommand, context: Con
},
});
if (commandPath == null) {
throw new Error(`Command not found: ${unresolvedCommand.name}`);
return false;
}
return {
kind: "path",
Expand Down

0 comments on commit 1d6ebdd

Please sign in to comment.