Skip to content

Commit

Permalink
chore: Full self coverage (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell committed Mar 4, 2019
1 parent b64d921 commit 0d7fcb7
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -3,6 +3,6 @@ coverage
node_modules
test/build/
.self_coverage
*.covered.js
self-coverage/
*.swp
needs-transpile.js
7 changes: 1 addition & 6 deletions bin/nyc.js
Expand Up @@ -2,12 +2,7 @@

const configUtil = require('../lib/config-util')
const foreground = require('foreground-child')
var NYC
try {
NYC = require('../index.covered.js')
} catch (e) {
NYC = require('../index.js')
}
const NYC = require('../index.js')
const processArgs = require('../lib/process-args')

const sw = require('spawn-wrap')
Expand Down
7 changes: 1 addition & 6 deletions bin/wrap.js
@@ -1,10 +1,5 @@
var sw = require('spawn-wrap')
var NYC
try {
NYC = require('../index.covered.js')
} catch (e) {
NYC = require('../index.js')
}
var NYC = require('../index.js')

var parentPid = process.env.NYC_PARENT_PID || '0'
process.env.NYC_PARENT_PID = process.pid
Expand Down
58 changes: 40 additions & 18 deletions build-self-coverage.js
@@ -1,21 +1,43 @@
var istanbul = require('istanbul-lib-instrument')
var fs = require('fs')
var path = require('path')

;[
'index.js',
'lib/process.js'
].forEach(function (name) {
var indexPath = path.join(__dirname, name)
var source = fs.readFileSync(indexPath, 'utf8')

var instrumentor = istanbul.createInstrumenter({
coverageVariable: '___NYC_SELF_COVERAGE___',
esModules: true
})
const path = require('path')
const fs = require('fs')
const istanbul = require('istanbul-lib-instrument')
const makeDir = require('make-dir')
const glob = require('glob')

const instrumenter = istanbul.createInstrumenter({
coverageVariable: '___NYC_SELF_COVERAGE___',
esModules: true
})

function instrumentFile (name) {
const indexPath = path.join(__dirname, name)
const outputPath = path.join(__dirname, 'self-coverage', name)

var instrumentedSource = instrumentor.instrumentSync(source, indexPath)
const source = fs.readFileSync(indexPath, 'utf8')
const instrumentedSource = name === 'package.json' ? source : instrumenter.instrumentSync(source, indexPath)

var outputPath = path.join(__dirname, name.replace(/\.js$/, '.covered.js'))
makeDir.sync(path.dirname(outputPath))
fs.writeFileSync(outputPath, instrumentedSource)
})
}

function instrumentGlob (pattern) {
const result = glob.sync(pattern, {
cwd: __dirname,
nodir: true
})

result.forEach(file => {
instrumentFile(file)
})
}

function instrumentAll () {
/* package.json is just being copied so the instrumented copy of lib/hash.js can find it. */
const globPatterns = ['package.json', 'index.js', 'bin/*.js', 'lib/**/*.js']

globPatterns.forEach(pattern => {
instrumentGlob(pattern)
})
}

instrumentAll()
12 changes: 3 additions & 9 deletions index.js
Expand Up @@ -24,17 +24,11 @@ const api = require('istanbul-api')

const debugLog = util.debuglog('nyc')

var ProcessInfo
try {
ProcessInfo = require('./lib/process.covered.js')
} catch (e) {
/* istanbul ignore next */
ProcessInfo = require('./lib/process.js')
}
const ProcessInfo = require('./lib/process.js')

/* istanbul ignore next */
if (/index\.covered\.js$/.test(__filename)) {
require('./lib/self-coverage-helper')
if (/self-coverage/.test(__dirname)) {
require('../self-coverage-helper')
}

function NYC (config) {
Expand Down
7 changes: 1 addition & 6 deletions lib/commands/check-coverage.js
@@ -1,9 +1,4 @@
var NYC
try {
NYC = require('../../index.covered.js')
} catch (e) {
NYC = require('../../index.js')
}
const NYC = require('../../index.js')

exports.command = 'check-coverage'

Expand Down
7 changes: 1 addition & 6 deletions lib/commands/instrument.js
@@ -1,9 +1,4 @@
var NYC
try {
NYC = require('../../index.covered.js')
} catch (e) {
NYC = require('../../index.js')
}
const NYC = require('../../index.js')

exports.command = 'instrument <input> [output]'

Expand Down
7 changes: 1 addition & 6 deletions lib/commands/merge.js
Expand Up @@ -3,12 +3,7 @@ const fs = require('fs')
const path = require('path')
const makeDir = require('make-dir')

var NYC
try {
NYC = require('../../index.covered.js')
} catch (e) {
NYC = require('../../index.js')
}
const NYC = require('../../index.js')

exports.command = 'merge <input-directory> [output-file]'

Expand Down
7 changes: 1 addition & 6 deletions lib/commands/report.js
@@ -1,9 +1,4 @@
var NYC
try {
NYC = require('../../index.covered.js')
} catch (e) {
NYC = require('../../index.js')
}
const NYC = require('../../index.js')

exports.command = 'report'

Expand Down

0 comments on commit 0d7fcb7

Please sign in to comment.