|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +if (!process.config.variables.v8_enable_inspector) return; |
| 4 | + |
| 5 | +const common = require('../common'); |
| 6 | +const assert = require('assert'); |
| 7 | +const fs = require('fs'); |
| 8 | +const path = require('path'); |
| 9 | +const { spawnSync } = require('child_process'); |
| 10 | + |
| 11 | +const tmpdir = require('../common/tmpdir'); |
| 12 | +tmpdir.refresh(); |
| 13 | + |
| 14 | +let dirc = 0; |
| 15 | +function nextdir() { |
| 16 | + return `cov_${++dirc}`; |
| 17 | +} |
| 18 | + |
| 19 | +// outputs coverage when event loop is drained, with no async logic. |
| 20 | +{ |
| 21 | + const coverageDirectory = path.join(tmpdir.path, nextdir()); |
| 22 | + const output = spawnSync(process.execPath, [ |
| 23 | + require.resolve('../fixtures/v8-coverage/basic') |
| 24 | + ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); |
| 25 | + assert.strictEqual(output.status, 0); |
| 26 | + const fixtureCoverage = getFixtureCoverage('basic.js', coverageDirectory); |
| 27 | + assert.ok(fixtureCoverage); |
| 28 | + // first branch executed. |
| 29 | + assert.strictEqual(fixtureCoverage.functions[1].ranges[0].count, 1); |
| 30 | + // second branch did not execute. |
| 31 | + assert.strictEqual(fixtureCoverage.functions[1].ranges[1].count, 0); |
| 32 | +} |
| 33 | + |
| 34 | +// outputs coverage when process.exit(1) exits process. |
| 35 | +{ |
| 36 | + const coverageDirectory = path.join(tmpdir.path, nextdir()); |
| 37 | + const output = spawnSync(process.execPath, [ |
| 38 | + require.resolve('../fixtures/v8-coverage/exit-1') |
| 39 | + ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); |
| 40 | + assert.strictEqual(output.status, 1); |
| 41 | + const fixtureCoverage = getFixtureCoverage('exit-1.js', coverageDirectory); |
| 42 | + assert.ok(fixtureCoverage, 'coverage not found for file'); |
| 43 | + // first branch executed. |
| 44 | + assert.strictEqual(fixtureCoverage.functions[1].ranges[0].count, 1); |
| 45 | + // second branch did not execute. |
| 46 | + assert.strictEqual(fixtureCoverage.functions[1].ranges[1].count, 0); |
| 47 | +} |
| 48 | + |
| 49 | +// outputs coverage when process.kill(process.pid, "SIGINT"); exits process. |
| 50 | +{ |
| 51 | + const coverageDirectory = path.join(tmpdir.path, nextdir()); |
| 52 | + const output = spawnSync(process.execPath, [ |
| 53 | + require.resolve('../fixtures/v8-coverage/sigint') |
| 54 | + ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); |
| 55 | + if (!common.isWindows) { |
| 56 | + assert.strictEqual(output.signal, 'SIGINT'); |
| 57 | + } |
| 58 | + const fixtureCoverage = getFixtureCoverage('sigint.js', coverageDirectory); |
| 59 | + assert.ok(fixtureCoverage); |
| 60 | + // first branch executed. |
| 61 | + assert.strictEqual(fixtureCoverage.functions[1].ranges[0].count, 1); |
| 62 | + // second branch did not execute. |
| 63 | + assert.strictEqual(fixtureCoverage.functions[1].ranges[1].count, 0); |
| 64 | +} |
| 65 | + |
| 66 | +// outputs coverage from subprocess. |
| 67 | +{ |
| 68 | + const coverageDirectory = path.join(tmpdir.path, nextdir()); |
| 69 | + const output = spawnSync(process.execPath, [ |
| 70 | + require.resolve('../fixtures/v8-coverage/spawn-subprocess') |
| 71 | + ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); |
| 72 | + assert.strictEqual(output.status, 0); |
| 73 | + const fixtureCoverage = getFixtureCoverage('subprocess.js', |
| 74 | + coverageDirectory); |
| 75 | + assert.ok(fixtureCoverage); |
| 76 | + // first branch executed. |
| 77 | + assert.strictEqual(fixtureCoverage.functions[2].ranges[0].count, 1); |
| 78 | + // second branch did not execute. |
| 79 | + assert.strictEqual(fixtureCoverage.functions[2].ranges[1].count, 0); |
| 80 | +} |
| 81 | + |
| 82 | +// does not output coverage if NODE_V8_COVERAGE is empty. |
| 83 | +{ |
| 84 | + const coverageDirectory = path.join(tmpdir.path, nextdir()); |
| 85 | + const output = spawnSync(process.execPath, [ |
| 86 | + require.resolve('../fixtures/v8-coverage/spawn-subprocess-no-cov') |
| 87 | + ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); |
| 88 | + assert.strictEqual(output.status, 0); |
| 89 | + const fixtureCoverage = getFixtureCoverage('subprocess.js', |
| 90 | + coverageDirectory); |
| 91 | + assert.strictEqual(fixtureCoverage, undefined); |
| 92 | +} |
| 93 | + |
| 94 | +// extracts the coverage object for a given fixture name. |
| 95 | +function getFixtureCoverage(fixtureFile, coverageDirectory) { |
| 96 | + const coverageFiles = fs.readdirSync(coverageDirectory); |
| 97 | + for (const coverageFile of coverageFiles) { |
| 98 | + const coverage = require(path.join(coverageDirectory, coverageFile)); |
| 99 | + for (const fixtureCoverage of coverage.result) { |
| 100 | + if (fixtureCoverage.url.indexOf(`${path.sep}${fixtureFile}`) !== -1) { |
| 101 | + return fixtureCoverage; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments