forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-snapshot-warning.js
161 lines (149 loc) · 4.75 KB
/
test-snapshot-warning.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
// This tests that the warning handler is cleaned up properly
// during snapshot serialization and installed again during
// deserialization.
require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const path = require('path');
const fs = require('fs');
const warningScript = fixtures.path('snapshot', 'warning.js');
const blobPath = path.join(tmpdir.path, 'snapshot.blob');
const empty = fixtures.path('empty.js');
tmpdir.refresh();
{
console.log('\n# Check snapshot scripts that do not emit warnings.');
let child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
empty,
], {
cwd: tmpdir.path
});
console.log('[stderr]:', child.stderr.toString());
console.log('[stdout]:', child.stdout.toString());
if (child.status !== 0) {
console.log(child.signal);
assert.strictEqual(child.status, 0);
}
const stats = fs.statSync(blobPath);
assert(stats.isFile());
child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
warningScript,
], {
cwd: tmpdir.path
});
console.log('[stderr]:', child.stderr.toString());
console.log('[stdout]:', child.stdout.toString());
if (child.status !== 0) {
console.log(child.signal);
assert.strictEqual(child.status, 0);
}
const match = child.stderr.toString().match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
}
tmpdir.refresh();
{
console.log('\n# Check snapshot scripts that emit ' +
'warnings and --trace-warnings hint.');
let child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
warningScript,
], {
cwd: tmpdir.path
});
console.log('[stderr]:', child.stderr.toString());
console.log('[stdout]:', child.stdout.toString());
if (child.status !== 0) {
console.log(child.signal);
assert.strictEqual(child.status, 0);
}
const stats = fs.statSync(blobPath);
assert(stats.isFile());
let match = child.stderr.toString().match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
match = child.stderr.toString().match(/Use `node --trace-warnings/g);
assert.strictEqual(match.length, 1);
child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
warningScript,
], {
cwd: tmpdir.path
});
console.log('[stderr]:', child.stderr.toString());
console.log('[stdout]:', child.stdout.toString());
if (child.status !== 0) {
console.log(child.signal);
assert.strictEqual(child.status, 0);
}
// Warnings should not be handled more than once.
match = child.stderr.toString().match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
match = child.stderr.toString().match(/Use `node --trace-warnings/g);
assert.strictEqual(match.length, 1);
}
tmpdir.refresh();
{
console.log('\n# Check --redirect-warnings');
const warningFile1 = path.join(tmpdir.path, 'warnings.txt');
const warningFile2 = path.join(tmpdir.path, 'warnings2.txt');
let child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'--redirect-warnings',
warningFile1,
'--build-snapshot',
warningScript,
], {
cwd: tmpdir.path
});
console.log('[stderr]:', child.stderr.toString());
console.log('[stdout]:', child.stdout.toString());
if (child.status !== 0) {
console.log(child.signal);
assert.strictEqual(child.status, 0);
}
const stats = fs.statSync(blobPath);
assert(stats.isFile());
const warnings1 = fs.readFileSync(warningFile1, 'utf8');
console.log(warningFile1, ':', warnings1);
let match = warnings1.match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
match = warnings1.match(/Use `node --trace-warnings/g);
assert.strictEqual(match.length, 1);
assert.doesNotMatch(child.stderr.toString(), /Warning: test warning/);
fs.rmSync(warningFile1, {
maxRetries: 3, recursive: false, force: true
});
child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'--redirect-warnings',
warningFile2,
warningScript,
], {
cwd: tmpdir.path
});
console.log('[stderr]:', child.stderr.toString());
console.log('[stdout]:', child.stdout.toString());
if (child.status !== 0) {
console.log(child.signal);
assert.strictEqual(child.status, 0);
}
assert(!fs.existsSync(warningFile1));
const warnings2 = fs.readFileSync(warningFile2, 'utf8');
console.log(warningFile2, ':', warnings1);
match = warnings2.match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
match = warnings2.match(/Use `node --trace-warnings/g);
assert.strictEqual(match.length, 1);
assert.doesNotMatch(child.stderr.toString(), /Warning: test warning/);
}