From a82cf49186ecf2d3d6546cf4ef7ae949a1fde29a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 9 Aug 2016 10:45:56 -0700 Subject: [PATCH] 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. --- index.js | 4 ++-- test/fixtures/cli/subdir/.gitignore | 1 + test/fixtures/cli/subdir/input-dir/index.js | 2 ++ test/src/nyc-bin.js | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/cli/subdir/.gitignore create mode 100644 test/fixtures/cli/subdir/input-dir/index.js diff --git a/index.js b/index.js index 774db7614..4d10f8102 100755 --- a/index.js +++ b/index.js @@ -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) { @@ -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') } diff --git a/test/fixtures/cli/subdir/.gitignore b/test/fixtures/cli/subdir/.gitignore new file mode 100644 index 000000000..21ba3bc07 --- /dev/null +++ b/test/fixtures/cli/subdir/.gitignore @@ -0,0 +1 @@ +output-dir diff --git a/test/fixtures/cli/subdir/input-dir/index.js b/test/fixtures/cli/subdir/input-dir/index.js new file mode 100644 index 000000000..239a04e74 --- /dev/null +++ b/test/fixtures/cli/subdir/input-dir/index.js @@ -0,0 +1,2 @@ +'use strict'; +console.log('Hello, World!') diff --git a/test/src/nyc-bin.js b/test/src/nyc-bin.js index dcf4fa973..4fbcdd68e 100644 --- a/test/src/nyc-bin.js +++ b/test/src/nyc-bin.js @@ -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 } @@ -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 () {