|
| 1 | +/** |
| 2 | + * F02 — where the environment comes from. |
| 3 | + * |
| 4 | + * These write real files and mutate the real `process.env`, because that is the |
| 5 | + * whole behaviour: `process.loadEnvFile` is Node's, and a test that stubbed it |
| 6 | + * would be asserting the shape of this module's own calls rather than the |
| 7 | + * precedence an operator actually gets. Every variable is named per test and |
| 8 | + * deleted afterwards — precedence here is "first write wins", so a name leaked |
| 9 | + * from one test would silently decide the next one's answer. |
| 10 | + */ |
| 11 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 12 | +import { tmpdir } from 'node:os' |
| 13 | +import { join } from 'node:path' |
| 14 | +import { afterEach, describe, expect, it } from 'vitest' |
| 15 | + |
| 16 | +import { findWorkspaceRoot, loadEnvFiles } from './env-files' |
| 17 | + |
| 18 | +const created: string[] = [] |
| 19 | +const touched: string[] = [] |
| 20 | + |
| 21 | +/** A throwaway workspace root: a directory with the marker file in it. */ |
| 22 | +function workspace(files: Record<string, string>): string { |
| 23 | + const root = mkdtempSync(join(tmpdir(), 'forum-env-')) |
| 24 | + created.push(root) |
| 25 | + writeFileSync(join(root, 'pnpm-workspace.yaml'), "packages:\n - 'packages/*'\n") |
| 26 | + for (const [name, contents] of Object.entries(files)) { |
| 27 | + writeFileSync(join(root, name), contents) |
| 28 | + } |
| 29 | + return root |
| 30 | +} |
| 31 | + |
| 32 | +/** A directory with no workspace above it. */ |
| 33 | +function orphanDir(): string { |
| 34 | + const dir = mkdtempSync(join(tmpdir(), 'forum-noworkspace-')) |
| 35 | + created.push(dir) |
| 36 | + return dir |
| 37 | +} |
| 38 | + |
| 39 | +/** Registers a variable for cleanup and returns its name. */ |
| 40 | +function owned(name: string): string { |
| 41 | + touched.push(name) |
| 42 | + return name |
| 43 | +} |
| 44 | + |
| 45 | +afterEach(() => { |
| 46 | + for (const name of touched.splice(0)) delete process.env[name] |
| 47 | + for (const dir of created.splice(0)) rmSync(dir, { recursive: true, force: true }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe('findWorkspaceRoot', () => { |
| 51 | + it('walks up to the directory holding pnpm-workspace.yaml', () => { |
| 52 | + const root = workspace({}) |
| 53 | + const deep = join(root, 'apps', 'forum') |
| 54 | + mkdirSync(deep, { recursive: true }) |
| 55 | + |
| 56 | + expect(findWorkspaceRoot(deep)).toBe(root) |
| 57 | + // Inclusive of the starting directory: the CLI is often run from the root. |
| 58 | + expect(findWorkspaceRoot(root)).toBe(root) |
| 59 | + }) |
| 60 | + |
| 61 | + it('gives up rather than guessing when there is no workspace above', () => { |
| 62 | + expect(findWorkspaceRoot(orphanDir())).toBeUndefined() |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +describe('loadEnvFiles', () => { |
| 67 | + it('loads .env from the workspace root when started from a nested app', () => { |
| 68 | + const name = owned('FORUM_TEST_FROM_ROOT') |
| 69 | + const root = workspace({ '.env': `${name}=from-dot-env\n` }) |
| 70 | + const deep = join(root, 'apps', 'cli') |
| 71 | + mkdirSync(deep, { recursive: true }) |
| 72 | + |
| 73 | + /* The case that motivated the module: `pnpm --filter @forum/cli start` runs |
| 74 | + with a cwd two levels below the file it needs. */ |
| 75 | + expect(loadEnvFiles(deep)).toEqual({ root, loaded: ['.env'] }) |
| 76 | + expect(process.env[name]).toBe('from-dot-env') |
| 77 | + }) |
| 78 | + |
| 79 | + it('prefers .env.local over .env', () => { |
| 80 | + const name = owned('FORUM_TEST_PRECEDENCE') |
| 81 | + const root = workspace({ |
| 82 | + '.env': `${name}=from-dot-env\n`, |
| 83 | + '.env.local': `${name}=from-dot-env-local\n`, |
| 84 | + }) |
| 85 | + |
| 86 | + expect(loadEnvFiles(root).loaded).toEqual(['.env.local', '.env']) |
| 87 | + expect(process.env[name]).toBe('from-dot-env-local') |
| 88 | + }) |
| 89 | + |
| 90 | + it('never overwrites a variable the environment already set', () => { |
| 91 | + const name = owned('FORUM_TEST_AMBIENT_WINS') |
| 92 | + process.env[name] = 'from-the-environment' |
| 93 | + const root = workspace({ '.env': `${name}=from-dot-env\n` }) |
| 94 | + |
| 95 | + loadEnvFiles(root) |
| 96 | + |
| 97 | + /* |
| 98 | + * The property CI, `docker run -e` and Playwright's `webServer.env` all |
| 99 | + * depend on. If a file could win, the e2e suite's explicit DATABASE_URL |
| 100 | + * would be quietly replaced by whatever a developer keeps in `.env`, and |
| 101 | + * the suite would write to their board. |
| 102 | + */ |
| 103 | + expect(process.env[name]).toBe('from-the-environment') |
| 104 | + }) |
| 105 | + |
| 106 | + it('reports the root and loads nothing when there are no env files', () => { |
| 107 | + const root = workspace({}) |
| 108 | + expect(loadEnvFiles(root)).toEqual({ root, loaded: [] }) |
| 109 | + }) |
| 110 | + |
| 111 | + it('is a no-op outside a workspace, which is the production case', () => { |
| 112 | + const orphan = orphanDir() |
| 113 | + // A `.env` here belongs to some other project, and is deliberately ignored. |
| 114 | + writeFileSync(join(orphan, '.env'), 'FORUM_TEST_STRAY=nope\n') |
| 115 | + |
| 116 | + expect(loadEnvFiles(orphan)).toEqual({ root: undefined, loaded: [] }) |
| 117 | + expect(process.env.FORUM_TEST_STRAY).toBeUndefined() |
| 118 | + }) |
| 119 | +}) |
0 commit comments