• 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');
30
31// Test preloading a single module works
32childProcess.exec(`"${nodeBinary}" ${preloadOption([fixtureA])} "${fixtureB}"`,
33                  function(err, stdout, stderr) {
34                    assert.ifError(err);
35                    assert.strictEqual(stdout, 'A\nB\n');
36                  });
37
38// Test preloading multiple modules works
39childProcess.exec(
40  `"${nodeBinary}" ${preloadOption([fixtureA, fixtureB])} "${fixtureC}"`,
41  function(err, stdout, stderr) {
42    assert.ifError(err);
43    assert.strictEqual(stdout, 'A\nB\nC\n');
44  }
45);
46
47// Test that preloading a throwing module aborts
48childProcess.exec(
49  `"${nodeBinary}" ${preloadOption([fixtureA, fixtureThrows])} "${fixtureB}"`,
50  function(err, stdout, stderr) {
51    if (err) {
52      assert.strictEqual(stdout, 'A\n');
53    } else {
54      throw new Error('Preload should have failed');
55    }
56  }
57);
58
59// Test that preload can be used with --eval
60childProcess.exec(
61  `"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');"`,
62  function(err, stdout, stderr) {
63    assert.ifError(err);
64    assert.strictEqual(stdout, 'A\nhello\n');
65  }
66);
67
68// Test that preload can be used with --frozen-intrinsics
69childProcess.exec(
70  `"${nodeBinary}" --frozen-intrinsics ${
71    preloadOption([fixtureE])
72  } ${
73    fixtureF
74  }`,
75  function(err, stdout) {
76    assert.ifError(err);
77    assert.strictEqual(stdout, 'smoosh\n');
78  }
79);
80childProcess.exec(
81  `"${
82    nodeBinary
83  }" --frozen-intrinsics ${
84    preloadOption([fixtureE])
85  } ${
86    fixtureG
87  } ${fixtureF}`,
88  function(err, stdout) {
89    assert.ifError(err);
90    assert.strictEqual(stdout, 'smoosh\n');
91  }
92);
93
94// Test that preload can be used with stdin
95const stdinProc = childProcess.spawn(
96  nodeBinary,
97  ['--require', fixtureA],
98  { stdio: 'pipe' }
99);
100stdinProc.stdin.end("console.log('hello');");
101let stdinStdout = '';
102stdinProc.stdout.on('data', function(d) {
103  stdinStdout += d;
104});
105stdinProc.on('close', function(code) {
106  assert.strictEqual(code, 0);
107  assert.strictEqual(stdinStdout, 'A\nhello\n');
108});
109
110// Test that preload can be used with repl
111const replProc = childProcess.spawn(
112  nodeBinary,
113  ['-i', '--require', fixtureA],
114  { stdio: 'pipe' }
115);
116replProc.stdin.end('.exit\n');
117let replStdout = '';
118replProc.stdout.on('data', (d) => {
119  replStdout += d;
120});
121replProc.on('close', function(code) {
122  assert.strictEqual(code, 0);
123  const output = [
124    'A',
125    '> '
126  ];
127  assert.ok(replStdout.startsWith(output[0]));
128  assert.ok(replStdout.endsWith(output[1]));
129});
130
131// Test that preload placement at other points in the cmdline
132// also test that duplicated preload only gets loaded once
133childProcess.exec(
134  `"${nodeBinary}" ${preloadOption([fixtureA])}-e "console.log('hello');" ${
135    preloadOption([fixtureA, fixtureB])}`,
136  function(err, stdout, stderr) {
137    assert.ifError(err);
138    assert.strictEqual(stdout, 'A\nB\nhello\n');
139  }
140);
141
142// Test that preload works with -i
143const interactive = childProcess.exec(
144  `"${nodeBinary}" ${preloadOption([fixtureD])}-i`,
145  common.mustCall(function(err, stdout, stderr) {
146    assert.ifError(err);
147    assert.ok(stdout.endsWith("> 'test'\n> "));
148  })
149);
150
151interactive.stdin.write('a\n');
152interactive.stdin.write('process.exit()\n');
153
154childProcess.exec(
155  `"${nodeBinary}" --require "${fixtures.path('cluster-preload.js')}" "${
156    fixtures.path('cluster-preload-test.js')}"`,
157  function(err, stdout, stderr) {
158    assert.ifError(err);
159    assert.ok(/worker terminated with code 43/.test(stdout));
160  }
161);
162
163// Test that preloading with a relative path works
164childProcess.exec(
165  `"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`,
166  { cwd: fixtures.fixturesDir },
167  common.mustCall(function(err, stdout, stderr) {
168    assert.ifError(err);
169    assert.strictEqual(stdout, 'A\nB\n');
170  })
171);
172if (common.isWindows) {
173  // https://github.com/nodejs/node/issues/21918
174  childProcess.exec(
175    `"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`,
176    { cwd: fixtures.fixturesDir },
177    common.mustCall(function(err, stdout, stderr) {
178      assert.ifError(err);
179      assert.strictEqual(stdout, 'A\nB\n');
180    })
181  );
182}
183
184// https://github.com/nodejs/node/issues/1691
185childProcess.exec(
186  `"${nodeBinary}" --require ` +
187     `"${fixtures.path('cluster-preload.js')}" cluster-preload-test.js`,
188  { cwd: fixtures.fixturesDir },
189  function(err, stdout, stderr) {
190    assert.ifError(err);
191    assert.ok(/worker terminated with code 43/.test(stdout));
192  }
193);
194