• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// This tests that Node.js refuses to load snapshots built with incompatible
4// V8 configurations.
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 entry = fixtures.path('empty.js');
17
18// The flag used can be any flag that makes a difference in
19// v8::ScriptCompiler::CachedDataVersionTag(). --harmony
20// is chosen here because it's stable enough and makes a difference.
21{
22  // Build a snapshot with --harmony.
23  const child = spawnSync(process.execPath, [
24    '--harmony',
25    '--snapshot-blob',
26    blobPath,
27    '--build-snapshot',
28    entry,
29  ], {
30    cwd: tmpdir.path
31  });
32  if (child.status !== 0) {
33    console.log(child.stderr.toString());
34    console.log(child.stdout.toString());
35    assert.strictEqual(child.status, 0);
36  }
37  const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
38  assert(stats.isFile());
39}
40
41{
42  // Now load the snapshot without --harmony, which should fail.
43  const child = spawnSync(process.execPath, [
44    '--snapshot-blob',
45    blobPath,
46  ], {
47    cwd: tmpdir.path,
48    env: {
49      ...process.env,
50    }
51  });
52
53  const stderr = child.stderr.toString().trim();
54  assert.match(stderr, /Failed to load the startup snapshot/);
55  assert.strictEqual(child.status, 1);
56}
57
58{
59  // Load it again with --harmony and it should work.
60  const child = spawnSync(process.execPath, [
61    '--harmony',
62    '--snapshot-blob',
63    blobPath,
64  ], {
65    cwd: tmpdir.path,
66    env: {
67      ...process.env,
68    }
69  });
70
71  if (child.status !== 0) {
72    console.log(child.stderr.toString());
73    console.log(child.stdout.toString());
74    assert.strictEqual(child.status, 0);
75  }
76}
77