Skip to content

Commit

Permalink
fix: make nyc instrument work in subdirectories (#343)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
addaleax authored and bcoe committed Aug 9, 2016
1 parent b8d4109 commit a82cf49
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
var visitor = function (filename) {
var ext
var transform
var inFile = path.relative(_this.cwd, path.resolve(inputDir, filename))
var inFile = path.resolve(inputDir, filename)
var code = fs.readFileSync(inFile, 'utf-8')

for (ext in _this.transforms) {
Expand All @@ -200,7 +200,7 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
if (!output) {
console.log(code)
} else {
var outFile = path.relative(_this.cwd, path.resolve(output, filename))
var outFile = path.resolve(output, filename)
mkdirp.sync(path.dirname(outFile))
fs.writeFileSync(outFile, code, 'utf-8')
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cli/subdir/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output-dir
2 changes: 2 additions & 0 deletions test/fixtures/cli/subdir/input-dir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
console.log('Hello, World!')
19 changes: 19 additions & 0 deletions test/src/nyc-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require('tap').mochaGlobals()
// beforeEach
rimraf.sync(path.resolve(fakebin, 'node'))
rimraf.sync(path.resolve(fakebin, 'npm'))
rimraf.sync(path.resolve(fixturesCLI, 'subdir', 'output-dir'))

describe('the nyc cli', function () {
var env = { PATH: process.env.PATH }
Expand Down Expand Up @@ -339,6 +340,24 @@ describe('the nyc cli', function () {
done()
})
})

it('works in directories without a package.json', function (done) {
var args = [bin, 'instrument', './input-dir', './output-dir']

var subdir = path.resolve(fixturesCLI, 'subdir')
var proc = spawn(process.execPath, args, {
cwd: subdir,
env: env
})

proc.on('exit', function (code) {
code.should.equal(0)
var target = path.resolve(subdir, 'output-dir', 'index.js')
fs.readFileSync(target, 'utf8')
.should.match(/console.log\('Hello, World!'\)/)
done()
})
})
})

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

0 comments on commit a82cf49

Please sign in to comment.