1'use strict'; 2 3// This tests that user land snapshots works when the instance restored from 4// the snapshot is launched with -p and -e 5 6require('../common'); 7const assert = require('assert'); 8const { spawnSync } = require('child_process'); 9const tmpdir = require('../common/tmpdir'); 10const fixtures = require('../common/fixtures'); 11const path = require('path'); 12const fs = require('fs'); 13 14tmpdir.refresh(); 15const blobPath = path.join(tmpdir.path, 'snapshot.blob'); 16const file = fixtures.path('snapshot', 'mutate-fs.js'); 17 18{ 19 // Create the snapshot. 20 const child = spawnSync(process.execPath, [ 21 '--snapshot-blob', 22 blobPath, 23 '--build-snapshot', 24 file, 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 -p works. 39 const child = spawnSync(process.execPath, [ 40 '--snapshot-blob', 41 blobPath, 42 '-p', 43 'require("fs").foo', 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 assert(/I am from the snapshot/.test(child.stdout.toString())); 54} 55 56{ 57 // Check -e works. 58 const child = spawnSync(process.execPath, [ 59 '--snapshot-blob', 60 blobPath, 61 '-e', 62 'console.log(require("fs").foo)', 63 ], { 64 cwd: tmpdir.path 65 }); 66 67 if (child.status !== 0) { 68 console.log(child.stderr.toString()); 69 console.log(child.stdout.toString()); 70 assert.strictEqual(child.status, 0); 71 } 72 assert(/I am from the snapshot/.test(child.stdout.toString())); 73} 74