• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const fixtures = require('../common/fixtures');
5// Refs: https://github.com/nodejs/node/pull/2253
6if (common.isSunOS)
7  common.skip('unreliable on SunOS');
8
9const assert = require('assert');
10const childProcess = require('child_process');
11
12const nodeBinary = process.argv[0];
13
14const preloadOption = (preloads) => {
15  let option = '';
16  preloads.forEach(function(preload, index) {
17    option += `-r "${preload}" `;
18  });
19  return option;
20};
21
22const fixtureA = fixtures.path('printA.js');
23const fixtureB = fixtures.path('printB.js');
24const fixtureC = fixtures.path('printC.js');
25const fixtureD = fixtures.path('define-global.js');
26const fixtureE = fixtures.path('intrinsic-mutation.js');
27const fixtureF = fixtures.path('print-intrinsic-mutation-name.js');
28const fixtureG = fixtures.path('worker-from-argv.js');
29const fixtureThrows = fixtures.path('throws_error4.js');
30const fixtureIsPreloading = fixtures.path('ispreloading.js');
31
32// Assert that module.isPreloading is false here
33assert(!module.isPreloading);
34
35// Test that module.isPreloading is set in preloaded module
36// Test preloading a single module works
37childProcess.exec(
38  `"${nodeBinary}" ${preloadOption([fixtureIsPreloading])} "${fixtureB}"`,
39  function(err, stdout, stderr) {
40    assert.ifError(err);
41    assert.strictEqual(stdout, 'B\n');
42  });
43
44// Test preloading a single module works
45childProcess.exec(`"${nodeBinary}" ${preloadOption([fixtureA])} "${fixtureB}"`,
46                  function(err, stdout, stderr) {
47                    assert.ifError(err);
48                    assert.strictEqual(stdout, 'A\nB\n');
49                  });
50
51// Test preloading multiple modules works
52childProcess.exec(
53  `"${nodeBinary}" ${preloadOption([fixtureA, fixtureB])} "${fixtureC}"`,
54  function(err, stdout, stderr) {
55    assert.ifError(err);
56    assert.strictEqual(stdout, 'A\nB\nC\n');
57  }
58);
59
60// Test that preloading a throwing module aborts
61childProcess.exec(
62  `"${nodeBinary}" ${preloadOption([fixtureA, fixtureThrows])} "${fixtureB}"`,
63  function(err, stdout, stderr) {
64    if (err) {
65      assert.strictEqual(stdout, 'A\n');
66    } else {
67      throw new Error('Preload should have failed');
68    }
69  }
70);
71
72// Test that preload can be used with --eval
73childProcess.exec(
74  `"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');"`,
75  function(err, stdout, stderr) {
76    assert.ifError(err);
77    assert.strictEqual(stdout, 'A\nhello\n');
78  }
79);
80
81// Test that preload can be used with --frozen-intrinsics
82childProcess.exec(
83  `"${nodeBinary}" --frozen-intrinsics ${
84    preloadOption([fixtureE])
85  } ${
86    fixtureF
87  }`,
88  function(err, stdout) {
89    assert.ifError(err);
90    assert.strictEqual(stdout, 'smoosh\n');
91  }
92);
93childProcess.exec(
94  `"${
95    nodeBinary
96  }" --frozen-intrinsics ${
97    preloadOption([fixtureE])
98  } ${
99    fixtureG
100  } ${fixtureF}`,
101  function(err, stdout) {
102    assert.ifError(err);
103    assert.strictEqual(stdout, 'smoosh\n');
104  }
105);
106
107// Test that preload can be used with stdin
108const stdinProc = childProcess.spawn(
109  nodeBinary,
110  ['--require', fixtureA],
111  { stdio: 'pipe' }
112);
113stdinProc.stdin.end("console.log('hello');");
114let stdinStdout = '';
115stdinProc.stdout.on('data', function(d) {
116  stdinStdout += d;
117});
118stdinProc.on('close', function(code) {
119  assert.strictEqual(code, 0);
120  assert.strictEqual(stdinStdout, 'A\nhello\n');
121});
122
123// Test that preload can be used with repl
124const replProc = childProcess.spawn(
125  nodeBinary,
126  ['-i', '--require', fixtureA],
127  { stdio: 'pipe' }
128);
129replProc.stdin.end('.exit\n');
130let replStdout = '';
131replProc.stdout.on('data', (d) => {
132  replStdout += d;
133});
134replProc.on('close', function(code) {
135  assert.strictEqual(code, 0);
136  const output = [
137    'A',
138    '> ',
139  ];
140  assert.ok(replStdout.startsWith(output[0]));
141  assert.ok(replStdout.endsWith(output[1]));
142});
143
144// Test that preload placement at other points in the cmdline
145// also test that duplicated preload only gets loaded once
146childProcess.exec(
147  `"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');" ${
148    preloadOption([fixtureA, fixtureB])}`,
149  function(err, stdout, stderr) {
150    assert.ifError(err);
151    assert.strictEqual(stdout, 'A\nB\nhello\n');
152  }
153);
154
155// Test that preload works with -i
156const interactive = childProcess.exec(
157  `"${nodeBinary}" ${preloadOption([fixtureD])}-i`,
158  common.mustSucceed((stdout, stderr) => {
159    assert.ok(stdout.endsWith("> 'test'\n> "));
160  })
161);
162
163interactive.stdin.write('a\n');
164interactive.stdin.write('process.exit()\n');
165
166childProcess.exec(
167  `"${nodeBinary}" --require "${fixtures.path('cluster-preload.js')}" "${
168    fixtures.path('cluster-preload-test.js')}"`,
169  function(err, stdout, stderr) {
170    assert.ifError(err);
171    assert.ok(/worker terminated with code 43/.test(stdout));
172  }
173);
174
175// Test that preloading with a relative path works
176childProcess.exec(
177  `"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`,
178  { cwd: fixtures.fixturesDir },
179  common.mustSucceed((stdout, stderr) => {
180    assert.strictEqual(stdout, 'A\nB\n');
181  })
182);
183if (common.isWindows) {
184  // https://github.com/nodejs/node/issues/21918
185  childProcess.exec(
186    `"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`,
187    { cwd: fixtures.fixturesDir },
188    common.mustSucceed((stdout, stderr) => {
189      assert.strictEqual(stdout, 'A\nB\n');
190    })
191  );
192}
193
194// https://github.com/nodejs/node/issues/1691
195childProcess.exec(
196  `"${nodeBinary}" --require ` +
197     `"${fixtures.path('cluster-preload.js')}" cluster-preload-test.js`,
198  { cwd: fixtures.fixturesDir },
199  function(err, stdout, stderr) {
200    assert.ifError(err);
201    assert.ok(/worker terminated with code 43/.test(stdout));
202  }
203);
204