Added global-dependencies library

This library permits reading, validating, and accessing a set of
globally accepted dependency criteria. It will eventually be used
to synchronize dependencies across multiple javascript projects.

Change-Id: I2ec7a218311cfcfc7d33c4a1e29e73ed8e2f7b42
This commit is contained in:
Michael Krotscheck 2016-05-16 13:29:36 -07:00
parent 1292783504
commit d6918c50a4
No known key found for this signature in database
GPG Key ID: 20E618D878DE38AB
4 changed files with 180 additions and 2 deletions

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2016 Hewlett Packard Enterprise Development Company, LP
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/**
* Access to the global-dependencies.json file.
*/
(function () {
'use strict';
var globalDependencies = require('../../../global-dependencies.json');
/**
* Returns whether a dependency is in the global-dependencies list.
*
* @param {String} name The name of the dependency.
* @returns {Boolean} True if the dependency exists, otherwise false.
*/
function containsDependency (name) {
return globalDependencies.hasOwnProperty(name);
}
/**
* Return the current acceptable version of the dependencies, or null.
*
* @param {String} name The dependency name.
* @returns {String|undefined} The version, or undefined.
*/
function getVersion (name) {
return globalDependencies[name] || undefined;
}
/**
* Given a list of dependencies, updates this list of dependencies to the versions that are
* currently set in global-dependencies.
*
* @param {{}} dependencies The list of dependencies.
* @returns {{}} The above list of dependencies, with only the appropriate versions updated.
*/
function synchronizeDependencies (dependencies) {
var results = {};
for (var key in dependencies) {
if (globalDependencies.hasOwnProperty(key)) {
results[key] = globalDependencies[key];
} else {
results[key] = dependencies[key];
}
}
return results;
}
module.exports = {
contains: containsDependency,
read: getVersion,
synchronize: synchronizeDependencies
};
})();

12
global-dependencies.json Normal file
View File

@ -0,0 +1,12 @@
{
"ini": "^1.3.4",
"js-yaml": "^3.5.5",
"q": "^1.4.1",
"eslint": "^1.10.3",
"eslint-config-openstack": "1.2.4",
"istanbul": "^0.4.2",
"jasmine": "^2.4.1",
"karma": "^0.13.22",
"karma-htmlfile-reporter": "^0.2.2",
"nsp": "^2.3.0"
}

View File

@ -15,7 +15,8 @@
"openstack"
],
"files": [
"generators"
"generators",
"global-dependencies.json"
],
"author": "OpenStack <openstack-dev@lists.openstack.org> (http://www.openstack.org/)",
"license": "Apache-2.0",
@ -23,6 +24,7 @@
"ini": "^1.3.4",
"js-yaml": "^3.5.5",
"q": "^1.4.1",
"semver": "^5.1.0",
"yeoman-generator": "^0.22.5"
},
"devDependencies": {
@ -39,4 +41,4 @@
"yeoman-test": "^1.1.0"
},
"repository": "https://github.com/krotscheck/generator-openstack"
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 2016 Hewlett Packard Enterprise Development Company, LP
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
(function () {
'use strict';
var builder = require('../../../generators/app/lib/global_dependencies');
var globals = require('../../../global-dependencies.json');
var semver = require('semver');
describe('lib/global_dependencies', function () {
describe('data', function () {
it('should contain all dependencies from the root global-dependencies.json',
function () {
for (var key in globals) {
if (globals.hasOwnProperty(key)) {
expect(builder.contains(key)).toBe(true);
}
}
});
it('should contain valid semver versions for all dependencies',
function () {
for (var key in globals) {
if (globals.hasOwnProperty(key)) {
var version = builder.read(key);
expect(semver.validRange(version)).toBeTruthy();
}
}
});
});
describe('contains()', function () {
it('should return true when a dependency exists', function () {
expect(builder.contains('eslint')).toBe(true);
});
it('should return false when a dependency doesn\'t exist', function () {
expect(builder.contains('notarealdependency')).toBe(false);
});
});
describe('read()', function () {
it('should return the version of a dependency', function () {
expect(builder.read('eslint')).toBe(globals.eslint);
});
it('should return undefined when a dependency doesn\'t exist', function () {
expect(builder.read('notarealdependency')).toBeUndefined();
});
});
describe('synchronize()', function () {
it('should update dependencies that are out of date.', function () {
var testDeps = {
eslint: '0.0.1'
};
var newDeps = builder.synchronize(testDeps);
expect(newDeps.eslint).toBeDefined();
expect(newDeps.eslint).not.toEqual(testDeps.eslint);
expect(newDeps.eslint).toEqual(globals.eslint);
});
it('should not update dependencies that are up to date.', function () {
var testDeps = {
eslint: globals.eslint
};
var newDeps = builder.synchronize(testDeps);
expect(newDeps).toEqual(testDeps);
});
it('should not touch unregistered dependencies.', function () {
var testDeps = {
notarealdependency: '0.0.1'
};
var newDeps = builder.synchronize(testDeps);
expect(newDeps.notarealdependency).toEqual(testDeps.notarealdependency);
});
});
});
})();