Skip to content

Commit

Permalink
Add --exact config for --save[-dev|-optional].
Browse files Browse the repository at this point in the history
  • Loading branch information
timoxley committed Feb 20, 2014
1 parent 2ddd060 commit 277b931
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ function save (where, installed, tree, pretty, hasArguments, cb) {
return w
}).reduce(function (set, k) {
var rangeDescriptor = semver.valid(k[1], true) &&
semver.gte(k[1], "0.1.0", true)
semver.gte(k[1], "0.1.0", true) &&
!npm.config.get("exact")
? "^" : ""
set[k[0]] = rangeDescriptor + k[1]
return set
Expand Down
91 changes: 91 additions & 0 deletions test/tap/install-save-exact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var common = require('../common-tap.js')
var test = require('tap').test
var npm = require('../../')
var osenv = require('osenv')
var path = require('path')
var fs = require('fs')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var pkg = path.join(__dirname, 'install-save-exact')
var mr = require("npm-registry-mock")

test("setup", function (t) {
mkdirp.sync(pkg)
mkdirp.sync(path.resolve(pkg, 'node_modules'))
process.chdir(pkg)
t.end()
})

test('"npm install --save --exact should install local pkg', function(t) {
resetPackageJSON(pkg)
mr(common.port, function (s) {
npm.load({
cache: pkg + "/cache",
loglevel: 'silent',
registry: common.registry }, function(err) {
t.ifError(err)
npm.config.set('save', true)
npm.config.set('exact', true)
npm.commands.install(['underscore@1.3.1'], function(err) {
t.ifError(err)
var p = path.resolve(pkg, 'node_modules/underscore/package.json')
t.ok(JSON.parse(fs.readFileSync(p)))
var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8'))
t.deepEqual(pkgJson.dependencies, {
'underscore': '1.3.1'
}, 'Underscore dependency should specify exactly 1.3.1')
npm.config.set('save', undefined)
npm.config.set('exact', undefined)
s.close()
t.end()
})
})
})
})

test('"npm install --save-dev --exact should install local pkg', function(t) {
resetPackageJSON(pkg)

mr(common.port, function (s) {
npm.load({
cache: pkg + "/cache",
loglevel: 'silent',
registry: common.registry }, function(err) {
t.ifError(err)
npm.config.set('save-dev', true)
npm.config.set('exact', true)
npm.commands.install(['underscore@1.3.1'], function(err) {
t.ifError(err)
var p = path.resolve(pkg, 'node_modules/underscore/package.json')
t.ok(JSON.parse(fs.readFileSync(p)))
var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8'))
console.log(pkgJson)
t.deepEqual(pkgJson.devDependencies, {
'underscore': '1.3.1'
}, 'underscore devDependency should specify exactly 1.3.1')
s.close()
npm.config.set('save-dev', undefined)
npm.config.set('exact', undefined)
t.end()
})
})
})
})

test('cleanup', function(t) {
process.chdir(__dirname)
rimraf.sync(path.resolve(pkg, 'node_modules'))
rimraf.sync(path.resolve(pkg, 'cache'))
resetPackageJSON(pkg)
t.end()
})

function resetPackageJSON(pkg) {
var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8'))
delete pkgJson.dependencies
delete pkgJson.devDependencies
delete pkgJson.optionalDependencies
fs.writeFileSync(pkg + '/package.json', JSON.stringify(pkgJson))
}


1 change: 1 addition & 0 deletions test/tap/install-save-exact/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# just a test
1 change: 1 addition & 0 deletions test/tap/install-save-exact/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = true
1 change: 1 addition & 0 deletions test/tap/install-save-exact/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"bla","description":"fixture","version":"0.0.1","main":"index.js","repository":"git://github.com/robertkowalski/bogusfixture"}

0 comments on commit 277b931

Please sign in to comment.