|
| 1 | +/* @flow */ |
| 2 | +import path from 'path'; |
| 3 | +import {makeEnv} from '../../src/util/execute-lifecycle-script'; |
| 4 | +import Config from '../../src/config'; |
| 5 | +import {NoopReporter} from '../../src/reporters'; |
| 6 | + |
| 7 | +const cwd = path.resolve(__dirname); |
| 8 | + |
| 9 | +const initConfig = async cfg => { |
| 10 | + const reporter = new NoopReporter(); |
| 11 | + const config = new Config(reporter); |
| 12 | + await config.init(cfg); |
| 13 | + return config; |
| 14 | +}; |
| 15 | + |
| 16 | +const withManifest = async (stage, value, keys = {}) => { |
| 17 | + const config = await initConfig({}); |
| 18 | + config.maybeReadManifest = dir => { |
| 19 | + expect(dir).toEqual(cwd); |
| 20 | + return Promise.resolve({ |
| 21 | + scripts: {[stage]: value}, |
| 22 | + ...keys, |
| 23 | + }); |
| 24 | + }; |
| 25 | + return config; |
| 26 | +}; |
| 27 | + |
| 28 | +describe('makeEnv', () => { |
| 29 | + it('assigns npm_lifecycle_event to env', async () => { |
| 30 | + const stage = 'my-script'; |
| 31 | + const config = await initConfig({}); |
| 32 | + const env = await makeEnv(stage, cwd, config); |
| 33 | + expect(env.npm_lifecycle_event).toEqual(stage); |
| 34 | + }); |
| 35 | + |
| 36 | + it('assigns NODE_ENV production', async () => { |
| 37 | + const config = await initConfig({production: true}); |
| 38 | + const env = await makeEnv('test-script', cwd, config); |
| 39 | + expect(env.NODE_ENV).toEqual('production'); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('npm_package_*', () => { |
| 43 | + it('assigns npm_lifecycle_script if manifest has a matching script', async () => { |
| 44 | + const stage = 'test-script'; |
| 45 | + const value = 'run this script'; |
| 46 | + const config = await withManifest(stage, value); |
| 47 | + const env = await makeEnv(stage, cwd, config); |
| 48 | + expect(env.npm_lifecycle_script).toEqual(value); |
| 49 | + }); |
| 50 | + |
| 51 | + it('does not overwrite npm_lifecycle_script if manifest does not have a matching script', async () => { |
| 52 | + const stage = 'test-script'; |
| 53 | + const config = await withManifest('wrong-stage', 'new value'); |
| 54 | + const env = await makeEnv(stage, cwd, config); |
| 55 | + expect(env.npm_lifecycle_script).toEqual(process.env.npm_lifecycle_script); |
| 56 | + }); |
| 57 | + |
| 58 | + it('recursively adds keys separated by _', async () => { |
| 59 | + const config = await withManifest('', '', { |
| 60 | + top: 'first', |
| 61 | + recursive: { |
| 62 | + key: 'value', |
| 63 | + more: {another: 'what'}, |
| 64 | + }, |
| 65 | + }); |
| 66 | + const env = await makeEnv('', cwd, config); |
| 67 | + expect(env['npm_package_top']).toEqual('first'); |
| 68 | + expect(env['npm_package_recursive_key']).toEqual('value'); |
| 69 | + expect(env['npm_package_recursive_more_another']).toEqual('what'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('replaces invalid chars with _', async () => { |
| 73 | + const config = await withManifest('', '', {'in^va!d_key': 'test'}); |
| 74 | + const env = await makeEnv('', cwd, config); |
| 75 | + expect(env['npm_package_in_va_d_key']).toEqual('test'); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('npm_package_config_*', () => { |
| 80 | + it('overwrites npm_package_config_* keys from yarn config or npm config', async () => { |
| 81 | + const name = 'manifest-name'; |
| 82 | + const config = await withManifest('', '', {name, config: {key: 'value', keytwo: 'valuetwo'}}); |
| 83 | + config.registries.yarn.config = {[`${name}:key`]: 'replaced'}; |
| 84 | + config.registries.npm.config = {[`${name}:keytwo`]: 'also replaced'}; |
| 85 | + const env = await makeEnv('', cwd, config); |
| 86 | + expect(env['npm_package_config_key']).toEqual('replaced'); |
| 87 | + expect(env['npm_package_config_keytwo']).toEqual('also replaced'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('does not overwrite if the name does not match the manifest', async () => { |
| 91 | + const name = 'manifest-name'; |
| 92 | + const config = await withManifest('', '', {name, config: {key: 'value', keytwo: 'valuetwo'}}); |
| 93 | + config.registries.yarn.config = {'wrong-name:key': 'replaced'}; |
| 94 | + config.registries.npm.config = {'another-name:keytwo': 'also replaced'}; |
| 95 | + const env = await makeEnv('', cwd, config); |
| 96 | + expect(env['npm_package_config_key']).toEqual('value'); |
| 97 | + expect(env['npm_package_config_keytwo']).toEqual('valuetwo'); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments