1'use strict'; 2 3const tmpdir = require('../common/tmpdir'); 4const { spawnSync } = require('child_process'); 5const path = require('path'); 6const fs = require('fs'); 7const assert = require('assert'); 8 9function buildSnapshot(entry, env) { 10 const child = spawnSync(process.execPath, [ 11 '--snapshot-blob', 12 path.join(tmpdir.path, 'snapshot.blob'), 13 '--build-snapshot', 14 entry, 15 ], { 16 cwd: tmpdir.path, 17 env: { 18 ...process.env, 19 ...env, 20 }, 21 }); 22 23 const stderr = child.stderr.toString(); 24 const stdout = child.stdout.toString(); 25 console.log('[stderr]'); 26 console.log(stderr); 27 console.log('[stdout]'); 28 console.log(stdout); 29 30 assert.strictEqual(child.status, 0); 31 32 const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob')); 33 assert(stats.isFile()); 34 35 return { child, stderr, stdout }; 36} 37 38function runWithSnapshot(entry, env) { 39 const args = ['--snapshot-blob', path.join(tmpdir.path, 'snapshot.blob')]; 40 if (entry !== undefined) { 41 args.push(entry); 42 } 43 const child = spawnSync(process.execPath, args, { 44 cwd: tmpdir.path, 45 env: { 46 ...process.env, 47 ...env, 48 }, 49 }); 50 51 const stderr = child.stderr.toString(); 52 const stdout = child.stdout.toString(); 53 console.log('[stderr]'); 54 console.log(stderr); 55 console.log('[stdout]'); 56 console.log(stdout); 57 58 assert.strictEqual(child.status, 0); 59 60 return { child, stderr, stdout }; 61} 62 63module.exports = { 64 buildSnapshot, 65 runWithSnapshot, 66}; 67