|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | +const assert = require('node:assert'); |
| 5 | +const fs = require('node:fs'); |
| 6 | +const path = require('node:path'); |
| 7 | +const { spawnSync } = require('node:child_process'); |
| 8 | +const tmpdir = require('../common/tmpdir'); |
| 9 | + |
| 10 | +// Env file flags are options of Node.js only when they appear before the |
| 11 | +// script name. After it they belong to the script, which must receive them |
| 12 | +// untouched without the file being read. |
| 13 | + |
| 14 | +tmpdir.refresh(); |
| 15 | + |
| 16 | +const envFile = path.join(tmpdir.path, 'test.env'); |
| 17 | +fs.writeFileSync(envFile, 'FOO=fromfile\nNODE_OPTIONS=--stack-trace-limit=7\n'); |
| 18 | + |
| 19 | +const script = path.join(tmpdir.path, 'script.js'); |
| 20 | +fs.writeFileSync(script, ` |
| 21 | +console.log(JSON.stringify({ |
| 22 | + foo: process.env.FOO ?? null, |
| 23 | + limit: Error.stackTraceLimit, |
| 24 | + args: process.argv.slice(2), |
| 25 | +})); |
| 26 | +`); |
| 27 | + |
| 28 | +const env = { ...process.env }; |
| 29 | +delete env.NODE_OPTIONS; |
| 30 | +delete env.FOO; |
| 31 | + |
| 32 | +// Builds without support for NODE_OPTIONS ignore the one set in the env file. |
| 33 | +const fileLimit = process.config.variables.node_without_node_options ? 10 : 7; |
| 34 | + |
| 35 | +function run(args) { |
| 36 | + const child = spawnSync(process.execPath, args, { env, encoding: 'utf8' }); |
| 37 | + assert.strictEqual(child.status, 0, child.stderr); |
| 38 | + return JSON.parse(child.stdout); |
| 39 | +} |
| 40 | + |
| 41 | +// Before the script the file is still read. |
| 42 | +for (const flagArgs of [ |
| 43 | + [`--env-file=${envFile}`], |
| 44 | + ['--env-file', envFile], |
| 45 | + [`--env-file-if-exists=${envFile}`], |
| 46 | + ['--env-file-if-exists', envFile], |
| 47 | +]) { |
| 48 | + assert.deepStrictEqual(run([...flagArgs, script, 'one']), |
| 49 | + { foo: 'fromfile', limit: fileLimit, args: ['one'] }); |
| 50 | +} |
| 51 | + |
| 52 | +// After the script the file is not read and the arguments are kept. |
| 53 | +for (const flagArgs of [ |
| 54 | + [`--env-file=${envFile}`], |
| 55 | + ['--env-file', envFile], |
| 56 | + [`--env-file-if-exists=${envFile}`], |
| 57 | + ['--env-file-if-exists', envFile], |
| 58 | + ['--env-file=missing.env'], |
| 59 | +]) { |
| 60 | + assert.deepStrictEqual(run([script, ...flagArgs]), |
| 61 | + { foo: null, limit: 10, args: flagArgs }); |
| 62 | + assert.deepStrictEqual(run([script, 'one', ...flagArgs]), |
| 63 | + { foo: null, limit: 10, args: ['one', ...flagArgs] }); |
| 64 | +} |
| 65 | + |
| 66 | +// A file before the script is read while the same flag after it is ignored. |
| 67 | +assert.deepStrictEqual( |
| 68 | + run([`--env-file=${envFile}`, script, '--env-file=missing.env']), |
| 69 | + { foo: 'fromfile', limit: fileLimit, args: ['--env-file=missing.env'] }); |
| 70 | + |
| 71 | +// Nothing is read after the separator either. |
| 72 | +assert.deepStrictEqual( |
| 73 | + run(['--', script, `--env-file=${envFile}`]), |
| 74 | + { foo: null, limit: 10, args: [`--env-file=${envFile}`] }); |
| 75 | + |
| 76 | +// A missing file before the script still stops the startup. |
| 77 | +{ |
| 78 | + const child = spawnSync(process.execPath, |
| 79 | + ['--env-file=missing.env', script], |
| 80 | + { env, encoding: 'utf8' }); |
| 81 | + assert.strictEqual(child.status, 9); |
| 82 | + assert.match(child.stderr, /missing\.env: not found/); |
| 83 | +} |
| 84 | + |
| 85 | +// Options of Node.js between the file and the script do not hide it. |
| 86 | +assert.deepStrictEqual( |
| 87 | + run([`--env-file=${envFile}`, '--no-warnings', '-r', 'node:path', script]), |
| 88 | + { foo: 'fromfile', limit: fileLimit, args: [] }); |
0 commit comments