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 with --help, --check
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_ci
161cb0ef41Sopenharmony_cilet snapshotScript = 'node:embedded_snapshot_main';
171cb0ef41Sopenharmony_ciif (!process.config.variables.node_use_node_snapshot) {
181cb0ef41Sopenharmony_ci  // Check that Node.js built without an embedded snapshot
191cb0ef41Sopenharmony_ci  // exits with 1 when node:embedded_snapshot_main is specified
201cb0ef41Sopenharmony_ci  // as snapshot entry point.
211cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
221cb0ef41Sopenharmony_ci    '--build-snapshot',
231cb0ef41Sopenharmony_ci    snapshotScript,
241cb0ef41Sopenharmony_ci  ], {
251cb0ef41Sopenharmony_ci    cwd: tmpdir.path
261cb0ef41Sopenharmony_ci  });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  assert.match(
291cb0ef41Sopenharmony_ci    child.stderr.toString(),
301cb0ef41Sopenharmony_ci    /Node\.js was built without embedded snapshot/);
311cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 1);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  snapshotScript = fixtures.path('empty.js');
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// By default, the snapshot blob path is cwd/snapshot.blob.
371cb0ef41Sopenharmony_ci{
381cb0ef41Sopenharmony_ci  // Create the snapshot.
391cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
401cb0ef41Sopenharmony_ci    '--build-snapshot',
411cb0ef41Sopenharmony_ci    snapshotScript,
421cb0ef41Sopenharmony_ci  ], {
431cb0ef41Sopenharmony_ci    cwd: tmpdir.path
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci  if (child.status !== 0) {
461cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
471cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
481cb0ef41Sopenharmony_ci    console.log(child.signal);
491cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci  const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
521cb0ef41Sopenharmony_ci  assert(stats.isFile());
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_citmpdir.refresh();
561cb0ef41Sopenharmony_ciconst blobPath = path.join(tmpdir.path, 'my-snapshot.blob');
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  // Create the snapshot.
591cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
601cb0ef41Sopenharmony_ci    '--snapshot-blob',
611cb0ef41Sopenharmony_ci    blobPath,
621cb0ef41Sopenharmony_ci    '--build-snapshot',
631cb0ef41Sopenharmony_ci    snapshotScript,
641cb0ef41Sopenharmony_ci  ], {
651cb0ef41Sopenharmony_ci    cwd: tmpdir.path
661cb0ef41Sopenharmony_ci  });
671cb0ef41Sopenharmony_ci  if (child.status !== 0) {
681cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
691cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
701cb0ef41Sopenharmony_ci    console.log(child.signal);
711cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
721cb0ef41Sopenharmony_ci  }
731cb0ef41Sopenharmony_ci  const stats = fs.statSync(blobPath);
741cb0ef41Sopenharmony_ci  assert(stats.isFile());
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci{
781cb0ef41Sopenharmony_ci  // Check --help.
791cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
801cb0ef41Sopenharmony_ci    '--snapshot-blob',
811cb0ef41Sopenharmony_ci    blobPath,
821cb0ef41Sopenharmony_ci    '--help',
831cb0ef41Sopenharmony_ci  ], {
841cb0ef41Sopenharmony_ci    cwd: tmpdir.path
851cb0ef41Sopenharmony_ci  });
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  if (child.status !== 0) {
881cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
891cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
901cb0ef41Sopenharmony_ci    console.log(child.signal);
911cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
921cb0ef41Sopenharmony_ci  }
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  assert(child.stdout.toString().includes('--help'));
951cb0ef41Sopenharmony_ci}
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci{
981cb0ef41Sopenharmony_ci  // Check -c.
991cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
1001cb0ef41Sopenharmony_ci    '--snapshot-blob',
1011cb0ef41Sopenharmony_ci    blobPath,
1021cb0ef41Sopenharmony_ci    '-c',
1031cb0ef41Sopenharmony_ci    fixtures.path('snapshot', 'marked.js'),
1041cb0ef41Sopenharmony_ci  ], {
1051cb0ef41Sopenharmony_ci    cwd: tmpdir.path
1061cb0ef41Sopenharmony_ci  });
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  // Check that it is a noop.
1091cb0ef41Sopenharmony_ci  assert.strictEqual(child.stdout.toString().trim(), '');
1101cb0ef41Sopenharmony_ci  assert.strictEqual(child.stderr.toString().trim(), '');
1111cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 0);
1121cb0ef41Sopenharmony_ci}
113