Skip to content

Commit 5c1eb38

Browse files
AndrewFinlayJaKXz
authored andcommitted
test(instrument): should return unmodified source if no transform found (#1036)
* Test that the instrument command returns unmodified source if there is no transform found for a file extension. Currently this behaviour can only be reached when trying to instrument a single file. In the case of instrumenting a directory, files with an extension with no matching transform are filtered out before they can be instrumented. * Cleanup instrumentation code again with a focus on paths The main aim of this has been to clarify whether we're working with relative or absolute file paths, and removing unnecessary transformations. Although I've made a few other 'small' changes here and there. Key changes: * Created a new private method `NYC._transform`, common to `_maybeInstrumentSource` and `instrumentAllFiles`. * Renamed the param in `walkAllFiles` forEach handler to `relFile` to explicitly state the file representation being used. * Let the `addAllFiles` visitor function rely on `testExclude` to determine which files to instrument
1 parent 1f6c3d4 commit 5c1eb38

File tree

3 files changed

+57
-41
lines changed

3 files changed

+57
-41
lines changed

index.js

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ NYC.prototype._createInstrumenter = function () {
134134
}
135135

136136
NYC.prototype.addFile = function (filename) {
137-
const relFile = path.relative(this.cwd, filename)
138-
const source = this._readTranspiledSource(path.resolve(this.cwd, filename))
139-
this._maybeInstrumentSource(source, filename, relFile)
137+
const source = this._readTranspiledSource(filename)
138+
this._maybeInstrumentSource(source, filename)
140139
}
141140

142141
NYC.prototype._readTranspiledSource = function (filePath) {
@@ -154,21 +153,16 @@ NYC.prototype._readTranspiledSource = function (filePath) {
154153
}
155154

156155
NYC.prototype.addAllFiles = function () {
157-
var _this = this
158-
159156
this._loadAdditionalModules()
160157

161158
this.fakeRequire = true
162-
this.walkAllFiles(this.cwd, function (filename) {
163-
filename = path.resolve(_this.cwd, filename)
164-
if (_this.exclude.shouldInstrument(filename)) {
165-
_this.addFile(filename)
166-
var coverage = coverageFinder()
167-
var lastCoverage = _this.instrumenter().lastFileCoverage()
168-
if (lastCoverage) {
169-
filename = lastCoverage.path
170-
coverage[filename] = lastCoverage
171-
}
159+
this.walkAllFiles(this.cwd, relFile => {
160+
const filename = path.resolve(this.cwd, relFile)
161+
this.addFile(filename)
162+
const coverage = coverageFinder()
163+
const lastCoverage = this.instrumenter().lastFileCoverage()
164+
if (lastCoverage) {
165+
coverage[lastCoverage.path] = lastCoverage
172166
}
173167
})
174168
this.fakeRequire = false
@@ -178,16 +172,13 @@ NYC.prototype.addAllFiles = function () {
178172

179173
NYC.prototype.instrumentAllFiles = function (input, output, cb) {
180174
let inputDir = '.' + path.sep
181-
const visitor = filename => {
182-
const inFile = path.resolve(inputDir, filename)
175+
const visitor = relFile => {
176+
const inFile = path.resolve(inputDir, relFile)
183177
const inCode = fs.readFileSync(inFile, 'utf-8')
184-
185-
const extname = path.extname(filename).toLowerCase()
186-
const transform = this.transforms[extname] || (code => code)
187-
const outCode = transform(inCode, { filename: inFile })
178+
const outCode = this._transform(inCode, inFile) || inCode
188179

189180
if (output) {
190-
const outFile = path.resolve(output, filename)
181+
const outFile = path.resolve(output, relFile)
191182
mkdirp.sync(path.dirname(outFile))
192183
fs.writeFileSync(outFile, outCode, 'utf-8')
193184
} else {
@@ -212,26 +203,24 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
212203
}
213204

214205
NYC.prototype.walkAllFiles = function (dir, visitor) {
215-
this.exclude.globSync(dir).forEach(filename => {
216-
visitor(filename)
206+
this.exclude.globSync(dir).forEach(relFile => {
207+
visitor(relFile)
217208
})
218209
}
219210

220-
NYC.prototype._maybeInstrumentSource = function (code, filename, relFile) {
221-
var instrument = this.exclude.shouldInstrument(filename, relFile)
222-
if (!instrument) {
223-
return null
224-
}
211+
NYC.prototype._transform = function (code, filename) {
212+
const extname = path.extname(filename).toLowerCase()
213+
const transform = this.transforms[extname] || (() => null)
225214

226-
var ext, transform
227-
for (ext in this.transforms) {
228-
if (filename.toLowerCase().substr(-ext.length) === ext) {
229-
transform = this.transforms[ext]
230-
break
231-
}
215+
return transform(code, { filename })
216+
}
217+
218+
NYC.prototype._maybeInstrumentSource = function (code, filename) {
219+
if (!this.exclude.shouldInstrument(filename)) {
220+
return null
232221
}
233222

234-
return transform ? transform(code, { filename: filename, relFile: relFile }) : null
223+
return this._transform(code, filename)
235224
}
236225

237226
NYC.prototype._transformFactory = function (cacheDir) {
@@ -265,11 +254,9 @@ NYC.prototype._transformFactory = function (cacheDir) {
265254
}
266255

267256
NYC.prototype._handleJs = function (code, options) {
268-
var filename = options.filename
269-
var relFile = path.relative(this.cwd, filename)
270257
// ensure the path has correct casing (see istanbuljs/nyc#269 and nodejs/node#6624)
271-
filename = path.resolve(this.cwd, relFile)
272-
return this._maybeInstrumentSource(code, filename, relFile) || code
258+
const filename = path.resolve(this.cwd, options.filename)
259+
return this._maybeInstrumentSource(code, filename) || code
273260
}
274261

275262
NYC.prototype._addHook = function (type) {
@@ -382,7 +369,7 @@ function coverageFinder () {
382369
}
383370

384371
NYC.prototype.getCoverageMapFromAllCoverageFiles = function (baseDirectory) {
385-
var map = libCoverage.createCoverageMap({})
372+
const map = libCoverage.createCoverageMap({})
386373

387374
this.eachReport(undefined, (report) => {
388375
map.merge(report)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var a = 0
2+
3+
a++
4+
5+
if (a === 0) {
6+
a++;
7+
a--;
8+
a++;
9+
}

test/nyc-integration.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,26 @@ describe('the nyc cli', function () {
581581
done()
582582
})
583583
})
584+
585+
it('returns unmodified source if there is no transform', function (done) {
586+
const args = [bin, 'instrument', './no-transform/half-covered.xjs']
587+
588+
const proc = spawn(process.execPath, args, {
589+
cwd: fixturesCLI,
590+
env: env
591+
})
592+
593+
let stdout = ''
594+
proc.stdout.on('data', function (chunk) {
595+
stdout += chunk
596+
})
597+
598+
proc.on('close', function (code) {
599+
code.should.equal(0)
600+
stdout.should.contain(`var a = 0`)
601+
done()
602+
})
603+
})
584604
})
585605

586606
describe('output folder specified', function () {

0 commit comments

Comments
 (0)