mirror of
https://github.com/denoland/deno
synced 2024-08-27 20:09:17 +00:00
Add Process.stderrOutput() (#1828)
This commit is contained in:
parent
c25e262b04
commit
bdc455dd25
|
@ -83,7 +83,7 @@ export class Process {
|
|||
}
|
||||
|
||||
/** Buffer the stdout and return it as Uint8Array after EOF.
|
||||
* You must have set stdout to "piped" in when creating the process.
|
||||
* You must set stdout to "piped" when creating the process.
|
||||
* This calls close() on stdout after its done.
|
||||
*/
|
||||
async output(): Promise<Uint8Array> {
|
||||
|
@ -97,6 +97,21 @@ export class Process {
|
|||
}
|
||||
}
|
||||
|
||||
/** Buffer the stderr and return it as Uint8Array after EOF.
|
||||
* You must set stderr to "piped" when creating the process.
|
||||
* This calls close() on stderr after its done.
|
||||
*/
|
||||
async stderrOutput(): Promise<Uint8Array> {
|
||||
if (!this.stderr) {
|
||||
throw new Error("Process.stderrOutput: stderr is undefined");
|
||||
}
|
||||
try {
|
||||
return await readAll(this.stderr);
|
||||
} finally {
|
||||
this.stderr.close();
|
||||
}
|
||||
}
|
||||
|
||||
close(): void {
|
||||
close(this.rid);
|
||||
}
|
||||
|
|
|
@ -187,6 +187,17 @@ testPerm({ run: true }, async function runOutput() {
|
|||
p.close();
|
||||
});
|
||||
|
||||
testPerm({ run: true }, async function runStderrOutput() {
|
||||
const p = run({
|
||||
args: ["python", "-c", "import sys; sys.stderr.write('error')"],
|
||||
stderr: "piped"
|
||||
});
|
||||
const error = await p.stderrOutput();
|
||||
const s = new TextDecoder().decode(error);
|
||||
assertEquals(s, "error");
|
||||
p.close();
|
||||
});
|
||||
|
||||
testPerm({ run: true }, async function runEnv() {
|
||||
const p = run({
|
||||
args: [
|
||||
|
|
|
@ -398,8 +398,13 @@ async function main() {
|
|||
|
||||
const { code } = await p.status();
|
||||
|
||||
const rawOutput = await p.output();
|
||||
Deno.stdout.write(rawOutput);
|
||||
if (code === 0) {
|
||||
const rawOutput = await p.output();
|
||||
Deno.stdout.write(rawOutput);
|
||||
} else {
|
||||
const rawError = await p.stderrOutput();
|
||||
Deno.stdout.write(rawError);
|
||||
}
|
||||
|
||||
Deno.exit(code);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue