diff options
author | 2023-05-28 03:43:08 +0000 | |
---|---|---|
committer | 2023-05-27 20:43:08 -0700 | |
commit | 4ba3ed512ac8407603c99d56d931038e0bdf0884 (patch) | |
tree | e72244662fa2dccc9ee2808077e6e836b3aee2a4 | |
parent | f870f97a945be8d03a99b1ba8a064e2b8579a0c3 (diff) | |
download | bun-4ba3ed512ac8407603c99d56d931038e0bdf0884.tar.gz bun-4ba3ed512ac8407603c99d56d931038e0bdf0884.tar.zst bun-4ba3ed512ac8407603c99d56d931038e0bdf0884.zip |
Implement process.env.npm_lifecycle_event (#3097)
* Update run_command.zig
* Update env.test.ts
* Add files via upload
* Update run-process-env.test.ts
* Update env.test.ts
* Update harness.ts
-rw-r--r-- | src/cli/run_command.zig | 1 | ||||
-rw-r--r-- | test/cli/run/env.test.ts | 44 | ||||
-rw-r--r-- | test/cli/run/run-process-env.test.ts | 16 | ||||
-rw-r--r-- | test/harness.ts | 62 |
4 files changed, 80 insertions, 43 deletions
diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index 17b12dcfb..24304f169 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -960,6 +960,7 @@ pub const RunCommand = struct { var ORIGINAL_PATH: string = ""; var this_bundler: bundler.Bundler = undefined; var root_dir_info = try configureEnvForRun(ctx, &this_bundler, null, &ORIGINAL_PATH, log_errors, force_using_bun); + this_bundler.env.map.putDefault("npm_lifecycle_event", script_name_to_search) catch unreachable; if (root_dir_info.enclosing_package_json) |package_json| { if (package_json.scripts) |scripts| { switch (script_name_to_search.len) { diff --git a/test/cli/run/env.test.ts b/test/cli/run/env.test.ts index 328162f0b..1a4ca7cb7 100644 --- a/test/cli/run/env.test.ts +++ b/test/cli/run/env.test.ts @@ -1,47 +1,5 @@ import { describe, expect, test } from "bun:test"; -import os from "os"; -import fs from "fs"; -import path from "path"; -import { bunEnv, bunExe } from "harness"; - -function tempDirWithFiles(basename: string, files: Record<string, string>) { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), basename + "_")); - for (const [name, contents] of Object.entries(files)) { - fs.writeFileSync(path.join(dir, name), contents); - } - return dir; -} - -function bunRun(file: string, env?: Record<string, string>) { - const result = Bun.spawnSync([bunExe(), file], { - cwd: path.dirname(file), - env: { - ...bunEnv, - NODE_ENV: undefined, - ...env, - }, - }); - if (!result.success) throw new Error(result.stderr.toString("utf8")); - return { - stdout: result.stdout.toString("utf8").trim(), - stderr: result.stderr.toString("utf8").trim(), - }; -} -function bunTest(file: string, env?: Record<string, string>) { - const result = Bun.spawnSync([bunExe(), "test", path.basename(file)], { - cwd: path.dirname(file), - env: { - ...bunEnv, - NODE_ENV: undefined, - ...env, - }, - }); - if (!result.success) throw new Error(result.stderr.toString("utf8")); - return { - stdout: result.stdout.toString("utf8").trim(), - stderr: result.stderr.toString("utf8").trim(), - }; -} +import { bunRun, bunTest, tempDirWithFiles } from "harness"; describe(".env file is loaded", () => { test(".env", () => { diff --git a/test/cli/run/run-process-env.test.ts b/test/cli/run/run-process-env.test.ts new file mode 100644 index 000000000..d245ff80c --- /dev/null +++ b/test/cli/run/run-process-env.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, test } from "bun:test"; +import { bunRunAsScript, tempDirWithFiles } from "harness"; + +describe("process.env", () => { + test("npm_lifecycle_event", () => { + const scriptName = 'start:dev'; + + const dir = tempDirWithFiles("processenv", { + "package.json": `{'scripts': {'${scriptName}': 'bun run index.ts'}}`, + "index.ts": "console.log(process.env.npm_lifecycle_event);", + }); + + const { stdout } = bunRunAsScript(dir, scriptName); + expect(stdout).toBe(scriptName); + }); +}); diff --git a/test/harness.ts b/test/harness.ts index bb27f53a4..72670156d 100644 --- a/test/harness.ts +++ b/test/harness.ts @@ -1,5 +1,8 @@ import { gc as bunGC, unsafe } from "bun"; import { heapStats } from "bun:jsc"; +import path from "path"; +import fs from 'fs'; +import os from 'os'; export const bunEnv: any = { ...process.env, @@ -75,3 +78,62 @@ export function hideFromStackTrace(block: CallableFunction) { writable: true, }); } + +export function tempDirWithFiles(basename: string, files: Record<string, string>) { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), basename + "_")); + for (const [name, contents] of Object.entries(files)) { + fs.writeFileSync(path.join(dir, name), contents); + } + return dir; +} + +export function bunRun(file: string, env?: Record<string, string>) { + const result = Bun.spawnSync([bunExe(), file], { + cwd: path.dirname(file), + env: { + ...bunEnv, + NODE_ENV: undefined, + ...env, + }, + }); + if (!result.success) throw new Error(result.stderr.toString("utf8")); + return { + stdout: result.stdout.toString("utf8").trim(), + stderr: result.stderr.toString("utf8").trim(), + }; +} + +export function bunTest(file: string, env?: Record<string, string>) { + const result = Bun.spawnSync([bunExe(), "test", path.basename(file)], { + cwd: path.dirname(file), + env: { + ...bunEnv, + NODE_ENV: undefined, + ...env, + }, + }); + if (!result.success) throw new Error(result.stderr.toString("utf8")); + return { + stdout: result.stdout.toString("utf8").trim(), + stderr: result.stderr.toString("utf8").trim(), + }; +} + +export function bunRunAsScript(dir: string, script: string, env?: Record<string, string>) { + const result = Bun.spawnSync([bunExe(), `run`, `${script}`], { + cwd: dir, + env: { + ...bunEnv, + NODE_ENV: undefined, + ...env, + }, + }); + + if (!result.success) + throw new Error(result.stderr.toString("utf8")); + + return { + stdout: result.stdout.toString("utf8").trim(), + stderr: result.stderr.toString("utf8").trim(), + }; +} |