11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests that the warning handler is cleaned up properly
41cb0ef41Sopenharmony_ci// during snapshot serialization and installed again during
51cb0ef41Sopenharmony_ci// deserialization.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cirequire('../common');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
111cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
121cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
131cb0ef41Sopenharmony_ciconst path = require('path');
141cb0ef41Sopenharmony_ciconst fs = require('fs');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst warningScript = fixtures.path('snapshot', 'warning.js');
171cb0ef41Sopenharmony_ciconst blobPath = path.join(tmpdir.path, 'snapshot.blob');
181cb0ef41Sopenharmony_ciconst empty = fixtures.path('empty.js');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_citmpdir.refresh();
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci  console.log('\n# Check snapshot scripts that do not emit warnings.');
231cb0ef41Sopenharmony_ci  let child = spawnSync(process.execPath, [
241cb0ef41Sopenharmony_ci    '--snapshot-blob',
251cb0ef41Sopenharmony_ci    blobPath,
261cb0ef41Sopenharmony_ci    '--build-snapshot',
271cb0ef41Sopenharmony_ci    empty,
281cb0ef41Sopenharmony_ci  ], {
291cb0ef41Sopenharmony_ci    cwd: tmpdir.path
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci  console.log('[stderr]:', child.stderr.toString());
321cb0ef41Sopenharmony_ci  console.log('[stdout]:', child.stdout.toString());
331cb0ef41Sopenharmony_ci  if (child.status !== 0) {
341cb0ef41Sopenharmony_ci    console.log(child.signal);
351cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci  const stats = fs.statSync(blobPath);
381cb0ef41Sopenharmony_ci  assert(stats.isFile());
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  child = spawnSync(process.execPath, [
411cb0ef41Sopenharmony_ci    '--snapshot-blob',
421cb0ef41Sopenharmony_ci    blobPath,
431cb0ef41Sopenharmony_ci    warningScript,
441cb0ef41Sopenharmony_ci  ], {
451cb0ef41Sopenharmony_ci    cwd: tmpdir.path
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci  console.log('[stderr]:', child.stderr.toString());
481cb0ef41Sopenharmony_ci  console.log('[stdout]:', child.stdout.toString());
491cb0ef41Sopenharmony_ci  if (child.status !== 0) {
501cb0ef41Sopenharmony_ci    console.log(child.signal);
511cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci  const match = child.stderr.toString().match(/Warning: test warning/g);
541cb0ef41Sopenharmony_ci  assert.strictEqual(match.length, 1);
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_citmpdir.refresh();
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  console.log('\n# Check snapshot scripts that emit ' +
601cb0ef41Sopenharmony_ci              'warnings and --trace-warnings hint.');
611cb0ef41Sopenharmony_ci  let child = spawnSync(process.execPath, [
621cb0ef41Sopenharmony_ci    '--snapshot-blob',
631cb0ef41Sopenharmony_ci    blobPath,
641cb0ef41Sopenharmony_ci    '--build-snapshot',
651cb0ef41Sopenharmony_ci    warningScript,
661cb0ef41Sopenharmony_ci  ], {
671cb0ef41Sopenharmony_ci    cwd: tmpdir.path
681cb0ef41Sopenharmony_ci  });
691cb0ef41Sopenharmony_ci  console.log('[stderr]:', child.stderr.toString());
701cb0ef41Sopenharmony_ci  console.log('[stdout]:', child.stdout.toString());
711cb0ef41Sopenharmony_ci  if (child.status !== 0) {
721cb0ef41Sopenharmony_ci    console.log(child.signal);
731cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
741cb0ef41Sopenharmony_ci  }
751cb0ef41Sopenharmony_ci  const stats = fs.statSync(blobPath);
761cb0ef41Sopenharmony_ci  assert(stats.isFile());
771cb0ef41Sopenharmony_ci  let match = child.stderr.toString().match(/Warning: test warning/g);
781cb0ef41Sopenharmony_ci  assert.strictEqual(match.length, 1);
791cb0ef41Sopenharmony_ci  match = child.stderr.toString().match(/Use `node --trace-warnings/g);
801cb0ef41Sopenharmony_ci  assert.strictEqual(match.length, 1);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  child = spawnSync(process.execPath, [
831cb0ef41Sopenharmony_ci    '--snapshot-blob',
841cb0ef41Sopenharmony_ci    blobPath,
851cb0ef41Sopenharmony_ci    warningScript,
861cb0ef41Sopenharmony_ci  ], {
871cb0ef41Sopenharmony_ci    cwd: tmpdir.path
881cb0ef41Sopenharmony_ci  });
891cb0ef41Sopenharmony_ci  console.log('[stderr]:', child.stderr.toString());
901cb0ef41Sopenharmony_ci  console.log('[stdout]:', child.stdout.toString());
911cb0ef41Sopenharmony_ci  if (child.status !== 0) {
921cb0ef41Sopenharmony_ci    console.log(child.signal);
931cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
941cb0ef41Sopenharmony_ci  }
951cb0ef41Sopenharmony_ci  // Warnings should not be handled more than once.
961cb0ef41Sopenharmony_ci  match = child.stderr.toString().match(/Warning: test warning/g);
971cb0ef41Sopenharmony_ci  assert.strictEqual(match.length, 1);
981cb0ef41Sopenharmony_ci  match = child.stderr.toString().match(/Use `node --trace-warnings/g);
991cb0ef41Sopenharmony_ci  assert.strictEqual(match.length, 1);
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_citmpdir.refresh();
1031cb0ef41Sopenharmony_ci{
1041cb0ef41Sopenharmony_ci  console.log('\n# Check --redirect-warnings');
1051cb0ef41Sopenharmony_ci  const warningFile1 = path.join(tmpdir.path, 'warnings.txt');
1061cb0ef41Sopenharmony_ci  const warningFile2 = path.join(tmpdir.path, 'warnings2.txt');
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  let child = spawnSync(process.execPath, [
1091cb0ef41Sopenharmony_ci    '--snapshot-blob',
1101cb0ef41Sopenharmony_ci    blobPath,
1111cb0ef41Sopenharmony_ci    '--redirect-warnings',
1121cb0ef41Sopenharmony_ci    warningFile1,
1131cb0ef41Sopenharmony_ci    '--build-snapshot',
1141cb0ef41Sopenharmony_ci    warningScript,
1151cb0ef41Sopenharmony_ci  ], {
1161cb0ef41Sopenharmony_ci    cwd: tmpdir.path
1171cb0ef41Sopenharmony_ci  });
1181cb0ef41Sopenharmony_ci  console.log('[stderr]:', child.stderr.toString());
1191cb0ef41Sopenharmony_ci  console.log('[stdout]:', child.stdout.toString());
1201cb0ef41Sopenharmony_ci  if (child.status !== 0) {
1211cb0ef41Sopenharmony_ci    console.log(child.signal);
1221cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
1231cb0ef41Sopenharmony_ci  }
1241cb0ef41Sopenharmony_ci  const stats = fs.statSync(blobPath);
1251cb0ef41Sopenharmony_ci  assert(stats.isFile());
1261cb0ef41Sopenharmony_ci  const warnings1 = fs.readFileSync(warningFile1, 'utf8');
1271cb0ef41Sopenharmony_ci  console.log(warningFile1, ':', warnings1);
1281cb0ef41Sopenharmony_ci  let match = warnings1.match(/Warning: test warning/g);
1291cb0ef41Sopenharmony_ci  assert.strictEqual(match.length, 1);
1301cb0ef41Sopenharmony_ci  match = warnings1.match(/Use `node --trace-warnings/g);
1311cb0ef41Sopenharmony_ci  assert.strictEqual(match.length, 1);
1321cb0ef41Sopenharmony_ci  assert.doesNotMatch(child.stderr.toString(), /Warning: test warning/);
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  fs.rmSync(warningFile1, {
1351cb0ef41Sopenharmony_ci    maxRetries: 3, recursive: false, force: true
1361cb0ef41Sopenharmony_ci  });
1371cb0ef41Sopenharmony_ci  child = spawnSync(process.execPath, [
1381cb0ef41Sopenharmony_ci    '--snapshot-blob',
1391cb0ef41Sopenharmony_ci    blobPath,
1401cb0ef41Sopenharmony_ci    '--redirect-warnings',
1411cb0ef41Sopenharmony_ci    warningFile2,
1421cb0ef41Sopenharmony_ci    warningScript,
1431cb0ef41Sopenharmony_ci  ], {
1441cb0ef41Sopenharmony_ci    cwd: tmpdir.path
1451cb0ef41Sopenharmony_ci  });
1461cb0ef41Sopenharmony_ci  console.log('[stderr]:', child.stderr.toString());
1471cb0ef41Sopenharmony_ci  console.log('[stdout]:', child.stdout.toString());
1481cb0ef41Sopenharmony_ci  if (child.status !== 0) {
1491cb0ef41Sopenharmony_ci    console.log(child.signal);
1501cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
1511cb0ef41Sopenharmony_ci  }
1521cb0ef41Sopenharmony_ci  assert(!fs.existsSync(warningFile1));
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci  const warnings2 = fs.readFileSync(warningFile2, 'utf8');
1551cb0ef41Sopenharmony_ci  console.log(warningFile2, ':', warnings1);
1561cb0ef41Sopenharmony_ci  match = warnings2.match(/Warning: test warning/g);
1571cb0ef41Sopenharmony_ci  assert.strictEqual(match.length, 1);
1581cb0ef41Sopenharmony_ci  match = warnings2.match(/Use `node --trace-warnings/g);
1591cb0ef41Sopenharmony_ci  assert.strictEqual(match.length, 1);
1601cb0ef41Sopenharmony_ci  assert.doesNotMatch(child.stderr.toString(), /Warning: test warning/);
1611cb0ef41Sopenharmony_ci}
162