Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: code tidy up and new test #1036

Merged
merged 3 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 28 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ NYC.prototype._createInstrumenter = function () {
}

NYC.prototype.addFile = function (filename) {
const relFile = path.relative(this.cwd, filename)
const source = this._readTranspiledSource(path.resolve(this.cwd, filename))
this._maybeInstrumentSource(source, filename, relFile)
const source = this._readTranspiledSource(filename)
this._maybeInstrumentSource(source, filename)
}

NYC.prototype._readTranspiledSource = function (filePath) {
Expand All @@ -154,21 +153,16 @@ NYC.prototype._readTranspiledSource = function (filePath) {
}

NYC.prototype.addAllFiles = function () {
var _this = this

this._loadAdditionalModules()

this.fakeRequire = true
this.walkAllFiles(this.cwd, function (filename) {
filename = path.resolve(_this.cwd, filename)
if (_this.exclude.shouldInstrument(filename)) {
_this.addFile(filename)
var coverage = coverageFinder()
var lastCoverage = _this.instrumenter().lastFileCoverage()
if (lastCoverage) {
filename = lastCoverage.path
coverage[filename] = lastCoverage
}
this.walkAllFiles(this.cwd, relFile => {
const filename = path.resolve(this.cwd, relFile)
this.addFile(filename)
const coverage = coverageFinder()
const lastCoverage = this.instrumenter().lastFileCoverage()
if (lastCoverage) {
coverage[lastCoverage.path] = lastCoverage
}
})
this.fakeRequire = false
Expand All @@ -178,16 +172,13 @@ NYC.prototype.addAllFiles = function () {

NYC.prototype.instrumentAllFiles = function (input, output, cb) {
let inputDir = '.' + path.sep
const visitor = filename => {
const inFile = path.resolve(inputDir, filename)
const visitor = relFile => {
const inFile = path.resolve(inputDir, relFile)
const inCode = fs.readFileSync(inFile, 'utf-8')

const extname = path.extname(filename).toLowerCase()
const transform = this.transforms[extname] || (code => code)
const outCode = transform(inCode, { filename: inFile })
const outCode = this._transform(inCode, inFile) || inCode

if (output) {
const outFile = path.resolve(output, filename)
const outFile = path.resolve(output, relFile)
mkdirp.sync(path.dirname(outFile))
fs.writeFileSync(outFile, outCode, 'utf-8')
} else {
Expand All @@ -212,26 +203,24 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
}

NYC.prototype.walkAllFiles = function (dir, visitor) {
this.exclude.globSync(dir).forEach(filename => {
visitor(filename)
this.exclude.globSync(dir).forEach(relFile => {
visitor(relFile)
})
}

NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) {
var instrument = this.exclude.shouldInstrument(filename, relFile)
if (!instrument) {
return null
}
NYC.prototype._transform = function (code, filename) {
const extname = path.extname(filename).toLowerCase()
const transform = this.transforms[extname] || (() => null)

var ext, transform
for (ext in this.transforms) {
if (filename.toLowerCase().substr(-ext.length) === ext) {
transform = this.transforms[ext]
break
}
return transform(code, { filename })
}

NYC.prototype._maybeInstrumentSource = function (code, filename) {
if (!this.exclude.shouldInstrument(filename)) {
return null
}

return transform ? transform(code, { filename: filename, relFile: relFile }) : null
return this._transform(code, filename)
}

NYC.prototype._transformFactory = function (cacheDir) {
Expand Down Expand Up @@ -265,11 +254,9 @@ NYC.prototype._transformFactory = function (cacheDir) {
}

NYC.prototype._handleJs = function (code, options) {
var filename = options.filename
var relFile = path.relative(this.cwd, filename)
// ensure the path has correct casing (see istanbuljs/nyc#269 and nodejs/node#6624)
filename = path.resolve(this.cwd, relFile)
return this._maybeInstrumentSource(code, filename, relFile) || code
const filename = path.resolve(this.cwd, options.filename)
return this._maybeInstrumentSource(code, filename) || code
}

NYC.prototype._addHook = function (type) {
Expand Down Expand Up @@ -382,7 +369,7 @@ function coverageFinder () {
}

NYC.prototype.getCoverageMapFromAllCoverageFiles = function (baseDirectory) {
var map = libCoverage.createCoverageMap({})
const map = libCoverage.createCoverageMap({})

this.eachReport(undefined, (report) => {
map.merge(report)
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/cli/no-transform/half-covered.xjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var a = 0

a++

if (a === 0) {
a++;
a--;
a++;
}
20 changes: 20 additions & 0 deletions test/nyc-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,26 @@ describe('the nyc cli', function () {
done()
})
})

it('returns unmodified source if there is no transform', function (done) {
const args = [bin, 'instrument', './no-transform/half-covered.xjs']

const proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

let stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
stdout.should.contain(`var a = 0`)
done()
})
})
})

describe('output folder specified', function () {
Expand Down