You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sg...@apache.org on 2014/09/26 23:20:08 UTC
[09/13] git commit: CB-6481 Fixed HooksRunner and tests Avoided issue
with parallel tests running Added checks for handling mocked config.xml and
package.json in HooksRunner and scriptsFinder Addressed jshint issues Renamed
ScriptsFinder to scriptsFinder
CB-6481 Fixed HooksRunner and tests
Avoided issue with parallel tests running
Added checks for handling mocked config.xml and package.json in HooksRunner and scriptsFinder
Addressed jshint issues
Renamed ScriptsFinder to scriptsFinder
Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/a3169336
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/a3169336
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/a3169336
Branch: refs/heads/master
Commit: a3169336634508246754b8e52bd452edba5ecc7f
Parents: eed4139
Author: daserge <v-...@microsoft.com>
Authored: Thu Jul 24 08:09:08 2014 -0700
Committer: daserge <da...@yandex.ru>
Committed: Thu Sep 25 19:04:55 2014 +0400
----------------------------------------------------------------------
cordova-lib/spec-cordova/HooksRunner.spec.js | 16 ++-
cordova-lib/src/cordova/plugin.js | 1 -
cordova-lib/src/hooks/HooksRunner.js | 10 +-
cordova-lib/src/hooks/ScriptsFinder.js | 156 --------------------
cordova-lib/src/hooks/scriptsFinder.js | 164 ++++++++++++++++++++++
5 files changed, 181 insertions(+), 166 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a3169336/cordova-lib/spec-cordova/HooksRunner.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/HooksRunner.spec.js b/cordova-lib/spec-cordova/HooksRunner.spec.js
index 1e89808..5cbb431 100644
--- a/cordova-lib/spec-cordova/HooksRunner.spec.js
+++ b/cordova-lib/spec-cordova/HooksRunner.spec.js
@@ -64,10 +64,6 @@ shell.chmod('-R', 'ug+x', hooksDir);
shell.chmod('-R', 'ug+x', hooksDirDot);
shell.chmod('-R', 'ug+x', scriptsDir);
-jasmine.getEnv().defaultTimeoutInterval = 3000;
-process.chdir(project);
-
-
describe('HooksRunner', function() {
var hooksRunner;
var hookOptions;
@@ -75,6 +71,14 @@ describe('HooksRunner', function() {
var projectRoot;
var fire;
+ beforeEach(function() {
+ process.chdir(project);
+ });
+
+ afterEach(function() {
+ process.chdir(path.join(__dirname, '..')); // Non e2e tests assume CWD is repo root.
+ });
+
it('should throw if provided directory is not a cordova project', function() {
expect(function() {
new HooksRunner(tmpDir);
@@ -590,10 +594,8 @@ describe('HooksRunner', function() {
}).fin(done);
});
});
-});
-describe('cleanup', function() {
-// Cleanup. Must be the last spec. Is there a better place for final cleanup in Jasmine?
+ // Cleanup. Must be the last spec. Is there a better place for final cleanup in Jasmine?
it('should not fail during cleanup', function () {
process.chdir(path.join(__dirname, '..')); // Non e2e tests assume CWD is repo root.
if (ext == 'sh') {
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a3169336/cordova-lib/src/cordova/plugin.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/plugin.js b/cordova-lib/src/cordova/plugin.js
index 9d6b8ab..6763560 100644
--- a/cordova-lib/src/cordova/plugin.js
+++ b/cordova-lib/src/cordova/plugin.js
@@ -24,7 +24,6 @@
var cordova_util = require('./util'),
path = require('path'),
semver = require('semver'),
- HooksRunner = require('../hooks/HooksRunner'),
config = require('./config'),
Q = require('q'),
CordovaError = require('../CordovaError'),
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a3169336/cordova-lib/src/hooks/HooksRunner.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/hooks/HooksRunner.js b/cordova-lib/src/hooks/HooksRunner.js
index 0f11da0..77ed54a 100644
--- a/cordova-lib/src/hooks/HooksRunner.js
+++ b/cordova-lib/src/hooks/HooksRunner.js
@@ -69,7 +69,13 @@ HooksRunner.prototype.prepareOptions = function(opts) {
opts.cordova = opts.cordova || {};
opts.cordova.platforms = opts.cordova.platforms || opts.platforms || cordovaUtil.listPlatforms(opts.projectRoot);
opts.cordova.plugins = opts.cordova.plugins || opts.plugins || cordovaUtil.findPlugins(path.join(opts.projectRoot, 'plugins'));
- opts.cordova.version = opts.cordova.version || require('../../package').version;
+
+ try {
+ opts.cordova.version = opts.cordova.version || require('../../package').version;
+ } catch(ex) {
+ events.emit('err', 'HooksRunner could not load package.json: ' + ex.message);
+ console.log('HooksRunner could not load package.json: ' + ex.message);
+ }
return opts;
};
@@ -124,7 +130,7 @@ function runScriptsSerially (scripts, context) {
}
executePendingScript();
return deferral.promise;
-};
+}
/**
* Async runs single script file.
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a3169336/cordova-lib/src/hooks/ScriptsFinder.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/hooks/ScriptsFinder.js b/cordova-lib/src/hooks/ScriptsFinder.js
deleted file mode 100644
index 3345af1..0000000
--- a/cordova-lib/src/hooks/ScriptsFinder.js
+++ /dev/null
@@ -1,156 +0,0 @@
-/**
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you 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.
- */
-
-var path = require('path'),
- fs = require('fs'),
- cordovaUtil = require('../cordova/util'),
- events = require('../events'),
- Q = require('q'),
- plugin = require('../cordova/plugin'),
- PluginInfo = require('../PluginInfo'),
- ConfigParser = require('../configparser/ConfigParser');
-
-/**
- * Implements logic to retrieve hook script files defined in special folders and configuration
- * files: config.xml, hooks/hook_type, plugins/../plugin.xml, etc
- */
-module.exports = {
- /**
- * Returns all script files for the hook type specified.
- */
- getHookScripts: function(hook, opts) {
- // args check
- if (!hook) {
- throw new Error('hook type is not specified');
- }
- return getApplicationHookScripts(hook, opts)
- .concat(getPluginsHookScripts(hook, opts));
- }
-};
-
-/**
- * Returns script files defined on application level.
- * They are stored in .cordova/hooks folders and in config.xml.
- */
-function getApplicationHookScripts(hook, opts) {
- // args check
- if (!hook) {
- throw new Error('hook type is not specified');
- }
- return getApplicationHookScriptsFromDir(path.join(opts.projectRoot, '.cordova', 'hooks', hook))
- .concat(getApplicationHookScriptsFromDir(path.join(opts.projectRoot, 'hooks', hook)))
- .concat(getScriptsFromConfigXml(hook, opts));
-}
-
-/**
- * Returns script files defined by plugin developers as part of plugin.xml.
- */
-function getPluginsHookScripts(hook, opts) {
- // args check
- if (!hook) {
- throw new Error('hook type is not specified');
- }
-
- // In case before_plugin_install, after_plugin_install, before_plugin_uninstall hooks we receive opts.plugin and
- // retrieve scripts exclusive for this plugin.
- if(opts.plugin) {
- events.emit('debug', 'Executing "' + hook + '" hook for "' + opts.plugin.id + '" on ' + opts.plugin.platform + '.');
-
- return getPluginScriptFiles(opts.plugin, hook, [ opts.plugin.platform ]);
- }
-
- events.emit('debug', 'Executing "' + hook + '" hook for all plugins.');
- return getAllPluginsHookScriptFiles(hook, opts);
-}
-
-/**
- * Gets application level hooks from the directrory specified.
- */
-function getApplicationHookScriptsFromDir(dir) {
- if (!(fs.existsSync(dir))) {
- return [];
- }
-
- var compareNumbers = function(a, b) {
- // TODO SG looks very complex, do we really need this?
- return isNaN (parseInt(a, 10)) ? a.toLowerCase().localeCompare(b.toLowerCase ? b.toLowerCase(): b)
- : parseInt(a, 10) > parseInt(b, 10) ? 1 : parseInt(a, 10) < parseInt(b, 10) ? -1 : 0;
- };
-
- var scripts = fs.readdirSync(dir).sort(compareNumbers).filter(function(s) {
- return s[0] != '.';
- });
- return scripts.map(function (scriptPath) {
- // for old style hook files we don't use module loader for backward compatibility
- return { path: scriptPath, fullPath: path.join(dir, scriptPath), useModuleLoader: false };
- });
-}
-
-/**
- * Gets all scripts defined in config.xml with the specified type and platforms.
- */
-function getScriptsFromConfigXml(hook, opts) {
- var configPath = cordovaUtil.projectConfig(opts.projectRoot);
- var configXml = new ConfigParser(configPath);
-
- return configXml.getHookScripts(hook, opts.cordova.platforms).map(function(scriptElement) {
- return {
- path: scriptElement.attrib.src,
- fullPath: path.join(opts.projectRoot, scriptElement.attrib.src)
- };
- });
-}
-
-/**
- * Gets hook scripts defined by the plugin.
- */
-function getPluginScriptFiles(plugin, hook, platforms) {
- var scriptElements = plugin.pluginInfo.getHookScripts(hook, platforms);
-
- return scriptElements.map(function(scriptElement) {
- return {
- path: scriptElement.attrib.src,
- fullPath: path.join(plugin.dir, scriptElement.attrib.src),
- plugin: plugin
- };
- });
-}
-
-/**
- * Gets hook scripts defined by all plugins.
- */
-function getAllPluginsHookScriptFiles(hook, opts) {
- var scripts = [];
- var pluginDir;
- var pluginInfo;
- var currentPluginOptions;
-
- opts.cordova.plugins.forEach(function(pluginId) {
- pluginDir = path.join(opts.projectRoot, 'plugins', pluginId);
-
- currentPluginOptions = {
- id: pluginId,
- pluginInfo: new PluginInfo.PluginInfo(pluginDir),
- dir: pluginDir
- };
-
- scripts = scripts.concat(getPluginScriptFiles(currentPluginOptions, hook, opts.cordova.platforms));
- });
- return scripts;
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a3169336/cordova-lib/src/hooks/scriptsFinder.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/hooks/scriptsFinder.js b/cordova-lib/src/hooks/scriptsFinder.js
new file mode 100644
index 0000000..9abbb8e
--- /dev/null
+++ b/cordova-lib/src/hooks/scriptsFinder.js
@@ -0,0 +1,164 @@
+/**
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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.
+ */
+
+var path = require('path'),
+ fs = require('fs'),
+ cordovaUtil = require('../cordova/util'),
+ events = require('../events'),
+ Q = require('q'),
+ plugin = require('../cordova/plugin'),
+ PluginInfo = require('../PluginInfo'),
+ ConfigParser = require('../configparser/ConfigParser');
+
+/**
+ * Implements logic to retrieve hook script files defined in special folders and configuration
+ * files: config.xml, hooks/hook_type, plugins/../plugin.xml, etc
+ */
+module.exports = {
+ /**
+ * Returns all script files for the hook type specified.
+ */
+ getHookScripts: function(hook, opts) {
+ // args check
+ if (!hook) {
+ throw new Error('hook type is not specified');
+ }
+ return getApplicationHookScripts(hook, opts)
+ .concat(getPluginsHookScripts(hook, opts));
+ }
+};
+
+/**
+ * Returns script files defined on application level.
+ * They are stored in .cordova/hooks folders and in config.xml.
+ */
+function getApplicationHookScripts(hook, opts) {
+ // args check
+ if (!hook) {
+ throw new Error('hook type is not specified');
+ }
+ return getApplicationHookScriptsFromDir(path.join(opts.projectRoot, '.cordova', 'hooks', hook))
+ .concat(getApplicationHookScriptsFromDir(path.join(opts.projectRoot, 'hooks', hook)))
+ .concat(getScriptsFromConfigXml(hook, opts));
+}
+
+/**
+ * Returns script files defined by plugin developers as part of plugin.xml.
+ */
+function getPluginsHookScripts(hook, opts) {
+ // args check
+ if (!hook) {
+ throw new Error('hook type is not specified');
+ }
+
+ // In case before_plugin_install, after_plugin_install, before_plugin_uninstall hooks we receive opts.plugin and
+ // retrieve scripts exclusive for this plugin.
+ if(opts.plugin) {
+ events.emit('debug', 'Executing "' + hook + '" hook for "' + opts.plugin.id + '" on ' + opts.plugin.platform + '.');
+
+ return getPluginScriptFiles(opts.plugin, hook, [ opts.plugin.platform ]);
+ }
+
+ events.emit('debug', 'Executing "' + hook + '" hook for all plugins.');
+ return getAllPluginsHookScriptFiles(hook, opts);
+}
+
+/**
+ * Gets application level hooks from the directrory specified.
+ */
+function getApplicationHookScriptsFromDir(dir) {
+ if (!(fs.existsSync(dir))) {
+ return [];
+ }
+
+ var compareNumbers = function(a, b) {
+ // TODO SG looks very complex, do we really need this?
+ return isNaN (parseInt(a, 10)) ? a.toLowerCase().localeCompare(b.toLowerCase ? b.toLowerCase(): b)
+ : parseInt(a, 10) > parseInt(b, 10) ? 1 : parseInt(a, 10) < parseInt(b, 10) ? -1 : 0;
+ };
+
+ var scripts = fs.readdirSync(dir).sort(compareNumbers).filter(function(s) {
+ return s[0] != '.';
+ });
+ return scripts.map(function (scriptPath) {
+ // for old style hook files we don't use module loader for backward compatibility
+ return { path: scriptPath, fullPath: path.join(dir, scriptPath), useModuleLoader: false };
+ });
+}
+
+/**
+ * Gets all scripts defined in config.xml with the specified type and platforms.
+ */
+function getScriptsFromConfigXml(hook, opts) {
+ var configPath = cordovaUtil.projectConfig(opts.projectRoot);
+ var configXml;
+
+ try {
+ configXml = new ConfigParser(configPath);
+ } catch(ex) {
+ events.emit('err', 'scriptsFinder could not load config.xml: ' + ex.message);
+ console.log('scriptsFinder could not load config.xml: ' + ex.message);
+ return [];
+ }
+
+ return configXml.getHookScripts(hook, opts.cordova.platforms).map(function(scriptElement) {
+ return {
+ path: scriptElement.attrib.src,
+ fullPath: path.join(opts.projectRoot, scriptElement.attrib.src)
+ };
+ });
+}
+
+/**
+ * Gets hook scripts defined by the plugin.
+ */
+function getPluginScriptFiles(plugin, hook, platforms) {
+ var scriptElements = plugin.pluginInfo.getHookScripts(hook, platforms);
+
+ return scriptElements.map(function(scriptElement) {
+ return {
+ path: scriptElement.attrib.src,
+ fullPath: path.join(plugin.dir, scriptElement.attrib.src),
+ plugin: plugin
+ };
+ });
+}
+
+/**
+ * Gets hook scripts defined by all plugins.
+ */
+function getAllPluginsHookScriptFiles(hook, opts) {
+ var scripts = [];
+ var pluginDir;
+ var pluginInfo;
+ var currentPluginOptions;
+
+ opts.cordova.plugins.forEach(function(pluginId) {
+ pluginDir = path.join(opts.projectRoot, 'plugins', pluginId);
+
+ currentPluginOptions = {
+ id: pluginId,
+ pluginInfo: new PluginInfo.PluginInfo(pluginDir),
+ dir: pluginDir
+ };
+
+ scripts = scripts.concat(getPluginScriptFiles(currentPluginOptions, hook, opts.cordova.platforms));
+ });
+ return scripts;
+}
\ No newline at end of file