-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
test-snapshot-repl.js
88 lines (77 loc) · 2.15 KB
/
test-snapshot-repl.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
'use strict';
// This tests that user land snapshots works when the instance restored from
// the snapshot is launched as a REPL.
const common = require('../common');
if (common.isWindows) {
// No way to send CTRL_C_EVENT to processes from JS right now.
common.skip('platform not supported');
}
const assert = require('assert');
const { spawnSync, spawn } = require('child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const path = require('path');
tmpdir.refresh();
const blobPath = path.join(tmpdir.path, 'my-snapshot.blob');
const file = fixtures.path('snapshot', 'mutate-fs.js');
{
// Create the snapshot.
const child = spawnSync(process.execPath, [
'--snapshot-main',
file,
'--snapshot-blob',
blobPath,
], {
cwd: tmpdir.path
});
if (child.status !== 0) {
console.log(child.stderr.toString());
console.log(child.stdout.toString());
assert.strictEqual(child.status, 0);
}
}
{
// Use the snapshot to run the REPL.
const child = spawn(process.execPath, [
'--snapshot-blob',
blobPath,
'-i',
], {
cwd: tmpdir.path,
env: {
...process.env,
REPL_TEST_PPID: process.pid
}
});
let stdout = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
stdout += data;
});
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.stdout.once('data', () => {
process.on('SIGUSR2', () => {
process.kill(child.pid, 'SIGINT');
child.stdout.once('data', () => {
// Make sure state from before the interruption is still available.
child.stdin.end('fs.foo\n');
});
});
child.stdin.write('process.kill(+process.env.REPL_TEST_PPID, "SIGUSR2");' +
'while(true){}\n');
});
child.on('close', common.mustCall(function(code) {
if (code !== 0) {
console.log(stderr);
console.log(stdout);
assert.strictEqual(code, 0);
}
// Check that fs.foo is mutated from the snapshot
assert(/I am from the snapshot/.test(stdout));
}));
child.stdin.end("require('fs').foo");
}