@@ -9,92 +9,129 @@ const {
99 spawnSyncAndExitWithoutError,
1010} = require ( '../common/child_process' ) ;
1111const fs = require ( 'fs' ) ;
12+ const os = require ( 'os' ) ;
1213
1314tmpdir . refresh ( ) ;
1415common . allowGlobals ( global . require ) ;
1516common . allowGlobals ( global . embedVars ) ;
1617
1718const binary = common . resolveBuiltBinary ( 'embedtest' ) ;
1819
19- function runTest ( testName , spawn , ...args ) {
20- process . stdout . write ( `Run test: ${ testName } ... ` ) ;
21- spawn ( binary , ...args ) ;
22- console . log ( 'ok' ) ;
23- }
20+ spawnSyncAndAssert (
21+ binary ,
22+ [ 'console.log(42)' ] ,
23+ {
24+ trim : true ,
25+ stdout : '42' ,
26+ } ) ;
2427
25- function runCommonApiTests ( apiType ) {
26- runTest (
27- `${ apiType } : console.log` ,
28- spawnSyncAndAssert ,
29- [ apiType , 'console.log(42)' ] ,
30- {
31- trim : true ,
32- stdout : '42' ,
33- } ,
34- ) ;
28+ spawnSyncAndAssert (
29+ binary ,
30+ [ 'console.log(embedVars.nön_ascıı)' ] ,
31+ {
32+ trim : true ,
33+ stdout : '🏳️🌈' ,
34+ } ) ;
3535
36- runTest (
37- `${ apiType } : console.log non-ascii` ,
38- spawnSyncAndAssert ,
39- [ apiType , 'console.log(embedVars.nön_ascıı)' ] ,
40- {
41- trim : true ,
42- stdout : '🏳️🌈' ,
43- } ,
44- ) ;
36+ spawnSyncAndExit (
37+ binary ,
38+ [ 'throw new Error()' ] ,
39+ {
40+ status : 1 ,
41+ signal : null ,
42+ } ) ;
4543
46- runTest (
47- `${ apiType } : throw new Error()` ,
48- spawnSyncAndExit ,
49- [ apiType , 'throw new Error()' ] ,
50- {
51- status : 1 ,
52- signal : null ,
53- } ,
54- ) ;
44+ spawnSyncAndExit (
45+ binary ,
46+ [ 'require("lib/internal/test/binding")' ] ,
47+ {
48+ status : 1 ,
49+ signal : null ,
50+ } ) ;
5551
56- runTest (
57- `${ apiType } : require("lib/internal/test/binding")` ,
58- spawnSyncAndExit ,
59- [ apiType , 'require("lib/internal/test/binding")' ] ,
60- {
61- status : 1 ,
62- signal : null ,
63- } ,
64- ) ;
52+ spawnSyncAndExit (
53+ binary ,
54+ [ 'process.exitCode = 8' ] ,
55+ {
56+ status : 8 ,
57+ signal : null ,
58+ } ) ;
59+
60+ const fixturePath = JSON . stringify ( fixtures . path ( 'exit.js' ) ) ;
61+ spawnSyncAndExit (
62+ binary ,
63+ [ `require(${ fixturePath } )` , 92 ] ,
64+ {
65+ status : 92 ,
66+ signal : null ,
67+ } ) ;
6568
66- runTest (
67- `${ apiType } : process.exitCode = 8` ,
68- spawnSyncAndExit ,
69- [ apiType , 'process.exitCode = 8' ] ,
70- {
71- status : 8 ,
72- signal : null ,
73- } ,
74- ) ;
69+ function getReadFileCodeForPath ( path ) {
70+ return `(require("fs").readFileSync(${ JSON . stringify ( path ) } , "utf8"))` ;
71+ }
7572
76- {
77- const fixturePath = JSON . stringify ( fixtures . path ( 'exit.js' ) ) ;
78- runTest (
79- `${ apiType } : require(fixturePath)` ,
80- spawnSyncAndExit ,
81- [ apiType , `require(${ fixturePath } )` , 92 ] ,
82- {
83- status : 92 ,
84- signal : null ,
85- } ,
86- ) ;
87- }
73+ // Basic snapshot support
74+ for ( const extraSnapshotArgs of [
75+ [ ] , [ '--embedder-snapshot-as-file' ] , [ '--without-code-cache' ] ,
76+ ] ) {
77+ // readSync + eval since snapshots don't support userland require() (yet)
78+ const snapshotFixture = fixtures . path ( 'snapshot' , 'echo-args.js' ) ;
79+ const blobPath = tmpdir . resolve ( 'embedder-snapshot.blob' ) ;
80+ const buildSnapshotExecArgs = [
81+ `eval(${ getReadFileCodeForPath ( snapshotFixture ) } )` , 'arg1' , 'arg2' ,
82+ ] ;
83+ const embedTestBuildArgs = [
84+ '--embedder-snapshot-blob' , blobPath , '--embedder-snapshot-create' ,
85+ ...extraSnapshotArgs ,
86+ ] ;
87+ const buildSnapshotArgs = [
88+ ...buildSnapshotExecArgs ,
89+ ...embedTestBuildArgs ,
90+ ] ;
91+
92+ const runSnapshotExecArgs = [
93+ 'arg3' , 'arg4' ,
94+ ] ;
95+ const embedTestRunArgs = [
96+ '--embedder-snapshot-blob' , blobPath ,
97+ ...extraSnapshotArgs ,
98+ ] ;
99+ const runSnapshotArgs = [
100+ ...runSnapshotExecArgs ,
101+ ...embedTestRunArgs ,
102+ ] ;
88103
89- runTest (
90- `${ apiType } : syntax error` ,
91- spawnSyncAndExit ,
92- [ apiType , '0syntax_error' ] ,
104+ fs . rmSync ( blobPath , { force : true } ) ;
105+ spawnSyncAndExitWithoutError (
106+ binary ,
107+ [ '--' , ...buildSnapshotArgs ] ,
108+ { cwd : tmpdir . path } ) ;
109+ spawnSyncAndAssert (
110+ binary ,
111+ [ '--' , ...runSnapshotArgs ] ,
112+ { cwd : tmpdir . path } ,
93113 {
94- status : 1 ,
95- stderr : / S y n t a x E r r o r : I n v a l i d o r u n e x p e c t e d t o k e n / ,
96- } ,
97- ) ;
114+ stdout ( output ) {
115+ assert . deepStrictEqual ( JSON . parse ( output ) , {
116+ originalArgv : [ binary , '__node_anonymous_main' , ...buildSnapshotExecArgs ] ,
117+ currentArgv : [ binary , ...runSnapshotExecArgs ] ,
118+ } ) ;
119+ return true ;
120+ } ,
121+ } ) ;
122+ }
123+
124+ // Create workers and vm contexts after deserialization
125+ {
126+ const snapshotFixture = fixtures . path ( 'snapshot' , 'create-worker-and-vm.js' ) ;
127+ const blobPath = tmpdir . resolve ( 'embedder-snapshot.blob' ) ;
128+ const buildSnapshotArgs = [
129+ `eval(${ getReadFileCodeForPath ( snapshotFixture ) } )` ,
130+ '--embedder-snapshot-blob' , blobPath , '--embedder-snapshot-create' ,
131+ ] ;
132+ const runEmbeddedArgs = [
133+ '--embedder-snapshot-blob' , blobPath ,
134+ ] ;
98135
99136 fs . rmSync ( blobPath , { force : true } ) ;
100137
@@ -116,137 +153,22 @@ if (!process.config.variables.node_without_node_options) {
116153 {
117154 env : {
118155 ...process . env ,
119- NODE_REPL_EXTERNAL_MODULE : 'fs' ,
156+ ' NODE_REPL_EXTERNAL_MODULE' : 'fs' ,
120157 } ,
121158 } ,
122159 {
123160 status : 9 ,
124161 signal : null ,
125- trim : true ,
126- stderr :
127- `${ binary } : NODE_REPL_EXTERNAL_MODULE can't be used with` +
128- ' kDisableNodeOptionsEnv' ,
129- } ,
130- ) ;
162+ stderr : `${ binary } : NODE_REPL_EXTERNAL_MODULE can't be used with kDisableNodeOptionsEnv${ os . EOL } ` ,
163+ } ) ;
131164}
132165
133- runCommonApiTests ( 'cpp-api' ) ;
134-
135- function getReadFileCodeForPath ( path ) {
136- return `(require("fs").readFileSync(${ JSON . stringify ( path ) } , "utf8"))` ;
137- }
138-
139- function runSnapshotTests ( apiType ) {
140- // Basic snapshot support
141- for ( const extraSnapshotArgs of [
142- [ ] , [ '--embedder-snapshot-as-file' ] , [ '--without-code-cache' ] ,
143- ] ) {
144- // readSync + eval since snapshots don't support userland require() (yet)
145- const snapshotFixture = fixtures . path ( 'snapshot' , 'echo-args.js' ) ;
146- const blobPath = tmpdir . resolve ( 'embedder-snapshot.blob' ) ;
147- const buildSnapshotExecArgs = [
148- `eval(${ getReadFileCodeForPath ( snapshotFixture ) } )` , 'arg1' , 'arg2' ,
149- ] ;
150- const embedTestBuildArgs = [
151- '--embedder-snapshot-blob' , blobPath , '--embedder-snapshot-create' ,
152- ...extraSnapshotArgs ,
153- ] ;
154- const buildSnapshotArgs = [
155- ...buildSnapshotExecArgs ,
156- ...embedTestBuildArgs ,
157- ] ;
158-
159- const runSnapshotExecArgs = [
160- 'arg3' , 'arg4' ,
161- ] ;
162- const embedTestRunArgs = [
163- '--embedder-snapshot-blob' , blobPath ,
164- ...extraSnapshotArgs ,
165- ] ;
166- const runSnapshotArgs = [
167- ...runSnapshotExecArgs ,
168- ...embedTestRunArgs ,
169- ] ;
170-
171- fs . rmSync ( blobPath , { force : true } ) ;
172-
173- runTest (
174- `${ apiType } : build basic snapshot ${ extraSnapshotArgs . join ( ' ' ) } ` ,
175- spawnSyncAndExitWithoutError ,
176- [ apiType , '--' , ...buildSnapshotArgs ] ,
177- {
178- cwd : tmpdir . path ,
179- } ,
180- ) ;
181-
182- runTest (
183- `${ apiType } : run basic snapshot ${ extraSnapshotArgs . join ( ' ' ) } ` ,
184- spawnSyncAndAssert ,
185- [ apiType , '--' , ...runSnapshotArgs ] ,
186- { cwd : tmpdir . path } ,
187- {
188- stdout : common . mustCall ( ( output ) => {
189- assert . deepStrictEqual ( JSON . parse ( output ) , {
190- originalArgv : [
191- binary ,
192- '__node_anonymous_main' ,
193- ...buildSnapshotExecArgs ,
194- ] ,
195- currentArgv : [ binary , ...runSnapshotExecArgs ] ,
196- } ) ;
197- return true ;
198- } ) ,
199- } ,
200- ) ;
201- }
202-
203- // Create workers and vm contexts after deserialization
204- {
205- const snapshotFixture = fixtures . path ( 'snapshot' , 'create-worker-and-vm.js' ) ;
206- const blobPath = tmpdir . resolve ( 'embedder-snapshot.blob' ) ;
207- const buildSnapshotArgs = [
208- `eval(${ getReadFileCodeForPath ( snapshotFixture ) } )` ,
209- '--embedder-snapshot-blob' , blobPath , '--embedder-snapshot-create' ,
210- ] ;
211- const runEmbeddedArgs = [
212- '--embedder-snapshot-blob' , blobPath ,
213- ] ;
214-
215- fs . rmSync ( blobPath , { force : true } ) ;
216-
217- runTest (
218- `${ apiType } : build create-worker-and-vm snapshot` ,
219- spawnSyncAndExitWithoutError ,
220- [ apiType , '--' , ...buildSnapshotArgs ] ,
221- {
222- cwd : tmpdir . path ,
223- } ,
224- ) ;
225-
226- runTest (
227- `${ apiType } : run create-worker-and-vm snapshot` ,
228- spawnSyncAndExitWithoutError ,
229- [ apiType , '--' , ...runEmbeddedArgs ] ,
230- {
231- cwd : tmpdir . path ,
232- } ,
233- ) ;
234- }
235- }
236-
237- runSnapshotTests ( 'cpp-api' ) ;
238-
239166// C-API specific tests
240- function runCApiTests ( apiType ) {
241- runTest (
242- `${ apiType } -nodejs-main: run Node.js CLI` ,
243- spawnSyncAndAssert ,
244- [ `${ apiType } -nodejs-main` , '--eval' , 'console.log("Hello World")' ] ,
245- {
246- trim : true ,
247- stdout : 'Hello World' ,
248- } ,
249- ) ;
250- }
251-
252- runCApiTests ( 'c-api' ) ;
167+ spawnSyncAndAssert (
168+ binary ,
169+ [ 'c-api-nodejs-main' , '--eval' , 'console.log("Hello World")' ] ,
170+ {
171+ trim : true ,
172+ stdout : 'Hello World' ,
173+ } ,
174+ )
0 commit comments