Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update npm-shrinkwrap to include semver #132

Merged
merged 7 commits into from
Jun 17, 2015
22 changes: 20 additions & 2 deletions lib/agent/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var semver = require('semver');
var semver = require('semver'),
exceptions = require('../exceptions');

var helpers = {};

Expand All @@ -19,7 +20,24 @@ helpers.run_via_service = function(){


// is_greater_than("1.3.10", "1.3.9") returns true
helpers.is_greater_than = function(first, second){
helpers.is_greater_than = function(first, second) {
var invalid = [];

[first, second].forEach(function (el, i) {

if(!semver.valid(el)) {

var label = i === 0 ? "first" : "second";
invalid.push(label);
}

});

if (invalid.length > 0) {
first = second = "0.0.1";
exceptions.send(new Error("Cannot run is_greater_than. Invalid versions: "+ invalid + ". Values: " + first + ", " + second));
}

return semver.gt(first, second);
};

Expand Down
4 changes: 1 addition & 3 deletions lib/agent/plugins/control-panel/api/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,12 @@ exports.use = function(obj) {
for (var key in obj) {
if (defaults.hasOwnProperty(key)) {

// logger.debug('Setting ' + key + ' to ' + obj[key]);

if (key == 'protocol' && ['http', 'https'].indexOf(obj[key]) === -1) {
logger.error('Invalid protocol: ' + obj[key]);
continue;
}

if (!obj[key]) {
if (key !== 'try_proxy' && !obj[key]) {
logger.error('Empty API value for key: ' + key);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('interval', function () {
};

before(function () {
api.use( { try_proxy: ''} );
set_spies();
});

Expand Down
11 changes: 8 additions & 3 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions test/lib/agent/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,29 @@ describe('helpers.is_greater_than', function() {
it('returns true when first is higher than second', function() {
helpers.is_greater_than("1.3.10", "1.3.9").should.equal(true);
});

it('returns false when both are equal', function() {
helpers.is_greater_than("1.3.10", "1.3.10").should.equal(false);
});

// In the following cases, there's no way to compare, hence it returns false

it('returns false when second is empty, null or undefined', function() {
helpers.is_greater_than("1.3.10", "").should.equal(false);
helpers.is_greater_than("1.3.10", null).should.equal(false);
helpers.is_greater_than("1.3.10", undefined).should.equal(false);
});

it('returns false when first is empty, null or undefined', function() {
helpers.is_greater_than("", "1.3.10").should.equal(false);
helpers.is_greater_than(null, "1.3.10").should.equal(false);
helpers.is_greater_than(undefined, "1.3.10").should.equal(false);
});

it('returns false if both are empty, null or undefined', function() {
helpers.is_greater_than("", "").should.equal(false);
helpers.is_greater_than(null, undefined).should.equal(false);
helpers.is_greater_than(undefined, null).should.equal(false);
});

});
82 changes: 82 additions & 0 deletions test/shrinkwrap_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var fs = require('fs'),
join = require('path').join,
should = require('should'),
getset = require('getset'),
is_windows = process.platform === 'win32',
base_dir = join(__dirname, '..'),
package_obj = JSON.parse(fs.readFileSync(join(base_dir, 'package.json'), 'utf8')),
shrinkwrap = JSON.parse(fs.readFileSync(join(base_dir, 'npm-shrinkwrap.json'), 'utf8'));

function get_deps(obj) {
return JSON.stringify(Object.keys(obj.dependencies).sort());
}

var shrinkwrap_mock = {
dependencies: {
"package1": {
"version": "0.0.1",
"from": "https://someurl.com/package1-0.0.1.tgz",
"resolved": "https://someurl.com/package1-0.0.1.tgz"
},
"package2": {
"version": "0.0.2",
"from": "https://someurl.com/package2-0.0.2.tgz",
"resolved": "https://someurl.com/package2-0.0.2.tgz"
}
}
};

describe('npm-shrinkwrap and package.json', function () {

// testingception
describe('when different', function() {

var package_mock = {
dependencies: {
"package1": "0.0.1",
"package2": "0.0.2",
"package3": "0.0.3"
}
};

it('equals false', function() {

shrinkwrap_str = get_deps(shrinkwrap_mock);
package_str = get_deps(package_mock);

shrinkwrap_str.should.not.equal(package_str);
});
});

// testingception the sequel
describe('when equal', function() {

var package_mock = {
dependencies: {
"package1": "0.0.1",
"package2": "0.0.2",
}
};

it('equals true', function() {

shrinkwrap_str = get_deps(shrinkwrap_mock);
package_str = get_deps(package_mock);

shrinkwrap_str.should.equal(package_str);
});
});

describe('actual files', function() {

it('should have same dependencies', function() {

shrinkwrap_str = get_deps(shrinkwrap);
package_str = get_deps(package_obj);

shrinkwrap_str.should.equal(package_str);
});

});

});