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:46 UTC

[5/10] git commit: sped up platform tests!

sped up platform tests!


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

Branch: refs/heads/master
Commit: 6c74e7fd9b3799f0fa0ba9430ef578dc2adbe928
Parents: 8d4ab38
Author: Fil Maj <ma...@gmail.com>
Authored: Wed Jan 30 16:39:01 2013 -0800
Committer: Fil Maj <ma...@gmail.com>
Committed: Wed Jan 30 16:39:01 2013 -0800

----------------------------------------------------------------------
 spec/platform.spec.js             |  210 +++++++++++++++-----------------
 src/metadata/android_parser.js    |    2 +-
 src/metadata/blackberry_parser.js |    2 +-
 src/metadata/ios_parser.js        |    2 +-
 src/platform.js                   |   61 ++++------
 5 files changed, 130 insertions(+), 147 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6c74e7fd/spec/platform.spec.js
----------------------------------------------------------------------
diff --git a/spec/platform.spec.js b/spec/platform.spec.js
index 031efa5..c49a53c 100644
--- a/spec/platform.spec.js
+++ b/spec/platform.spec.js
@@ -1,4 +1,3 @@
-
 /**
     Licensed to the Apache Software Foundation (ASF) under one
     or more contributor license agreements.  See the NOTICE file
@@ -31,6 +30,7 @@ var cordova = require('../cordova'),
     tempDir = path.join(__dirname, '..', 'temp');
     android_parser = require('../src/metadata/android_parser'),
     ios_parser = require('../src/metadata/ios_parser'),
+    cordova_project = path.join(__dirname, 'fixtures', 'projects', 'cordova'),
     blackberry_parser = require('../src/metadata/blackberry_parser');
 
 var cwd = process.cwd();
@@ -68,8 +68,7 @@ describe('platform command', function() {
 
     describe('`ls`', function() { 
         beforeEach(function() {
-            cordova.create(tempDir);
-            process.chdir(tempDir);
+            process.chdir(cordova_project);
         });
 
         afterEach(function() {
@@ -77,25 +76,15 @@ describe('platform command', function() {
         });
 
         it('should list out no platforms for a fresh project', function() {
+            shell.mv('-f', path.join(cordova_project, 'platforms', '*'), tempDir);
+            this.after(function() {
+                shell.mv('-f', path.join(tempDir, '*'), path.join(cordova_project, 'platforms'));
+            });
             expect(cordova.platform('list').length).toEqual(0);
         });
 
         it('should list out added platforms in a project', function() {
-            var cbtwo = jasmine.createSpy();
-            var cb = jasmine.createSpy();
-
-            runs(function() {
-                cordova.platform('add', 'android', cb);
-            });
-            waitsFor(function() { return cb.wasCalled; }, "android create callback");
-            runs(function() {
-                expect(cordova.platform('list')[0]).toEqual('android');
-                cordova.platform('add', 'ios', cbtwo);
-            });
-            waitsFor(function() { return cbtwo.wasCalled; }, "create callback number two");
-            runs(function() {
-                expect(cordova.platform('list')[1]).toEqual('ios');
-            });
+            expect(cordova.platform('list').length).toEqual(3);
         });
     });
 
@@ -109,135 +98,125 @@ describe('platform command', function() {
             process.chdir(cwd);
         });
 
-        describe('without any libraries cloned', function() {
-            var bkup = path.join(util.libDirectory, '..', 'bkup');
+        describe('android', function() {
+            var sh, cr;
+            var fake_reqs_check = function() {
+                cr.mostRecentCall.args[0](false);
+            };
+            var fake_create = function(a_path) {
+                shell.mkdir('-p', a_path);
+                fs.writeFileSync(path.join(a_path, 'AndroidManifest.xml'), 'hi', 'utf-8');
+                sh.mostRecentCall.args[2](0, '');
+            };
             beforeEach(function() {
-                shell.mkdir('-p', bkup);
-                shell.mv(util.libDirectory, bkup);
+                sh = spyOn(shell, 'exec');
+                cr = spyOn(android_parser, 'check_requirements');
             });
-            afterEach(function() {
-                shell.mv(path.join(bkup, fs.readdirSync(bkup)[0]), path.join(util.libDirectory, '..'));
-                shell.rm('-rf', bkup);
-            });
-            it('should download the cordova library', function() {
-                var s = spyOn(request, 'get');
-                try {
-                    cordova.platform('add', 'android', function() {});
-                } catch(e) {}
 
-                expect(s).toHaveBeenCalled();
-                expect(s.calls[0].args[0]).toMatch(/apache.org\/dist\/cordova.*\.zip$/);
-            });
-        });
-
-        describe('android', function() {
-            it('should add a basic android project', function() {
+            it('should shell out to android ./bin/create', function() {
                 cordova.platform('add', 'android');
-                expect(fs.existsSync(path.join(tempDir, 'platforms', 'android', 'AndroidManifest.xml'))).toBe(true);
+                fake_reqs_check();
+                var shell_cmd = sh.mostRecentCall.args[0];
+                expect(shell_cmd).toMatch(/android\/bin\/create/);
             });
             it('should call android_parser\'s update_project', function() {
                 var s = spyOn(android_parser.prototype, 'update_project');
                 cordova.platform('add', 'android');
+                fake_reqs_check();
+                fake_create(path.join(tempDir, 'platforms', 'android'));
                 expect(s).toHaveBeenCalled();
             });
         });
         describe('ios', function() {
-            it('should add a basic ios project', function() {
-                var cb = jasmine.createSpy();
-
-                runs(function() {
-                    cordova.platform('add', 'ios', cb);
-                });
-                waitsFor(function() { return cb.wasCalled; }, "platform add ios callback");
-                runs(function() {
-                    expect(fs.existsSync(path.join(tempDir, 'platforms', 'ios', 'www'))).toBe(true);
-                });
+            var sh, cr;
+            var fake_reqs_check = function() {
+                cr.mostRecentCall.args[0](false);
+            };
+            var fake_create = function(a_path) {
+                shell.mkdir('-p', a_path);
+                fs.writeFileSync(path.join(a_path, 'poo.xcodeproj'), 'hi', 'utf-8');
+                shell.mkdir('-p', path.join(a_path, 'poo'));
+                shell.cp(path.join(cordova_project, 'www', 'config.xml'), path.join(a_path, 'poo', 'config.xml'));
+                sh.mostRecentCall.args[2](0, '');
+            };
+            beforeEach(function() {
+                sh = spyOn(shell, 'exec');
+                cr = spyOn(ios_parser, 'check_requirements');
+            });
+            it('should shell out to ios ./bin/create', function() {
+                cordova.platform('add', 'ios');
+                fake_reqs_check();
+                var shell_cmd = sh.mostRecentCall.args[0];
+                expect(shell_cmd).toMatch(/ios\/bin\/create/);
             });
             it('should call ios_parser\'s update_project', function() {
                 var s = spyOn(ios_parser.prototype, 'update_project');
                 cordova.platform('add', 'ios');
+                fake_reqs_check();
+                fake_create(path.join(tempDir, 'platforms', 'ios'));
                 expect(s).toHaveBeenCalled();
             });
-            it('should error out if user does not have xcode 4.5 or above installed', function() {
-                var s = spyOn(shell, 'exec').andReturn({code:0,output:'Xcode 4.2.1'});
-                expect(function() {
-                    cordova.platform('add', 'ios');
-                }).toThrow();
-            });
-            it('should error out if user does not have xcode installed at all', function() {
-                var s = spyOn(shell, 'exec').andReturn({code:1});
-                expect(function() {
-                    cordova.platform('add', 'ios');
-                }).toThrow();
-            });
         });
         describe('blackberry', function() {
-            it('should add a basic blackberry project', function() {
-                var cb = jasmine.createSpy();
-                var s = spyOn(require('prompt'), 'get').andReturn(true);
-
-                runs(function() {
-                    cordova.platform('add', 'blackberry', cb);
-                    s.mostRecentCall.args[1](null, {}); // fake out prompt
-                });
-                waitsFor(function() { return cb.wasCalled; }, "platform add blackberry");
-                runs(function() {
-                    expect(fs.existsSync(path.join(tempDir, 'platforms', 'blackberry', 'www'))).toBe(true);
-                });
+            var sh, cr;
+            var fake_reqs_check = function() {
+                cr.mostRecentCall.args[0](false);
+            };
+            var fake_create = function(a_path) {
+                shell.mkdir('-p', path.join(a_path, 'www'));
+                fs.writeFileSync(path.join(a_path, 'project.properties'), 'hi', 'utf-8');
+                fs.writeFileSync(path.join(a_path, 'build.xml'), 'hi', 'utf-8');
+                shell.cp(path.join(cordova_project, 'www', 'config.xml'), path.join(a_path, 'www', 'config.xml'));
+                sh.mostRecentCall.args[2](0, '');
+            };
+            beforeEach(function() {
+                sh = spyOn(shell, 'exec');
+                cr = spyOn(blackberry_parser, 'check_requirements');
+            });
+            it('should shell out to blackberry bin/create', function() {
+                cordova.platform('add', 'blackberry');
+                fake_reqs_check();
+                var shell_cmd = sh.mostRecentCall.args[0];
+                expect(shell_cmd).toMatch(/blackberry\/bin\/create/);
             });
             it('should call blackberry_parser\'s update_project', function() {
                 var s = spyOn(blackberry_parser.prototype, 'update_project');
                 cordova.platform('add', 'blackberry');
+                fake_reqs_check();
+                fake_create(path.join(tempDir, 'platforms', 'blackberry'));
                 expect(s).toHaveBeenCalled();
             });
         });
         it('should handle multiple platforms', function() {
-            var cb = jasmine.createSpy();
-            runs(function() {
-                cordova.platform('add', ['android', 'ios'], cb);
-            });
-            waitsFor(function() { return cb.wasCalled; }, "platform add ios+android callback");
-            runs(function() {
-                expect(fs.existsSync(path.join(tempDir, 'platforms', 'android', 'AndroidManifest.xml'))).toBe(true);
-                expect(fs.existsSync(path.join(tempDir, 'platforms', 'ios', 'www'))).toBe(true);
-            });
+            var arc = spyOn(android_parser, 'check_requirements');
+            var irc = spyOn(ios_parser, 'check_requirements');
+            var sh = spyOn(shell, 'exec');
+            cordova.platform('add', ['android', 'ios']);
+            arc.mostRecentCall.args[0](false);
+            irc.mostRecentCall.args[0](false);
+            expect(sh.argsForCall[0][0]).toMatch(/android\/bin\/create/);
+            expect(sh.argsForCall[1][0]).toMatch(/ios\/bin\/create/);
         });
     });
 
     describe('`remove`',function() { 
         beforeEach(function() {
-            cordova.create(tempDir);
-            process.chdir(tempDir);
+            process.chdir(cordova_project);
+            shell.cp('-rf', path.join(cordova_project, 'platforms' ,'*'), tempDir);
         });
 
         afterEach(function() {
             process.chdir(cwd);
+            shell.cp('-rf', path.join(tempDir, '*'), path.join(cordova_project, 'platforms')); 
         });
 
         it('should remove a supported and added platform', function() {
-            var cb = jasmine.createSpy();
-
-            runs(function() {
-                cordova.platform('add', ['android', 'ios'], cb);
-            });
-            waitsFor(function() { return cb.wasCalled; }, "android+ios platfomr add callback");
-            runs(function() {
-                cordova.platform('remove', 'android');
-                expect(cordova.platform('ls').length).toEqual(1);
-                expect(cordova.platform('ls')[0]).toEqual('ios');
-            });
+            cordova.platform('remove', 'android');
+            expect(cordova.platform('ls').length).toEqual(2);
         });
         it('should be able to remove multiple platforms', function() {
-            var cb = jasmine.createSpy();
-
-            runs(function() {
-                cordova.platform('add', ['android', 'ios'], cb);
-            });
-            waitsFor(function() { return cb.wasCalled; }, "android+ios platfomr add callback");
-            runs(function() {
-                cordova.platform('remove', ['android','ios']);
-                expect(cordova.platform('ls').length).toEqual(0);
-            });
+            cordova.platform('remove', ['android','ios']);
+            expect(cordova.platform('ls').length).toEqual(1);
         });
     });
 
@@ -274,12 +253,25 @@ describe('platform command', function() {
             });
         });
         describe('add hooks', function() {
-            it('should fire before hooks through the hooker module', function() {
-                cordova.platform('add', 'android');
-                expect(s).toHaveBeenCalledWith('before_platform_add');
+            var sh, cr;
+            var fake_reqs_check = function() {
+                cr.mostRecentCall.args[0](false);
+            };
+            var fake_create = function(a_path) {
+                shell.mkdir('-p', a_path);
+                fs.writeFileSync(path.join(a_path, 'AndroidManifest.xml'), 'hi', 'utf-8');
+                sh.mostRecentCall.args[2](0, '');
+            };
+            beforeEach(function() {
+                sh = spyOn(shell, 'exec');
+                cr = spyOn(android_parser, 'check_requirements');
             });
-            it('should fire after hooks through the hooker module', function() {
+            it('should fire before and after hooks through the hooker module', function() {
+                spyOn(android_parser.prototype, 'update_project');
                 cordova.platform('add', 'android');
+                fake_reqs_check();
+                fake_create(path.join(tempDir, 'platforms', 'android'));
+                expect(s).toHaveBeenCalledWith('before_platform_add');
                 expect(s).toHaveBeenCalledWith('after_platform_add');
             });
         });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6c74e7fd/src/metadata/android_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/android_parser.js b/src/metadata/android_parser.js
index 16737be..253cf67 100644
--- a/src/metadata/android_parser.js
+++ b/src/metadata/android_parser.js
@@ -31,7 +31,7 @@ var default_prefs = {
 
 module.exports = function android_parser(project) {
     if (!fs.existsSync(path.join(project, 'AndroidManifest.xml'))) {
-        throw 'The provided path is not an Android project.';
+        throw new Error('The provided path "' + project + '" is not an Android project.');
     }
     this.path = project;
     this.strings = path.join(this.path, 'res', 'values', 'strings.xml');

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6c74e7fd/src/metadata/blackberry_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/blackberry_parser.js b/src/metadata/blackberry_parser.js
index 8d5902a..8d5a823 100644
--- a/src/metadata/blackberry_parser.js
+++ b/src/metadata/blackberry_parser.js
@@ -27,7 +27,7 @@ var fs            = require('fs'),
 
 module.exports = function blackberry_parser(project) {
     if (!fs.existsSync(path.join(project, 'project.properties')) || !fs.existsSync(path.join(project, 'build.xml'))) {
-        throw 'The provided path is not a Cordova BlackBerry WebWorks project.';
+        throw new Error('The provided path "' + project + '" is not a Cordova BlackBerry WebWorks project.');
     }
     this.path = project;
     this.config_path = path.join(this.path, 'www', 'config.xml');

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6c74e7fd/src/metadata/ios_parser.js
----------------------------------------------------------------------
diff --git a/src/metadata/ios_parser.js b/src/metadata/ios_parser.js
index e9e665a..cdc6802 100644
--- a/src/metadata/ios_parser.js
+++ b/src/metadata/ios_parser.js
@@ -46,7 +46,7 @@ var default_prefs = {
 module.exports = function ios_parser(project) {
     try {
         var xcodeproj_dir = fs.readdirSync(project).filter(function(e) { return e.match(/\.xcodeproj$/i); })[0];
-        if (!xcodeproj_dir) throw 'The provided path is not a Cordova iOS project.';
+        if (!xcodeproj_dir) throw new Error('The provided path "' + project + '" is not a Cordova iOS project.');
         this.xcodeproj = path.join(project, xcodeproj_dir);
         this.originalName = this.xcodeproj.substring(this.xcodeproj.lastIndexOf('/'), this.xcodeproj.indexOf('.xcodeproj'));
         this.cordovaproj = path.join(project, this.originalName);

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6c74e7fd/src/platform.js
----------------------------------------------------------------------
diff --git a/src/platform.js b/src/platform.js
index 9c4ef44..834c514 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -1,4 +1,3 @@
-
 /**
     Licensed to the Apache Software Foundation (ASF) under one
     or more contributor license agreements.  See the NOTICE file
@@ -22,19 +21,10 @@ var config_parser     = require('./config_parser'),
     util              = require('util'),
     fs                = require('fs'),
     path              = require('path'),
-    android_parser    = require('./metadata/android_parser'),
-    blackberry_parser = require('./metadata/blackberry_parser'),
-    ios_parser        = require('./metadata/ios_parser'),
     hooker            = require('./hooker'),
     n                 = require('ncallbacks'),
     shell             = require('shelljs');
 
-var check_requirements = {
-    "ios":ios_parser.check_requirements,
-    "android":android_parser.check_requirements,
-    "blackberry":blackberry_parser.check_requirements
-};
-
 module.exports = function platform(command, targets, callback) {
     var projectRoot = cordova_util.isCordova(process.cwd());
 
@@ -75,7 +65,7 @@ module.exports = function platform(command, targets, callback) {
                 }
 
                 // Make sure we have minimum requirements to work with specified platform
-                check_requirements[target](function(err) {
+                require('./metadata/' + target + '_parser').check_requirements(function(err) {
                     if (err) {
                         throw new Error('Your system does not meet the requirements to create ' + target + ' projects: ' + err);
                     } else {
@@ -89,33 +79,34 @@ module.exports = function platform(command, targets, callback) {
                         // TODO: keep tabs on CB-2300
                         var command = util.format('"%s" "%s" "%s" "%s"', bin, output, (target=='blackberry'?name:pkg), name);
 
-                        var create = shell.exec(command, {silent:true});
-                        if (create.code > 0) {
-                            throw new Error('An error occured during creation of ' + target + ' sub-project. ' + create.output);
-                        }
+                        shell.exec(command, {silent:true,async:true}, function(code, create_output) {
+                            if (code > 0) {
+                                throw new Error('An error occured during creation of ' + target + ' sub-project. ' + create_output);
+                            }
 
-                        switch(target) {
-                            case 'android':
-                                var android = new android_parser(output);
-                                android.update_project(cfg);
-                                hooks.fire('after_platform_add');
-                                end();
-                                break;
-                            case 'ios':
-                                var ios = new ios_parser(output);
-                                ios.update_project(cfg, function() {
-                                    hooks.fire('after_platform_add');
-                                    end();
-                                });
-                                break;
-                            case 'blackberry':
-                                var bb = new blackberry_parser(output);
-                                bb.update_project(cfg, function() {
+                            switch(target) {
+                                case 'android':
+                                    var android = new android_parser(output);
+                                    android.update_project(cfg);
                                     hooks.fire('after_platform_add');
                                     end();
-                                });
-                                break;
-                        }
+                                    break;
+                                case 'ios':
+                                    var ios = new ios_parser(output);
+                                    ios.update_project(cfg, function() {
+                                        hooks.fire('after_platform_add');
+                                        end();
+                                    });
+                                    break;
+                                case 'blackberry':
+                                    var bb = new blackberry_parser(output);
+                                    bb.update_project(cfg, function() {
+                                        hooks.fire('after_platform_add');
+                                        end();
+                                    });
+                                    break;
+                            }
+                        });
                     }
                 });
             });