Skip to content

Commit a82cf49

Browse files
addaleaxbcoe
authored andcommitted
fix: make nyc instrument work in subdirectories (#343)
There is no real reason to use a relative path when performing the I/O for instrumenting. Absolute paths should work just as well and will be correct, even when `nyc.cwd` is off a bit because it is modified based on what `pkgUp` says.
1 parent b8d4109 commit a82cf49

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
183183
var visitor = function (filename) {
184184
var ext
185185
var transform
186-
var inFile = path.relative(_this.cwd, path.resolve(inputDir, filename))
186+
var inFile = path.resolve(inputDir, filename)
187187
var code = fs.readFileSync(inFile, 'utf-8')
188188

189189
for (ext in _this.transforms) {
@@ -200,7 +200,7 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
200200
if (!output) {
201201
console.log(code)
202202
} else {
203-
var outFile = path.relative(_this.cwd, path.resolve(output, filename))
203+
var outFile = path.resolve(output, filename)
204204
mkdirp.sync(path.dirname(outFile))
205205
fs.writeFileSync(outFile, code, 'utf-8')
206206
}

test/fixtures/cli/subdir/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
output-dir
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
console.log('Hello, World!')

test/src/nyc-bin.js

+19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require('tap').mochaGlobals()
1616
// beforeEach
1717
rimraf.sync(path.resolve(fakebin, 'node'))
1818
rimraf.sync(path.resolve(fakebin, 'npm'))
19+
rimraf.sync(path.resolve(fixturesCLI, 'subdir', 'output-dir'))
1920

2021
describe('the nyc cli', function () {
2122
var env = { PATH: process.env.PATH }
@@ -339,6 +340,24 @@ describe('the nyc cli', function () {
339340
done()
340341
})
341342
})
343+
344+
it('works in directories without a package.json', function (done) {
345+
var args = [bin, 'instrument', './input-dir', './output-dir']
346+
347+
var subdir = path.resolve(fixturesCLI, 'subdir')
348+
var proc = spawn(process.execPath, args, {
349+
cwd: subdir,
350+
env: env
351+
})
352+
353+
proc.on('exit', function (code) {
354+
code.should.equal(0)
355+
var target = path.resolve(subdir, 'output-dir', 'index.js')
356+
fs.readFileSync(target, 'utf8')
357+
.should.match(/console.log\('Hello, World!'\)/)
358+
done()
359+
})
360+
})
342361
})
343362

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

0 commit comments

Comments
 (0)