Skip to content

Commit 8ed3b59

Browse files
committed
test: add more user snapshot tests
1 parent 0624d1c commit 8ed3b59

7 files changed

+270
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const assert = require('assert');
5+
6+
assert.strictEqual(fs.foo, 'I am from the snapshot');

test/fixtures/snapshot/mutate-fs.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const fs = require('fs');
24

35
fs.foo = 'I am from the snapshot';
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
// This tests that user land snapshots works when the instance restored from
4+
// the snapshot is launched as a CJS module.
5+
6+
require('../common');
7+
const assert = require('assert');
8+
const { spawnSync } = require('child_process');
9+
const tmpdir = require('../common/tmpdir');
10+
const fixtures = require('../common/fixtures');
11+
const path = require('path');
12+
const fs = require('fs');
13+
14+
tmpdir.refresh();
15+
const blobPath = path.join(tmpdir.path, 'my-snapshot.blob');
16+
const file = fixtures.path('snapshot', 'mutate-fs.js');
17+
const checkFile = fixtures.path('snapshot', 'check-mutate-fs.js');
18+
19+
{
20+
// Create the snapshot.
21+
const child = spawnSync(process.execPath, [
22+
'--snapshot-main',
23+
file,
24+
'--snapshot-blob',
25+
blobPath,
26+
], {
27+
cwd: tmpdir.path
28+
});
29+
if (child.status !== 0) {
30+
console.log(child.stderr.toString());
31+
console.log(child.stdout.toString());
32+
assert.strictEqual(child.status, 0);
33+
}
34+
const stats = fs.statSync(blobPath);
35+
assert(stats.isFile());
36+
}
37+
38+
{
39+
// Run the check file as a CJS module
40+
const child = spawnSync(process.execPath, [
41+
'--snapshot-blob',
42+
blobPath,
43+
checkFile,
44+
], {
45+
cwd: tmpdir.path
46+
});
47+
48+
if (child.status !== 0) {
49+
console.log(child.stderr.toString());
50+
console.log(child.stdout.toString());
51+
assert.strictEqual(child.status, 0);
52+
}
53+
}

test/parallel/test-snapshot-blob.js renamed to test/parallel/test-snapshot-eval.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
// This tests that user land snapshots works when the instance restored from
4+
// the snapshot is launched with -p and -e
5+
36
require('../common');
47
const assert = require('assert');
58
const { spawnSync } = require('child_process');
@@ -13,9 +16,12 @@ const blobPath = path.join(tmpdir.path, 'my-snapshot.blob');
1316
const file = fixtures.path('snapshot', 'mutate-fs.js');
1417

1518
{
19+
// Create the snapshot.
1620
const child = spawnSync(process.execPath, [
1721
'--snapshot-main',
1822
file,
23+
'--snapshot-blob',
24+
blobPath,
1925
], {
2026
cwd: tmpdir.path
2127
});
@@ -24,32 +30,36 @@ const file = fixtures.path('snapshot', 'mutate-fs.js');
2430
console.log(child.stdout.toString());
2531
assert.strictEqual(child.status, 0);
2632
}
27-
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
33+
const stats = fs.statSync(blobPath);
2834
assert(stats.isFile());
2935
}
3036

3137
{
32-
let child = spawnSync(process.execPath, [
33-
'--snapshot-main',
34-
file,
38+
// Check -p works.
39+
const child = spawnSync(process.execPath, [
3540
'--snapshot-blob',
3641
blobPath,
42+
'-p',
43+
'require("fs").foo',
3744
], {
3845
cwd: tmpdir.path
3946
});
47+
4048
if (child.status !== 0) {
4149
console.log(child.stderr.toString());
4250
console.log(child.stdout.toString());
4351
assert.strictEqual(child.status, 0);
4452
}
45-
const stats = fs.statSync(blobPath);
46-
assert(stats.isFile());
53+
assert(/I am from the snapshot/.test(child.stdout.toString()));
54+
}
4755

48-
child = spawnSync(process.execPath, [
56+
{
57+
// Check -e works.
58+
const child = spawnSync(process.execPath, [
4959
'--snapshot-blob',
5060
blobPath,
51-
'-p',
52-
'require("fs").foo',
61+
'-e',
62+
'console.log(require("fs").foo)',
5363
], {
5464
cwd: tmpdir.path
5565
});

test/parallel/test-snapshot-main.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
// This tests the basic functionality of --snapshot-main.
4+
5+
require('../common');
6+
const assert = require('assert');
7+
const { spawnSync } = require('child_process');
8+
const tmpdir = require('../common/tmpdir');
9+
const fixtures = require('../common/fixtures');
10+
const path = require('path');
11+
const fs = require('fs');
12+
13+
tmpdir.refresh();
14+
const file = fixtures.path('snapshot', 'mutate-fs.js');
15+
16+
{
17+
// By default, the snapshot blob path is snapshot.blob at cwd
18+
const child = spawnSync(process.execPath, [
19+
'--snapshot-main',
20+
file,
21+
], {
22+
cwd: tmpdir.path
23+
});
24+
if (child.status !== 0) {
25+
console.log(child.stderr.toString());
26+
console.log(child.stdout.toString());
27+
assert.strictEqual(child.status, 0);
28+
}
29+
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
30+
assert(stats.isFile());
31+
}
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
// This tests that user land snapshots works when the instance restored from
4+
// the snapshot is launched with --help, --check
5+
6+
require('../common');
7+
const assert = require('assert');
8+
const { spawnSync } = require('child_process');
9+
const tmpdir = require('../common/tmpdir');
10+
const fixtures = require('../common/fixtures');
11+
const path = require('path');
12+
const fs = require('fs');
13+
14+
tmpdir.refresh();
15+
const blobPath = path.join(tmpdir.path, 'my-snapshot.blob');
16+
const file = fixtures.path('snapshot', 'mutate-fs.js');
17+
18+
{
19+
// Create the snapshot.
20+
const child = spawnSync(process.execPath, [
21+
'--snapshot-main',
22+
file,
23+
'--snapshot-blob',
24+
blobPath,
25+
], {
26+
cwd: tmpdir.path
27+
});
28+
if (child.status !== 0) {
29+
console.log(child.stderr.toString());
30+
console.log(child.stdout.toString());
31+
assert.strictEqual(child.status, 0);
32+
}
33+
const stats = fs.statSync(blobPath);
34+
assert(stats.isFile());
35+
}
36+
37+
{
38+
// Check --help.
39+
const child = spawnSync(process.execPath, [
40+
'--snapshot-blob',
41+
blobPath,
42+
'--help',
43+
], {
44+
cwd: tmpdir.path
45+
});
46+
47+
if (child.status !== 0) {
48+
console.log(child.stderr.toString());
49+
console.log(child.stdout.toString());
50+
assert.strictEqual(child.status, 0);
51+
}
52+
53+
assert(child.stdout.toString().includes('--help'));
54+
}
55+
56+
{
57+
// Check -c.
58+
const child = spawnSync(process.execPath, [
59+
'--snapshot-blob',
60+
blobPath,
61+
'-c',
62+
file,
63+
], {
64+
cwd: tmpdir.path
65+
});
66+
67+
// Check that it is a noop.
68+
assert.strictEqual(child.stdout.toString().trim(), '');
69+
assert.strictEqual(child.stderr.toString().trim(), '');
70+
assert.strictEqual(child.status, 0);
71+
}

test/parallel/test-snapshot-repl.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// This tests that user land snapshots works when the instance restored from
4+
// the snapshot is launched as a REPL.
5+
6+
const common = require('../common');
7+
if (common.isWindows) {
8+
// No way to send CTRL_C_EVENT to processes from JS right now.
9+
common.skip('platform not supported');
10+
}
11+
const assert = require('assert');
12+
const { spawnSync, spawn } = require('child_process');
13+
const tmpdir = require('../common/tmpdir');
14+
const fixtures = require('../common/fixtures');
15+
const path = require('path');
16+
17+
tmpdir.refresh();
18+
const blobPath = path.join(tmpdir.path, 'my-snapshot.blob');
19+
const file = fixtures.path('snapshot', 'mutate-fs.js');
20+
21+
{
22+
// Create the snapshot.
23+
const child = spawnSync(process.execPath, [
24+
'--snapshot-main',
25+
file,
26+
'--snapshot-blob',
27+
blobPath,
28+
], {
29+
cwd: tmpdir.path
30+
});
31+
if (child.status !== 0) {
32+
console.log(child.stderr.toString());
33+
console.log(child.stdout.toString());
34+
assert.strictEqual(child.status, 0);
35+
}
36+
}
37+
38+
{
39+
// Use the snapshot to run the REPL.
40+
const child = spawn(process.execPath, [
41+
'--snapshot-blob',
42+
blobPath,
43+
'-i',
44+
], {
45+
cwd: tmpdir.path,
46+
env: {
47+
...process.env,
48+
REPL_TEST_PPID: process.pid
49+
}
50+
});
51+
52+
let stdout = '';
53+
child.stdout.setEncoding('utf8');
54+
child.stdout.on('data', (data) => {
55+
stdout += data;
56+
});
57+
58+
let stderr = '';
59+
child.stderr.setEncoding('utf8');
60+
child.stderr.on('data', (data) => {
61+
stderr += data;
62+
});
63+
64+
child.stdout.once('data', () => {
65+
process.on('SIGUSR2', () => {
66+
process.kill(child.pid, 'SIGINT');
67+
child.stdout.once('data', () => {
68+
// Make sure state from before the interruption is still available.
69+
child.stdin.end('fs.foo\n');
70+
});
71+
});
72+
73+
child.stdin.write('process.kill(+process.env.REPL_TEST_PPID, "SIGUSR2");' +
74+
'while(true){}\n');
75+
});
76+
77+
child.on('close', common.mustCall(function(code) {
78+
if (code !== 0) {
79+
console.log(stderr);
80+
console.log(stdout);
81+
assert.strictEqual(code, 0);
82+
}
83+
// Check that fs.foo is mutated from the snapshot
84+
assert(/I am from the snapshot/.test(stdout));
85+
}));
86+
87+
child.stdin.end("require('fs').foo");
88+
}

0 commit comments

Comments
 (0)