Skip to content

Commit

Permalink
feat: playing around with initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Oct 26, 2017
0 parents commit 18f5471
Show file tree
Hide file tree
Showing 10 changed files with 695 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
14 changes: 14 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2017, Contributors

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice
appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# c8 - native code-coverage

Code-coverage using [v8's Profiler](https://nodejs.org/dist/latest-v8.x/docs/api/inspector.html)
compatible with [Istanbul's reporters](https://istanbul.js.org/docs/advanced/alternative-reporters/).

Like [nyc](https://github.com/istanbuljs/nyc), c8 just magically works, simply:

```bash
npm i c8 -g
c8 node foo.js
```

The above example will collect coverage for `foo.js` using v8's profiler.

TODO:

- [ ] write logic for converting v8 coverage output to [Istanbul Coverage.json format](https://github.com/gotwarlost/istanbul/blob/master/coverage.json.md).
- [ ] talk to Node.js project about silencing messages:

> `Debugger listening on ws://127.0.0.1:56399/e850110a-c5df-41d8-8ef2-400f6829617f`.
- [ ] figure out why `detailed` mode does not appear to be working.
- [ ] figure out a better way to determine that all processes in event loop
have terminated (except the inspector session).
10 changes: 10 additions & 0 deletions bin/c8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node

const argv = require('yargs').parse()
const foreground = require('foreground-child')
const sw = require('spawn-wrap')

if (argv._.length) {
sw([require.resolve('./wrap')])
foreground(process.argv.slice(2))
}
52 changes: 52 additions & 0 deletions bin/wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const fs = require('fs')
const sw = require('spawn-wrap')
const CRI = require('chrome-remote-interface');
const getPort = require('get-port');
const inspector = require('inspector')

// if there are N or less active handles
// in the event loop, dump coverage and exit.
const EXIT_HANDLE_COUNT = 4

getPort().then(async port => {
// start an inspector session on an unused port.
inspector.open(port, true)
const client = await CRI({port: port})
const {Profiler} = client
await Profiler.enable()
await Profiler.startPreciseCoverage({callCount: true, detailed: true})

// run the original "main" now that we've started the inspector.
sw.runMain()

// wait for everything in event loop to terminate
// except for inspector session.
setInterval(() => {
const handleCount = process._getActiveHandles().length
+ process._getActiveRequests().length
if (handleCount <= EXIT_HANDLE_COUNT) {
outputCoverageAndExit(client, Profiler)
}
}, 100)
}).catch(err => {
throw err
})

async function outputCoverageAndExit (client, Profiler) {
const IGNORED_PATHS = [
/\/bin\/wrap.js/,
/\/node_modules\//,
/node-spawn-wrap/
]
let {result} = await Profiler.takePreciseCoverage()
result = result.filter(coverage => {
for (var ignored, i = 0; (ignored = IGNORED_PATHS[i]) !== undefined; i++) {
if (ignored.test(coverage.url)) return false
}
if (!/^\//.test(coverage.url)) return false
else return true
})
console.log(JSON.stringify(result, null, 2))
client.close()
process.exit(0)
}
Empty file added index.js
Empty file.
Loading

0 comments on commit 18f5471

Please sign in to comment.