You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2013/06/13 09:53:56 UTC

git commit: cleaned up plugins and fixed specs

Updated Branches:
  refs/heads/lazy b29e344f1 -> a5a861ebb


cleaned up plugins and fixed specs


Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/a5a861eb
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/a5a861eb
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/a5a861eb

Branch: refs/heads/lazy
Commit: a5a861ebbd2a537df5f4aaa842f265195a604e62
Parents: b29e344
Author: Fil Maj <ma...@gmail.com>
Authored: Thu Jun 13 00:53:47 2013 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu Jun 13 00:53:47 2013 -0700

----------------------------------------------------------------------
 spec/plugin.spec.js | 231 +++++++++++++++++++++++++++--------------------
 src/plugin.js       |  12 +--
 src/util.js         |   2 +
 3 files changed, 136 insertions(+), 109 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/a5a861eb/spec/plugin.spec.js
----------------------------------------------------------------------
diff --git a/spec/plugin.spec.js b/spec/plugin.spec.js
index d042841..737a846 100644
--- a/spec/plugin.spec.js
+++ b/spec/plugin.spec.js
@@ -16,131 +16,164 @@
     specific language governing permissions and limitations
     under the License.
 */
-var cordova = require('../../cordova'),
+var cordova = require('../cordova'),
     path = require('path'),
     shell = require('shelljs'),
+    plugman = require('plugman'),
     fs = require('fs'),
-    hooker = require('../../src/hooker'),
-    tempDir = path.join(__dirname, '..', '..', 'temp'),
-    fixturesDir = path.join(__dirname, '..', 'fixtures'),
-    testPlugin = path.join(fixturesDir, 'plugins', 'test'),
-    cordova_project = path.join(fixturesDir, 'projects', 'cordova'),
-    androidPlugin = path.join(fixturesDir, 'plugins', 'android');
+    util = require('../src/util'),
+    config = require('../src/config'),
+    hooker = require('../src/hooker'),
+    platforms = require('../platforms');
 
 var cwd = process.cwd();
+var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www'; });
+var sample_plugins = ['one','two'];
+var project_dir = path.join('some','path');
+var plugins_dir = path.join(project_dir, 'plugins');
 
 describe('plugin command', function() {
+    var is_cordova, list_platforms, fire, find_plugins, rm, mkdir, existsSync, exec, prep_spy, plugman_install, plugman_fetch, parsers = {}, uninstall;
     beforeEach(function() {
-        // Make a temp directory
-        shell.rm('-rf', tempDir);
-        shell.mkdir('-p', tempDir);
+        is_cordova = spyOn(util, 'isCordova').andReturn(project_dir);
+        fire = spyOn(hooker.prototype, 'fire').andCallFake(function(e, opts, cb) {
+            if (cb === undefined) cb = opts;
+            cb(false);
+        });
+        supported_platforms.forEach(function(p) {
+            parsers[p] = jasmine.createSpy(p + ' update_project').andCallFake(function(cfg, cb) {
+                cb();
+            });
+            spyOn(platforms[p], 'parser').andReturn({
+                staging_dir:function(){return ''}
+            });
+        });
+        list_platforms = spyOn(util, 'listPlatforms').andReturn(supported_platforms);
+        find_plugins = spyOn(util, 'findPlugins').andReturn(sample_plugins);
+        rm = spyOn(shell, 'rm');
+        mkdir = spyOn(shell, 'mkdir');
+        existsSync = spyOn(fs, 'existsSync').andReturn(false);
+        exec = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) {
+            cb(0, '');
+        });
+        prep_spy = spyOn(cordova, 'prepare').andCallFake(function(t, cb) {
+            cb();
+        });
+        plugman_install = spyOn(plugman, 'install');
+        plugman_fetch = spyOn(plugman, 'fetch').andCallFake(function(target, plugins_dir, opts, cb) { cb(false, path.join(plugins_dir, target)); });
+        uninstall = spyOn(plugman, 'uninstall');
     });
 
-    it('should run inside a Cordova-based project', function() {
-        this.after(function() {
-            process.chdir(cwd);
+    describe('failure', function() {
+        it('should not run outside of a Cordova-based project by calling util.isCordova', function() {
+            is_cordova.andReturn(false);
+            expect(function() {
+                cordova.plugin();
+                expect(is_cordova).toHaveBeenCalled();
+            }).toThrow('Current working directory is not a Cordova-based project.');
         });
-
-        cordova.create(tempDir);
-
-        process.chdir(tempDir);
-
-        expect(function() {
-            cordova.plugin();
-        }).not.toThrow();
     });
-    it('should not run outside of a Cordova-based project', function() {
-        this.after(function() {
-            process.chdir(cwd);
-        });
-
-        process.chdir(tempDir);
 
-        expect(function() {
+    describe('success', function() {
+        it('should run inside a Cordova-based project by calling util.isCordova', function() {
             cordova.plugin();
-        }).toThrow();
-    });
-
-    describe('edge cases', function() {
-       beforeEach(function() {
-           cordova.create(tempDir);
-           process.chdir(tempDir);
-       });
-
-       afterEach(function() {
-           process.chdir(cwd);
-           cordova.removeAllListeners('results');
-       });
-
-       it('should not fail when the plugins directory is missing', function() {
-           fs.rmdirSync('plugins');
-
-           expect(function() {
-               cordova.plugin();
-           }).not.toThrow();
-       });
-
-       it('should ignore files, like .gitignore, in the plugins directory', function(done) {
-           var someFile = path.join(tempDir, 'plugins', '.gitignore');
-           fs.writeFileSync(someFile, 'not a plugin');
-           cordova.on('results', function(res) {
-               expect(res).toEqual('No plugins added. Use `cordova plugin add <plugin>`.');
-               done();
-           });
+            expect(is_cordova).toHaveBeenCalled();
+        });
 
-           cordova.plugin('list');
-       });
-    });
+        describe('`ls`', function() { 
+            afterEach(function() {
+                cordova.removeAllListeners('results');
+            });
+            it('should list out no plugins for a fresh project', function(done) {
+                find_plugins.andReturn([]);
+                cordova.on('results', function(res) {
+                    expect(res).toEqual('No plugins added. Use `cordova plugin add <plugin>`.');
+                    done();
+                });
+                cordova.plugin('list');
+            });
 
-    describe('`ls`', function() {
-        beforeEach(function() {
-            cordova.create(tempDir);
-            process.chdir(tempDir);
+            it('should list out added plugins in a project', function(done) {
+                cordova.on('results', function(res) {
+                    expect(res).toEqual(sample_plugins);
+                    done();
+                });
+                cordova.plugin('list');
+            });
         });
-
-        afterEach(function() {
-            process.chdir(cwd);
-            cordova.removeAllListeners('results');
+        describe('`add`', function() {
+            it('should call plugman.fetch for each plugin', function() {
+                cordova.plugin('add', sample_plugins);
+                sample_plugins.forEach(function(p) {
+                    expect(plugman_fetch).toHaveBeenCalledWith(p, plugins_dir, {}, jasmine.any(Function)); 
+                });
+            });
+            it('should call plugman.install, for each plugin, for every platform', function() {
+                cordova.plugin('add', sample_plugins);
+                sample_plugins.forEach(function(plug) {
+                    supported_platforms.forEach(function(plat) {
+                        expect(plugman_install).toHaveBeenCalledWith((plat=='blackberry'?'blackberry10':plat), path.join(project_dir, 'platforms', plat), plug, plugins_dir, jasmine.any(Object)); 
+                    });
+                });
+            });
         });
+        describe('`remove`',function() {
+            var plugin_parser;
+            var subset = ['android', 'wp7'];
+            beforeEach(function() {
+                plugin_parser = spyOn(util, 'plugin_parser').andReturn({
+                    platforms:subset
+                });
+            });
+            it('should throw if plugin is not installed', function() {
+                expect(function() {
+                    cordova.plugin('rm', 'somethingrandom');
+                }).toThrow('Plugin "somethingrandom" not added to project.');
+            });
 
-        it('should list out no plugins for a fresh project', function(done) {
-            cordova.on('results', function(res) {
-                expect(res).toEqual('No plugins added. Use `cordova plugin add <plugin>`.');
-                done();
+            it('should call plugman.uninstall for every matching installedplugin-supportedplatform pair', function() {
+                cordova.plugin('rm', sample_plugins);
+                sample_plugins.forEach(function(plug) {
+                    subset.forEach(function(plat) {
+                        expect(uninstall).toHaveBeenCalledWith(plat, path.join(project_dir, 'platforms', plat), plug, plugins_dir, jasmine.any(Object));
+                    });
+                });
             });
-            cordova.plugin('list');
-        });
-        it('should list out any added plugins in a project', function(done) {
-            var random_plug = 'randomplug';
-            shell.mkdir('-p', path.join(tempDir, 'plugins', random_plug));
-            cordova.on('results', function(res) {
-                expect(res).toEqual([random_plug]);
-                done();
-            });
-            cordova.plugin('list');
         });
     });
-
-    describe('`add`', function() {
+    describe('hooks', function() {
+        var plugin_parser;
         beforeEach(function() {
-            cordova.create(tempDir);
-            process.chdir(tempDir);
+            plugin_parser = spyOn(util, 'plugin_parser').andReturn({
+                platforms:supported_platforms
+            });
         });
-
-        afterEach(function() {
-            process.chdir(cwd);
+        describe('list (ls) hooks', function() {
+            it('should fire before hooks through the hooker module', function() {
+                cordova.plugin();
+                expect(fire).toHaveBeenCalledWith('before_plugin_ls', jasmine.any(Function));
+            });
+            it('should fire after hooks through the hooker module', function() {
+                cordova.plugin();
+                expect(fire).toHaveBeenCalledWith('after_plugin_ls', jasmine.any(Function));
+            });
         });
-        describe('failure', function() {
-            it('should throw if plugin does not have a plugin.xml', function() {
-                process.chdir(cordova_project);
-                this.after(function() {
-                    process.chdir(cwd);
-                });
-                expect(function() {
-                    cordova.plugin('add', fixturesDir);
-                }).toThrow();
+        describe('remove (rm) hooks', function() {
+            it('should fire before hooks through the hooker module', function() {
+                cordova.plugin('rm', 'two');
+                expect(fire).toHaveBeenCalledWith('before_plugin_rm', {plugins:['two']}, jasmine.any(Function));
+            });
+            it('should fire after hooks through the hooker module', function() {
+                cordova.plugin('rm', 'one');
+                expect(fire).toHaveBeenCalledWith('after_plugin_rm', {plugins:['one']}, jasmine.any(Function));
+            });
+        });
+        describe('add hooks', function() {
+            it('should fire before and after hooks through the hooker module', function() {
+                cordova.plugin('add', 'android');
+                expect(fire).toHaveBeenCalledWith('before_plugin_add', {plugins:['android']}, jasmine.any(Function));
+                expect(fire).toHaveBeenCalledWith('after_plugin_add', {plugins:['android']}, jasmine.any(Function));
             });
         });
     });
 });
-

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/a5a861eb/src/plugin.js
----------------------------------------------------------------------
diff --git a/src/plugin.js b/src/plugin.js
index 8fbe355..38e0359 100644
--- a/src/plugin.js
+++ b/src/plugin.js
@@ -19,16 +19,12 @@
 var cordova_util  = require('./util'),
     util          = require('util'),
     fs            = require('fs'),
-    shell         = require('shelljs'),
     path          = require('path'),
     shell         = require('shelljs'),
     platforms     = require('../platforms'),
-    config_parser = require('./config_parser'),
     n             = require('ncallbacks'),
     hooker        = require('./hooker'),
     events        = require('./events'),
-    plugin_parser = require('./plugin_parser'),
-    ls            = fs.readdirSync,
     plugman       = require('plugman');
 
 module.exports = function plugin(command, targets, callback) {
@@ -43,10 +39,6 @@ module.exports = function plugin(command, targets, callback) {
     if (arguments.length === 0) command = 'ls';
 
     var hooks = new hooker(projectRoot);
-
-    // Grab config info for the project
-    var xml = cordova_util.projectConfig(projectRoot);
-    var cfg = new config_parser(xml);
     var platformList = cordova_util.listPlatforms(projectRoot);
 
     // Massage plugin name(s) / path(s)
@@ -148,7 +140,7 @@ module.exports = function plugin(command, targets, callback) {
                             var targetPath = path.join(pluginPath, target);
                             // Check if there is at least one match between plugin
                             // supported platforms and app platforms
-                            var pluginXml = new plugin_parser(path.join(targetPath, 'plugin.xml'));
+                            var pluginXml = new cordova_util.plugin_parser(path.join(targetPath, 'plugin.xml'));
                             var intersection = pluginXml.platforms.filter(function(e) {
                                 if (platformList.indexOf(e) == -1) return false;
                                 else return true;
@@ -162,7 +154,7 @@ module.exports = function plugin(command, targets, callback) {
                                 var platformRoot = path.join(projectRoot, 'platforms', platform);
                                 var parser = new platforms[platform].parser(platformRoot);
                                 events.emit('log', 'Calling plugman.uninstall on plugin "' + target + '" for platform "' + platform + '"');
-                                plugman.uninstall(platform, platformRoot, target, path.join(projectRoot, 'plugins'), { www_dir: parser.staging_dir() });
+                                plugman.uninstall((platform=='blackberry'?'blackberry10':platform), platformRoot, target, path.join(projectRoot, 'plugins'), { www_dir: parser.staging_dir() });
                             });
                             end();
                         } else {

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/a5a861eb/src/util.js
----------------------------------------------------------------------
diff --git a/src/util.js b/src/util.js
index 6749c8f..47fcbed 100644
--- a/src/util.js
+++ b/src/util.js
@@ -19,6 +19,7 @@
 var fs            = require('fs'),
     path          = require('path'),
     config_parser = require('./config_parser'),
+    plugin_parser = require('./plugin_parser'),
     shell         = require('shelljs');
 
 // Global configuration paths
@@ -54,6 +55,7 @@ module.exports = {
         } else return false;
     },
     config_parser:config_parser,
+    plugin_parser:plugin_parser,
     // Recursively deletes .svn folders from a target path
     deleteSvnFolders:function(dir) {
         var contents = fs.readdirSync(dir);