|
| 1 | +const { execFileSync } = require('child_process') |
| 2 | +const fs = require('fs') |
| 3 | +const os = require('os') |
| 4 | +const path = require('path') |
| 5 | + |
| 6 | +describe('dotenv run integration', () => { |
| 7 | + let directory |
| 8 | + let env |
| 9 | + const dotenvPackagePath = require.resolve('dotenv/package.json') |
| 10 | + const cli = path.resolve(path.dirname(dotenvPackagePath), require(dotenvPackagePath).bin.dotenv) |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + directory = fs.mkdtempSync(path.join(os.tmpdir(), 'react-native-dotenv-cli-')) |
| 14 | + env = { ...process.env } |
| 15 | + for (const key of Object.keys(env)) { |
| 16 | + if (/^(DOTENV_|RN_DOTENV_CLI_)/.test(key) || ['NODE_ENV', 'BABEL_ENV', 'APP_ENV'].includes(key)) { |
| 17 | + delete env[key] |
| 18 | + } |
| 19 | + } |
| 20 | + fs.writeFileSync(path.join(directory, '.env'), 'RN_DOTENV_CLI_URL=base\n') |
| 21 | + fs.writeFileSync(path.join(directory, '.env.local'), 'RN_DOTENV_CLI_URL=local\n') |
| 22 | + fs.writeFileSync(path.join(directory, '.env.staging'), 'RN_DOTENV_CLI_URL=staging\nRN_DOTENV_CLI_ONLY=extra\n') |
| 23 | + }) |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + fs.rmSync(directory, { recursive: true, force: true }) |
| 27 | + }) |
| 28 | + |
| 29 | + function transform (args, source, options = {}) { |
| 30 | + const script = ` |
| 31 | + const { transformSync } = require(${JSON.stringify(require.resolve('@babel/core'))}) |
| 32 | + const result = transformSync(${JSON.stringify(source)}, { |
| 33 | + configFile: false, |
| 34 | + babelrc: false, |
| 35 | + plugins: [[${JSON.stringify(require.resolve('../index.js'))}, ${JSON.stringify({ quiet: true, ...options })}]] |
| 36 | + }) |
| 37 | + console.log(result.code) |
| 38 | + ` |
| 39 | + return execFileSync(process.execPath, [cli, 'run', '-q', ...args, '--', process.execPath, '-e', script], { |
| 40 | + cwd: directory, |
| 41 | + env, |
| 42 | + encoding: 'utf8' |
| 43 | + }).trim() |
| 44 | + } |
| 45 | + |
| 46 | + it('inlines CLI values through imports while leaving CLI-only process.env references intact', () => { |
| 47 | + expect(transform(['-f', '.env.staging'], |
| 48 | + 'import { RN_DOTENV_CLI_URL, RN_DOTENV_CLI_ONLY } from "@env"; console.log(RN_DOTENV_CLI_URL, RN_DOTENV_CLI_ONLY, process.env.RN_DOTENV_CLI_URL, process.env.RN_DOTENV_CLI_ONLY)' |
| 49 | + )).toBe('console.log("staging", "extra", "staging", process.env.RN_DOTENV_CLI_ONLY);') |
| 50 | + }) |
| 51 | + |
| 52 | + it('preserves safe mode restrictions on CLI-only imports', () => { |
| 53 | + expect(transform(['-f', '.env.staging'], |
| 54 | + 'import { RN_DOTENV_CLI_URL, RN_DOTENV_CLI_ONLY } from "@env"; console.log(RN_DOTENV_CLI_URL, RN_DOTENV_CLI_ONLY)', |
| 55 | + { safe: true } |
| 56 | + )).toBe('console.log("staging", undefined);') |
| 57 | + }) |
| 58 | + |
| 59 | + it('gives shell values priority unless the CLI uses --override', () => { |
| 60 | + env.RN_DOTENV_CLI_URL = 'shell' |
| 61 | + const source = 'import { RN_DOTENV_CLI_URL } from "@env"; console.log(RN_DOTENV_CLI_URL)' |
| 62 | + expect(transform(['-f', '.env.staging'], source)).toBe('console.log("shell");') |
| 63 | + expect(transform(['--override', '-f', '.env.staging'], source)).toBe('console.log("staging");') |
| 64 | + }) |
| 65 | + |
| 66 | + it('gives CLI-loaded .env values priority over plugin-loaded .env.local values', () => { |
| 67 | + expect(transform([], 'import { RN_DOTENV_CLI_URL } from "@env"; console.log(RN_DOTENV_CLI_URL)')) |
| 68 | + .toBe('console.log("base");') |
| 69 | + }) |
| 70 | +}) |
0 commit comments