11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests that the errors in the snapshot script can be handled
41cb0ef41Sopenharmony_ci// properly.
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 entry = fixtures.path('snapshot', 'error.js');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// --build-snapshot should be run with an entry point.
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
211cb0ef41Sopenharmony_ci    '--snapshot-blob',
221cb0ef41Sopenharmony_ci    blobPath,
231cb0ef41Sopenharmony_ci    '--build-snapshot',
241cb0ef41Sopenharmony_ci  ], {
251cb0ef41Sopenharmony_ci    cwd: tmpdir.path
261cb0ef41Sopenharmony_ci  });
271cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
281cb0ef41Sopenharmony_ci  console.log(child.status);
291cb0ef41Sopenharmony_ci  console.log(stderr);
301cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
311cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 9);
321cb0ef41Sopenharmony_ci  assert.match(stderr,
331cb0ef41Sopenharmony_ci               /--build-snapshot must be used with an entry point script/);
341cb0ef41Sopenharmony_ci  assert(!fs.existsSync(path.join(tmpdir.path, 'snapshot.blob')));
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// Loading a non-existent snapshot should fail.
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
401cb0ef41Sopenharmony_ci    '--snapshot-blob',
411cb0ef41Sopenharmony_ci    blobPath,
421cb0ef41Sopenharmony_ci    entry,
431cb0ef41Sopenharmony_ci  ], {
441cb0ef41Sopenharmony_ci    cwd: tmpdir.path
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
471cb0ef41Sopenharmony_ci  console.log(child.status);
481cb0ef41Sopenharmony_ci  console.log(stderr);
491cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
501cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 1);
511cb0ef41Sopenharmony_ci  assert.match(stderr, /Cannot open/);
521cb0ef41Sopenharmony_ci  assert(!fs.existsSync(path.join(tmpdir.path, 'snapshot.blob')));
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci// Running an script that throws an error should result in an exit code of 1.
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
591cb0ef41Sopenharmony_ci    '--snapshot-blob',
601cb0ef41Sopenharmony_ci    blobPath,
611cb0ef41Sopenharmony_ci    '--build-snapshot',
621cb0ef41Sopenharmony_ci    entry,
631cb0ef41Sopenharmony_ci  ], {
641cb0ef41Sopenharmony_ci    cwd: tmpdir.path
651cb0ef41Sopenharmony_ci  });
661cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString();
671cb0ef41Sopenharmony_ci  console.log(child.status);
681cb0ef41Sopenharmony_ci  console.log(stderr);
691cb0ef41Sopenharmony_ci  console.log(child.stdout.toString());
701cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 1);
711cb0ef41Sopenharmony_ci  assert.match(stderr, /error\.js:1/);
721cb0ef41Sopenharmony_ci  assert(!fs.existsSync(path.join(tmpdir.path, 'snapshot.blob')));
731cb0ef41Sopenharmony_ci}
74