diff options
author | Lucas Holmquist <lholmqui@redhat.com> | 2016-09-30 21:22:44 +0300 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2016-10-07 01:09:33 +0300 |
commit | bf3bd1e4347ee2c5de08d23558c4444749178c8b (patch) | |
tree | 79a32ebca41d9626a824369e0fd473e002d77125 | |
parent | c3b6cdfedcdb4d9e7712be5245d9b274828d88d1 (diff) |
version: Update npm-shrinkwrap even when cwd != project root
There was a bug where the `npm-shrinkwrap.json` wouldn't be updated
if you ran `npm version` from a subdirectory of your project.
Fixes: #14009
Credit: @lholmquist
Reviewed-By: @iarna
PR-URL: https://github.com/npm/npm/pull/14143
-rw-r--r-- | lib/version.js | 2 | ||||
-rw-r--r-- | test/tap/version-sub-directory-shrinkwrap.js | 80 |
2 files changed, 81 insertions, 1 deletions
diff --git a/lib/version.js b/lib/version.js index e69560fa9..f3caf4332 100644 --- a/lib/version.js +++ b/lib/version.js @@ -263,7 +263,7 @@ function _commit (version, localData, cb) { chain( [ git.chainableExec([ 'add', packagePath ], options), - localData.hasShrinkwrap && git.chainableExec([ 'add', 'npm-shrinkwrap.json' ], options), + localData.hasShrinkwrap && git.chainableExec([ 'add', path.join(npm.localPrefix, 'npm-shrinkwrap.json') ], options), git.chainableExec([ 'commit', '-m', message ], options), !localData.existingTag && git.chainableExec([ 'tag', diff --git a/test/tap/version-sub-directory-shrinkwrap.js b/test/tap/version-sub-directory-shrinkwrap.js new file mode 100644 index 000000000..0455b62ab --- /dev/null +++ b/test/tap/version-sub-directory-shrinkwrap.js @@ -0,0 +1,80 @@ +var common = require('../common-tap.js') +var fs = require('fs') +var path = require('path') + +var mkdirp = require('mkdirp') +var osenv = require('osenv') +var rimraf = require('rimraf') +var test = require('tap').test + +var npm = require('../../lib/npm.js') + +var pkg = path.resolve(__dirname, 'version-sub-directory') +var subDirectory = path.resolve(pkg, 'sub-directory') +var packagePath = path.resolve(pkg, 'package.json') +var shrinkwrapPath = path.resolve(pkg, 'npm-shrinkwrap.json') +var cache = path.resolve(pkg, 'cache') + +var json = { name: 'cat', version: '0.1.2' } + +test('npm version <semver> from a subdirectory', function (t) { + setup() + npmLoad() + + function npmLoad () { + npm.load({ cache: cache }, function () { + common.makeGitRepo({ + path: pkg, + added: ['package.json', 'npm-shrinkwrap.json'] + }, version) + }) + } + + function version (er, stdout, stderr) { + t.ifError(er, 'git repo initialized without issue') + t.notOk(stderr, 'no error output') + npm.config.set('sign-git-tag', false) + npm.commands.version(['patch'], checkVersion) + } + + function checkVersion (er) { + var newShrinkwrap = JSON.parse(fs.readFileSync(shrinkwrapPath)) + t.is(newShrinkwrap.version, '0.1.3', 'shrinkwrap has right version') + var newPackage = JSON.parse(fs.readFileSync(packagePath)) + t.is(newPackage.version, '0.1.3', 'package.json has right version') + var git = require('../../lib/utils/git.js') + t.ifError(er, 'version command ran without error') + git.whichAndExec( + ['log'], + { cwd: pkg, env: process.env }, + checkCommit + ) + } + + function checkCommit (er, log, stderr) { + t.ifError(er, 'git log ran without issue') + t.notOk(stderr, 'no error output') + t.ok(log.match(/0\.1\.3/g), 'commited from subdirectory') + t.end() + } +}) + +test('cleanup', function (t) { + cleanup() + t.end() +}) + +function cleanup () { + // windows fix for locked files + process.chdir(osenv.tmpdir()) + rimraf.sync(pkg) +} + +function setup () { + cleanup() + mkdirp.sync(cache) + mkdirp.sync(subDirectory) + process.chdir(subDirectory) + fs.writeFileSync(packagePath, JSON.stringify(json), 'utf8') + fs.writeFileSync(shrinkwrapPath, JSON.stringify(json), 'utf8') +} |