1'use strict';
2
3if (!process.features.inspector) return;
4
5require('../common');
6const fixtures = require('../common/fixtures');
7const tmpdir = require('../common/tmpdir');
8const assert = require('assert');
9const fs = require('fs');
10const path = require('path');
11const { spawnSync } = require('child_process');
12
13tmpdir.refresh();
14const intervals = 40;
15// Outputs coverage when v8.takeCoverage() is invoked.
16{
17  const output = spawnSync(process.execPath, [
18    '-r',
19    fixtures.path('v8-coverage', 'take-coverage'),
20    fixtures.path('v8-coverage', 'interval'),
21  ], {
22    env: {
23      ...process.env,
24      NODE_V8_COVERAGE: tmpdir.path,
25      NODE_DEBUG_NATIVE: 'INSPECTOR_PROFILER',
26      TEST_INTERVALS: intervals
27    },
28  });
29  console.log(output.stderr.toString());
30  assert.strictEqual(output.status, 0);
31  const coverageFiles = fs.readdirSync(tmpdir.path);
32
33  let coverages = [];
34  for (const coverageFile of coverageFiles) {
35    const coverage = require(path.join(tmpdir.path, coverageFile));
36    for (const result of coverage.result) {
37      if (result.url.includes('/interval')) {
38        coverages.push({
39          file: coverageFile,
40          func: result.functions.find((f) => f.functionName === 'interval'),
41          timestamp: coverage.timestamp
42        });
43      }
44    }
45  }
46
47  coverages = coverages.sort((a, b) => { return a.timestamp - b.timestamp; });
48  // There should be two coverages taken, one triggered by v8.takeCoverage(),
49  // the other by process exit.
50  console.log('Coverages:', coverages);
51  assert.strictEqual(coverages.length, 3);
52
53  let blockHitsTotal = 0;
54  for (let i = 0; i < coverages.length; ++i) {
55    const { ranges } = coverages[i].func;
56    console.log('coverage', i, ranges);
57
58    if (i !== coverages.length - 1) {
59      // When the first two coverages are taken:
60      assert.strictEqual(ranges.length, 2);
61      const blockHits = ranges[0].count;
62      // The block inside interval() should be hit at least once.
63      assert.notStrictEqual(blockHits, 0);
64      blockHitsTotal += blockHits;
65      // The else branch should not be hit.
66      const elseBranchHits = ranges[1].count;
67      assert.strictEqual(elseBranchHits, 0);
68    } else {
69      // At process exit:
70      assert.strictEqual(ranges.length, 3);
71      const blockHits = ranges[0].count;
72      // The block inside interval() should be hit at least once more.
73      assert.notStrictEqual(blockHits, 0);
74      blockHitsTotal += blockHits;
75      // The else branch should be hit exactly once.
76      const elseBranchHits = ranges[2].count;
77      assert.strictEqual(elseBranchHits, 1);
78      const ifBranchHits = ranges[1].count;
79      assert.strictEqual(ifBranchHits, blockHits - elseBranchHits);
80    }
81  }
82
83  // The block should be hit `intervals` times in total.
84  assert.strictEqual(blockHitsTotal, intervals);
85}
86