diff options
author | isaacs <i@izs.me> | 2016-07-28 22:00:05 +0300 |
---|---|---|
committer | Kat Marchán <kzm@sykosomatic.org> | 2016-09-09 00:53:10 +0300 |
commit | 116b6c60a174ea0cc49e4d62717e4e26175b6534 (patch) | |
tree | 398ad9bd44f912dd921be8d6ec39b7122c774473 | |
parent | ff879382fda21dac7216a5f666287b3a7e74a947 (diff) |
lifecycle: split node_module path parts properly
In lifecycle scripts, any `node_modules/.bin` existing in the heirarchy
should be turned into an entry in the PATH environment variable.
However, prior to this commit, it was splitting based on the string
`node_modules`, rather than restricting it to only path portions like
`/node_modules/` or `\node_modules\`. So, a path containing an entry
like `my_node_modules` would be improperly split.
Fixes: #13456
PR-URL: https://github.com/npm/npm/pull/13518
Credit: @isaacs
Reviewed-By: @zkat
-rw-r--r-- | lib/utils/lifecycle.js | 2 | ||||
-rw-r--r-- | test/tap/node-modules-path-munge.js | 39 |
2 files changed, 40 insertions, 1 deletions
diff --git a/lib/utils/lifecycle.js b/lib/utils/lifecycle.js index 3d9a34461..adf534d88 100644 --- a/lib/utils/lifecycle.js +++ b/lib/utils/lifecycle.js @@ -89,7 +89,7 @@ function _incorrectWorkingDirectory (wd, pkg) { function lifecycle_ (pkg, stage, wd, env, unsafe, failOk, cb) { var pathArr = [] - var p = wd.split('node_modules') + var p = wd.split(/[\\\/]node_modules[\\\/]/) var acc = path.resolve(p.shift()) p.forEach(function (pp) { diff --git a/test/tap/node-modules-path-munge.js b/test/tap/node-modules-path-munge.js new file mode 100644 index 000000000..fdca0393c --- /dev/null +++ b/test/tap/node-modules-path-munge.js @@ -0,0 +1,39 @@ +var common = require('../common-tap.js') +var t = require('tap') +var fs = require('fs') +var rimraf = require('rimraf') +var mkdirp = require('mkdirp') +var path = require('path') +var dir = path.join(__dirname, 'my_node_modules') +var script = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH' + +t.test('setup', function (t) { + rimraf.sync(dir) + mkdirp.sync(dir) + fs.writeFileSync(dir + '/package.json', JSON.stringify({ + name: 'my_node_modules', + version: '1.2.3', + scripts: { + test: script + } + })) + t.end() +}) + +t.test('verify PATH is munged right', function (t) { + common.npm(['test'], { cwd: dir }, function (err, code, stdout, stderr) { + if (err) { + throw err + } + t.equal(code, 0, 'exit ok') + t.notOk(stderr, 'should have no stderr') + var expect = path.resolve(dir, 'node_modules', '.bin').toLowerCase() + t.contains(stdout.toLowerCase(), expect) + t.end() + }) +}) + +t.test('cleanup', function (t) { + rimraf.sync(dir) + t.end() +}) |