Skip to content

Commit 9303852

Browse files
committed
test: use expectSyncExit{WithErrors} in snapshot tests
..and replace the similar code added for logging.
1 parent 7b106ed commit 9303852

File tree

3 files changed

+69
-87
lines changed

3 files changed

+69
-87
lines changed

‎test/parallel/test-snapshot-api.js‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const assert = require('assert');
77
const{ spawnSync }=require('child_process');
88
consttmpdir=require('../common/tmpdir');
99
constfixtures=require('../common/fixtures');
10+
const{ expectSyncExitWithoutError }=require('../common/child_process');
1011
constfs=require('fs');
1112

1213
constv8=require('v8');
@@ -36,11 +37,8 @@ const entry = fixtures.path('snapshot', 'v8-startup-snapshot-api.js');
3637
],{
3738
cwd: tmpdir.path
3839
});
39-
if(child.status!==0){
40-
console.log(child.stderr.toString());
41-
console.log(child.stdout.toString());
42-
assert.strictEqual(child.status,0);
43-
}
40+
41+
expectSyncExitWithoutError(child);
4442
conststats=fs.statSync(tmpdir.resolve('snapshot.blob'));
4543
assert(stats.isFile());
4644
}
@@ -58,9 +56,9 @@ const entry = fixtures.path('snapshot', 'v8-startup-snapshot-api.js');
5856
}
5957
});
6058

61-
conststdout=child.stdout.toString().trim();
62-
conststderr=child.stderr.toString().trim();
63-
assert.strictEqual(stderr,'Reading book1.en_US.txt');
64-
assert.strictEqual(stdout,'This is book1.en_US.txt');
65-
assert.strictEqual(child.status,0);
59+
expectSyncExitWithoutError(child,{
60+
stderr: 'Reading book1.en_US.txt',
61+
stdout: 'This is book1.en_US.txt',
62+
trim: true
63+
});
6664
}

‎test/parallel/test-snapshot-basic.js‎

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ const assert = require('assert');
88
const{ spawnSync }=require('child_process');
99
consttmpdir=require('../common/tmpdir');
1010
constfixtures=require('../common/fixtures');
11+
const{ expectSyncExitWithoutError, expectSyncExit }=require('../common/child_process');
1112
constfs=require('fs');
1213

1314
tmpdir.refresh();
1415

1516
letsnapshotScript='node:embedded_snapshot_main';
1617
if(!process.config.variables.node_use_node_snapshot){
1718
// Check that Node.js built without an embedded snapshot
18-
// exits with 1 when node:embedded_snapshot_main is specified
19+
// exits with 9 when node:embedded_snapshot_main is specified
1920
// as snapshot entry point.
2021
constchild=spawnSync(process.execPath,[
2122
'--build-snapshot',
@@ -24,10 +25,11 @@ if (!process.config.variables.node_use_node_snapshot){
2425
cwd: tmpdir.path
2526
});
2627

27-
assert.match(
28-
child.stderr.toString(),
29-
/Node\.jswasbuiltwithoutembeddedsnapshot/);
30-
assert.strictEqual(child.status,9);
28+
expectSyncExit(child,{
29+
status: 9,
30+
signal: null,
31+
stderr: /Node\.jswasbuiltwithoutembeddedsnapshot/
32+
});
3133

3234
snapshotScript=fixtures.path('empty.js');
3335
}
@@ -41,12 +43,7 @@ if (!process.config.variables.node_use_node_snapshot){
4143
],{
4244
cwd: tmpdir.path
4345
});
44-
if(child.status!==0){
45-
console.log(child.stderr.toString());
46-
console.log(child.stdout.toString());
47-
console.log(child.signal);
48-
assert.strictEqual(child.status,0);
49-
}
46+
expectSyncExitWithoutError(child);
5047
conststats=fs.statSync(tmpdir.resolve('snapshot.blob'));
5148
assert(stats.isFile());
5249
}
@@ -63,12 +60,7 @@ const blobPath = tmpdir.resolve('my-snapshot.blob');
6360
],{
6461
cwd: tmpdir.path
6562
});
66-
if(child.status!==0){
67-
console.log(child.stderr.toString());
68-
console.log(child.stdout.toString());
69-
console.log(child.signal);
70-
assert.strictEqual(child.status,0);
71-
}
63+
expectSyncExitWithoutError(child);
7264
conststats=fs.statSync(blobPath);
7365
assert(stats.isFile());
7466
}
@@ -82,13 +74,7 @@ const blobPath = tmpdir.resolve('my-snapshot.blob');
8274
],{
8375
cwd: tmpdir.path
8476
});
85-
86-
if(child.status!==0){
87-
console.log(child.stderr.toString());
88-
console.log(child.stdout.toString());
89-
console.log(child.signal);
90-
assert.strictEqual(child.status,0);
91-
}
77+
expectSyncExitWithoutError(child);
9278

9379
assert(child.stdout.toString().includes('--help'));
9480
}
@@ -105,7 +91,9 @@ const blobPath = tmpdir.resolve('my-snapshot.blob');
10591
});
10692

10793
// Check that it is a noop.
108-
assert.strictEqual(child.stdout.toString().trim(),'');
109-
assert.strictEqual(child.stderr.toString().trim(),'');
110-
assert.strictEqual(child.status,0);
94+
expectSyncExitWithoutError(child,{
95+
stderr: '',
96+
stdout: '',
97+
trim: true
98+
});
11199
}

‎test/parallel/test-snapshot-warning.js‎

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const assert = require('assert');
1010
const{ spawnSync }=require('child_process');
1111
consttmpdir=require('../common/tmpdir');
1212
constfixtures=require('../common/fixtures');
13+
const{ expectSyncExitWithoutError }=require('../common/child_process');
1314
constfs=require('fs');
1415

1516
constwarningScript=fixtures.path('snapshot','warning.js');
@@ -27,12 +28,7 @@ tmpdir.refresh();
2728
],{
2829
cwd: tmpdir.path
2930
});
30-
console.log('[stderr]:',child.stderr.toString());
31-
console.log('[stdout]:',child.stdout.toString());
32-
if(child.status!==0){
33-
console.log(child.signal);
34-
assert.strictEqual(child.status,0);
35-
}
31+
expectSyncExitWithoutError(child);
3632
conststats=fs.statSync(blobPath);
3733
assert(stats.isFile());
3834

@@ -43,14 +39,14 @@ tmpdir.refresh();
4339
],{
4440
cwd: tmpdir.path
4541
});
46-
console.log('[stderr]:',child.stderr.toString());
47-
console.log('[stdout]:',child.stdout.toString());
48-
if(child.status!==0){
49-
console.log(child.signal);
50-
assert.strictEqual(child.status,0);
51-
}
52-
constmatch=child.stderr.toString().match(/Warning:testwarning/g);
53-
assert.strictEqual(match.length,1);
42+
expectSyncExitWithoutError(child,{
43+
stderr(output){
44+
constmatch=output.match(/Warning:testwarning/g);
45+
assert.strictEqual(match.length,1);
46+
returntrue;
47+
}
48+
});
49+
5450
}
5551

5652
tmpdir.refresh();
@@ -65,18 +61,17 @@ tmpdir.refresh();
6561
],{
6662
cwd: tmpdir.path
6763
});
68-
console.log('[stderr]:',child.stderr.toString());
69-
console.log('[stdout]:',child.stdout.toString());
70-
if(child.status!==0){
71-
console.log(child.signal);
72-
assert.strictEqual(child.status,0);
73-
}
64+
expectSyncExitWithoutError(child,{
65+
stderr(output){
66+
letmatch=output.match(/Warning:testwarning/g);
67+
assert.strictEqual(match.length,1);
68+
match=output.match(/Use`node--trace-warnings/g);
69+
assert.strictEqual(match.length,1);
70+
returntrue;
71+
}
72+
});
7473
conststats=fs.statSync(blobPath);
7574
assert(stats.isFile());
76-
letmatch=child.stderr.toString().match(/Warning:testwarning/g);
77-
assert.strictEqual(match.length,1);
78-
match=child.stderr.toString().match(/Use`node--trace-warnings/g);
79-
assert.strictEqual(match.length,1);
8075

8176
child=spawnSync(process.execPath,[
8277
'--snapshot-blob',
@@ -85,17 +80,17 @@ tmpdir.refresh();
8580
],{
8681
cwd: tmpdir.path
8782
});
88-
console.log('[stderr]:',child.stderr.toString());
89-
console.log('[stdout]:',child.stdout.toString());
90-
if(child.status!==0){
91-
console.log(child.signal);
92-
assert.strictEqual(child.status,0);
93-
}
94-
// Warnings should not be handled more than once.
95-
match=child.stderr.toString().match(/Warning:testwarning/g);
96-
assert.strictEqual(match.length,1);
97-
match=child.stderr.toString().match(/Use`node--trace-warnings/g);
98-
assert.strictEqual(match.length,1);
83+
84+
expectSyncExitWithoutError(child,{
85+
stderr(output){
86+
// Warnings should not be handled more than once.
87+
letmatch=output.match(/Warning:testwarning/g);
88+
assert.strictEqual(match.length,1);
89+
match=output.match(/Use`node--trace-warnings/g);
90+
assert.strictEqual(match.length,1);
91+
returntrue;
92+
}
93+
});
9994
}
10095

10196
tmpdir.refresh();
@@ -114,25 +109,26 @@ tmpdir.refresh();
114109
],{
115110
cwd: tmpdir.path
116111
});
117-
console.log('[stderr]:',child.stderr.toString());
118-
console.log('[stdout]:',child.stdout.toString());
119-
if(child.status!==0){
120-
console.log(child.signal);
121-
assert.strictEqual(child.status,0);
122-
}
112+
113+
expectSyncExitWithoutError(child,{
114+
stderr(output){
115+
assert.doesNotMatch(output,/Warning:testwarning/);
116+
}
117+
});
118+
123119
conststats=fs.statSync(blobPath);
124120
assert(stats.isFile());
121+
125122
constwarnings1=fs.readFileSync(warningFile1,'utf8');
126123
console.log(warningFile1,':',warnings1);
127124
letmatch=warnings1.match(/Warning:testwarning/g);
128125
assert.strictEqual(match.length,1);
129126
match=warnings1.match(/Use`node--trace-warnings/g);
130127
assert.strictEqual(match.length,1);
131-
assert.doesNotMatch(child.stderr.toString(),/Warning:testwarning/);
132-
133128
fs.rmSync(warningFile1,{
134129
maxRetries: 3,recursive: false,force: true
135130
});
131+
136132
child=spawnSync(process.execPath,[
137133
'--snapshot-blob',
138134
blobPath,
@@ -142,12 +138,13 @@ tmpdir.refresh();
142138
],{
143139
cwd: tmpdir.path
144140
});
145-
console.log('[stderr]:',child.stderr.toString());
146-
console.log('[stdout]:',child.stdout.toString());
147-
if(child.status!==0){
148-
console.log(child.signal);
149-
assert.strictEqual(child.status,0);
150-
}
141+
142+
expectSyncExitWithoutError(child,{
143+
stderr(output){
144+
assert.doesNotMatch(output,/Warning:testwarning/);
145+
returntrue;
146+
}
147+
});
151148
assert(!fs.existsSync(warningFile1));
152149

153150
constwarnings2=fs.readFileSync(warningFile2,'utf8');
@@ -156,5 +153,4 @@ tmpdir.refresh();
156153
assert.strictEqual(match.length,1);
157154
match=warnings2.match(/Use`node--trace-warnings/g);
158155
assert.strictEqual(match.length,1);
159-
assert.doesNotMatch(child.stderr.toString(),/Warning:testwarning/);
160156
}

0 commit comments

Comments
(0)