From 8ed3b593aa52944e388843648741f1ffb452b299 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 12 Jun 2021 03:08:54 +0800 Subject: [PATCH] test: add more user snapshot tests --- test/fixtures/snapshot/check-mutate-fs.js | 6 ++ test/fixtures/snapshot/mutate-fs.js | 2 + test/parallel/test-snapshot-cjs-main.js | 53 +++++++++++ ...snapshot-blob.js => test-snapshot-eval.js} | 28 ++++-- test/parallel/test-snapshot-main.js | 31 +++++++ test/parallel/test-snapshot-misc-main.js | 71 +++++++++++++++ test/parallel/test-snapshot-repl.js | 88 +++++++++++++++++++ 7 files changed, 270 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/snapshot/check-mutate-fs.js create mode 100644 test/parallel/test-snapshot-cjs-main.js rename test/parallel/{test-snapshot-blob.js => test-snapshot-eval.js} (74%) create mode 100644 test/parallel/test-snapshot-main.js create mode 100644 test/parallel/test-snapshot-misc-main.js create mode 100644 test/parallel/test-snapshot-repl.js diff --git a/test/fixtures/snapshot/check-mutate-fs.js b/test/fixtures/snapshot/check-mutate-fs.js new file mode 100644 index 00000000000000..2b746957b82b56 --- /dev/null +++ b/test/fixtures/snapshot/check-mutate-fs.js @@ -0,0 +1,6 @@ +'use strict'; + +const fs = require('fs'); +const assert = require('assert'); + +assert.strictEqual(fs.foo, 'I am from the snapshot'); diff --git a/test/fixtures/snapshot/mutate-fs.js b/test/fixtures/snapshot/mutate-fs.js index ae17874455fa5f..f789ab63db54f9 100644 --- a/test/fixtures/snapshot/mutate-fs.js +++ b/test/fixtures/snapshot/mutate-fs.js @@ -1,3 +1,5 @@ +'use strict'; + const fs = require('fs'); fs.foo = 'I am from the snapshot'; diff --git a/test/parallel/test-snapshot-cjs-main.js b/test/parallel/test-snapshot-cjs-main.js new file mode 100644 index 00000000000000..017932cfb2c99f --- /dev/null +++ b/test/parallel/test-snapshot-cjs-main.js @@ -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); + } +} diff --git a/test/parallel/test-snapshot-blob.js b/test/parallel/test-snapshot-eval.js similarity index 74% rename from test/parallel/test-snapshot-blob.js rename to test/parallel/test-snapshot-eval.js index e3a23a36c0e010..f09f9481262ab8 100644 --- a/test/parallel/test-snapshot-blob.js +++ b/test/parallel/test-snapshot-eval.js @@ -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'); @@ -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 }); @@ -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 }); diff --git a/test/parallel/test-snapshot-main.js b/test/parallel/test-snapshot-main.js new file mode 100644 index 00000000000000..72ce525a5f0fd4 --- /dev/null +++ b/test/parallel/test-snapshot-main.js @@ -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()); +} diff --git a/test/parallel/test-snapshot-misc-main.js b/test/parallel/test-snapshot-misc-main.js new file mode 100644 index 00000000000000..1954db9e52e42e --- /dev/null +++ b/test/parallel/test-snapshot-misc-main.js @@ -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); +} diff --git a/test/parallel/test-snapshot-repl.js b/test/parallel/test-snapshot-repl.js new file mode 100644 index 00000000000000..2542662ee0a0c2 --- /dev/null +++ b/test/parallel/test-snapshot-repl.js @@ -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"); +}