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/01/31 23:03:45 UTC

[9/10] git commit: tests redone for build

tests redone for build


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

Branch: refs/heads/master
Commit: 2cfdf55f2791045a69d1a8e240c3a6b1fc66be07
Parents: f38040c
Author: Fil Maj <ma...@gmail.com>
Authored: Thu Jan 31 13:28:05 2013 -0800
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu Jan 31 13:28:05 2013 -0800

----------------------------------------------------------------------
 spec/build.spec.js |  203 +++++++++++-----------------------------------
 src/build.js       |   31 ++++----
 2 files changed, 65 insertions(+), 169 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2cfdf55f/spec/build.spec.js
----------------------------------------------------------------------
diff --git a/spec/build.spec.js b/spec/build.spec.js
index 7aedec2..26e1749 100644
--- a/spec/build.spec.js
+++ b/spec/build.spec.js
@@ -32,11 +32,11 @@ var cordova = require('../cordova'),
     cordova_project = path.join(fixtures, 'projects', 'cordova');
 
 var cwd = process.cwd();
-shell.rm('-rf', tempDir);
 
-xdescribe('build command', function() {
-    afterEach(function() {
+describe('build command', function() {
+    beforeEach(function() {
         shell.rm('-rf', tempDir);
+        shell.mkdir('-p', tempDir);
     });
 
     it('should not run inside a Cordova-based project with no added platforms', function() {
@@ -52,8 +52,12 @@ xdescribe('build command', function() {
     });
     
     it('should run inside a Cordova-based project with at least one added platform', function() {
+        shell.mv('-f', path.join(cordova_project, 'platforms', 'blackberry'), path.join(tempDir));
+        shell.mv('-f', path.join(cordova_project, 'platforms', 'ios'), path.join(tempDir));
         this.after(function() {
             process.chdir(cwd);
+            shell.mv('-f', path.join(tempDir, 'blackberry'), path.join(cordova_project, 'platforms', 'blackberry'));
+            shell.mv('-f', path.join(tempDir, 'ios'), path.join(cordova_project, 'platforms', 'ios'));
         });
 
         process.chdir(cordova_project);
@@ -77,10 +81,9 @@ xdescribe('build command', function() {
             cordova.build();
         }).toThrow();
     });
-    xdescribe('per platform', function() {
+    describe('per platform', function() {
         beforeEach(function() {
-            cordova.create(tempDir);
-            process.chdir(tempDir);
+            process.chdir(cordova_project);
         });
 
         afterEach(function() {
@@ -88,184 +91,68 @@ xdescribe('build command', function() {
         });
        
         describe('Android', function() {
-            beforeEach(function() {
-                cordova.platform('add', 'android');
-            });
-
             it('should shell out to build command on Android', function() {
                 var s = spyOn(require('shelljs'), 'exec').andReturn({code:0});
-                cordova.build();
+                cordova.build('android');
                 expect(s.mostRecentCall.args[0].match(/\/cordova\/build/)).not.toBeNull();
             });
             it('should call android_parser\'s update_project', function() {
                 spyOn(require('shelljs'), 'exec').andReturn({code:0});
                 var s = spyOn(android_parser.prototype, 'update_project');
-                cordova.build();
+                cordova.build('android');
                 expect(s).toHaveBeenCalled();
             });
         });
         describe('iOS', function() {
             it('should shell out to build command on iOS', function() {
-                var cb = jasmine.createSpy();
-                var buildcb = jasmine.createSpy();
-                var s;
-
-                runs(function() {
-                    cordova.platform('add', 'ios', cb);
-                });
-                waitsFor(function() { return cb.wasCalled; }, 'platform add ios callback');
-                runs(function() {
-                    s = spyOn(require('shelljs'), 'exec').andReturn({code:0});
-                    cordova.build(buildcb);
-                });
-                waitsFor(function() { return buildcb.wasCalled; }, 'ios build');
-                runs(function() {
-                    expect(s).toHaveBeenCalled();
-                    expect(s.mostRecentCall.args[0].match(/\/cordova\/build/)).not.toBeNull();
-                });
+                var s = spyOn(require('shelljs'), 'exec');
+                var proj_spy = spyOn(ios_parser.prototype, 'update_project');
+                cordova.build('ios');
+                proj_spy.mostRecentCall.args[1]();
+                expect(s).toHaveBeenCalled();
+                expect(s.mostRecentCall.args[0].match(/\/cordova\/build/)).not.toBeNull();
             });
             it('should call ios_parser\'s update_project', function() {
-                var s;
-                var cb = jasmine.createSpy();
-                runs(function() {
-                    cordova.platform('add', 'ios', cb);
-                });
-                waitsFor(function() { return cb.wasCalled; }, 'ios add platform');
-                runs(function() {
-                    s = spyOn(ios_parser.prototype, 'update_project');
-                    cordova.build();
-                    expect(s).toHaveBeenCalled();
-                });
+                var s = spyOn(ios_parser.prototype, 'update_project');
+                cordova.build('ios');
+                expect(s).toHaveBeenCalled();
             });
         });
         describe('BlackBerry', function() {
             it('should shell out to ant command on blackberry', function() {
-                var buildcb = jasmine.createSpy();
-                var cb = jasmine.createSpy();
-                var s;
-
-                runs(function() {
-                    var t = spyOn(require('prompt'), 'get').andReturn(true);
-                    cordova.platform('add', 'blackberry', cb);
-                    // Fake prompt invoking its callback
-                    t.mostRecentCall.args[1](null, {});
-                });
-                waitsFor(function() { return cb.wasCalled; }, 'platform add blackberry callback');
-                runs(function() {
-                    s = spyOn(require('shelljs'), 'exec').andReturn({code:0});
-                    cordova.build(buildcb);
-                });
-                waitsFor(function() { return buildcb.wasCalled; }, 'build call', 20000);
-                runs(function() {
-                    expect(s).toHaveBeenCalled();
-                    expect(s.mostRecentCall.args[0]).toMatch(/ant -f .*build\.xml" qnx load-device/);
-                });
+                var proj_spy = spyOn(blackberry_parser.prototype, 'update_project');
+                var s = spyOn(require('shelljs'), 'exec');
+                cordova.build('blackberry');
+                proj_spy.mostRecentCall.args[1](); // update_project fake
+                expect(s).toHaveBeenCalled();
+                expect(s.mostRecentCall.args[0]).toMatch(/ant -f .*build\.xml" qnx load-device/);
             });
             it('should call blackberry_parser\'s update_project', function() {
-                var cb = jasmine.createSpy();
-                fs.writeFileSync(path.join(tempDir, '.cordova', 'config.json'), JSON.stringify({
-                    blackberry:{
-                        qnx:{}
-                    }
-                }), 'utf-8');
-                runs(function() {
-                    cordova.platform('add', 'blackberry', cb);
-                });
-                waitsFor(function() { return cb.wasCalled; }, 'bb add platform');
-                runs(function() {
-                    var s = spyOn(blackberry_parser.prototype, 'update_project');
-                    cordova.build();
-                    expect(s).toHaveBeenCalled();
-                });
-            });
-        });
-    });
-
-    xdescribe('specifying platforms to build', function() {
-        beforeEach(function() {
-            cordova.create(tempDir);
-            process.chdir(tempDir);
-            cordova.platform('add', 'android');
-        });
-
-        afterEach(function() {
-            process.chdir(cwd);
-        });
-        it('should only build the specified platform (array notation)', function() {
-            var cb = jasmine.createSpy();
-            var buildcb = jasmine.createSpy();
-            var s;
-            runs(function() {
-                cordova.platform('add', 'ios', cb);
-            });
-            waitsFor(function() { return cb.wasCalled; }, 'platform add ios');
-            runs(function() {
-                s = spyOn(shell, 'exec').andReturn({code:0});
-                cordova.build(['android'], buildcb);
-            });
-            waitsFor(function() { return buildcb.wasCalled; }, 'build android');
-            runs(function() {
-                expect(s.callCount).toEqual(1);
-            });
-        });
-        it('should only build the specified platform (string notation)', function() {
-            var cb = jasmine.createSpy();
-            var buildcb = jasmine.createSpy();
-            var s;
-            runs(function() {
-                cordova.platform('add', 'ios', cb);
-            });
-            waitsFor(function() { return cb.wasCalled; }, 'platform add ios');
-            runs(function() {
-                s = spyOn(shell, 'exec').andReturn({code:0});
-                cordova.build('android', buildcb);
-            });
-            waitsFor(function() { return buildcb.wasCalled; }, 'build android');
-            runs(function() {
-                expect(s.callCount).toEqual(1);
-            });
-        });
-        it('should handle multiple platforms to be built', function() {
-            var cb = jasmine.createSpy();
-            var bbcb = jasmine.createSpy();
-            var buildcb = jasmine.createSpy();
-            var s;
-            runs(function() {
-                cordova.platform('add', 'ios', cb);
-            });
-            waitsFor(function() { return cb.wasCalled; }, 'platform add ios');
-            runs(function() {
-                var g = spyOn(require('prompt'), 'get');
-                cordova.platform('add', 'blackberry', bbcb);
-                g.mostRecentCall.args[1](null, {}); // fake out prompt io
-            });
-            waitsFor(function() { return bbcb.wasCalled; }, 'platform add bb');
-            runs(function() {
-                s = spyOn(shell, 'exec').andReturn({code:0});
-                cordova.build(['android','ios'], buildcb);
-            });
-            waitsFor(function() { return buildcb.wasCalled; }, 'build android+ios');
-            runs(function() {
-                expect(s.callCount).toEqual(2);
+                var s = spyOn(blackberry_parser.prototype, 'update_project');
+                cordova.build('blackberry');
+                expect(s).toHaveBeenCalled();
             });
         });
     });
 
-    xdescribe('hooks', function() {
-        var s;
+    describe('hooks', function() {
+        var s, sh, ap;
         beforeEach(function() {
-            cordova.create(tempDir);
-            process.chdir(tempDir);
             s = spyOn(hooker.prototype, 'fire').andReturn(true);
         });
-        afterEach(function() {
-            process.chdir(cwd);
-        });
 
         describe('when platforms are added', function() {
             beforeEach(function() {
-                cordova.platform('add', 'android');
-                spyOn(shell, 'exec').andReturn({code:0});
+                shell.mv('-f', path.join(cordova_project, 'platforms', 'blackberry'), path.join(tempDir));
+                shell.mv('-f', path.join(cordova_project, 'platforms', 'ios'), path.join(tempDir));
+                sh = spyOn(shell, 'exec');
+                ap = spyOn(android_parser.prototype, 'update_project');
+                process.chdir(cordova_project);
+            });
+            afterEach(function() {
+                shell.mv('-f', path.join(tempDir, 'blackberry'), path.join(cordova_project, 'platforms', 'blackberry'));
+                shell.mv('-f', path.join(tempDir, 'ios'), path.join(cordova_project, 'platforms', 'ios'));
+                process.chdir(cwd);
             });
 
             it('should fire before hooks through the hooker module', function() {
@@ -274,11 +161,19 @@ xdescribe('build command', function() {
             });
             it('should fire after hooks through the hooker module', function() {
                 cordova.build();
+                sh.mostRecentCall.args[2](0); //fake shell call
                 expect(s).toHaveBeenCalledWith('after_build');
             });
         });
 
         describe('with no platforms added', function() {
+            beforeEach(function() {
+                cordova.create(tempDir);
+                process.chdir(tempDir);
+            });
+            afterEach(function() {
+                process.chdir(cwd);
+            });
             it('should not fire the hooker', function() {
                 spyOn(shell, 'exec').andReturn({code:0});
                 expect(function() {

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2cfdf55f/src/build.js
----------------------------------------------------------------------
diff --git a/src/build.js b/src/build.js
index 19af147..0cd52db 100644
--- a/src/build.js
+++ b/src/build.js
@@ -32,9 +32,8 @@ var cordova_util  = require('./util'),
     prompt        = require('prompt'),
     util          = require('util');
 
-function shell_out_to_debug(projectRoot, platform) {
+function shell_out_to_debug(projectRoot, platform, callback) {
     var cmd = path.join(projectRoot, 'platforms', platform);
-    // TODO: wait for https://issues.apache.org/jira/browse/CB-1548 to be fixed before we axe this
     // TODO: this is bb10 only for now
     // TODO: PLATFORM LIBRARY INCONSISTENCY
     if (platform == 'blackberry') {
@@ -42,15 +41,20 @@ function shell_out_to_debug(projectRoot, platform) {
     } else {
         cmd = '"' + cmd + '/cordova/build"';
     }
-    var response = shell.exec(cmd, {silent:true});
-    if (response.code > 0) throw 'An error occurred while building the ' + platform + ' project. ' + response.output;
+    shell.exec(cmd, {silent:true, async:true}, function(code, output) {
+        if (code > 0) {
+            throw new Error('An error occurred while building the ' + platform + ' project. ' + output);
+        } else {
+            callback();
+        }
+    });
 }
 
-module.exports = function build (platforms, callback) {
+module.exports = function build(platforms, callback) {
     var projectRoot = cordova_util.isCordova(process.cwd());
 
     if (!projectRoot) {
-        throw 'Current working directory is not a Cordova-based project.';
+        throw new Error('Current working directory is not a Cordova-based project.');
     }
 
     var xml = path.join(projectRoot, 'www', 'config.xml');
@@ -65,16 +69,16 @@ module.exports = function build (platforms, callback) {
         platforms = ls(path.join(projectRoot, 'platforms'));
     }
 
-    if (platforms.length === 0) throw 'No platforms added to this project. Please use `cordova platform add <platform>`.';
+    if (platforms.length === 0) throw new Error('No platforms added to this project. Please use `cordova platform add <platform>`.');
 
     var hooks = new hooker(projectRoot);
     if (!(hooks.fire('before_build'))) {
-        throw 'before_build hooks exited with non-zero code. Aborting.';
+        throw new Error('before_build hooks exited with non-zero code. Aborting.');
     }
 
     var end = n(platforms.length, function() {
         if (!(hooks.fire('after_build'))) {
-            throw 'after_build hooks exited with non-zero code. Aborting.';
+            throw new Error('after_build hooks exited with non-zero code. Aborting.');
         }
         if (callback) callback();
     });
@@ -90,8 +94,7 @@ module.exports = function build (platforms, callback) {
 
                 // Update the related platform project from the config
                 parser.update_project(cfg);
-                shell_out_to_debug(projectRoot, 'android');
-                end();
+                shell_out_to_debug(projectRoot, 'android', end);
                 break;
             case 'blackberry':
                 platformPath = path.join(projectRoot, 'platforms', 'blackberry');
@@ -100,8 +103,7 @@ module.exports = function build (platforms, callback) {
                 // Update the related platform project from the config
                 parser.update_project(cfg, function() {
                     // Shell it
-                    shell_out_to_debug(projectRoot, 'blackberry');
-                    end();
+                    shell_out_to_debug(projectRoot, 'blackberry', end);
                 });
                 break;
             case 'ios':
@@ -110,8 +112,7 @@ module.exports = function build (platforms, callback) {
 
                 // Update the related platform project from the config
                 parser.update_project(cfg, function() {
-                    shell_out_to_debug(projectRoot, 'ios');
-                    end();
+                    shell_out_to_debug(projectRoot, 'ios', end);
                 });
                 break;
         }