Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support subshells #232

Merged
merged 5 commits into from
Jan 28, 2024
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
52 changes: 52 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,58 @@ Deno.test("piping in command", async () => {
}
});

Deno.test("subshells", async () => {
{
const result = await $`(echo 1 && echo 2) | cat -`.text();
assertEquals(result, "1\n2");
}
{
const result = await $`(echo 1 && echo 2) && echo 3`.text();
assertEquals(result, "1\n2\n3");
}
{
const result = await $`(echo 1 && echo 2) || echo 3`.text();
assertEquals(result, "1\n2");
}
{
const result = await $`echo 1 && (echo 2 || echo 3)`.text();
assertEquals(result, "1\n2");
}
{
const result = await $`echo 1 && (echo 2 && echo 3) || echo 4`.text();
assertEquals(result, "1\n2\n3");
}
{
const result = await $`echo 1 && (echo 2 || echo 3) && echo 4`.text();
assertEquals(result, "1\n2\n4");
}
// exiting shouldn't exit the parent
{
const result = await $`echo 1 && (echo 2 && exit 0 && echo 3) && echo 4`.text();
assertEquals(result, "1\n2\n4");
}
{
const result = await $`echo 1 && (echo 2 && exit 1 && echo 3) || echo 4`.text();
assertEquals(result, "1\n2\n4");
}
// shouldn't change the environment either
{
assertEquals(await $`export VAR=5 && echo $VAR`.text(), "5"); // for reference
const result = await $`(export VAR=5) && echo $VAR`.text();
assertEquals(result, "");
}
{
const result = await $`echo 1 && (echo 2 && export VAR=5 && echo $VAR) && echo $VAR`.text();
assertEquals(result, "1\n2\n5\n");
}
await withTempDir(async (tempDir) => {
const subDir = tempDir.join("subDir");
subDir.mkdirSync();
const result = await $`(cd subDir && pwd) && pwd`.cwd(tempDir).text();
assertEquals(result, `${subDir}\n${tempDir}`);
});
});

Deno.test("output redirects", async () => {
await withTempDir(async (tempDir) => {
// absolute
Expand Down
21 changes: 15 additions & 6 deletions src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface Command {
redirect: Redirect | undefined;
}

export type CommandInner = SimpleCommand | TaggedSequentialList;
export type CommandInner = SimpleCommand | Subshell;

export interface SimpleCommand {
kind: "simple";
Expand Down Expand Up @@ -81,8 +81,8 @@ export interface Quoted {
value: WordPart[];
}

export interface TaggedSequentialList extends SequentialList {
kind: "sequentialList";
export interface Subshell extends SequentialList {
kind: "subshell";
}

export interface PipeSequence {
Expand Down Expand Up @@ -873,9 +873,12 @@ function executeCommandInner(command: CommandInner, context: Context): Promise<E
switch (command.kind) {
case "simple":
return executeSimpleCommand(command, context);
case "sequentialList":
default:
throw new Error(`Not implemented: ${command.kind}`);
case "subshell":
return executeSubshell(command, context);
default: {
const _assertNever: never = command;
throw new Error(`Not implemented: ${(command as CommandInner).kind}`);
}
}
}

Expand Down Expand Up @@ -1011,6 +1014,12 @@ async function executeCommandArgs(commandArgs: string[], context: Context): Prom
}
}

async function executeSubshell(subshell: Subshell, context: Context): Promise<ExecuteResult> {
const result = await executeSequentialList(subshell, context);
// sub shells do not change the environment or cause an exit
return { code: result.code };
}

async function pipeReaderToWritable(reader: Reader, writable: WritableStream<Uint8Array>, signal: AbortSignal) {
const abortedPromise = new Promise<void>((resolve) => {
signal.addEventListener("abort", listener);
Expand Down
Loading