From d6918c50a4f7de1e3af9a1b9608df8e11c4f46ab Mon Sep 17 00:00:00 2001 From: Michael Krotscheck Date: Mon, 16 May 2016 13:29:36 -0700 Subject: [PATCH] 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 --- generators/app/lib/global_dependencies.js | 69 ++++++++++++++++ global-dependencies.json | 12 +++ package.json | 6 +- spec/app/lib/global_dependencies.js | 95 +++++++++++++++++++++++ 4 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 generators/app/lib/global_dependencies.js create mode 100644 global-dependencies.json create mode 100644 spec/app/lib/global_dependencies.js diff --git a/generators/app/lib/global_dependencies.js b/generators/app/lib/global_dependencies.js new file mode 100644 index 0000000..d9422f4 --- /dev/null +++ b/generators/app/lib/global_dependencies.js @@ -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 + }; +})(); diff --git a/global-dependencies.json b/global-dependencies.json new file mode 100644 index 0000000..45cd9d2 --- /dev/null +++ b/global-dependencies.json @@ -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" +} diff --git a/package.json b/package.json index 4e471ba..63f00db 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "openstack" ], "files": [ - "generators" + "generators", + "global-dependencies.json" ], "author": "OpenStack (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" -} \ No newline at end of file +} diff --git a/spec/app/lib/global_dependencies.js b/spec/app/lib/global_dependencies.js new file mode 100644 index 0000000..2e6868d --- /dev/null +++ b/spec/app/lib/global_dependencies.js @@ -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); + }); + }); + }); +})();