|
3 | 3 | const { describe, it } = require('mocha') |
4 | 4 | const assert = require('assert') |
5 | 5 | const gyp = require('../lib/node-gyp') |
| 6 | +const log = require('../lib/log') |
6 | 7 |
|
7 | 8 | describe('options', function () { |
8 | 9 | it('options in environment', () => { |
9 | 10 | // `npm test` dumps a ton of npm_config_* variables in the environment. |
10 | 11 | Object.keys(process.env) |
11 | | - .filter((key) => /^npm_config_/.test(key)) |
| 12 | + .filter((key) => /^npm_config_/i.test(key) || /^npm_package_config_node_gyp_/i.test(key)) |
12 | 13 | .forEach((key) => { delete process.env[key] }) |
13 | 14 |
|
14 | 15 | // in some platforms, certain keys are stubborn and cannot be removed |
15 | 16 | const keys = Object.keys(process.env) |
16 | | - .filter((key) => /^npm_config_/.test(key)) |
| 17 | + .filter((key) => /^npm_config_/i.test(key) || /^npm_package_config_node_gyp_/i.test(key)) |
17 | 18 | .map((key) => key.substring('npm_config_'.length)) |
18 | | - .concat('argv', 'x') |
| 19 | + |
| 20 | + // Environment variables with the following prefixes should be added to opts. |
| 21 | + // - `npm_config_` for npm versions before v11. |
| 22 | + // - `npm_package_config_node_gyp_` for npm versions 11 and later. |
19 | 23 |
|
20 | 24 | // Zero-length keys should get filtered out. |
21 | 25 | process.env.npm_config_ = '42' |
| 26 | + process.env.npm_package_config_node_gyp_ = '42' |
22 | 27 | // Other keys should get added. |
| 28 | + process.env.npm_package_config_node_gyp_foo = '42' |
23 | 29 | process.env.npm_config_x = '42' |
24 | | - // Except loglevel. |
25 | | - process.env.npm_config_loglevel = 'debug' |
| 30 | + process.env.npm_config_y = '41' |
| 31 | + // Package config should take precedence over npm_config_ keys. |
| 32 | + process.env.npm_package_config_node_gyp_y = '42' |
| 33 | + // loglevel does not get added to opts but will change the logger's level. |
| 34 | + process.env.npm_config_loglevel = 'silly' |
26 | 35 |
|
27 | 36 | const g = gyp() |
| 37 | + |
| 38 | + assert.strictEqual(log.logger.level.id, 'info') |
| 39 | + |
28 | 40 | g.parseArgv(['rebuild']) // Also sets opts.argv. |
29 | 41 |
|
30 | | - assert.deepStrictEqual(Object.keys(g.opts).sort(), keys.sort()) |
| 42 | + assert.strictEqual(log.logger.level.id, 'silly') |
| 43 | + |
| 44 | + assert.deepStrictEqual(Object.keys(g.opts).sort(), [...keys, 'argv', 'x', 'y', 'foo'].sort()) |
| 45 | + assert.strictEqual(g.opts['x'], '42') |
| 46 | + assert.strictEqual(g.opts['y'], '42') |
| 47 | + assert.strictEqual(g.opts['foo'], '42') |
31 | 48 | }) |
32 | 49 |
|
33 | 50 | it('options with spaces in environment', () => { |
|
0 commit comments