fixed up platform specs. also platform add now calls a prepare for that platform.
Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/f2857158 Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/f2857158 Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/f2857158 Branch: refs/heads/master2 Commit: f28571584dca351e70c65adae419ebe04ba419c0 Parents: 85be90a Author: Fil Maj <[email protected]> Authored: Wed Jun 12 22:15:09 2013 -0700 Committer: Fil Maj <[email protected]> Committed: Thu Jun 13 11:13:22 2013 -0700 ---------------------------------------------------------------------- spec/platform.spec.js | 270 +++++++++++++++++++-------------------------- src/platform.js | 37 +++---- 2 files changed, 130 insertions(+), 177 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/f2857158/spec/platform.spec.js ---------------------------------------------------------------------- diff --git a/spec/platform.spec.js b/spec/platform.spec.js index fed1f3b..132c308 100644 --- a/spec/platform.spec.js +++ b/spec/platform.spec.js @@ -16,210 +16,173 @@ 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'), - util = require('../../src/util'), - hooker = require('../../src/hooker'), - platform = require('../../src/platform'), - platforms = require('../../platforms'), - tempDir = path.join(__dirname, '..', '..', 'temp'); - android_parser = require('../../src/metadata/android_parser'); + util = require('../src/util'), + config = require('../src/config'), + hooker = require('../src/hooker'), + lazy_load = require('../src/lazy_load'), + platform = require('../src/platform'), + platforms = require('../platforms'); var cwd = process.cwd(); +var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www'; }); describe('platform command', function() { + var is_cordova, list_platforms, fire, config_parser, find_plugins, config_read, load_custom, load_cordova, rm, mkdir, existsSync, supports, pkg, name, exec, prep_spy, plugman_install; + var project_dir = '/some/path'; 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); + }); + name = jasmine.createSpy('config name').andReturn('magical mystery tour'); + pkg = jasmine.createSpy('config packageName').andReturn('ca.filmaj.id'); + config_parser = spyOn(util, 'config_parser').andReturn({ + packageName:pkg, + name:name + }); + find_plugins = spyOn(util, 'findPlugins').andReturn([]); + list_platforms = spyOn(util, 'listPlatforms').andReturn(supported_platforms); + config_read = spyOn(config, 'read').andReturn({}); + load_custom = spyOn(lazy_load, 'custom').andCallFake(function(uri, id, platform, version, cb) { + cb(); + }); + load_cordova = spyOn(lazy_load, 'cordova').andCallFake(function(platform, cb) { + cb(); + }); + rm = spyOn(shell, 'rm'); + mkdir = spyOn(shell, 'mkdir'); + existsSync = spyOn(fs, 'existsSync').andReturn(false); + supports = spyOn(platform, 'supports').andCallFake(function(name, cb) { + cb(); + }); + 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'); }); - it('should run inside a Cordova-based project', function() { - this.after(function() { - process.chdir(cwd); - }); - - cordova.create(tempDir); - - process.chdir(tempDir); - - expect(function() { - cordova.platform(); - }).not.toThrow(); - }); - it('should not run outside of a Cordova-based project', function() { - this.after(function() { - process.chdir(cwd); - }); - - process.chdir(tempDir); - - expect(function() { - cordova.platform(); - }).toThrow(); - }); - - describe('`ls`', function() { - beforeEach(function() { - cordova.create(tempDir); - process.chdir(tempDir); - }); - afterEach(function() { - process.chdir(cwd); - cordova.removeAllListeners('results'); // clean up event listener - }); - - it('should list out no platforms for a fresh project', function(done) { - shell.rm('-rf', path.join(tempDir, 'platforms', '*')); - cordova.on('results', function(res) { - expect(res).toEqual('No platforms added. Use `cordova platform add <platform>`.'); - done(); - }); - cordova.platform('list'); - }); - - it('should list out added platforms in a project', function(done) { - var platforms = path.join(tempDir, 'platforms'); - shell.mkdir(path.join(platforms, 'android')); - shell.mkdir(path.join(platforms, 'ios')); - - cordova.on('results', function(res) { - expect(res.length).toEqual(2); - done(); - }); - cordova.platform('list'); + 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.platform(); + expect(is_cordova).toHaveBeenCalled(); + }).toThrow('Current working directory is not a Cordova-based project.'); }); }); - describe('`add`', function() { - beforeEach(function() { - cordova.create(tempDir); - process.chdir(tempDir); - }); - - afterEach(function() { - process.chdir(cwd); + describe('success', function() { + it('should run inside a Cordova-based project by calling util.isCordova', function() { + cordova.platform(); + expect(is_cordova).toHaveBeenCalled(); }); - it('should handle multiple platforms and shell out to specified platform\'s bin/create', function() { - spyOn(platform, 'supports').andCallFake(function(target, callback) { - callback(null); + describe('`ls`', function() { + afterEach(function() { + cordova.removeAllListeners('results'); }); - var sh = spyOn(shell, 'exec'); - cordova.platform('add', ['foo', 'bar']); - var foo_create = path.join('foo', 'bin', 'create'); - var bar_create = path.join('bar', 'bin', 'create'); - expect(sh.argsForCall[0][0]).toContain(foo_create); - expect(sh.argsForCall[1][0]).toContain(bar_create); - }); - }); - - describe('`remove`',function() { - beforeEach(function() { - cordova.create(tempDir); - process.chdir(tempDir); - }); - - afterEach(function() { - process.chdir(cwd); - cordova.removeAllListeners('results'); - }); - - it('should remove a supported and added platform', function(done) { - shell.mkdir(path.join(tempDir, 'platforms', 'android')); - shell.mkdir(path.join(tempDir, 'platforms', 'ios')); - cordova.platform('remove', 'android', function() { + it('should list out no platforms for a fresh project', function(done) { + list_platforms.andReturn([]); cordova.on('results', function(res) { - expect(res.length).toEqual(1); + expect(res).toEqual('No platforms added. Use `cordova platform add <platform>`.'); done(); }); cordova.platform('list'); }); - }); - it('should be able to remove multiple platforms', function(done) { - shell.mkdir(path.join(tempDir, 'platforms', 'android')); - shell.mkdir(path.join(tempDir, 'platforms', 'blackberry')); - shell.mkdir(path.join(tempDir, 'platforms', 'ios')); - cordova.platform('remove', ['android','blackberry'], function() { + it('should list out added platforms in a project', function(done) { cordova.on('results', function(res) { - expect(res.length).toEqual(1); + expect(res.length).toEqual(5); done(); }); cordova.platform('list'); }); }); - }); - - describe('hooks', function() { - var s; - beforeEach(function() { - cordova.create(tempDir); - process.chdir(tempDir); - s = spyOn(hooker.prototype, 'fire').andCallFake(function(hook, opts, cb) { - if (cb) cb(); - else opts(); + describe('`add`', function() { + it('should shell out to specified platform\'s bin/create', function() { + cordova.platform('add', 'android'); + expect(exec.mostRecentCall.args[0]).toMatch(/lib.android.cordova.......bin.create/gi); + expect(exec.mostRecentCall.args[0]).toContain(project_dir); + }); + it('should support using custom versions of libraries by calling into lazy_load.custom', function() { + config_read.andReturn({ + lib:{ + 'wp7':{ + uri:'haha', + id:'phonegap', + version:'bleeding edge' + } + } + }); + cordova.platform('add', 'wp7'); + expect(load_custom).toHaveBeenCalledWith('haha', 'phonegap', 'wp7', 'bleeding edge', jasmine.any(Function)); + expect(exec.mostRecentCall.args[0]).toMatch(/lib.wp7.phonegap.bleeding edge.bin.create/gi); + expect(exec.mostRecentCall.args[0]).toContain(project_dir); }); }); - afterEach(function() { - process.chdir(cwd); - shell.rm('-rf', tempDir); - }); + describe('`remove`',function() { + it('should remove a supported and added platform', function() { + cordova.platform('remove', 'android'); + expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'platforms', 'android')); + expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'merges', 'android')); + }); + it('should be able to remove multiple platforms', function() { + cordova.platform('remove', ['android', 'blackberry']); + expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'platforms', 'android')); + expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'merges', 'android')); + expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'platforms', 'blackberry')); + expect(rm).toHaveBeenCalledWith('-rf', path.join(project_dir, 'merges', 'blackberry')); + }); + }); + }); + describe('hooks', function() { describe('list (ls) hooks', function() { it('should fire before hooks through the hooker module', function() { cordova.platform(); - expect(s).toHaveBeenCalledWith('before_platform_ls', jasmine.any(Function)); + expect(fire).toHaveBeenCalledWith('before_platform_ls', jasmine.any(Function)); }); it('should fire after hooks through the hooker module', function() { cordova.platform(); - expect(s).toHaveBeenCalledWith('after_platform_ls', jasmine.any(Function)); + expect(fire).toHaveBeenCalledWith('after_platform_ls', jasmine.any(Function)); }); }); describe('remove (rm) hooks', function() { it('should fire before hooks through the hooker module', function() { cordova.platform('rm', 'android'); - expect(s).toHaveBeenCalledWith('before_platform_rm', {platforms:['android']}, jasmine.any(Function)); + expect(fire).toHaveBeenCalledWith('before_platform_rm', {platforms:['android']}, jasmine.any(Function)); }); it('should fire after hooks through the hooker module', function() { cordova.platform('rm', 'android'); - expect(s).toHaveBeenCalledWith('after_platform_rm', {platforms:['android']}, jasmine.any(Function)); + expect(fire).toHaveBeenCalledWith('after_platform_rm', {platforms:['android']}, jasmine.any(Function)); }); }); describe('add hooks', function() { - var sh, cr; - beforeEach(function() { - sh = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) { - var a_path = path.join(tempDir, 'platforms','android'); - shell.mkdir('-p',a_path); - fs.writeFileSync(path.join(a_path, 'AndroidManifest.xml'), 'hi', 'utf-8'); - cb(0, 'mkay'); - }); - cr = spyOn(android_parser.prototype, 'update_project').andCallFake(function(cfg, cb) { - cb(); - }); - spyOn(platform, 'supports').andCallFake(function (t, cb) { - cb(); - }); - }); it('should fire before and after hooks through the hooker module', function() { cordova.platform('add', 'android'); - expect(s).toHaveBeenCalledWith('before_platform_add', {platforms:['android']}, jasmine.any(Function)); - expect(s).toHaveBeenCalledWith('after_platform_add', {platforms:['android']}, jasmine.any(Function)); + expect(fire).toHaveBeenCalledWith('before_platform_add', {platforms:['android']}, jasmine.any(Function)); + expect(fire).toHaveBeenCalledWith('after_platform_add', {platforms:['android']}, jasmine.any(Function)); }); }); }); }); describe('platform.supports(name, callback)', function() { - var androidParser = require('../../src/metadata/android_parser'); - + var supports = {}; beforeEach(function() { - spyOn(androidParser, 'check_requirements'); + supported_platforms.forEach(function(p) { + supports[p] = spyOn(platforms[p].parser, 'check_requirements').andCallFake(function(cb) { cb(); }); + }); }); - it('should require a platform name', function() { expect(function() { cordova.platform.supports(undefined, function(e){}); @@ -242,12 +205,6 @@ describe('platform.supports(name, callback)', function() { }); describe('when platform is supported', function() { - beforeEach(function() { - androidParser.check_requirements.andCallFake(function(callback) { - callback(null); - }); - }); - it('should trigger callback without error', function(done) { cordova.platform.supports('android', function(e) { expect(e).toBeNull(); @@ -257,13 +214,10 @@ describe('platform.supports(name, callback)', function() { }); describe('when platform is unsupported', function() { - beforeEach(function() { - androidParser.check_requirements.andCallFake(function(callback) { - callback(new Error('could not find the android sdk')); - }); - }); - it('should trigger callback with error', function(done) { + supported_platforms.forEach(function(p) { + supports[p].andCallFake(function(cb) { cb(new Error('no sdk')); }); + }); cordova.platform.supports('android', function(e) { expect(e).toEqual(jasmine.any(Error)); done(); http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/f2857158/src/platform.js ---------------------------------------------------------------------- diff --git a/src/platform.js b/src/platform.js index 58be9af..da783fb 100644 --- a/src/platform.js +++ b/src/platform.js @@ -16,8 +16,7 @@ specific language governing permissions and limitations under the License. */ -var config_parser = require('./config_parser'), - config = require('./config'), +var config = require('./config'), cordova_util = require('./util'), util = require('util'), fs = require('fs'), @@ -56,7 +55,7 @@ module.exports = function platform(command, targets, callback) { } var xml = cordova_util.projectConfig(projectRoot); - var cfg = new config_parser(xml); + var cfg = new cordova_util.config_parser(xml); var opts = { platforms:targets }; @@ -64,7 +63,7 @@ module.exports = function platform(command, targets, callback) { switch(command) { case 'ls': case 'list': - var platforms_on_fs = fs.readdirSync(path.join(projectRoot, 'platforms')); + var platforms_on_fs = cordova_util.listPlatforms(projectRoot); hooks.fire('before_platform_ls', function(err) { if (err) { if (callback) callback(err); @@ -105,7 +104,7 @@ module.exports = function platform(command, targets, callback) { if (callback) callback(err); else throw err; } else { - call_into_create(t, projectRoot, true /* is_custom */, cfg, config_json.lib[t].id, config_json.lib[t].version, callback, end); + call_into_create(t, projectRoot, cfg, config_json.lib[t].id, config_json.lib[t].version, callback, end); } }); } else { @@ -115,7 +114,7 @@ module.exports = function platform(command, targets, callback) { if (callback) callback(err); else throw err; } else { - call_into_create(t, projectRoot, false /* is_custom */, cfg, 'cordova', cordova_util.cordovaTag, callback, end); + call_into_create(t, projectRoot, cfg, 'cordova', cordova_util.cordovaTag, callback, end); } }); } @@ -201,7 +200,7 @@ function createOverrides(projectRoot, target) { shell.mkdir('-p', path.join(cordova_util.appDir(projectRoot), 'merges', target)); }; -function call_into_create(target, projectRoot, is_custom, cfg, id, version, callback, end) { +function call_into_create(target, projectRoot, cfg, id, version, callback, end) { var output = path.join(projectRoot, 'platforms', target); // Check if output directory already exists. @@ -233,20 +232,20 @@ function call_into_create(target, projectRoot, is_custom, cfg, id, version, call if (callback) callback(err); else throw err; } else { - var parser = new platforms[target].parser(output); - events.emit('log', 'Updating ' + target + ' project from config.xml...'); - parser.update_project(cfg, function() { - createOverrides(projectRoot, target); - end(); //platform add is done by now. - // Install all currently installed plugins into this new platform. - var pluginsDir = path.join(projectRoot, 'plugins'); - var plugins = fs.readdirSync(pluginsDir); - plugins && plugins.forEach(function(plugin) { - if (fs.statSync(path.join(projectRoot, 'plugins', plugin)).isDirectory()) { + require('../cordova').prepare(target, function(err) { + if (err) { + if (callback) callback(err); + else throw err; + } else { + createOverrides(projectRoot, target); + end(); //platform add is done by now. + // Install all currently installed plugins into this new platform. + var plugins = cordova_util.findPlugins(projectRoot); + plugins && plugins.forEach(function(plugin) { events.emit('log', 'Installing plugin "' + plugin + '" following successful platform add of ' + target); plugman.install(target, output, path.basename(plugin), pluginsDir, { www_dir: parser.staging_dir() }); - } - }); + }); + } }); } });
