tweaked shell-out commands to have access to callback for firing an error. fixed up emulate 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/20d8b8a5 Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/20d8b8a5 Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/20d8b8a5 Branch: refs/heads/master2 Commit: 20d8b8a5a47c3a3faca1f92409f958c2cf5c396e Parents: 4b8118c Author: Fil Maj <[email protected]> Authored: Wed Jun 12 22:57:06 2013 -0700 Committer: Fil Maj <[email protected]> Committed: Thu Jun 13 11:13:22 2013 -0700 ---------------------------------------------------------------------- spec/emulate.spec.js | 116 +++++++++++++++++++++------------------------- src/compile.js | 10 ++-- src/emulate.js | 18 +++---- 3 files changed, 68 insertions(+), 76 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/20d8b8a5/spec/emulate.spec.js ---------------------------------------------------------------------- diff --git a/spec/emulate.spec.js b/spec/emulate.spec.js index 4f8e14b..d9f505f 100644 --- a/spec/emulate.spec.js +++ b/spec/emulate.spec.js @@ -16,98 +16,87 @@ specific language governing permissions and limitations under the License. */ -var cordova = require('../../cordova'), - et = require('elementtree'), +var cordova = require('../cordova'), + platforms = require('../platforms'), shell = require('shelljs'), path = require('path'), fs = require('fs'), - hooker = require('../../src/hooker'), - tempDir = path.join(__dirname, '..', '..', 'temp'); + hooker = require('../src/hooker'), + util = require('../src/util'); -var cwd = process.cwd(); +var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www'; }); describe('emulate command', function() { + var is_cordova, list_platforms, fire, exec; + var project_dir = '/some/path'; + var prepare_spy; beforeEach(function() { - shell.rm('-rf', tempDir); - cordova.create(tempDir); + is_cordova = spyOn(util, 'isCordova').andReturn(project_dir); + list_platforms = spyOn(util, 'listPlatforms').andReturn(supported_platforms); + fire = spyOn(hooker.prototype, 'fire').andCallFake(function(e, opts, cb) { + cb(false); + }); + prepare_spy = spyOn(cordova, 'prepare').andCallFake(function(platforms, cb) { + cb(); + }); + exec = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) { cb(0, ''); }); }); - describe('failure', function() { - afterEach(function() { - process.chdir(cwd); - }); - it('should not run inside a Cordova-based project with no added platforms', function() { - process.chdir(tempDir); + it('should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function() { + list_platforms.andReturn([]); expect(function() { cordova.emulate(); - }).toThrow(); + }).toThrow('No platforms added to this project. Please use `cordova platform add <platform>`.'); }); it('should not run outside of a Cordova-based project', function() { - shell.mkdir('-p', tempDir); - process.chdir(tempDir); - + is_cordova.andReturn(false); expect(function() { cordova.emulate(); - }).toThrow(); + }).toThrow('Current working directory is not a Cordova-based project.'); + }); + it('should throw if no BlackBerry simulator targets exist and blackberry is to be emulated', function() { + var bb_project = path.join(project_dir, 'platforms', 'blackberry'); + spyOn(platforms.blackberry, 'parser').andReturn({ + has_simulator_target:function() { return false; } + }); + expect(function() { + cordova.emulate('blackberry'); + }).toThrow('No BlackBerry simulator targets defined. If you want to run emulate with BB10, please add a simulator target. For more information run "' + path.join(bb_project, 'cordova', 'target') + '" -h'); }); }); describe('success', function() { - beforeEach(function() { - process.chdir(tempDir); - spyOn(cordova, 'prepare').andCallFake(function(ps, cb) { - cb(); + it('should run inside a Cordova-based project with at least one added platform and call prepare and shell out to the emulate script', function(done) { + cordova.emulate(['android','ios'], function(err) { + expect(prepare_spy).toHaveBeenCalledWith(['android', 'ios'], jasmine.any(Function)); + expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'android', 'cordova', 'run') + '" --emulator', jasmine.any(Object), jasmine.any(Function)); + expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'ios', 'cordova', 'run') + '" --emulator', jasmine.any(Object), jasmine.any(Function)); + done(); }); }); - afterEach(function() { - process.chdir(cwd); - }); - it('should run inside a Cordova-based project with at least one added platform', function(done) { - var s = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) { - cb(0, 'yokay'); - }); - cordova.emulate(['android', 'beer'], function(err) { - expect(s).toHaveBeenCalled(); - expect(s.mostRecentCall.args[0]).toMatch(/cordova.run" --emulator$/gi); - done(); + it('should execute a different BlackBerry-specific command to emulate blackberry', function() { + var bb_project = path.join(project_dir, 'platforms', 'blackberry'); + spyOn(platforms.blackberry, 'parser').andReturn({ + has_simulator_target:function() { return true; }, + get_simulator_targets:function() { return [{name:'fifi'}]; }, + get_cordova_config:function() { return {signing_password:'secret'}; } }); + expect(function() { + cordova.emulate('blackberry'); + expect(exec.mostRecentCall.args[0]).toMatch(/blackberry.cordova.run" --target=fifi -k secret/gi); + }).not.toThrow(); }); }); describe('hooks', function() { - var hook_spy; - var shell_spy; - var prepare_spy; - beforeEach(function() { - hook_spy = spyOn(hooker.prototype, 'fire').andCallFake(function(hook, opts, cb) { - if (cb) cb(); - else opts(); - }); - prepare_spy = spyOn(cordova, 'prepare').andCallFake(function(ps, cb) { - cb(); - }); - shell_spy = spyOn(shell, 'exec').andCallFake(function(cmd, opts, cb) { - cb(0, 'yup'); // fake out shell so system thinks every shell-out is successful - }); - process.chdir(tempDir); - }); - afterEach(function() { - hook_spy.reset(); - prepare_spy.reset(); - shell_spy.reset(); - process.chdir(cwd); - }); - describe('when platforms are added', function() { - it('should fire before hooks through the hooker module', function(done) { - cordova.emulate(['android', 'ios'], function(err) { - expect(hook_spy).toHaveBeenCalledWith('before_emulate', {platforms:['android', 'ios']}, jasmine.any(Function)); - done(); - }); + it('should fire before hooks through the hooker module', function() { + cordova.emulate(['android', 'ios']); + expect(fire).toHaveBeenCalledWith('before_emulate', {platforms:['android', 'ios']}, jasmine.any(Function)); }); it('should fire after hooks through the hooker module', function(done) { cordova.emulate('android', function() { - expect(hook_spy).toHaveBeenCalledWith('after_emulate', {platforms:['android']}, jasmine.any(Function)); + expect(fire).toHaveBeenCalledWith('after_emulate', {platforms:['android']}, jasmine.any(Function)); done(); }); }); @@ -115,10 +104,11 @@ describe('emulate command', function() { describe('with no platforms added', function() { it('should not fire the hooker', function() { + list_platforms.andReturn([]); expect(function() { cordova.emulate(); }).toThrow(); - expect(hook_spy).not.toHaveBeenCalled(); + expect(fire).not.toHaveBeenCalled(); }); }); }); http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/20d8b8a5/src/compile.js ---------------------------------------------------------------------- diff --git a/src/compile.js b/src/compile.js index d2608cd..a99f109 100644 --- a/src/compile.js +++ b/src/compile.js @@ -25,16 +25,18 @@ var cordova_util = require('./util'), events = require('./events'), n = require('ncallbacks'); -function shell_out_to_build(projectRoot, platform, callback) { +function shell_out_to_build(projectRoot, platform, error_callback, done) { var cmd = '"' + path.join(projectRoot, 'platforms', platform, 'cordova', 'build') + '"'; events.emit('log', 'Compiling platform "' + platform + '" with command "' + cmd + '" (output to follow)...'); shell.exec(cmd, {silent:true, async:true}, function(code, output) { events.emit('log', output); if (code > 0) { - throw new Error('An error occurred while building the ' + platform + ' project. ' + output); + var err = new Error('An error occurred while building the ' + platform + ' project. ' + output); + if (error_callback) error_callback(err); + else throw err; } else { events.emit('log', 'Platform "' + platform + '" compiled successfully.'); - if (callback) callback(); + if (done) done(); } }); } @@ -87,7 +89,7 @@ module.exports = function compile(platformList, callback) { // Iterate over each added platform platformList.forEach(function(platform) { try { - shell_out_to_build(projectRoot, platform, end); + shell_out_to_build(projectRoot, platform, callback, end); } catch(e) { if (callback) callback(e); else throw e; http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/20d8b8a5/src/emulate.js ---------------------------------------------------------------------- diff --git a/src/emulate.js b/src/emulate.js index 5553fed..d41d01c 100644 --- a/src/emulate.js +++ b/src/emulate.js @@ -19,16 +19,15 @@ var cordova_util = require('./util'), path = require('path'), shell = require('shelljs'), - config_parser = require('./config_parser'), platforms = require('../platforms'), platform = require('./platform'), events = require('./events'), fs = require('fs'), n = require('ncallbacks'), - hooker = require('../src/hooker'), + hooker = require('./hooker'), util = require('util'); -function shell_out_to_emulate(root, platform, done) { +function shell_out_to_emulate(root, platform, error_callback, done) { var cmd = '"' + path.join(root, 'platforms', platform, 'cordova', 'run') + '" --emulator'; // TODO: inconsistent API for BB10 run command if (platform == 'blackberry') { @@ -39,14 +38,18 @@ function shell_out_to_emulate(root, platform, done) { var sim = project.get_simulator_targets()[0].name; cmd = '"' + path.join(bb_project, 'cordova', 'run') + '" --target=' + sim + ' -k ' + bb_config.signing_password; } else { - throw new Error('No BlackBerry simulator targets defined. If you want to run emulate with BB10, please add a simulator target. For more information run "' + path.join(bb_project, 'cordova', 'target') + '" -h'); + var err = new Error('No BlackBerry simulator targets defined. If you want to run emulate with BB10, please add a simulator target. For more information run "' + path.join(bb_project, 'cordova', 'target') + '" -h'); + if (error_callback) return error_callback(err); + else throw err; } } events.emit('log', 'Running on emulator for platform "' + platform + '" via command "' + cmd + '" (output to follow)...'); shell.exec(cmd, {silent:true, async:true}, function(code, output) { events.emit('log', output); if (code > 0) { - throw new Error('An error occurred while emulating/deploying the ' + platform + ' project.' + output); + var err = new Error('An error occurred while emulating/deploying the ' + platform + ' project.' + output); + if (error_callback) return error_callback(err); + else throw err; } else { events.emit('log', 'Platform "' + platform + '" deployed to emulator.'); done(); @@ -64,9 +67,6 @@ module.exports = function emulate (platformList, callback) { return; } - var xml = cordova_util.projectConfig(projectRoot); - var cfg = new config_parser(xml); - if (arguments.length === 0 || (platformList instanceof Array && platformList.length === 0)) { platformList = cordova_util.listPlatforms(projectRoot); } else if (typeof platformList == 'string') platformList = [platformList]; @@ -110,7 +110,7 @@ module.exports = function emulate (platformList, callback) { } else { platformList.forEach(function(platform) { try { - shell_out_to_emulate(projectRoot, platform, end); + shell_out_to_emulate(projectRoot, platform, callback, end); } catch(e) { if (callback) callback(e); else throw e;
