| 1 |
'use strict' |
| 2 |
const t = require('tap') |
| 3 |
const {ProcessDB} = require('../') |
| 4 |
const uuidRe = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/ |
| 5 |
const NYC = require('nyc') |
| 6 |
const rimraf = require('rimraf').sync |
| 7 |
const fs = require('fs') |
| 8 |
const path = require('path') |
| 9 |
const directory = path.resolve(__dirname, 'fixtures/.nyc_output/processinfo') |
| 10 |
const indexFile = path.resolve(directory, 'index.json') |
| 11 |
|
| 12 |
t.test('basic creation', t => { |
| 13 |
const pdb = new ProcessDB(directory) |
| 14 |
t.throws(() => { |
| 15 |
pdb.directory = 'foo' |
| 16 |
}, TypeError) |
| 17 |
t.equal(pdb.directory, directory, 'directory is read-only') |
| 18 |
t.equal(pdb.label, 'nyc') |
| 19 |
t.end() |
| 20 |
}) |
| 21 |
|
| 22 |
t.test('writing and reading index', async t => { |
| 23 |
const pdb = new ProcessDB(directory) |
| 24 |
rimraf(indexFile) |
| 25 |
t.throws(() => fs.readFileSync(indexFile)) |
| 26 |
|
| 27 |
// put a borked json in there just for funsies |
| 28 |
const bad = path.resolve(directory, 'gleepglorp.json') |
| 29 |
fs.writeFileSync(bad, 'this is not json, it is bad') |
| 30 |
t.teardown(() => rimraf(bad)) |
| 31 |
|
| 32 |
// this should never happen, but we are resilient against it anyway |
| 33 |
const dupe = path.resolve(directory, 'duplicate.json') |
| 34 |
const src = path.resolve(directory, '62c33964-203a-4e6a-b1ff-8a046eeb8912.json') |
| 35 |
fs.writeFileSync(dupe, fs.readFileSync(src)) |
| 36 |
t.teardown(() => rimraf(dupe)) |
| 37 |
|
| 38 |
t.matchSnapshot(await pdb.readIndex()) |
| 39 |
}) |
| 40 |
|
| 41 |
t.test('duplicate externalId throws', async t => { |
| 42 |
const dupe = path.resolve(directory, 'duplicate.json') |
| 43 |
const src = path.resolve(directory, '625ef291-93c0-40b2-a869-70587f7e8fac.json') |
| 44 |
fs.writeFileSync(dupe, fs.readFileSync(src)) |
| 45 |
t.teardown(() => rimraf(dupe)) |
| 46 |
const pdb = new ProcessDB(directory) |
| 47 |
await t.rejects(pdb.writeIndex()) |
| 48 |
}) |
| 49 |
|
| 50 |
t.test('readProcessInfos', async t => { |
| 51 |
// put a borked json in there just for funsies |
| 52 |
const bad = path.resolve(directory, 'gleepglorp.json') |
| 53 |
fs.writeFileSync(bad, 'this is not json, it is bad') |
| 54 |
t.teardown(() => rimraf(bad)) |
| 55 |
|
| 56 |
const pdb = new ProcessDB(directory) |
| 57 |
t.matchSnapshot(await pdb.readProcessInfos()) |
| 58 |
}) |
| 59 |
|
| 60 |
t.test('buildProcessTree with invalid index', async t => { |
| 61 |
const pdb = new ProcessDB(directory) |
| 62 |
t.teardown(() => pdb.writeIndex()) |
| 63 |
|
| 64 |
const idx = await pdb.readIndex() |
| 65 |
idx.processes.fleepflorp = { what: 'that mean' } |
| 66 |
const json = JSON.stringify(idx) |
| 67 |
fs.writeFileSync(path.resolve(directory, 'index.json'), json) |
| 68 |
|
| 69 |
await t.rejects(pdb.buildProcessTree(), { |
| 70 |
message: 'Invalid entry in processinfo index: fleepflorp' |
| 71 |
}) |
| 72 |
}) |
| 73 |
|
| 74 |
t.test('render process tree', async t => { |
| 75 |
const nyc = new NYC({ |
| 76 |
tempDir: __dirname + '/fixtures/.nyc_output', |
| 77 |
cwd: path.resolve(__dirname + '/..'), |
| 78 |
}) |
| 79 |
|
| 80 |
const pdb = new ProcessDB(directory) |
| 81 |
|
| 82 |
t.matchSnapshot(await pdb.renderTree(nyc), 'render the tree') |
| 83 |
t.matchSnapshot(await pdb.getCoverageMap(), 'coverage map after render') |
| 84 |
t.matchSnapshot(pdb.label, 'label after render') |
| 85 |
}) |
| 86 |
|
| 87 |
t.test('spawn', async t => { |
| 88 |
const tempDir = __dirname + '/fixtures/.nyc_output' |
| 89 |
const esc = process.platform === 'win32' |
| 90 |
? path.resolve('escape.cmd') |
| 91 |
: path.resolve('escape.sh') |
| 92 |
t.teardown(() => rimraf(esc)) |
| 93 |
const node = process.execPath |
| 94 |
const escapePath = `${path.dirname(node)}:${process.env.PATH}` |
| 95 |
const nyc = require.resolve('nyc/bin/nyc.js') |
| 96 |
|
| 97 |
const script = process.platform === 'win32' |
| 98 |
? ` |
| 99 |
@SETLOCAL\r |
| 100 |
@SET PATH=${escapePath};%PATH%\r |
| 101 |
@SET NYC_CONFIG=\r |
| 102 |
@SET NYC_CWD=\r |
| 103 |
@SET NYC_ROOT_ID=\r |
| 104 |
@SET NYC_INSTRUMENTER=\r |
| 105 |
@SET NYC_CONFIG_OVERRIDE=\r |
| 106 |
@SET NYC_PROCESS_ID=\r |
| 107 |
${node} ${nyc} --show-process-tree --cache=false --clean=false --temp-dir=${tempDir} %*\r |
| 108 |
` : `#!/bin/bash |
| 109 |
export PATH=${escapePath} |
| 110 |
export NYC_CONFIG= |
| 111 |
export NYC_CWD= |
| 112 |
export NYC_ROOT_ID= |
| 113 |
export NYC_INSTRUMENTER= |
| 114 |
export NYC_CONFIG_OVERRIDE= |
| 115 |
export NYC_PROCESS_ID= |
| 116 |
${node} ${nyc} --show-process-tree --cache=false --clean=false --temp-dir=${tempDir} "$@" |
| 117 |
` |
| 118 |
fs.writeFileSync(esc, script) |
| 119 |
const ok = path.resolve('ok.js') |
| 120 |
t.teardown(() => rimraf(ok)) |
| 121 |
fs.writeFileSync(ok, ` |
| 122 |
if (process.argv[2] !== 'child') { |
| 123 |
console.log('TAP version 13') |
| 124 |
console.log('ok 1 - child {') |
| 125 |
require('child_process').spawn(process.execPath, [__filename, 'child'], { |
| 126 |
stdio: 'inherit' |
| 127 |
}).on('close', () => console.log('}\\n1..1')) |
| 128 |
} else { |
| 129 |
console.log(' 1..1') |
| 130 |
console.log(' ok') |
| 131 |
} |
| 132 |
`) |
| 133 |
|
| 134 |
fs.chmodSync(esc, 0o755) |
| 135 |
const pdb = new ProcessDB(directory) |
| 136 |
|
| 137 |
const c = await pdb.spawn('named test', esc, [node, ok], { |
| 138 |
stdio: ['ignore', 'ignore', 'inherit'] |
| 139 |
}) |
| 140 |
|
| 141 |
await new Promise(resolve => { |
| 142 |
c.on('close', (code, signal) => { |
| 143 |
t.equal(code, 0) |
| 144 |
t.equal(signal, null) |
| 145 |
resolve() |
| 146 |
}) |
| 147 |
}) |
| 148 |
|
| 149 |
// got the named test in there |
| 150 |
t.match(await pdb.writeIndex(), { externalIds: { 'named test': Object } }) |
| 151 |
|
| 152 |
await pdb.expunge('named test') |
| 153 |
t.notMatch(await pdb.writeIndex(), { externalIds: { 'named test': Object } }) |
| 154 |
}) |
| 155 |
|
| 156 |
t.test('spawn args parsing', t => { |
| 157 |
const _spawnArgs = Symbol.for('spawnArgs') |
| 158 |
const sa = ProcessDB.prototype[_spawnArgs] |
| 159 |
t.match(sa('name', 'file', {some:'options'}), ['name', 'file', [], {some:'options'}]) |
| 160 |
t.match(sa('name', 'file'), ['name', 'file', [], {}]) |
| 161 |
t.end() |
| 162 |
}) |