mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Add tests for file path redirection + tweak
This commit is contained in:
@@ -83,15 +83,25 @@ In memory-constrained environments, use the `--smol` flag to reduce memory usage
|
||||
$ bun --smol run index.tsx
|
||||
```
|
||||
|
||||
### `-`
|
||||
### `bun run -` to pipe code from stdin
|
||||
|
||||
Execute code from the CLI without storing it in a file first by piping Javascript or Typescript directly to Bun.
|
||||
`bun run -` lets you read JavaScript, TypeScript, TSX, or JSX from stdin and execute it without writing to a temporary file first.
|
||||
|
||||
```bash
|
||||
$ echo "console.log('Hello')" | bun run -
|
||||
Hello
|
||||
```
|
||||
|
||||
You can also use `bun run -` to redirect files into Bun. For example, to run a `.js` file as if it were a `.ts` file:
|
||||
|
||||
```bash
|
||||
$ echo "console.log!('This is TypeScript!' as any)" > secretly-typescript.js
|
||||
$ bun run - < secretly-typescript.js
|
||||
Hello
|
||||
```
|
||||
|
||||
For convenience, all code is treated as TypeScript with JSX support when using `bun run -`.
|
||||
|
||||
## Run a `package.json` script
|
||||
|
||||
{% note %}
|
||||
|
||||
@@ -41,46 +41,77 @@ describe("bun -e", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("echo | bun run -", () => {
|
||||
function group(run: (code: string) => ReturnType<typeof Bun.spawnSync>) {
|
||||
test("it works", async () => {
|
||||
let { stdout } = Bun.spawnSync({
|
||||
cmd: [bunExe(), "run", "-"],
|
||||
env: bunEnv,
|
||||
stdin: Buffer.from('console.log("hello world")'),
|
||||
});
|
||||
const { stdout } = run('console.log("hello world")');
|
||||
expect(stdout.toString("utf8")).toEqual("hello world\n");
|
||||
});
|
||||
|
||||
test("it gets a correct specifer", async () => {
|
||||
let { stdout } = Bun.spawnSync({
|
||||
cmd: [bunExe(), "run", "-"],
|
||||
env: bunEnv,
|
||||
stdin: Buffer.from("console.log(import.meta.path)"),
|
||||
});
|
||||
const { stdout } = run("console.log(import.meta.path)");
|
||||
expect(stdout.toString("utf8")).toEndWith(sep + "[stdin]\n");
|
||||
});
|
||||
|
||||
test("it can require", async () => {
|
||||
let { stdout } = Bun.spawnSync({
|
||||
cmd: [bunExe(), "run", "-"],
|
||||
env: bunEnv,
|
||||
stdin: Buffer.from(`
|
||||
const { stdout } = run(`
|
||||
const process = require("node:process");
|
||||
console.log(process.platform);
|
||||
`),
|
||||
});
|
||||
`);
|
||||
expect(stdout.toString("utf8")).toEqual(process.platform + "\n");
|
||||
});
|
||||
|
||||
test("it can import", async () => {
|
||||
let { stdout } = Bun.spawnSync({
|
||||
cmd: [bunExe(), "run", "-"],
|
||||
env: bunEnv,
|
||||
stdin: Buffer.from(`
|
||||
const { stdout } = run(`
|
||||
import * as process from "node:process";
|
||||
console.log(process.platform);
|
||||
`),
|
||||
});
|
||||
`);
|
||||
expect(stdout.toString("utf8")).toEqual(process.platform + "\n");
|
||||
});
|
||||
}
|
||||
|
||||
describe("bun run - < file-path.js", () => {
|
||||
function run(code: string) {
|
||||
const file = join(tmpdir(), "bun-run-eval-test.js");
|
||||
require("fs").writeFileSync(file, code);
|
||||
try {
|
||||
const result = Bun.spawnSync({
|
||||
cmd: ["bash", "-c", `${bunExe()} run - < ${file}`],
|
||||
env: bunEnv,
|
||||
stderr: "inherit",
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
queueMicrotask(() => {
|
||||
throw new Error("bun run - < file-path.js failed");
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
} finally {
|
||||
try {
|
||||
require("fs").unlinkSync(file);
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
|
||||
group(run);
|
||||
});
|
||||
|
||||
describe("echo | bun run -", () => {
|
||||
function run(code: string) {
|
||||
const result = Bun.spawnSync({
|
||||
cmd: [bunExe(), "run", "-"],
|
||||
env: bunEnv,
|
||||
stdin: Buffer.from(code),
|
||||
});
|
||||
if (!result.success) {
|
||||
queueMicrotask(() => {
|
||||
throw new Error("bun run - failed");
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
group(run);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user