From 42af9b7d6c1542bb042bfdbc393261117e7bee63 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 18 Nov 2015 17:33:15 -0500 Subject: [PATCH] feat(devDependencies): Checking both dependencies and dev dependencies for fuzzy characters --- src/make-exact-spec.js | 84 +++++++++++++++++++++++++++++++++++------- src/make-exact.js | 18 ++++++--- 2 files changed, 84 insertions(+), 18 deletions(-) diff --git a/src/make-exact-spec.js b/src/make-exact-spec.js index a5d476a..4ff7ca3 100644 --- a/src/make-exact-spec.js +++ b/src/make-exact-spec.js @@ -4,30 +4,88 @@ var check = require('check-more-types'); describe('make exact', function () { var makeExact = require('./make-exact'); - it('is a function', function () { la(check.fn(makeExact)); }); - it('returns false if there are no dependencies', function () { var needSaving = makeExact({}); la(!needSaving); }); - it('returns false if empty dependencies', function () { - var needSaving = makeExact({ - dependencies: {} + describe('dependencies', function () { + it('returns false if empty dependencies', function () { + var needSaving = makeExact({ + dependencies: {} + }); + la(!needSaving); + }); + + it('returns false is exact semver', function () { + var needSaving = makeExact({ + dependencies: { + foo: '1.0.2', + bar: '2.0.1' + } + }); + la(!needSaving); + }); + + it('returns true and cleans fuzzy dependencies', function () { + var pkg = { + dependencies: { + foo: '^1.0.2', + bar: '2.0.1' + } + }; + var needSaving = makeExact(pkg); + la(needSaving, 'needs saving'); + la(pkg.dependencies.foo === '1.0.2', 'removed fuzziness'); + }); + + it('returns true and cleans two fuzzy dependencies', function () { + var pkg = { + dependencies: { + foo: '^1.0.2', + bar: '~2.0.1' + } + }; + var needSaving = makeExact(pkg); + la(needSaving, 'needs saving'); + la(pkg.dependencies.foo === '1.0.2', 'removed foo fuzziness'); + la(pkg.dependencies.bar === '2.0.1', 'removed bar fuzziness'); }); - la(!needSaving); }); - it('returns false is exact semver', function () { - var needSaving = makeExact({ - dependencies: { - foo: '1.0.2', - bar: '2.0.1' - } + describe('dev dependencies', function () { + it('returns false if empty dev dependencies', function () { + var needSaving = makeExact({ + dependencies: {}, + devDependencies: {} + }); + la(!needSaving); + }); + + it('returns false is exact semver', function () { + var needSaving = makeExact({ + devDependencies: { + foo: '1.0.2', + bar: '2.0.1' + } + }); + la(!needSaving); + }); + + it('returns true and cleans two fuzzy dependencies', function () { + var pkg = { + devDependencies: { + foo: '^1.0.2', + bar: '~2.0.1' + } + }; + var needSaving = makeExact(pkg); + la(needSaving, 'needs saving'); + la(pkg.devDependencies.foo === '1.0.2', 'removed foo fuzziness'); + la(pkg.devDependencies.bar === '2.0.1', 'removed bar fuzziness'); }); - la(!needSaving); }); }); diff --git a/src/make-exact.js b/src/make-exact.js index 891632d..0a0db97 100644 --- a/src/make-exact.js +++ b/src/make-exact.js @@ -1,14 +1,14 @@ var isStrictSemver = require('./is-strict-semver'); var toExact = require('./to-exact'); -function makeExact(pkg) { +function makeExactDependencies(dependencies) { var needSaving; - if (pkg.dependencies) { - Object.keys(pkg.dependencies).forEach(function (name) { - var version = pkg.dependencies[name]; + if (dependencies) { + Object.keys(dependencies).forEach(function (name) { + var version = dependencies[name]; if (!isStrictSemver(version)) { version = toExact(version); - pkg.dependencies[name] = version; + dependencies[name] = version; needSaving = true; } }); @@ -17,4 +17,12 @@ function makeExact(pkg) { return needSaving; } +function makeExact(pkg) { + if (!pkg) { + throw new Error('Missing package object'); + } + return makeExactDependencies(pkg.dependencies) || + makeExactDependencies(pkg.devDependencies); +} + module.exports = makeExact;