|
| 1 | +const t = require('tap') |
| 2 | +const setEnvs = require('../../../lib/config/set-envs.js') |
| 3 | +const { defaults } = require('../../../lib/config/defaults.js') |
| 4 | + |
| 5 | +t.test('set envs that are not defaults and not already in env', t => { |
| 6 | + const envConf = Object.create(defaults) |
| 7 | + const cliConf = Object.create(envConf) |
| 8 | + const npm = { |
| 9 | + config: { |
| 10 | + list: [cliConf, envConf] |
| 11 | + } |
| 12 | + } |
| 13 | + const extras = { |
| 14 | + npm_execpath: require.main.filename, |
| 15 | + npm_node_execpath: process.execPath, |
| 16 | + npm_command: undefined |
| 17 | + } |
| 18 | + const env = {} |
| 19 | + setEnvs(npm, env) |
| 20 | + t.strictSame(env, { ...extras }, 'no new environment vars to create') |
| 21 | + envConf.call = 'me, maybe' |
| 22 | + setEnvs(npm, env) |
| 23 | + t.strictSame(env, { ...extras }, 'no new environment vars to create, already in env') |
| 24 | + delete envConf.call |
| 25 | + cliConf.call = 'me, maybe' |
| 26 | + setEnvs(npm, env) |
| 27 | + t.strictSame(env, { |
| 28 | + ...extras, |
| 29 | + npm_config_call: 'me, maybe' |
| 30 | + }, 'set in env, because changed from default in cli') |
| 31 | + envConf.call = 'me, maybe' |
| 32 | + cliConf.call = '' |
| 33 | + cliConf['node-options'] = 'some options for node' |
| 34 | + setEnvs(npm, env) |
| 35 | + t.strictSame(env, { |
| 36 | + ...extras, |
| 37 | + npm_config_call: '', |
| 38 | + npm_config_node_options: 'some options for node', |
| 39 | + NODE_OPTIONS: 'some options for node' |
| 40 | + }, 'set in env, because changed from default in env, back to default in cli') |
| 41 | + t.end() |
| 42 | +}) |
| 43 | + |
| 44 | +t.test('set envs that are not defaults and not already in env, array style', t => { |
| 45 | + const envConf = Object.create(defaults) |
| 46 | + const cliConf = Object.create(envConf) |
| 47 | + const npm = { |
| 48 | + config: { |
| 49 | + list: [cliConf, envConf] |
| 50 | + } |
| 51 | + } |
| 52 | + const extras = { |
| 53 | + npm_execpath: require.main.filename, |
| 54 | + npm_node_execpath: process.execPath, |
| 55 | + npm_command: undefined |
| 56 | + } |
| 57 | + const env = {} |
| 58 | + setEnvs(npm, env) |
| 59 | + t.strictSame(env, { ...extras }, 'no new environment vars to create') |
| 60 | + envConf.omit = ['dev'] |
| 61 | + setEnvs(npm, env) |
| 62 | + t.strictSame(env, { ...extras }, 'no new environment vars to create, already in env') |
| 63 | + delete envConf.omit |
| 64 | + cliConf.omit = ['dev', 'optional'] |
| 65 | + setEnvs(npm, env) |
| 66 | + t.strictSame(env, { |
| 67 | + ...extras, |
| 68 | + npm_config_omit: 'dev\n\noptional' |
| 69 | + }, 'set in env, because changed from default in cli') |
| 70 | + envConf.omit = ['optional', 'peer'] |
| 71 | + cliConf.omit = [] |
| 72 | + setEnvs(npm, env) |
| 73 | + t.strictSame(env, { |
| 74 | + ...extras, |
| 75 | + npm_config_omit: '' |
| 76 | + }, 'set in env, because changed from default in env, back to default in cli') |
| 77 | + t.end() |
| 78 | +}) |
| 79 | + |
| 80 | +t.test('set envs that are not defaults and not already in env, boolean edition', t => { |
| 81 | + const envConf = Object.create(defaults) |
| 82 | + const cliConf = Object.create(envConf) |
| 83 | + const npm = { |
| 84 | + config: { |
| 85 | + list: [cliConf, envConf] |
| 86 | + } |
| 87 | + } |
| 88 | + const extras = { |
| 89 | + npm_execpath: require.main.filename, |
| 90 | + npm_node_execpath: process.execPath, |
| 91 | + npm_command: undefined |
| 92 | + } |
| 93 | + const env = {} |
| 94 | + setEnvs(npm, env) |
| 95 | + t.strictSame(env, { ...extras }, 'no new environment vars to create') |
| 96 | + envConf.audit = false |
| 97 | + setEnvs(npm, env) |
| 98 | + t.strictSame(env, { ...extras }, 'no new environment vars to create, already in env') |
| 99 | + delete envConf.audit |
| 100 | + cliConf.audit = false |
| 101 | + cliConf.ignoreObjects = { |
| 102 | + some: { object: 12345 } |
| 103 | + } |
| 104 | + setEnvs(npm, env) |
| 105 | + t.strictSame(env, { |
| 106 | + ...extras, |
| 107 | + npm_config_audit: '' |
| 108 | + }, 'set in env, because changed from default in cli') |
| 109 | + envConf.audit = false |
| 110 | + cliConf.audit = true |
| 111 | + setEnvs(npm, env) |
| 112 | + t.strictSame(env, { |
| 113 | + ...extras, |
| 114 | + npm_config_audit: 'true' |
| 115 | + }, 'set in env, because changed from default in env, back to default in cli') |
| 116 | + t.end() |
| 117 | +}) |
| 118 | + |
| 119 | +t.test('default to process.env', t => { |
| 120 | + const envConf = Object.create(defaults) |
| 121 | + const cliConf = Object.create(envConf) |
| 122 | + const npm = { |
| 123 | + config: { |
| 124 | + list: [cliConf, envConf] |
| 125 | + } |
| 126 | + } |
| 127 | + const extras = { |
| 128 | + npm_execpath: require.main.filename, |
| 129 | + npm_node_execpath: process.execPath, |
| 130 | + npm_command: undefined |
| 131 | + } |
| 132 | + const env = {} |
| 133 | + const envDescriptor = Object.getOwnPropertyDescriptor(process, 'env') |
| 134 | + Object.defineProperty(process, 'env', { |
| 135 | + value: env, |
| 136 | + configurable: true, |
| 137 | + enumerable: true, |
| 138 | + writable: true |
| 139 | + }) |
| 140 | + t.teardown(() => Object.defineProperty(process, env, envDescriptor)) |
| 141 | + |
| 142 | + setEnvs(npm) |
| 143 | + t.strictSame(env, { ...extras }, 'no new environment vars to create') |
| 144 | + envConf.audit = false |
| 145 | + setEnvs(npm) |
| 146 | + t.strictSame(env, { ...extras }, 'no new environment vars to create, already in env') |
| 147 | + delete envConf.audit |
| 148 | + cliConf.audit = false |
| 149 | + cliConf.ignoreObjects = { |
| 150 | + some: { object: 12345 } |
| 151 | + } |
| 152 | + setEnvs(npm) |
| 153 | + t.strictSame(env, { |
| 154 | + ...extras, |
| 155 | + npm_config_audit: '' |
| 156 | + }, 'set in env, because changed from default in cli') |
| 157 | + envConf.audit = false |
| 158 | + cliConf.audit = true |
| 159 | + setEnvs(npm) |
| 160 | + t.strictSame(env, { |
| 161 | + ...extras, |
| 162 | + npm_config_audit: 'true' |
| 163 | + }, 'set in env, because changed from default in env, back to default in cli') |
| 164 | + t.end() |
| 165 | +}) |
0 commit comments