Skip to content

Commit

Permalink
test: add more user snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Jun 11, 2021
1 parent 0624d1c commit 8ed3b59
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 9 deletions.
6 changes: 6 additions & 0 deletions test/fixtures/snapshot/check-mutate-fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

const fs = require('fs');
const assert = require('assert');

assert.strictEqual(fs.foo, 'I am from the snapshot');
2 changes: 2 additions & 0 deletions test/fixtures/snapshot/mutate-fs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const fs = require('fs');

fs.foo = 'I am from the snapshot';
53 changes: 53 additions & 0 deletions test/parallel/test-snapshot-cjs-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

// This tests that user land snapshots works when the instance restored from
// the snapshot is launched as a CJS module.

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');

tmpdir.refresh();
const blobPath = path.join(tmpdir.path, 'my-snapshot.blob');
const file = fixtures.path('snapshot', 'mutate-fs.js');
const checkFile = fixtures.path('snapshot', 'check-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);
}
const stats = fs.statSync(blobPath);
assert(stats.isFile());
}

{
// Run the check file as a CJS module
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
checkFile,
], {
cwd: tmpdir.path
});

if (child.status !== 0) {
console.log(child.stderr.toString());
console.log(child.stdout.toString());
assert.strictEqual(child.status, 0);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

// This tests that user land snapshots works when the instance restored from
// the snapshot is launched with -p and -e

require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
Expand All @@ -13,9 +16,12 @@ 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
});
Expand All @@ -24,32 +30,36 @@ const file = fixtures.path('snapshot', 'mutate-fs.js');
console.log(child.stdout.toString());
assert.strictEqual(child.status, 0);
}
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
const stats = fs.statSync(blobPath);
assert(stats.isFile());
}

{
let child = spawnSync(process.execPath, [
'--snapshot-main',
file,
// Check -p works.
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'-p',
'require("fs").foo',
], {
cwd: tmpdir.path
});

if (child.status !== 0) {
console.log(child.stderr.toString());
console.log(child.stdout.toString());
assert.strictEqual(child.status, 0);
}
const stats = fs.statSync(blobPath);
assert(stats.isFile());
assert(/I am from the snapshot/.test(child.stdout.toString()));
}

child = spawnSync(process.execPath, [
{
// Check -e works.
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'-p',
'require("fs").foo',
'-e',
'console.log(require("fs").foo)',
], {
cwd: tmpdir.path
});
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-snapshot-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

// This tests the basic functionality of --snapshot-main.

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');

tmpdir.refresh();
const file = fixtures.path('snapshot', 'mutate-fs.js');

{
// By default, the snapshot blob path is snapshot.blob at cwd
const child = spawnSync(process.execPath, [
'--snapshot-main',
file,
], {
cwd: tmpdir.path
});
if (child.status !== 0) {
console.log(child.stderr.toString());
console.log(child.stdout.toString());
assert.strictEqual(child.status, 0);
}
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
assert(stats.isFile());
}
71 changes: 71 additions & 0 deletions test/parallel/test-snapshot-misc-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

// This tests that user land snapshots works when the instance restored from
// the snapshot is launched with --help, --check

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');

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);
}
const stats = fs.statSync(blobPath);
assert(stats.isFile());
}

{
// Check --help.
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'--help',
], {
cwd: tmpdir.path
});

if (child.status !== 0) {
console.log(child.stderr.toString());
console.log(child.stdout.toString());
assert.strictEqual(child.status, 0);
}

assert(child.stdout.toString().includes('--help'));
}

{
// Check -c.
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'-c',
file,
], {
cwd: tmpdir.path
});

// Check that it is a noop.
assert.strictEqual(child.stdout.toString().trim(), '');
assert.strictEqual(child.stderr.toString().trim(), '');
assert.strictEqual(child.status, 0);
}
88 changes: 88 additions & 0 deletions test/parallel/test-snapshot-repl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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");
}

0 comments on commit 8ed3b59

Please sign in to comment.