mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 23:34:47 -05:00
fix(runtime/js/spawn): Pass stdio options for spawn() and spawnSync() (#14358)
This commit is contained in:
parent
4b7d306a19
commit
e9041b9edc
6 changed files with 58 additions and 53 deletions
4
cli/dts/lib.deno.unstable.d.ts
vendored
4
cli/dts/lib.deno.unstable.d.ts
vendored
|
@ -1442,7 +1442,7 @@ declare namespace Deno {
|
|||
/**
|
||||
* Executes a subprocess, waiting for it to finish and
|
||||
* collecting all of its output.
|
||||
* The stdio options are ignored.
|
||||
* Will throw an error if `stdin: "piped"` is passed.
|
||||
*
|
||||
* ```ts
|
||||
* const { status, stdout, stderr } = await Deno.spawn(Deno.execPath(), {
|
||||
|
@ -1464,7 +1464,7 @@ declare namespace Deno {
|
|||
/**
|
||||
* Synchronously executes a subprocess, waiting for it to finish and
|
||||
* collecting all of its output.
|
||||
* The stdio options are ignored.
|
||||
* Will throw an error if `stdin: "piped"` is passed.
|
||||
*
|
||||
* ```ts
|
||||
* const { status, stdout, stderr } = Deno.spawnSync(Deno.execPath(), {
|
||||
|
|
|
@ -2764,3 +2764,8 @@ itest!(report_error_handled {
|
|||
args: "run --quiet report_error_handled.ts",
|
||||
output: "report_error_handled.ts.out",
|
||||
});
|
||||
|
||||
itest!(spawn_stdout_inherit {
|
||||
args: "run --quiet --unstable -A spawn_stdout_inherit.ts",
|
||||
output: "spawn_stdout_inherit.ts.out",
|
||||
});
|
||||
|
|
8
cli/tests/testdata/spawn_stdout_inherit.ts
vendored
Normal file
8
cli/tests/testdata/spawn_stdout_inherit.ts
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
await Deno.spawn(Deno.execPath(), {
|
||||
args: ["eval", "--quiet", "console.log('Hello, world! 1')"],
|
||||
stdout: "inherit",
|
||||
});
|
||||
Deno.spawnSync(Deno.execPath(), {
|
||||
args: ["eval", "--quiet", "console.log('Hello, world! 2')"],
|
||||
stdout: "inherit",
|
||||
});
|
2
cli/tests/testdata/spawn_stdout_inherit.ts.out
vendored
Normal file
2
cli/tests/testdata/spawn_stdout_inherit.ts.out
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
Hello, world! 1
|
||||
Hello, world! 2
|
|
@ -468,46 +468,6 @@ Deno.test(
|
|||
},
|
||||
);
|
||||
|
||||
Deno.test(
|
||||
{ permissions: { run: true, read: true } },
|
||||
async function spawnOverrideStdio() {
|
||||
const { stdout, stderr } = await Deno.spawn(Deno.execPath(), {
|
||||
args: [
|
||||
"eval",
|
||||
"console.log('hello'); console.error('world')",
|
||||
],
|
||||
stdin: "piped",
|
||||
stdout: "null",
|
||||
stderr: "null",
|
||||
});
|
||||
|
||||
// @ts-ignore: for testing
|
||||
assertEquals(new TextDecoder().decode(stdout), "hello\n");
|
||||
// @ts-ignore: for testing
|
||||
assertEquals(new TextDecoder().decode(stderr), "world\n");
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test(
|
||||
{ permissions: { run: true, read: true } },
|
||||
function spawnSyncOverrideStdio() {
|
||||
const { stdout, stderr } = Deno.spawnSync(Deno.execPath(), {
|
||||
args: [
|
||||
"eval",
|
||||
"console.log('hello'); console.error('world')",
|
||||
],
|
||||
stdin: "piped",
|
||||
stdout: "null",
|
||||
stderr: "null",
|
||||
});
|
||||
|
||||
// @ts-ignore: for testing
|
||||
assertEquals(new TextDecoder().decode(stdout), "hello\n");
|
||||
// @ts-ignore: for testing
|
||||
assertEquals(new TextDecoder().decode(stderr), "world\n");
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test(
|
||||
{ permissions: { run: true, read: true } },
|
||||
async function spawnEnv() {
|
||||
|
@ -685,3 +645,25 @@ Deno.test(
|
|||
}
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test(async function spawnStdinPipedFails() {
|
||||
await assertRejects(
|
||||
() =>
|
||||
Deno.spawn("id", {
|
||||
stdin: "piped",
|
||||
}),
|
||||
TypeError,
|
||||
"Piped stdin is not supported for this function, use 'Deno.spawnChild()' instead",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test(function spawnSyncStdinPipedFails() {
|
||||
assertThrows(
|
||||
() =>
|
||||
Deno.spawnSync("id", {
|
||||
stdin: "piped",
|
||||
}),
|
||||
TypeError,
|
||||
"Piped stdin is not supported for this function, use 'Deno.spawnChild()' instead",
|
||||
);
|
||||
});
|
||||
|
|
|
@ -166,13 +166,13 @@
|
|||
}
|
||||
}
|
||||
|
||||
function spawn(command, options) { // TODO(@crowlKats): more options (like input)?
|
||||
return spawnChild(command, {
|
||||
...options,
|
||||
stdin: "null",
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
}).output();
|
||||
function spawn(command, options) {
|
||||
if (options?.stdin === "piped") {
|
||||
throw new TypeError(
|
||||
"Piped stdin is not supported for this function, use 'Deno.spawnChild()' instead",
|
||||
);
|
||||
}
|
||||
return spawnChild(command, options).output();
|
||||
}
|
||||
|
||||
function spawnSync(command, {
|
||||
|
@ -182,7 +182,15 @@
|
|||
env = {},
|
||||
uid = undefined,
|
||||
gid = undefined,
|
||||
} = {}) { // TODO(@crowlKats): more options (like input)?
|
||||
stdin = "null",
|
||||
stdout = "piped",
|
||||
stderr = "piped",
|
||||
} = {}) {
|
||||
if (stdin === "piped") {
|
||||
throw new TypeError(
|
||||
"Piped stdin is not supported for this function, use 'Deno.spawnChild()' instead",
|
||||
);
|
||||
}
|
||||
return core.opSync("op_spawn_sync", {
|
||||
cmd: pathFromURL(command),
|
||||
args: ArrayPrototypeMap(args, String),
|
||||
|
@ -191,9 +199,9 @@
|
|||
env: ObjectEntries(env),
|
||||
uid,
|
||||
gid,
|
||||
stdin: "null",
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
stdin,
|
||||
stdout,
|
||||
stderr,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue