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 <fishrock123@rocketmail.com>
1 parent 577e132 commit 0eb25cb

1 file changed

Lines changed: 59 additions & 16 deletions

File tree

‎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
constcommon=require('../common');
34
constassert=require('assert');
45
constpath=require('path');
5-
constchild_process=require('child_process');
6+
constchildProcess=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-
varnodeBinary=process.argv[0];
14+
constnodeBinary=process.argv[0];
815

9-
varpreloadOption=function(preloads){
16+
constpreloadOption=function(preloads){
1017
varoption='';
1118
preloads.forEach(function(preload,index){
1219
option+='-r '+preload+' ';
1320
});
1421
returnoption;
1522
};
1623

17-
varfixture=function(name){
24+
constfixture=function(name){
1825
returnpath.join(__dirname,'../fixtures/'+name);
1926
};
2027

21-
varfixtureA=fixture('printA.js');
22-
varfixtureB=fixture('printB.js');
23-
varfixtureC=fixture('printC.js');
28+
constfixtureA=fixture('printA.js');
29+
constfixtureB=fixture('printB.js');
30+
constfixtureC=fixture('printC.js');
2431
constfixtureD=fixture('define-global.js');
25-
varfixtureThrows=fixture('throws_error4.js');
32+
constfixtureThrows=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)throwerr;
6370
assert.equal(stdout,'A\nhello\n');
6471
});
6572

73+
// test that preload can be used with stdin
74+
conststdinProc=childProcess.spawn(
75+
nodeBinary,
76+
['--require',fixtureA],
77+
{stdio: 'pipe'}
78+
);
79+
stdinProc.stdin.end('console.log(\'hello\');');
80+
varstdinStdout='';
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+
constreplProc=childProcess.spawn(
91+
nodeBinary,
92+
['-i','--require',fixtureA],
93+
{stdio: 'pipe'}
94+
);
95+
replProc.stdin.end('.exit\n');
96+
varreplStdout='';
97+
replProc.stdout.on('data',function(d){
98+
replStdout+=d;
99+
});
100+
replProc.on('exit',function(code){
101+
assert.equal(code,0);
102+
constoutput=[
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-
constinteractive=child_process.exec(nodeBinary+' '
121+
constinteractive=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)