1'use strict';
2
3// This tests that user land snapshots works when the instance restored from
4// the snapshot is launched with --help, --check
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();
15
16let snapshotScript = 'node:embedded_snapshot_main';
17if (!process.config.variables.node_use_node_snapshot) {
18  // Check that Node.js built without an embedded snapshot
19  // exits with 1 when node:embedded_snapshot_main is specified
20  // as snapshot entry point.
21  const child = spawnSync(process.execPath, [
22    '--build-snapshot',
23    snapshotScript,
24  ], {
25    cwd: tmpdir.path
26  });
27
28  assert.match(
29    child.stderr.toString(),
30    /Node\.js was built without embedded snapshot/);
31  assert.strictEqual(child.status, 1);
32
33  snapshotScript = fixtures.path('empty.js');
34}
35
36// By default, the snapshot blob path is cwd/snapshot.blob.
37{
38  // Create the snapshot.
39  const child = spawnSync(process.execPath, [
40    '--build-snapshot',
41    snapshotScript,
42  ], {
43    cwd: tmpdir.path
44  });
45  if (child.status !== 0) {
46    console.log(child.stderr.toString());
47    console.log(child.stdout.toString());
48    console.log(child.signal);
49    assert.strictEqual(child.status, 0);
50  }
51  const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
52  assert(stats.isFile());
53}
54
55tmpdir.refresh();
56const blobPath = path.join(tmpdir.path, 'my-snapshot.blob');
57{
58  // Create the snapshot.
59  const child = spawnSync(process.execPath, [
60    '--snapshot-blob',
61    blobPath,
62    '--build-snapshot',
63    snapshotScript,
64  ], {
65    cwd: tmpdir.path
66  });
67  if (child.status !== 0) {
68    console.log(child.stderr.toString());
69    console.log(child.stdout.toString());
70    console.log(child.signal);
71    assert.strictEqual(child.status, 0);
72  }
73  const stats = fs.statSync(blobPath);
74  assert(stats.isFile());
75}
76
77{
78  // Check --help.
79  const child = spawnSync(process.execPath, [
80    '--snapshot-blob',
81    blobPath,
82    '--help',
83  ], {
84    cwd: tmpdir.path
85  });
86
87  if (child.status !== 0) {
88    console.log(child.stderr.toString());
89    console.log(child.stdout.toString());
90    console.log(child.signal);
91    assert.strictEqual(child.status, 0);
92  }
93
94  assert(child.stdout.toString().includes('--help'));
95}
96
97{
98  // Check -c.
99  const child = spawnSync(process.execPath, [
100    '--snapshot-blob',
101    blobPath,
102    '-c',
103    fixtures.path('snapshot', 'marked.js'),
104  ], {
105    cwd: tmpdir.path
106  });
107
108  // Check that it is a noop.
109  assert.strictEqual(child.stdout.toString().trim(), '');
110  assert.strictEqual(child.stderr.toString().trim(), '');
111  assert.strictEqual(child.status, 0);
112}
113