@@ -3,7 +3,10 @@ const common = require('../common');
3
3
const fixtures = require ( '../common/fixtures' ) ;
4
4
const tmpdir = require ( '../common/tmpdir' ) ;
5
5
const assert = require ( 'assert' ) ;
6
- const child_process = require ( 'child_process' ) ;
6
+ const {
7
+ spawnSyncAndExit,
8
+ spawnSyncAndExitWithoutError,
9
+ } = require ( '../common/child_process' ) ;
7
10
const path = require ( 'path' ) ;
8
11
const fs = require ( 'fs' ) ;
9
12
@@ -21,39 +24,54 @@ function resolveBuiltBinary(bin) {
21
24
22
25
const binary = resolveBuiltBinary ( 'embedtest' ) ;
23
26
24
- assert . strictEqual (
25
- child_process . spawnSync ( binary , [ 'console.log(42)' ] )
26
- . stdout . toString ( ) . trim ( ) ,
27
- '42' ) ;
28
-
29
- assert . strictEqual (
30
- child_process . spawnSync ( binary , [ 'console.log(embedVars.nön_ascıı)' ] )
31
- . stdout . toString ( ) . trim ( ) ,
32
- '🏳️🌈' ) ;
33
-
34
- assert . strictEqual (
35
- child_process . spawnSync ( binary , [ 'console.log(42)' ] )
36
- . stdout . toString ( ) . trim ( ) ,
37
- '42' ) ;
27
+ spawnSyncAndExitWithoutError (
28
+ binary ,
29
+ [ 'console.log(42)' ] ,
30
+ {
31
+ trim : true ,
32
+ stdout : '42' ,
33
+ } ) ;
38
34
39
- assert . strictEqual (
40
- child_process . spawnSync ( binary , [ 'throw new Error()' ] ) . status ,
41
- 1 ) ;
35
+ spawnSyncAndExitWithoutError (
36
+ binary ,
37
+ [ 'console.log(embedVars.nön_ascıı)' ] ,
38
+ {
39
+ trim : true ,
40
+ stdout : '🏳️🌈' ,
41
+ } ) ;
42
42
43
- // Cannot require internals anymore:
44
- assert . strictEqual (
45
- child_process . spawnSync ( binary , [ 'require("lib/internal/test/binding")' ] ) . status ,
46
- 1 ) ;
43
+ spawnSyncAndExit (
44
+ binary ,
45
+ [ 'throw new Error()' ] ,
46
+ {
47
+ status : 1 ,
48
+ signal : null ,
49
+ } ) ;
47
50
48
- assert . strictEqual (
49
- child_process . spawnSync ( binary , [ 'process.exitCode = 8' ] ) . status ,
50
- 8 ) ;
51
+ spawnSyncAndExit (
52
+ binary ,
53
+ [ 'require("lib/internal/test/binding")' ] ,
54
+ {
55
+ status : 1 ,
56
+ signal : null ,
57
+ } ) ;
51
58
59
+ spawnSyncAndExit (
60
+ binary ,
61
+ [ 'process.exitCode = 8' ] ,
62
+ {
63
+ status : 8 ,
64
+ signal : null ,
65
+ } ) ;
52
66
53
67
const fixturePath = JSON . stringify ( fixtures . path ( 'exit.js' ) ) ;
54
- assert . strictEqual (
55
- child_process . spawnSync ( binary , [ `require(${ fixturePath } )` , 92 ] ) . status ,
56
- 92 ) ;
68
+ spawnSyncAndExit (
69
+ binary ,
70
+ [ `require(${ fixturePath } )` , 92 ] ,
71
+ {
72
+ status : 92 ,
73
+ signal : null ,
74
+ } ) ;
57
75
58
76
function getReadFileCodeForPath ( path ) {
59
77
return `(require("fs").readFileSync(${ JSON . stringify ( path ) } , "utf8"))` ;
@@ -64,31 +82,49 @@ for (const extraSnapshotArgs of [[], ['--embedder-snapshot-as-file']]) {
64
82
// readSync + eval since snapshots don't support userland require() (yet)
65
83
const snapshotFixture = fixtures . path ( 'snapshot' , 'echo-args.js' ) ;
66
84
const blobPath = tmpdir . resolve ( 'embedder-snapshot.blob' ) ;
67
- const buildSnapshotArgs = [
85
+ const buildSnapshotExecArgs = [
68
86
`eval(${ getReadFileCodeForPath ( snapshotFixture ) } )` , 'arg1' , 'arg2' ,
87
+ ] ;
88
+ const embedTestBuildArgs = [
69
89
'--embedder-snapshot-blob' , blobPath , '--embedder-snapshot-create' ,
70
90
...extraSnapshotArgs ,
71
91
] ;
72
- const runEmbeddedArgs = [
73
- '--embedder-snapshot-blob' , blobPath , ...extraSnapshotArgs , 'arg3' , 'arg4' ,
92
+ const buildSnapshotArgs = [
93
+ ...buildSnapshotExecArgs ,
94
+ ...embedTestBuildArgs ,
95
+ ] ;
96
+
97
+ const runSnapshotExecArgs = [
98
+ 'arg3' , 'arg4' ,
99
+ ] ;
100
+ const embedTestRunArgs = [
101
+ '--embedder-snapshot-blob' , blobPath ,
102
+ ...extraSnapshotArgs ,
103
+ ] ;
104
+ const runSnapshotArgs = [
105
+ ...runSnapshotExecArgs ,
106
+ ...embedTestRunArgs ,
74
107
] ;
75
108
76
109
fs . rmSync ( blobPath , { force : true } ) ;
77
- const child = child_process . spawnSync ( binary , [
78
- '--' , ...buildSnapshotArgs ,
79
- ] , {
80
- cwd : tmpdir . path ,
81
- } ) ;
82
- if ( child . status !== 0 ) {
83
- console . log ( child . stderr . toString ( ) ) ;
84
- console . log ( child . stdout . toString ( ) ) ;
85
- }
86
- assert . strictEqual ( child . status , 0 ) ;
87
- const spawnResult = child_process . spawnSync ( binary , [ '--' , ...runEmbeddedArgs ] ) ;
88
- assert . deepStrictEqual ( JSON . parse ( spawnResult . stdout ) , {
89
- originalArgv : [ binary , ...buildSnapshotArgs ] ,
90
- currentArgv : [ binary , ...runEmbeddedArgs ] ,
91
- } ) ;
110
+ spawnSyncAndExitWithoutError (
111
+ binary ,
112
+ [ '--' , ...buildSnapshotArgs ] ,
113
+ { cwd : tmpdir . path } ,
114
+ { } ) ;
115
+ spawnSyncAndExitWithoutError (
116
+ binary ,
117
+ [ '--' , ...runSnapshotArgs ] ,
118
+ { cwd : tmpdir . path } ,
119
+ {
120
+ stdout ( output ) {
121
+ assert . deepStrictEqual ( JSON . parse ( output ) , {
122
+ originalArgv : [ binary , '__node_anonymous_main' , ...buildSnapshotExecArgs ] ,
123
+ currentArgv : [ binary , ...runSnapshotExecArgs ] ,
124
+ } ) ;
125
+ return true ;
126
+ } ,
127
+ } ) ;
92
128
}
93
129
94
130
// Create workers and vm contexts after deserialization
@@ -99,14 +135,20 @@ for (const extraSnapshotArgs of [[], ['--embedder-snapshot-as-file']]) {
99
135
`eval(${ getReadFileCodeForPath ( snapshotFixture ) } )` ,
100
136
'--embedder-snapshot-blob' , blobPath , '--embedder-snapshot-create' ,
101
137
] ;
138
+ const runEmbeddedArgs = [
139
+ '--embedder-snapshot-blob' , blobPath ,
140
+ ] ;
102
141
103
142
fs . rmSync ( blobPath , { force : true } ) ;
104
- assert . strictEqual ( child_process . spawnSync ( binary , [
105
- '--' , ...buildSnapshotArgs ,
106
- ] , {
107
- cwd : tmpdir . path ,
108
- } ) . status , 0 ) ;
109
- assert . strictEqual (
110
- child_process . spawnSync ( binary , [ '--' , '--embedder-snapshot-blob' , blobPath ] ) . status ,
111
- 0 ) ;
143
+
144
+ spawnSyncAndExitWithoutError (
145
+ binary ,
146
+ [ '--' , ...buildSnapshotArgs ] ,
147
+ { cwd : tmpdir . path } ,
148
+ { } ) ;
149
+ spawnSyncAndExitWithoutError (
150
+ binary ,
151
+ [ '--' , ...runEmbeddedArgs ] ,
152
+ { cwd : tmpdir . path } ,
153
+ { } ) ;
112
154
}
0 commit comments