File: execute-lifecycle-script.js

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (124 lines) | stat: -rw-r--r-- 4,766 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* @flow */
import path from 'path';
import {IGNORE_MANIFEST_KEYS, makeEnv} from '../../src/util/execute-lifecycle-script';
import Config from '../../src/config';
import {NoopReporter} from '../../src/reporters';

const cwd = path.resolve(__dirname);

const initConfig = async cfg => {
  const reporter = new NoopReporter();
  const config = new Config(reporter);
  await config.init(cfg);
  return config;
};

const withManifest = async (stage, value, keys = {}) => {
  const config = await initConfig({});
  config.maybeReadManifest = dir => {
    expect(dir).toEqual(cwd);
    return Promise.resolve({
      scripts: {[stage]: value},
      ...keys,
    });
  };
  return config;
};

describe('makeEnv', () => {
  it('assigns npm_lifecycle_event to env', async () => {
    const stage = 'my-script';
    const config = await initConfig({});
    const env = await makeEnv(stage, cwd, config);
    expect(env.npm_lifecycle_event).toEqual(stage);
  });

  it('assigns NODE_ENV production', async () => {
    const config = await initConfig({production: true});
    const env = await makeEnv('test-script', cwd, config);
    expect(env.NODE_ENV).toEqual('production');
  });

  it('assigns INIT_CWD to env', async () => {
    const config = await initConfig();
    const env = await makeEnv('test-script', cwd, config);
    expect(env.INIT_CWD).toEqual(process.cwd());
  });

  describe('npm_package_*', () => {
    it('assigns npm_lifecycle_script if manifest has a matching script', async () => {
      const stage = 'test-script';
      const value = 'run this script';
      const config = await withManifest(stage, value);
      const env = await makeEnv(stage, cwd, config);
      expect(env.npm_lifecycle_script).toEqual(value);
    });

    it('does not overwrite npm_lifecycle_script if manifest does not have a matching script', async () => {
      const stage = 'test-script';
      const config = await withManifest('wrong-stage', 'new value');
      const env = await makeEnv(stage, cwd, config);
      expect(env.npm_lifecycle_script).toEqual(process.env.npm_lifecycle_script);
    });

    it('recursively adds keys separated by _', async () => {
      const config = await withManifest('', '', {
        top: 'first',
        recursive: {
          key: 'value',
          more: {another: 'what'},
        },
      });
      const env = await makeEnv('', cwd, config);
      expect(env['npm_package_top']).toEqual('first');
      expect(env['npm_package_recursive_key']).toEqual('value');
      expect(env['npm_package_recursive_more_another']).toEqual('what');
    });

    it('replaces invalid chars with _', async () => {
      const config = await withManifest('', '', {'in^va!d_key': 'test'});
      const env = await makeEnv('', cwd, config);
      expect(env['npm_package_in_va_d_key']).toEqual('test');
    });

    it('it omits certain fields which tend to be too large', async () => {
      const testManifest = {hello: 'I shall stay'};
      for (const key of IGNORE_MANIFEST_KEYS) {
        testManifest[key] = 'some long text we want to omit';
        // ensure we don't carry an previously set environment variables for these
        // to make tests consistent across runs with
        delete process.env[`npm_package_${key}`];
      }

      const config = await withManifest('', '', testManifest);
      const env = await makeEnv('', cwd, config);

      expect(env).toHaveProperty('npm_package_hello', 'I shall stay');
      for (const key of IGNORE_MANIFEST_KEYS) {
        expect(env).not.toHaveProperty(`npm_package_${key}`);
      }
    });
  });

  describe('npm_package_config_*', () => {
    it('overwrites npm_package_config_* keys from yarn config or npm config', async () => {
      const name = 'manifest-name';
      const config = await withManifest('', '', {name, config: {key: 'value', keytwo: 'valuetwo'}});
      config.registries.yarn.config = {[`${name}:key`]: 'replaced'};
      config.registries.npm.config = {[`${name}:keytwo`]: 'also replaced'};
      const env = await makeEnv('', cwd, config);
      expect(env['npm_package_config_key']).toEqual('replaced');
      expect(env['npm_package_config_keytwo']).toEqual('also replaced');
    });

    it('does not overwrite if the name does not match the manifest', async () => {
      const name = 'manifest-name';
      const config = await withManifest('', '', {name, config: {key: 'value', keytwo: 'valuetwo'}});
      config.registries.yarn.config = {'wrong-name:key': 'replaced'};
      config.registries.npm.config = {'another-name:keytwo': 'also replaced'};
      const env = await makeEnv('', cwd, config);
      expect(env['npm_package_config_key']).toEqual('value');
      expect(env['npm_package_config_keytwo']).toEqual('valuetwo');
    });
  });
});