1
1
'use strict' ;
2
+
2
3
const common = require ( '../common' ) ;
3
4
const assert = require ( 'assert' ) ;
4
5
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
+ }
6
13
7
- var nodeBinary = process . argv [ 0 ] ;
14
+ const nodeBinary = process . argv [ 0 ] ;
8
15
9
- var preloadOption = function ( preloads ) {
16
+ const preloadOption = function ( preloads ) {
10
17
var option = '' ;
11
18
preloads . forEach ( function ( preload , index ) {
12
19
option += '-r ' + preload + ' ' ;
13
20
} ) ;
14
21
return option ;
15
22
} ;
16
23
17
- var fixture = function ( name ) {
24
+ const fixture = function ( name ) {
18
25
return path . join ( __dirname , '../fixtures/' + name ) ;
19
26
} ;
20
27
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' ) ;
24
31
const fixtureD = fixture ( 'define-global.js' ) ;
25
- var fixtureThrows = fixture ( 'throws_error4.js' ) ;
32
+ const fixtureThrows = fixture ( 'throws_error4.js' ) ;
26
33
27
34
// test preloading a single module works
28
- child_process . exec ( nodeBinary + ' '
35
+ childProcess . exec ( nodeBinary + ' '
29
36
+ preloadOption ( [ fixtureA ] ) + ' '
30
37
+ fixtureB ,
31
38
function ( err , stdout , stderr ) {
@@ -34,7 +41,7 @@ child_process.exec(nodeBinary + ' '
34
41
} ) ;
35
42
36
43
// test preloading multiple modules works
37
- child_process . exec ( nodeBinary + ' '
44
+ childProcess . exec ( nodeBinary + ' '
38
45
+ preloadOption ( [ fixtureA , fixtureB ] ) + ' '
39
46
+ fixtureC ,
40
47
function ( err , stdout , stderr ) {
@@ -43,7 +50,7 @@ child_process.exec(nodeBinary + ' '
43
50
} ) ;
44
51
45
52
// test that preloading a throwing module aborts
46
- child_process . exec ( nodeBinary + ' '
53
+ childProcess . exec ( nodeBinary + ' '
47
54
+ preloadOption ( [ fixtureA , fixtureThrows ] ) + ' '
48
55
+ fixtureB ,
49
56
function ( err , stdout , stderr ) {
@@ -55,17 +62,53 @@ child_process.exec(nodeBinary + ' '
55
62
} ) ;
56
63
57
64
// test that preload can be used with --eval
58
- child_process . exec ( nodeBinary + ' '
65
+ childProcess . exec ( nodeBinary + ' '
59
66
+ preloadOption ( [ fixtureA ] )
60
67
+ '-e "console.log(\'hello\');"' ,
61
68
function ( err , stdout , stderr ) {
62
69
if ( err ) throw err ;
63
70
assert . equal ( stdout , 'A\nhello\n' ) ;
64
71
} ) ;
65
72
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
+
66
109
// test that preload placement at other points in the cmdline
67
110
// also test that duplicated preload only gets loaded once
68
- child_process . exec ( nodeBinary + ' '
111
+ childProcess . exec ( nodeBinary + ' '
69
112
+ preloadOption ( [ fixtureA ] )
70
113
+ '-e "console.log(\'hello\');" '
71
114
+ preloadOption ( [ fixtureA , fixtureB ] ) ,
@@ -75,7 +118,7 @@ child_process.exec(nodeBinary + ' '
75
118
} ) ;
76
119
77
120
// test that preload works with -i
78
- const interactive = child_process . exec ( nodeBinary + ' '
121
+ const interactive = childProcess . exec ( nodeBinary + ' '
79
122
+ preloadOption ( [ fixtureD ] )
80
123
+ '-i' ,
81
124
common . mustCall ( function ( err , stdout , stderr ) {
@@ -86,7 +129,7 @@ const interactive = child_process.exec(nodeBinary + ' '
86
129
interactive . stdin . write ( 'a\n' ) ;
87
130
interactive . stdin . write ( 'process.exit()\n' ) ;
88
131
89
- child_process . exec ( nodeBinary + ' '
132
+ childProcess . exec ( nodeBinary + ' '
90
133
+ '--require ' + fixture ( 'cluster-preload.js' ) + ' '
91
134
+ fixture ( 'cluster-preload-test.js' ) ,
92
135
function ( err , stdout , stderr ) {
@@ -96,7 +139,7 @@ child_process.exec(nodeBinary + ' '
96
139
97
140
// https://github.com/nodejs/node/issues/1691
98
141
process . chdir ( path . join ( __dirname , '../fixtures/' ) ) ;
99
- child_process . exec ( nodeBinary + ' '
142
+ childProcess . exec ( nodeBinary + ' '
100
143
+ '--expose_debug_as=v8debug '
101
144
+ '--require ' + fixture ( 'cluster-preload.js' ) + ' '
102
145
+ 'cluster-preload-test.js' ,
0 commit comments