You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2013/07/28 02:56:11 UTC

[49/50] git commit: updating specs

updating specs


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

Branch: refs/heads/master
Commit: 582fdfa6e4dadf07f1b356696204088226316a7a
Parents: fd679f3
Author: Anis Kadri <an...@apache.org>
Authored: Sat Jul 27 17:46:27 2013 -0700
Committer: Anis Kadri <an...@apache.org>
Committed: Sat Jul 27 17:46:27 2013 -0700

----------------------------------------------------------------------
 plugman.js                          |   5 +-
 spec/config.js                      |  10 ---
 spec/config.spec.js                 |  11 +++
 spec/plugins/DummyPlugin/plugin.xml |   9 ++-
 spec/registry/registry.js           |  20 ------
 spec/registry/registry.spec.js      |  94 +++++++++++++++++++++++++
 src/registry/manifest.js            |  25 +++----
 src/registry/registry.js            | 117 +++++++++++++++----------------
 8 files changed, 187 insertions(+), 104 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/582fdfa6/plugman.js
----------------------------------------------------------------------
diff --git a/plugman.js b/plugman.js
index 716c5ef..b921f4d 100755
--- a/plugman.js
+++ b/plugman.js
@@ -27,7 +27,7 @@ plugman = {
     uninstall:          require('./src/uninstall'),
     fetch:              require('./src/fetch'),
     prepare:            require('./src/prepare'),
-    config:             require('./config'), 
+    config:             require('./src/config'), 
     adduser:            require('./src/adduser'),
     publish:            require('./src/publish'),
     unpublish:          require('./src/unpublish'),
@@ -40,6 +40,9 @@ plugman = {
 };
 
 plugman.commands =  {
+    'config'   : function(cli_opts) {
+        plugman.config(cli_opts.argv.remain);
+    },
     'install'  : function(cli_opts) {
         if(!cli_opts.platform || !cli_opts.project || !cli_opts.plugin) {
             return console.log(plugman.help());

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/582fdfa6/spec/config.js
----------------------------------------------------------------------
diff --git a/spec/config.js b/spec/config.js
deleted file mode 100644
index 5f4f781..0000000
--- a/spec/config.js
+++ /dev/null
@@ -1,10 +0,0 @@
-var config = require('../src/config'),
-    registry = require('../src/registry/registry');
-
-describe('config', function() {
-    it('should run config', function() {
-        var sConfig = spyOn(registry, 'config');
-        config(function(err, result) { });
-        expect(sConfig).toHaveBeenCalledWith(jasmine.any(Function));
-    });
-});

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/582fdfa6/spec/config.spec.js
----------------------------------------------------------------------
diff --git a/spec/config.spec.js b/spec/config.spec.js
new file mode 100644
index 0000000..fe54636
--- /dev/null
+++ b/spec/config.spec.js
@@ -0,0 +1,11 @@
+var config = require('../src/config'),
+    registry = require('../src/registry/registry');
+
+describe('config', function() {
+    it('should run config', function() {
+        var sConfig = spyOn(registry, 'config');
+        var params = ['set', 'registry', 'http://registry.cordova.io'];
+        config(params, function(err, result) { });
+        expect(sConfig).toHaveBeenCalledWith(params, jasmine.any(Function));
+    });
+});

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/582fdfa6/spec/plugins/DummyPlugin/plugin.xml
----------------------------------------------------------------------
diff --git a/spec/plugins/DummyPlugin/plugin.xml b/spec/plugins/DummyPlugin/plugin.xml
index deba345..162044a 100644
--- a/spec/plugins/DummyPlugin/plugin.xml
+++ b/spec/plugins/DummyPlugin/plugin.xml
@@ -23,7 +23,14 @@
     id="com.phonegap.plugins.dummyplugin"
     version="0.6.0">
 
-    <name>Dummy Plugin</name>
+    <!-- new requirement: NO SPACES -->
+    <name>dummyplugin</name>
+    <!-- These are going to be required by plugman-registry -->
+    <description>my description</description>
+    <author>Jackson Badman</author> 
+    <keywords>dummy,plugin</keywords>
+    <license>BSD</license>
+    <!-- end plugman-registry requirements -->
 
     <asset src="www/dummyplugin.js" target="dummyplugin.js" />
     <asset src="www/dummyplugin" target="dummyplugin" />

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/582fdfa6/spec/registry/registry.js
----------------------------------------------------------------------
diff --git a/spec/registry/registry.js b/spec/registry/registry.js
deleted file mode 100644
index d4baf59..0000000
--- a/spec/registry/registry.js
+++ /dev/null
@@ -1,20 +0,0 @@
-var registry = require('../../src/registry/registry'),
-    npm = require('npm');
-
-describe('registry', function() {
-    beforeEach(function() {
-        registry.settings = {
-            cache: plugmanCacheDir,
-            force: true,
-            logstream: fs.createWriteStream(path.resolve(plugmanConfigDir, 'plugman.log')),
-            userconfig: path.resolve(plugmanConfigDir, 'config')
-        };
-    });
-    it('should run config', function() {
-        var sLoad = spyOn(npm, 'load').andCallThrough(); 
-        var sConfig = spyOn(npm, 'config');
-        registry.config(new Array('/path/to/myplugin'));
-        expect(sLoad).toHaveBeenCalledWith(registry.settings, jasmine.any(Function));
-        expect(sPublish).toHaveBeenCalledWith(['/path/to/myplugin'], jasmine.any(Function));
-    });
-});

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/582fdfa6/spec/registry/registry.spec.js
----------------------------------------------------------------------
diff --git a/spec/registry/registry.spec.js b/spec/registry/registry.spec.js
new file mode 100644
index 0000000..8b26c1f
--- /dev/null
+++ b/spec/registry/registry.spec.js
@@ -0,0 +1,94 @@
+var registry = require('../../src/registry/registry'),
+    manifest = require('../../src/registry/manifest'),
+    fs = require('fs'),
+    path = require('path'),
+    npm = require('npm');
+
+describe('registry', function() {
+    describe('manifest', function() {
+        var pluginDir, packageJson;
+        beforeEach(function() {
+            pluginDir = __dirname + '/../plugins/DummyPlugin';
+            packageJson = path.resolve(pluginDir, 'package.json');
+        });
+        afterEach(function() {
+            fs.unlink(packageJson);
+            
+        });
+        it('should generate a package.json from a plugin.xml', function() {
+            manifest.generatePackageJsonFromPluginXml(pluginDir);
+            expect(fs.existsSync(packageJson));
+            expect(JSON.parse(fs.readFileSync(packageJson)).name).toEqual('dummyplugin');
+            expect(JSON.parse(fs.readFileSync(packageJson)).version).toEqual('0.6.0');
+        });
+    });
+    describe('actions', function() {
+        beforeEach(function() {
+            var fakeSettings = {
+                cache: '/some/cache/dir',
+                logstream: 'somelogstream@2313213',
+                userconfig: '/some/config/dir'
+            };
+            var fakeNPMCommands = {
+                config: function() {},
+                adduser: function() {},
+                publish: function() {},
+                unpublish: function() {},
+                search: function() {}
+            }
+            registry.settings = fakeSettings;
+            npm.commands = fakeNPMCommands;
+        });
+        it('should run config', function() {
+            var params = ['set', 'registry', 'http://registry.cordova.io'];
+            var sLoad = spyOn(npm, 'load').andCallFake(function(err, cb) {
+                cb();   
+            });
+            var sConfig = spyOn(npm.commands, 'config');
+            registry.config(params, function() {});
+            expect(sLoad).toHaveBeenCalledWith(registry.settings, jasmine.any(Function));
+            expect(sConfig).toHaveBeenCalledWith(params, jasmine.any(Function));
+        });
+        it('should run adduser', function() {
+            var sLoad = spyOn(npm, 'load').andCallFake(function(err, cb) {
+                cb();   
+            });
+            var sAddUser = spyOn(npm.commands, 'adduser');
+            registry.adduser(null, function() {});
+            expect(sLoad).toHaveBeenCalledWith(registry.settings, jasmine.any(Function));
+            expect(sAddUser).toHaveBeenCalledWith(null, jasmine.any(Function));
+        });
+        it('should run publish', function() {
+            var params = [__dirname + '/../plugins/DummyPlugin'];
+            var sLoad = spyOn(npm, 'load').andCallFake(function(err, cb) {
+                cb();
+            });
+            var sPublish = spyOn(npm.commands, 'publish');
+            var sGenerate = spyOn(manifest, 'generatePackageJsonFromPluginXml');
+            registry.publish(params, function() {});
+            expect(sLoad).toHaveBeenCalledWith(registry.settings, jasmine.any(Function));
+            expect(sGenerate).toHaveBeenCalledWith(params[0]);
+            expect(sPublish).toHaveBeenCalledWith(params, jasmine.any(Function));
+        });
+        it('should run unpublish', function() {
+            var params = ['dummyplugin@0.6.0'];
+            var sLoad = spyOn(npm, 'load').andCallFake(function(err, cb) {
+                cb();
+            });
+            var sUnpublish = spyOn(npm.commands, 'unpublish');
+            registry.unpublish(params, function() {});
+            expect(sLoad).toHaveBeenCalledWith(registry.settings, jasmine.any(Function));
+            expect(sUnpublish).toHaveBeenCalledWith(params, jasmine.any(Function));
+        });
+        it('should run search', function() {
+            var params = ['dummyplugin', 'plugin'];
+            var sLoad = spyOn(npm, 'load').andCallFake(function(err, cb) {
+                cb();
+            });
+            var sSearch = spyOn(npm.commands, 'search');
+            registry.search(params, function() {});
+            expect(sLoad).toHaveBeenCalledWith(registry.settings, jasmine.any(Function));
+            expect(sSearch).toHaveBeenCalledWith(params, true, jasmine.any(Function));
+        });
+    });
+});

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/582fdfa6/src/registry/manifest.js
----------------------------------------------------------------------
diff --git a/src/registry/manifest.js b/src/registry/manifest.js
index 59f7bb6..9a6e66c 100644
--- a/src/registry/manifest.js
+++ b/src/registry/manifest.js
@@ -2,8 +2,14 @@ var DOMParser = require('xmldom').DOMParser,
     path = require('path'),
     fs = require('fs');
 
+function handleError(err, cb) {
+    if(typeof cb == 'function') {
+        return cb(err);
+    }
+    throw err;
+}
+
 // Java world big-up!
-// TODO use CALLBACK
 function generatePackageJsonFromPluginXml(plugin_path, cb) {
   var package_json = {};
   var plugin_xml = fs.readFileSync(path.resolve(plugin_path, 'plugin.xml'), "utf8");
@@ -18,11 +24,7 @@ function generatePackageJsonFromPluginXml(plugin_path, cb) {
   var version = doc.documentElement.getAttribute('version')
   if(!version) {
     var e = new Error('`version` required');
-    if(cb) {
-      cb(e);
-      return null;
-    }
-    throw e;
+    return handleError(e, cb)
   }
   package_json.version = version;
 
@@ -30,14 +32,13 @@ function generatePackageJsonFromPluginXml(plugin_path, cb) {
   if(doc.documentElement.getElementsByTagName('name').length != 1 ||
      !doc.documentElement.getElementsByTagName('name').item(0).firstChild) {
     var e = new Error('`name` is required');
-    if(cb) {
-      cb(e);
-      return null;
-    }
-    throw e;
+    return handleError(e, cb)
   }
   var name = doc.documentElement.getElementsByTagName('name').item(0).firstChild.nodeValue;
-  if(!name.match(/^\w+|-*$/)) throw new Error('`name` can only contain alphanumberic characters and -')
+  if(!name.match(/^\w+|-*$/)) {
+      var e = new Error('`name` can only contain alphanumberic characters and -')
+      return handleError(e, cb);
+  }
   package_json.name = name.toLowerCase();
 
   // OPTIONAL fields: description, license, keywords. TODO: add more!

http://git-wip-us.apache.org/repos/asf/cordova-plugman/blob/582fdfa6/src/registry/registry.js
----------------------------------------------------------------------
diff --git a/src/registry/registry.js b/src/registry/registry.js
index bf99a15..a56f36a 100644
--- a/src/registry/registry.js
+++ b/src/registry/registry.js
@@ -9,57 +9,12 @@ var npm = require('npm'),
     plugmanConfigDir = path.resolve(process.env.HOME, '.plugman'),
     plugmanCacheDir = path.resolve(plugmanConfigDir, 'cache');
 
-var settings = module.exports.settings;
-
-function checkConfig(cb) {
-    if(settings != null) {
-        cb();
-    } else {
-        use(cb);
-    }
-}
-
 function handleError(err, cb) {
     if(typeof cb == 'function') {
         return cb(err);
     }
     throw err;
 }
-/**
- * @method use
- * @param {String} registry NPM registry URL 
- * @param {Function} cb callback
- */
-function use(cb) {
-    if(typeof cb != 'function') throw new Error('Please provide a callback');
-    // check if settings already set
-    if(settings != null) return cb();
-    
-    // setting up settings
-    // obviously if settings dir does not exist settings is going to be empty
-    if(!fs.existsSync(plugmanConfigDir)) {
-        fs.mkdirSync(plugmanConfigDir);
-        fs.mkdirSync(plugmanCacheDir);
-    }
-
-    settings = rc('plugman', {
-                 cache: plugmanCacheDir,
-                 force: true,
-                 logstream: fs.createWriteStream(path.resolve(plugmanConfigDir, 'plugman.log')),
-                 userconfig: path.resolve(plugmanConfigDir, 'config')
-              });
-    
-    if(settings.registry) {
-        cb();
-    } else {
-       // setting registry
-       npm.load(settings, function(er) {
-           if (er) return handlError(er);
-           npm.commands.config(['set', 'registry', 'http://registry.cordova.io'], cb);
-       });
-    }
-
-}
 
 /**
  * @method getPackageInfo
@@ -68,11 +23,13 @@ function use(cb) {
  */
 function getPackageInfo(args, cb) {
     var thing = args.length ? args.shift().split("@") : [],
-            name = thing.shift(),
-            version = thing.join("@"),
+                              name = thing.shift(),
+                              version = thing.join("@");
     
     version = version ? version : 'latest';
     
+    var settings = module.exports.settings;
+    
     http.get(settings.registry + '/' + name + '/' + version, function(res) {
          if(res.statusCode != 200) {
                  var err = new Error('Error');
@@ -98,6 +55,8 @@ function getPackageInfo(args, cb) {
  * @param {Function} cb callback 
  */
 function fetchPackage(info, cb) {
+    var settings = module.exports.settings;
+    
     var cached = path.resolve(settings.cache, info.name, info.version, 'package');
     if(fs.existsSync(cached)) {
         cb(null, cached);
@@ -130,10 +89,10 @@ module.exports = {
      * @param {Function} cb Command callback
      */
     config: function(args, cb) {
-        checkConfig(function(err) {
-            if(err) return handleError(err, cb)
+        initSettings(function(err, settings) {
+            if(err) return handleError(err, cb);
             npm.load(settings, function(er) {
-                if (er) return handlError(er);
+                if (er) return handleError(er);
                 npm.commands.config(args, cb);
             });
         });
@@ -144,10 +103,10 @@ module.exports = {
      * @param {Function} cb Command callback
      */
     adduser: function(args, cb) {
-        checkConfig(function(err) {
-            if(err) return handleError(err, cb)
+        initSettings(function(err, settings) {
+            if(err) return handleError(err, cb);
             npm.load(settings, function(er) {
-                if (er) return handlError(er);
+                if (er) return handleError(er);
                 npm.commands.adduser(args, cb);
             });
         });
@@ -158,11 +117,11 @@ module.exports = {
      * @param {Function} cb Command callback
      */
     publish: function(args, cb) {
-        checkConfig(function(err) {
+        initSettings(function(err, settings) {
             if(err) return handleError(err, cb);
             manifest.generatePackageJsonFromPluginXml(args[0]);
             npm.load(settings, function(er) {
-                if (er) return handlError(er);
+                if (er) return handleError(er);
                 npm.commands.publish(args, function(err, data) {
                     fs.unlink(path.resolve(args[0], 'package.json'));
                     cb(err, data);
@@ -176,10 +135,10 @@ module.exports = {
      * @param {Function} cb Command callback
      */
     search: function(args, cb) {
-        checkConfig(function(err) {
+        initSettings(function(err, settings) {
             if(err) return handleError(err, cb);
             npm.load(settings, function(er) {
-                if (er) return handlError(er, cb);
+                if (er) return handleError(er, cb);
                 npm.commands.search(args, true, cb);
             });
         });
@@ -190,7 +149,7 @@ module.exports = {
      * @param {Function} cb Command callback
      */
     unpublish: function(args, cb) {
-        checkConfig(function(err) {
+        initSettings(function(err, settings) {
             if(err) return handleError(err, cb);
             npm.load(settings, function(er) {
                 if (er) return handlError(er);
@@ -207,8 +166,8 @@ module.exports = {
      * @param {Function} cb Command callback
      */
     fetch: function(args, cb) {
-        checkConfig(function(err) {
-            if(err) return handleError(err, cb)
+        initSettings(function(err, settings) {
+            if(err) return handleError(err, cb);
             getPackageInfo(args, function(err, info) {
                 if(err) return handleError(err, cb)
                 fetchPackage(info, cb);
@@ -216,3 +175,41 @@ module.exports = {
         });
     }
 }
+
+/**
+ * @method initSettings
+ * @param {Function} cb callback
+ */
+function initSettings(cb) {
+    var settings = module.exports.settings;
+    if(typeof cb != 'function') throw new Error('Please provide a callback');
+    // check if settings already set
+    if(settings != null) return cb(null, settings);
+    
+    // setting up settings
+    // obviously if settings dir does not exist settings is going to be empty
+    if(!fs.existsSync(plugmanConfigDir)) {
+        fs.mkdirSync(plugmanConfigDir);
+        fs.mkdirSync(plugmanCacheDir);
+    }
+
+    settings = rc('plugman', {
+                 cache: plugmanCacheDir,
+                 force: true,
+                 logstream: fs.createWriteStream(path.resolve(plugmanConfigDir, 'plugman.log')),
+                 userconfig: path.resolve(plugmanConfigDir, 'config')
+              });
+    
+    if(settings.registry) {
+        cb(null, settings);
+    } else {
+       // setting registry
+       npm.load(settings, function(er) {
+           if (er) return handlError(er);
+           npm.commands.config(['set', 'registry', 'http://registry.cordova.io'], function(err) {
+                cb(err, settings);
+           });
+       });
+    }
+
+}