1'use strict'; 2 3// This tests that the warning handler is cleaned up properly 4// during snapshot serialization and installed again during 5// deserialization. 6 7require('../common'); 8 9const assert = require('assert'); 10const { spawnSync } = require('child_process'); 11const tmpdir = require('../common/tmpdir'); 12const fixtures = require('../common/fixtures'); 13const path = require('path'); 14const fs = require('fs'); 15 16const warningScript = fixtures.path('snapshot', 'warning.js'); 17const blobPath = path.join(tmpdir.path, 'snapshot.blob'); 18const empty = fixtures.path('empty.js'); 19 20tmpdir.refresh(); 21{ 22 console.log('\n# Check snapshot scripts that do not emit warnings.'); 23 let child = spawnSync(process.execPath, [ 24 '--snapshot-blob', 25 blobPath, 26 '--build-snapshot', 27 empty, 28 ], { 29 cwd: tmpdir.path 30 }); 31 console.log('[stderr]:', child.stderr.toString()); 32 console.log('[stdout]:', child.stdout.toString()); 33 if (child.status !== 0) { 34 console.log(child.signal); 35 assert.strictEqual(child.status, 0); 36 } 37 const stats = fs.statSync(blobPath); 38 assert(stats.isFile()); 39 40 child = spawnSync(process.execPath, [ 41 '--snapshot-blob', 42 blobPath, 43 warningScript, 44 ], { 45 cwd: tmpdir.path 46 }); 47 console.log('[stderr]:', child.stderr.toString()); 48 console.log('[stdout]:', child.stdout.toString()); 49 if (child.status !== 0) { 50 console.log(child.signal); 51 assert.strictEqual(child.status, 0); 52 } 53 const match = child.stderr.toString().match(/Warning: test warning/g); 54 assert.strictEqual(match.length, 1); 55} 56 57tmpdir.refresh(); 58{ 59 console.log('\n# Check snapshot scripts that emit ' + 60 'warnings and --trace-warnings hint.'); 61 let child = spawnSync(process.execPath, [ 62 '--snapshot-blob', 63 blobPath, 64 '--build-snapshot', 65 warningScript, 66 ], { 67 cwd: tmpdir.path 68 }); 69 console.log('[stderr]:', child.stderr.toString()); 70 console.log('[stdout]:', child.stdout.toString()); 71 if (child.status !== 0) { 72 console.log(child.signal); 73 assert.strictEqual(child.status, 0); 74 } 75 const stats = fs.statSync(blobPath); 76 assert(stats.isFile()); 77 let match = child.stderr.toString().match(/Warning: test warning/g); 78 assert.strictEqual(match.length, 1); 79 match = child.stderr.toString().match(/Use `node --trace-warnings/g); 80 assert.strictEqual(match.length, 1); 81 82 child = spawnSync(process.execPath, [ 83 '--snapshot-blob', 84 blobPath, 85 warningScript, 86 ], { 87 cwd: tmpdir.path 88 }); 89 console.log('[stderr]:', child.stderr.toString()); 90 console.log('[stdout]:', child.stdout.toString()); 91 if (child.status !== 0) { 92 console.log(child.signal); 93 assert.strictEqual(child.status, 0); 94 } 95 // Warnings should not be handled more than once. 96 match = child.stderr.toString().match(/Warning: test warning/g); 97 assert.strictEqual(match.length, 1); 98 match = child.stderr.toString().match(/Use `node --trace-warnings/g); 99 assert.strictEqual(match.length, 1); 100} 101 102tmpdir.refresh(); 103{ 104 console.log('\n# Check --redirect-warnings'); 105 const warningFile1 = path.join(tmpdir.path, 'warnings.txt'); 106 const warningFile2 = path.join(tmpdir.path, 'warnings2.txt'); 107 108 let child = spawnSync(process.execPath, [ 109 '--snapshot-blob', 110 blobPath, 111 '--redirect-warnings', 112 warningFile1, 113 '--build-snapshot', 114 warningScript, 115 ], { 116 cwd: tmpdir.path 117 }); 118 console.log('[stderr]:', child.stderr.toString()); 119 console.log('[stdout]:', child.stdout.toString()); 120 if (child.status !== 0) { 121 console.log(child.signal); 122 assert.strictEqual(child.status, 0); 123 } 124 const stats = fs.statSync(blobPath); 125 assert(stats.isFile()); 126 const warnings1 = fs.readFileSync(warningFile1, 'utf8'); 127 console.log(warningFile1, ':', warnings1); 128 let match = warnings1.match(/Warning: test warning/g); 129 assert.strictEqual(match.length, 1); 130 match = warnings1.match(/Use `node --trace-warnings/g); 131 assert.strictEqual(match.length, 1); 132 assert.doesNotMatch(child.stderr.toString(), /Warning: test warning/); 133 134 fs.rmSync(warningFile1, { 135 maxRetries: 3, recursive: false, force: true 136 }); 137 child = spawnSync(process.execPath, [ 138 '--snapshot-blob', 139 blobPath, 140 '--redirect-warnings', 141 warningFile2, 142 warningScript, 143 ], { 144 cwd: tmpdir.path 145 }); 146 console.log('[stderr]:', child.stderr.toString()); 147 console.log('[stdout]:', child.stdout.toString()); 148 if (child.status !== 0) { 149 console.log(child.signal); 150 assert.strictEqual(child.status, 0); 151 } 152 assert(!fs.existsSync(warningFile1)); 153 154 const warnings2 = fs.readFileSync(warningFile2, 'utf8'); 155 console.log(warningFile2, ':', warnings1); 156 match = warnings2.match(/Warning: test warning/g); 157 assert.strictEqual(match.length, 1); 158 match = warnings2.match(/Use `node --trace-warnings/g); 159 assert.strictEqual(match.length, 1); 160 assert.doesNotMatch(child.stderr.toString(), /Warning: test warning/); 161} 162