11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests that user land snapshots works when the instance restored from
41cb0ef41Sopenharmony_ci// the snapshot is launched as a CJS module.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cirequire('../common');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
91cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
101cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
111cb0ef41Sopenharmony_ciconst path = require('path');
121cb0ef41Sopenharmony_ciconst fs = require('fs');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_citmpdir.refresh();
151cb0ef41Sopenharmony_ciconst blobPath = path.join(tmpdir.path, 'snapshot.blob');
161cb0ef41Sopenharmony_ciconst file = fixtures.path('snapshot', 'mutate-fs.js');
171cb0ef41Sopenharmony_ciconst checkFile = fixtures.path('snapshot', 'check-mutate-fs.js');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  // Create the snapshot.
211cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
221cb0ef41Sopenharmony_ci    '--snapshot-blob',
231cb0ef41Sopenharmony_ci    blobPath,
241cb0ef41Sopenharmony_ci    '--build-snapshot',
251cb0ef41Sopenharmony_ci    file,
261cb0ef41Sopenharmony_ci  ], {
271cb0ef41Sopenharmony_ci    cwd: tmpdir.path
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci  if (child.status !== 0) {
301cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
311cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
321cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci  const stats = fs.statSync(blobPath);
351cb0ef41Sopenharmony_ci  assert(stats.isFile());
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  // Run the check file as a CJS module
401cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
411cb0ef41Sopenharmony_ci    '--snapshot-blob',
421cb0ef41Sopenharmony_ci    blobPath,
431cb0ef41Sopenharmony_ci    checkFile,
441cb0ef41Sopenharmony_ci  ], {
451cb0ef41Sopenharmony_ci    cwd: tmpdir.path
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  if (child.status !== 0) {
491cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
501cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
511cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci}
54