Skip to content

Commit 0eb25cb

Browse files
bmeckevanlucas
authored andcommitted
test: test preloaded modules using stdin or repl
This test fails on Solaris, see the PR for discussion. PR-URL: #2253 Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 577e132 commit 0eb25cb

File tree

1 file changed

+59
-16
lines changed

1 file changed

+59
-16
lines changed

test/parallel/test-preload.js

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
'use strict';
2+
23
const common = require('../common');
34
const assert = require('assert');
45
const path = require('path');
5-
const child_process = require('child_process');
6+
const childProcess = require('child_process');
7+
8+
// Refs: https://github.com/nodejs/node/pull/2253
9+
if (common.isSunOS) {
10+
console.log('1..0 # Skipped: unreliable on SunOS');
11+
return;
12+
}
613

7-
var nodeBinary = process.argv[0];
14+
const nodeBinary = process.argv[0];
815

9-
var preloadOption = function(preloads) {
16+
const preloadOption = function(preloads) {
1017
var option = '';
1118
preloads.forEach(function(preload, index) {
1219
option += '-r ' + preload + ' ';
1320
});
1421
return option;
1522
};
1623

17-
var fixture = function(name) {
24+
const fixture = function(name) {
1825
return path.join(__dirname, '../fixtures/' + name);
1926
};
2027

21-
var fixtureA = fixture('printA.js');
22-
var fixtureB = fixture('printB.js');
23-
var fixtureC = fixture('printC.js');
28+
const fixtureA = fixture('printA.js');
29+
const fixtureB = fixture('printB.js');
30+
const fixtureC = fixture('printC.js');
2431
const fixtureD = fixture('define-global.js');
25-
var fixtureThrows = fixture('throws_error4.js');
32+
const fixtureThrows = fixture('throws_error4.js');
2633

2734
// test preloading a single module works
28-
child_process.exec(nodeBinary + ' '
35+
childProcess.exec(nodeBinary + ' '
2936
+ preloadOption([fixtureA]) + ' '
3037
+ fixtureB,
3138
function(err, stdout, stderr) {
@@ -34,7 +41,7 @@ child_process.exec(nodeBinary + ' '
3441
});
3542

3643
// test preloading multiple modules works
37-
child_process.exec(nodeBinary + ' '
44+
childProcess.exec(nodeBinary + ' '
3845
+ preloadOption([fixtureA, fixtureB]) + ' '
3946
+ fixtureC,
4047
function(err, stdout, stderr) {
@@ -43,7 +50,7 @@ child_process.exec(nodeBinary + ' '
4350
});
4451

4552
// test that preloading a throwing module aborts
46-
child_process.exec(nodeBinary + ' '
53+
childProcess.exec(nodeBinary + ' '
4754
+ preloadOption([fixtureA, fixtureThrows]) + ' '
4855
+ fixtureB,
4956
function(err, stdout, stderr) {
@@ -55,17 +62,53 @@ child_process.exec(nodeBinary + ' '
5562
});
5663

5764
// test that preload can be used with --eval
58-
child_process.exec(nodeBinary + ' '
65+
childProcess.exec(nodeBinary + ' '
5966
+ preloadOption([fixtureA])
6067
+ '-e "console.log(\'hello\');"',
6168
function(err, stdout, stderr) {
6269
if (err) throw err;
6370
assert.equal(stdout, 'A\nhello\n');
6471
});
6572

73+
// test that preload can be used with stdin
74+
const stdinProc = childProcess.spawn(
75+
nodeBinary,
76+
['--require', fixtureA],
77+
{stdio: 'pipe'}
78+
);
79+
stdinProc.stdin.end('console.log(\'hello\');');
80+
var stdinStdout = '';
81+
stdinProc.stdout.on('data', function(d) {
82+
stdinStdout += d;
83+
});
84+
stdinProc.on('exit', function(code) {
85+
assert.equal(code, 0);
86+
assert.equal(stdinStdout, 'A\nhello\n');
87+
});
88+
89+
// test that preload can be used with repl
90+
const replProc = childProcess.spawn(
91+
nodeBinary,
92+
['-i', '--require', fixtureA],
93+
{stdio: 'pipe'}
94+
);
95+
replProc.stdin.end('.exit\n');
96+
var replStdout = '';
97+
replProc.stdout.on('data', function(d) {
98+
replStdout += d;
99+
});
100+
replProc.on('exit', function(code) {
101+
assert.equal(code, 0);
102+
const output = [
103+
'A',
104+
'> '
105+
].join('\n');
106+
assert.equal(replStdout, output);
107+
});
108+
66109
// test that preload placement at other points in the cmdline
67110
// also test that duplicated preload only gets loaded once
68-
child_process.exec(nodeBinary + ' '
111+
childProcess.exec(nodeBinary + ' '
69112
+ preloadOption([fixtureA])
70113
+ '-e "console.log(\'hello\');" '
71114
+ preloadOption([fixtureA, fixtureB]),
@@ -75,7 +118,7 @@ child_process.exec(nodeBinary + ' '
75118
});
76119

77120
// test that preload works with -i
78-
const interactive = child_process.exec(nodeBinary + ' '
121+
const interactive = childProcess.exec(nodeBinary + ' '
79122
+ preloadOption([fixtureD])
80123
+ '-i',
81124
common.mustCall(function(err, stdout, stderr) {
@@ -86,7 +129,7 @@ const interactive = child_process.exec(nodeBinary + ' '
86129
interactive.stdin.write('a\n');
87130
interactive.stdin.write('process.exit()\n');
88131

89-
child_process.exec(nodeBinary + ' '
132+
childProcess.exec(nodeBinary + ' '
90133
+ '--require ' + fixture('cluster-preload.js') + ' '
91134
+ fixture('cluster-preload-test.js'),
92135
function(err, stdout, stderr) {
@@ -96,7 +139,7 @@ child_process.exec(nodeBinary + ' '
96139

97140
// https://github.com/nodejs/node/issues/1691
98141
process.chdir(path.join(__dirname, '../fixtures/'));
99-
child_process.exec(nodeBinary + ' '
142+
childProcess.exec(nodeBinary + ' '
100143
+ '--expose_debug_as=v8debug '
101144
+ '--require ' + fixture('cluster-preload.js') + ' '
102145
+ 'cluster-preload-test.js',

0 commit comments

Comments
 (0)