Files
bun.sh/test/js/bun/shell/shelloutput.test.ts
Zack Radisic 2b56451a11 Shell changes/fixes (#8846)
* Fix #8403

* Throw on error by default

* Add the shell promise utilities to `ShellOutput` and `ShellError`

* Fix tests

* [autofix.ci] apply automated fixes

* Fix memleak

* [autofix.ci] apply automated fixes

* Woops

* `Bun.gc(true)` in fd leak test

* fd leak test should check if `fd <= baseline`

* wtf

* oob check

* [autofix.ci] apply automated fixes

* Fix double free

* Fix #8550

* increase mem threshold for linux

* Requested changes and make not throw on by default

* [autofix.ci] apply automated fixes

* more requested changes

* Do destructuring in function definition

* delete

* Change shell output test to enable throwing

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2024-02-16 05:01:55 -08:00

39 lines
1.4 KiB
TypeScript

import { $, ShellError, ShellPromise } from "bun";
import { beforeAll, describe, test, expect } from "bun:test";
import { runWithErrorPromise } from "harness";
describe("ShellOutput + ShellError", () => {
test("output", async () => {
let output = await $`echo hi`;
expect(output.text()).toBe("hi\n");
output = await $`echo '{"hello": 123}'`;
expect(output.json()).toEqual({ hello: 123 });
output = await $`echo hello`;
expect(output.blob()).toEqual(new Blob([new TextEncoder().encode("hello")]));
});
test("error", async () => {
$.throws(true);
let output = await withErr($`echo hi; ls oogabooga`);
expect(output.stderr.toString()).toEqual("ls: oogabooga: No such file or directory\n");
expect(output.text()).toBe("hi\n");
output = await withErr($`echo '{"hello": 123}'; ls oogabooga`);
expect(output.stderr.toString()).toEqual("ls: oogabooga: No such file or directory\n");
expect(output.json()).toEqual({ hello: 123 });
output = await withErr($`echo hello; ls oogabooga`);
expect(output.stderr.toString()).toEqual("ls: oogabooga: No such file or directory\n");
expect(output.blob()).toEqual(new Blob([new TextEncoder().encode("hello")]));
});
});
async function withErr(promise: ShellPromise): Promise<ShellError> {
let err: ShellError | undefined;
try {
await promise;
} catch (e) {
err = e as ShellError;
}
expect(err).toBeDefined();
return err as ShellError;
}