| 1 |
'use strict' |
| 2 |
|
| 3 |
const { resolve } = require('path') |
| 4 |
const { promisify } = require('util') |
| 5 |
const { spawn } = require('child_process') |
| 6 |
const t = require('tap') |
| 7 |
const rimraf = promisify(require('rimraf')) |
| 8 |
const fs = require('../lib/fs-promises') |
| 9 |
|
| 10 |
const node = process.execPath |
| 11 |
const bin = resolve(__dirname, '../self-coverage/bin/nyc') |
| 12 |
const fixturesCLI = resolve(__dirname, './fixtures/cli') |
| 13 |
const tmp = 'processinfo-test' |
| 14 |
const resolvedJS = resolve(fixturesCLI, 'selfspawn-fibonacci.js') |
| 15 |
|
| 16 |
rimraf.sync(resolve(fixturesCLI, tmp)) |
| 17 |
t.teardown(() => rimraf(resolve(fixturesCLI, tmp))) |
| 18 |
|
| 19 |
t.test('build some processinfo', t => { |
| 20 |
var args = [ |
| 21 |
bin, '-t', tmp, |
| 22 |
node, 'selfspawn-fibonacci.js', '5' |
| 23 |
] |
| 24 |
var proc = spawn(process.execPath, args, { |
| 25 |
cwd: fixturesCLI, |
| 26 |
env: { |
| 27 |
PATH: process.env.PATH, |
| 28 |
NYC_PROCESSINFO_EXTERNAL_ID: 'blorp' |
| 29 |
} |
| 30 |
}) |
| 31 |
// don't actually care about the output for this test, just the data |
| 32 |
proc.stderr.resume() |
| 33 |
proc.stdout.resume() |
| 34 |
proc.on('close', (code, signal) => { |
| 35 |
t.equal(code, 0) |
| 36 |
t.equal(signal, null) |
| 37 |
t.end() |
| 38 |
}) |
| 39 |
}) |
| 40 |
|
| 41 |
t.test('validate the created processinfo data', async t => { |
| 42 |
const covs = (await fs.readdir(resolve(fixturesCLI, tmp))) |
| 43 |
.filter(f => f !== 'processinfo') |
| 44 |
|
| 45 |
await Promise.all(covs.map(async f => { |
| 46 |
const covdata = JSON.parse(await fs.readFile(resolve(fixturesCLI, tmp, f), 'utf8')) |
| 47 |
t.same(Object.keys(covdata), [resolvedJS]) |
| 48 |
|
| 49 |
// should have matching processinfo for each cov json |
| 50 |
const procInfoFile = resolve(fixturesCLI, tmp, 'processinfo', f) |
| 51 |
const procInfoData = JSON.parse(await fs.readFile(procInfoFile, 'utf8')) |
| 52 |
t.match(procInfoData, { |
| 53 |
pid: Number, |
| 54 |
ppid: Number, |
| 55 |
uuid: f.replace(/\.json$/, ''), |
| 56 |
argv: [ |
| 57 |
node, |
| 58 |
resolvedJS, |
| 59 |
/[1-5]/ |
| 60 |
], |
| 61 |
execArgv: [], |
| 62 |
cwd: fixturesCLI, |
| 63 |
time: Number, |
| 64 |
coverageFilename: resolve(fixturesCLI, tmp, f), |
| 65 |
files: [resolvedJS] |
| 66 |
}) |
| 67 |
})) |
| 68 |
}) |
| 69 |
|
| 70 |
t.test('check out the index', async t => { |
| 71 |
const indexFile = resolve(fixturesCLI, tmp, 'processinfo', 'index.json') |
| 72 |
const indexJson = await fs.readFile(indexFile, 'utf-8') |
| 73 |
const index = JSON.parse(indexJson) |
| 74 |
const u = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/ |
| 75 |
t.match(index, { |
| 76 |
processes: {}, |
| 77 |
files: { |
| 78 |
[resolvedJS]: [u, u, u, u, u, u, u, u, u] |
| 79 |
}, |
| 80 |
externalIds: { |
| 81 |
blorp: { |
| 82 |
children: [u, u, u, u, u, u, u, u] |
| 83 |
} |
| 84 |
} |
| 85 |
}) |
| 86 |
}) |