|
| 1 | +import assert from "node:assert"; |
| 2 | +import { mkdir, readFile, writeFile } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import dedent from "ts-dedent"; |
| 5 | +import { bundleWorker } from "../deployment-bundle/bundle"; |
| 6 | +import { noopModuleCollector } from "../deployment-bundle/module-collection"; |
| 7 | +import { isProcessEnvPopulated } from "../process-env"; |
| 8 | +import { mockConsoleMethods } from "./helpers/mock-console"; |
| 9 | +import { runInTempDir } from "./helpers/run-in-tmp"; |
| 10 | + |
| 11 | +/* |
| 12 | + * This file contains inline comments with the word "javascript" |
| 13 | + * This signals to a compatible editor extension that the template string |
| 14 | + * contents should be syntax-highlighted as JavaScript. One such extension |
| 15 | + * is zjcompt.es6-string-javascript, but there are others. |
| 16 | + */ |
| 17 | + |
| 18 | +async function seedFs(files: Record<string, string>): Promise<void> { |
| 19 | + for (const [location, contents] of Object.entries(files)) { |
| 20 | + await mkdir(path.dirname(location), { recursive: true }); |
| 21 | + await writeFile(location, contents); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe("isProcessEnvPopulated", () => { |
| 26 | + test("default", () => { |
| 27 | + expect(isProcessEnvPopulated(undefined, ["nodejs_compat"])).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + test("future date", () => { |
| 31 | + expect(isProcessEnvPopulated("2026-01-01", ["nodejs_compat"])).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + test("old date", () => { |
| 35 | + expect(isProcessEnvPopulated("2000-01-01", ["nodejs_compat"])).toBe(false); |
| 36 | + }); |
| 37 | + |
| 38 | + test("switch date", () => { |
| 39 | + expect(isProcessEnvPopulated("2025-04-01", ["nodejs_compat"])).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + test("old date, but with flag", () => { |
| 43 | + expect( |
| 44 | + isProcessEnvPopulated("2000-01-01", [ |
| 45 | + "nodejs_compat", |
| 46 | + "nodejs_compat_populate_process_env", |
| 47 | + ]) |
| 48 | + ).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + test("old date, with disable flag", () => { |
| 52 | + expect( |
| 53 | + isProcessEnvPopulated("2000-01-01", [ |
| 54 | + "nodejs_compat", |
| 55 | + "nodejs_compat_do_not_populate_process_env", |
| 56 | + ]) |
| 57 | + ).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + test("future date, but with disable flag", () => { |
| 61 | + expect( |
| 62 | + isProcessEnvPopulated("2026-01-01", [ |
| 63 | + "nodejs_compat", |
| 64 | + "nodejs_compat_do_not_populate_process_env", |
| 65 | + ]) |
| 66 | + ).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + test("future date, with enable flag", () => { |
| 70 | + expect( |
| 71 | + isProcessEnvPopulated("2026-01-01", [ |
| 72 | + "nodejs_compat", |
| 73 | + "nodejs_compat_populate_process_env", |
| 74 | + ]) |
| 75 | + ).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + test("future date without nodejs_compat", () => { |
| 79 | + expect(isProcessEnvPopulated("2026-01-01")).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + test("future date, with enable flag but without nodejs_compat", () => { |
| 83 | + expect( |
| 84 | + isProcessEnvPopulated("2026-01-01", [ |
| 85 | + "nodejs_compat_populate_process_env", |
| 86 | + ]) |
| 87 | + ).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + test("errors with disable and enable flags specified", () => { |
| 91 | + try { |
| 92 | + isProcessEnvPopulated("2024-01-01", [ |
| 93 | + "nodejs_compat_populate_process_env", |
| 94 | + "nodejs_compat_do_not_populate_process_env", |
| 95 | + ]); |
| 96 | + assert(false, "Unreachable"); |
| 97 | + } catch (e) { |
| 98 | + expect(e).toMatchInlineSnapshot( |
| 99 | + `[Error: Can't both enable and disable a flag]` |
| 100 | + ); |
| 101 | + } |
| 102 | + }); |
| 103 | +}); |
0 commit comments