Skip to content

Commit

Permalink
SQUASH: Update process index based on review
Browse files Browse the repository at this point in the history
- Adds a NYC_PROCESS_ID to environment
- Adds `parent` to processInfo object, a uuid referring to parent.
- Rebase onto processinfo-numeric-pids branch
- Avoid re-writing the processinfo/{uuid}.json files

Next:

- Update process tree output to rely on process index instead of
  duplicating effort.
  • Loading branch information
isaacs committed Apr 6, 2019
1 parent b67e46b commit e4bfd9f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 34 deletions.
1 change: 1 addition & 0 deletions bin/wrap.js
Expand Up @@ -8,6 +8,7 @@ config.isChildProcess = true
config._processInfo = {
pid: process.pid,
ppid: process.ppid,
parent: process.env.NYC_PROCESS_ID,
root: process.env.NYC_ROOT_ID
}
if (process.env.NYC_PROCESSINFO_EXTERNAL_ID) {
Expand Down
34 changes: 5 additions & 29 deletions index.js
Expand Up @@ -312,6 +312,7 @@ NYC.prototype._wrapExit = function () {
}

NYC.prototype.wrap = function (bin) {
process.env.NYC_PROCESS_ID = this.processInfo.uuid
this._addRequireHooks()
this._wrapExit()
this._loadAdditionalModules()
Expand Down Expand Up @@ -341,7 +342,7 @@ NYC.prototype.writeCoverageFile = function () {
coverage = this.sourceMaps.remapCoverage(coverage)
}

var id = this.generateUniqueID()
var id = this.processInfo.uuid
var coverageFilename = path.resolve(this.tempDirectory(), id + '.json')

fs.writeFileSync(
Expand All @@ -355,6 +356,7 @@ NYC.prototype.writeCoverageFile = function () {
}

this.processInfo.coverageFilename = coverageFilename
this.processInfo.files = Object.keys(coverage)

fs.writeFileSync(
path.resolve(this.processInfoDirectory(), id + '.json'),
Expand Down Expand Up @@ -421,8 +423,7 @@ NYC.prototype.writeProcessIndex = function () {
const infos = fs.readdirSync(dir).filter(f => f !== 'index.json').map(f => {
try {
const info = JSON.parse(fs.readFileSync(path.resolve(dir, f), 'utf-8'))
// on thiis first read pass, also map the pids to uuids
info.uuid = path.basename(f, '.json')
info.children = []
pidToUid.set(info.uuid, info.pid)
pidToUid.set(info.pid, info.uuid)
infoByUid.set(info.uuid, info)
Expand All @@ -436,34 +437,17 @@ NYC.prototype.writeProcessIndex = function () {
}).filter(Boolean)

// create all the parent-child links and write back the updated info
const needsUpdate = new Set()
infos.forEach(info => {
if (info.ppid && info.ppid !== '0' && !info.parent) {
info.parent = pidToUid.get(info.ppid)
needsUpdate.add(info)
}
if (info.parent) {
const parentInfo = infoByUid.get(info.parent)
if (parentInfo.children.indexOf(info.uuid) === -1) {
parentInfo.children.push(info.uuid)
needsUpdate.add(parentInfo)
}
}
})

// figure out which files were touched by each process.
const files = infos.reduce((files, info) => {
if (!info.files) {
try {
info.files = Object.keys(JSON.parse(fs.readFileSync(
path.resolve(this.tempDirectory(), info.coverageFilename),
'utf-8'
)))
} catch (er) {
return files
}
needsUpdate.add(info)
}
info.files.forEach(f => {
files[f] = files[f] || []
files[f].push(info.uuid)
Expand All @@ -484,9 +468,7 @@ NYC.prototype.writeProcessIndex = function () {
children: info.children
}
}
if (info.children && info.children.length) {
index.processes[info.uuid].children = Array.from(info.children)
}
index.processes[info.uuid].children = Array.from(info.children || [])
return index
}, { processes: {}, files: files, externalIds: {} })

Expand All @@ -502,12 +484,6 @@ NYC.prototype.writeProcessIndex = function () {
}
})

needsUpdate.forEach(info => {
fs.writeFileSync(
path.resolve(dir, info.uuid + '.json'), JSON.stringify(info)
)
})

fs.writeFileSync(path.resolve(dir, 'index.json'), JSON.stringify(index))
}

Expand Down
14 changes: 9 additions & 5 deletions lib/process.js
@@ -1,9 +1,12 @@
const archy = require('archy')
const libCoverage = require('istanbul-lib-coverage')
const uuid = require('uuid/v4')

function ProcessInfo (defaults) {
defaults = defaults || {}

this.uuid = null
this.parent = null
this.pid = String(process.pid)
this.argv = process.argv
this.execArgv = process.execArgv
Expand All @@ -12,14 +15,14 @@ function ProcessInfo (defaults) {
this.ppid = null
this.root = null
this.coverageFilename = null
this.nodes = [] // list of children, filled by buildProcessTree()
this.children = [] // just uuids, not full nodes

this._coverageMap = null

for (var key in defaults) {
this[key] = defaults[key]
}

if (!this.uuid) {
this.uuid = uuid()
}
}

Object.defineProperty(ProcessInfo.prototype, 'label', {
Expand All @@ -37,14 +40,15 @@ Object.defineProperty(ProcessInfo.prototype, 'label', {
})

ProcessInfo.buildProcessTree = function (infos) {
var treeRoot = new ProcessInfo({ _label: 'nyc' })
var treeRoot = new ProcessInfo({ _label: 'nyc', nodes: [] })
var nodes = { }

infos = infos.sort(function (a, b) {
return a.time - b.time
})

infos.forEach(function (p) {
p.nodes = []
nodes[p.root + ':' + p.pid] = p
})

Expand Down

0 comments on commit e4bfd9f

Please sign in to comment.