Repository: istanbuljs/nyc
Pull request #620 (2017-06-30 13:14:00)

Add support for --cwd (#542)
I added support for --root option
I found out that the `cwd` option does exactly what the --root is supposed to do except:

 - the NYC_CWD environment has no documentation
 - the implicit lookup for upward package.json is not something I would expect

So I added a --root option that simply force cwd to be what I tell it to be.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/12205183/badge)](https://coveralls.io/builds/12205183)

Changes Unknown when pulling **28d8df723812b37373580849977be4bd04950134 on Speedy37:master** into ** on istanbuljs:master**.

bcoe: @Speedy37 rather than adding a new `root` option, I think I'd rather than we make `cwd` optionally configurable; basically, just renaming `root` to `cwd`.
Speedy37: NYC_CWD isn't the same as nyc working directory. 
I think NYC_CWD isn't the right name for this, because it change the root path where it looks for files. The generated coverage file path are still relative to nyc working directory.
Speedy37: I updated this PR to use cwd naming.

But I'm not convinced this is exactly what you want, because cwd doesn't only impact file lookup, it also impact cache location.
coveralls: 
[![Coverage Status](https://coveralls.io/builds/12490402/badge)](https://coveralls.io/builds/12490402)

Changes Unknown when pulling **3b5b8a87be2206077b9f42afbf1775a56ddbb328 on Speedy37:master** into ** on istanbuljs:master**.

bcoe: @Speedy37 maybe I'm just not fully understanding the problem you're looking to solve. Do you have a repo that benefits from manually setting the root directory, is the problem not addressed by `--cwd`?

Just want to make sure we're solving a specific issue you're running into before we land anything.

Patch:
diff --git a/README.md b/README.md
index 6c7025c..f6196f1 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,13 @@
 # nyc
 
-[![Join the chat at https://gitter.im/istanbuljs/nyc](https://badges.gitter.im/istanbuljs/nyc.svg)](https://gitter.im/istanbuljs/nyc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
 [![Build Status](https://travis-ci.org/istanbuljs/nyc.svg?branch=master)](https://travis-ci.org/istanbuljs/nyc)
 [![Coverage Status](https://coveralls.io/repos/bcoe/nyc/badge.svg?branch=)](https://coveralls.io/r/bcoe/nyc?branch=master)
 [![NPM version](https://img.shields.io/npm/v/nyc.svg)](https://www.npmjs.com/package/nyc)
 [![Windows Tests](https://img.shields.io/appveyor/ci/bcoe/nyc-ilw23/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/bcoe/nyc-ilw23)
 [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
+[![community slack](http://devtoolscommunity.herokuapp.com/badge.svg)](http://devtoolscommunity.herokuapp.com)
+
+_Having problems? want to contribute? join our [community slack](http://devtoolscommunity.herokuapp.com)_.
 
 Istanbul's state of the art command line interface, with support for:
 
diff --git a/index.js b/index.js
index 4a98974..f9b38c5 100755
--- a/index.js
+++ b/index.js
@@ -367,6 +367,13 @@ NYC.prototype.writeCoverageFile = function () {
   var coverage = coverageFinder()
   if (!coverage) return
 
+  // Remove any files that should be excluded but snuck into the coverage
+  Object.keys(coverage).forEach(function (absFile) {
+    if (!this.exclude.shouldInstrument(absFile)) {
+      delete coverage[absFile]
+    }
+  }, this)
+
   if (this.cache) {
     Object.keys(coverage).forEach(function (absFile) {
       if (this.hashCache[absFile] && coverage[absFile]) {
@@ -407,12 +414,16 @@ function coverageFinder () {
 }
 
 NYC.prototype._getCoverageMapFromAllCoverageFiles = function () {
+  var _this = this
   var map = libCoverage.createCoverageMap({})
 
   this.loadReports().forEach(function (report) {
     map.merge(report)
   })
   map.data = this.sourceMaps.remapCoverage(map.data)
+  map.filter(function (filename) {
+    return _this.exclude.shouldInstrument(filename)
+  })
   return map
 }
 
diff --git a/lib/config-util.js b/lib/config-util.js
index e355d87..42cad87 100644
--- a/lib/config-util.js
+++ b/lib/config-util.js
@@ -41,7 +41,6 @@ Config.loadConfig = function (argv, cwd) {
   config.extension = arrify(config.extension)
   config.exclude = arrify(config.exclude)
   config.include = arrify(config.include)
-  config.cwd = cwd
 
   return config
 }
@@ -136,6 +135,10 @@ Config.buildYargs = function (cwd) {
       describe: 'a list of specific files that should be covered, glob patterns are supported',
       global: false
     })
+    .option('cwd', {
+      describe: 'working directory to lookup for files',
+      global: false
+    })
     .option('require', {
       alias: 'i',
       default: [],


Test patch:
diff --git a/test/src/nyc-tap.js b/test/src/nyc-tap.js
index 7f123fe..bb57388 100644
--- a/test/src/nyc-tap.js
+++ b/test/src/nyc-tap.js
@@ -61,6 +61,11 @@ describe('nyc', function () {
       var nyc = new NYC(configUtil.loadConfig([], __dirname))
       nyc.cwd.should.eql(path.join(__dirname, '../..'))
     })
+
+    it('uses --cwd for cwd if it is set (highest priority and no look upwards for package.json) ', function () {
+      var nyc = new NYC(configUtil.loadConfig(['--cwd', __dirname], __dirname))
+      nyc.cwd.should.eql(__dirname)
+    })
   })
 
   describe('config', function () {