cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject cordova-lib git commit: CB-8225 Add Unit Tests for platform.js/add function (closes #138)
Date Sun, 04 Jan 2015 01:01:57 GMT
Repository: cordova-lib
Updated Branches:
  refs/heads/master 7a0833c62 -> 9079bb856


CB-8225 Add Unit Tests for platform.js/add function (closes #138)


Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/9079bb85
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/9079bb85
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/9079bb85

Branch: refs/heads/master
Commit: 9079bb8560615572b571924b908b0124a2845a2e
Parents: 7a0833c
Author: Omar Mefire <ommenjik@microsoft.com>
Authored: Wed Dec 17 16:22:01 2014 -0800
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Sat Jan 3 20:01:11 2015 -0500

----------------------------------------------------------------------
 cordova-lib/package.json                  |   3 +-
 cordova-lib/spec-cordova/platform.spec.js | 423 ++++++++++++++++++++++++-
 cordova-lib/src/cordova/platform.js       |  20 +-
 3 files changed, 431 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/9079bb85/cordova-lib/package.json
----------------------------------------------------------------------
diff --git a/cordova-lib/package.json b/cordova-lib/package.json
index 95101f0..1ddbb23 100644
--- a/cordova-lib/package.json
+++ b/cordova-lib/package.json
@@ -43,7 +43,8 @@
   "devDependencies": {
     "istanbul": "^0.3.4",
     "jasmine-node": "1.14.5",
-    "jshint": "2.5.8"
+    "jshint": "2.5.8",
+    "rewire": "2.1.3"
   },
   "scripts": {
     "test": "npm run jasmine && npm run jshint",

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/9079bb85/cordova-lib/spec-cordova/platform.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/platform.spec.js b/cordova-lib/spec-cordova/platform.spec.js
index fb59ef2..e54173e 100644
--- a/cordova-lib/spec-cordova/platform.spec.js
+++ b/cordova-lib/spec-cordova/platform.spec.js
@@ -26,15 +26,17 @@ var helpers = require('./helpers'),
     config = require('../src/cordova/config'),
     Q = require('q'),
     events = require('../src/events'),
-    cordova = require('../src/cordova/cordova');
+    cordova = require('../src/cordova/cordova'),
+    rewire = require('rewire'),
+    platform = rewire('../src/cordova/platform.js');
 
-var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www';
});
-var tmpDir = helpers.tmpDir('platform_test');
-var project = path.join(tmpDir, 'project');
+describe('platform end-to-end', function () {
 
-var platformParser = platforms[helpers.testPlatform].parser;
+    var supported_platforms = Object.keys(platforms).filter(function (p) { return p != 'www';
});
+    var tmpDir = helpers.tmpDir('platform_test');
+    var project = path.join(tmpDir, 'project');
+    var platformParser = platforms[helpers.testPlatform].parser;
 
-describe('platform end-to-end', function() {
     var results;
 
     beforeEach(function() {
@@ -123,3 +125,412 @@ describe('platform end-to-end', function() {
     });
 });
 
+describe('add function', function () {
+
+    var projectRoot = 'C:\\Projects\\cordova-projects\\move-tracker';
+
+    // Directory containing the 'bin/create' script. 
+    var libDir = "C:\\Projects\\cordova-projects\\cordova-android\\";
+
+    var cfg;
+    var opts;
+    var hooksRunnerMock;
+    var hostSupportsOriginal = platform.__get__('hostSupports');
+    var ConfigParserOriginal = platform.__get__('ConfigParser');
+    var configOriginal = platform.__get__('config');
+    var fsOriginal = platform.__get__('fs');
+    var platformsOriginal = platform.__get__('platforms');
+    var promiseUtilOriginal = platform.__get__('promiseutil');
+    var getPackageJsonContentOriginal = platform.__get__('getPackageJsonContent');
+
+    beforeEach(function () {
+
+        opts = {};
+
+        hooksRunnerMock = {
+            fire: function () {
+                return Q();
+            }
+        };
+
+        platform.__set__('hostSupports', function (platform) {
+            return true;
+        });
+
+        platform.__set__('ConfigParser', function (xml) {
+            cfg = {};
+            return cfg;
+        });
+
+        platform.__set__('config', {
+            read: function (projectRoot) {
+                return {};
+            }
+        });
+
+        platform.__set__('fs', {
+            existsSync: function (path) {
+                return true;
+            }
+        });
+
+
+	platform.__set__('resolvePath', function(p){
+	    return p;
+	});
+
+	platform.__set__('getPackageJsonContent', function (p) {
+            return p + '\\package';
+        });
+
+
+        // These are the recognized/legal platforms
+        platform.__set__('platforms', {
+            'android': {
+                'parser': './metadata/android_parser',
+                'url': 'https://git-wip-us.apache.org/repos/asf?p=cordova-android.git',
+                'version': '3.6.4'
+            },
+            'ios': {
+                'hostos': ['darwin'],
+                'parser': './metadata/ios_parser',
+                'url': 'https://git-wip-us.apache.org/repos/asf?p=cordova-ios.git',
+                'version': '3.7.0'
+            },
+            'wp8': {
+                'hostos': ['win32'],
+                'parser': './metadata/wp8_parser',
+                'url': 'https://git-wip-us.apache.org/repos/asf?p=cordova-wp8.git',
+                'version': '3.7.0',
+                'altplatform': 'wp'
+            },
+            'amazon-fireos': {
+                'name': 'cordova-amazon-fireos',
+                'version': '3.7.0-dev',
+                'description': 'cordova-amazon-fireos release',
+                'main': 'bin/create'
+            }
+        });
+    });
+
+    afterEach(function () {
+
+        platform.__set__('hostSupports', hostSupportsOriginal);
+        platform.__set__('ConfigParser', ConfigParserOriginal);
+        platform.__set__('config', configOriginal);
+        platform.__set__('fs', fsOriginal);
+        platform.__set__('platforms', platformsOriginal);
+        platform.__set__('promiseutil', promiseUtilOriginal);
+        platform.__set__('getPackageJsonContent', getPackageJsonContentOriginal);
+    });
+
+    it('throws if the target list is empty', function (done) {
+        var targets = [];
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
+            expect(error.message).toBe("No platform specified. Please specify a platform
to add. See `cordova platform list`.");
+            done();
+        });
+    });
+
+    it('throws if the target list is undefined or null', function (done) {
+
+        // case 1 : target list undefined
+        var targets; // = undefined;
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
+            expect(error.message).toBe("No platform specified. Please specify a platform
to add. See `cordova platform list`.");
+        });
+
+        // case 2 : target list null
+        targets = null;
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
+            expect(error.message).toBe("No platform specified. Please specify a platform
to add. See `cordova platform list`.");
+            done();
+        });
+    });
+
+    it('creates the "platforms" directory if it does not exist', function (done) {
+
+        var targets = ['C:\\Projects\\cordova-projects\\cordova-android'];
+        var wasPlatformDirectoryCreated = false;
+
+        // Make it so that 'platforms' directory doesn't exist
+        platform.__set__('fs', {
+            existsSync: function () {
+                return false;
+            }
+        });
+
+        // Do nothing with the targets (only in this test)
+        platform.__set__('promiseutil', {
+            Q_chainmap: function () {
+                return Q();
+            }
+        });
+
+        platform.__set__('shell', {
+            mkdir: function () {
+                wasPlatformDirectoryCreated = true;
+            }
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).then(function () {
+            expect(wasPlatformDirectoryCreated).toBeTruthy();
+            done();
+        });
+    });
+
+    it('fires events "before_platform_add" and "after_platform_add" in the right order and
at the right time', function (done) {
+
+        var targets = ['C:\\Projects\\cordova-projects\\cordova-android'];
+
+        // events 
+        var before_platform_add_fired = false;
+        var after_platform_add_fired = false;
+        var platforms_added = false;
+
+        var hooksRunnerMock = {
+            fire: function (event, opts) {
+                switch (event) {
+                    case 'before_platform_add':
+                        before_platform_add_fired = true;
+                        expect(after_platform_add_fired).toBeFalsy();
+                        expect(platforms_added).toBeFalsy();
+                        break;
+                    case 'after_platform_add':
+                        after_platform_add_fired = true;
+                        expect(before_platform_add_fired).toBeTruthy();
+                        expect(platforms_added).toBeTruthy();
+                        break;
+                    default:
+                        // Induce test failure if invalid event is passed.
+		        // Jasmine doesn't have an explicit 'make test fail' function, so we'll use this
one
+                        expect(1).toBe(2);
+                }
+                return Q();
+            }
+        };
+        platform.__set__('promiseutil', {
+            Q_chainmap: function (targets, func) {
+                platforms_added = true;
+                expect(before_platform_add_fired).toBeTruthy();
+                expect(after_platform_add_fired).toBeFalsy();
+                return Q();
+            }
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).then(function () {
+            expect(before_platform_add_fired).toBeTruthy();
+            expect(after_platform_add_fired).toBeTruthy();
+            expect(platforms_added).toBeTruthy();
+            done();
+        });
+    });
+
+    it('runs the create script in the target directory when passed a directory as target',
function (done) {
+
+        var targets = ['C:\\Projects\\cordova-projects\\cordova-android'];
+
+        var call_into_create_mock = jasmine.createSpy();
+        platform.__set__('call_into_create', call_into_create_mock);
+
+        // Mock out package.json content
+        platform.__set__('getPackageJsonContent', function (p) {
+            return {
+                'name': 'cordova-android',
+                'version': '3.7.0-dev',
+                'description': 'cordova-android release',
+                'main': 'bin/create'
+            };
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).then(function () {
+            expect(call_into_create_mock).toHaveBeenCalledWith('android', projectRoot, cfg,
targets[0], null, opts);
+            done();
+        });
+    });
+
+    it('throws if target directory supplied does not contain package.json file', function
(done) {
+
+        var targets = ['C:\\Projects\\cordova-projects\\cordova-android'];
+
+        var call_into_create_mock = jasmine.createSpy();
+        platform.__set__('call_into_create', call_into_create_mock);
+
+        platform.__set__('getPackageJsonContent', function (p) {
+            var pPath = path.join(p, 'package');
+            var msg = "Cannot find module '" + pPath + "'";
+            var err = new Error(msg);
+            err.code = 'MODULE_NOT_FOUND';
+            throw err;
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
+            var packagePath = path.join(targets[0], 'package');
+            expect(error.message).toBe('The provided path does not seem to contain a ' +
'Cordova platform: ' + targets[0] +
+                '\n' + 'Cannot find module ' + "'" + packagePath + "'");
+            done();
+        });
+    });
+
+    it('throws if package.json file does not contain name property', function (done) {
+
+        var targets = ['C:\\Projects\\cordova-projects\\cordova-android'];
+
+        var call_into_create_mock = jasmine.createSpy();
+        platform.__set__('call_into_create', call_into_create_mock);
+
+        platform.__set__('getPackageJsonContent', function (p) {
+            return {
+                //'name': 'cordova-android', // Don't return any name
+                'version': '3.7.0-dev',
+                'description': 'cordova-android release',
+                'main': 'bin/create'
+            };
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
+            var packagePath = path.join(targets[0], 'package');
+            expect(error.message).toBe('The provided path does not seem to contain a ' +
'Cordova platform: ' + targets[0]);
+            done();
+        });
+    });
+
+    it('replaces "amazon" by "amazon-fireos" if package.json returns "amazon"', function
(done) {
+
+        var targets = ['C:\\Projects\\cordova-projects\\cordova-amazon-fireos'];
+
+        var call_into_create_mock = jasmine.createSpy();
+        platform.__set__('call_into_create', call_into_create_mock);
+
+        platform.__set__('getPackageJsonContent', function (p) {
+            return {
+                'name': 'cordova-amazon', // use 'cordova-amazon' instead of 'cordova-amazon-fireos'
+                'version': '3.7.0-dev',
+                'description': 'cordova-amazon-fireos release',
+                'main': 'bin/create'
+            };
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).then(function (error) {
+            expect(call_into_create_mock).toHaveBeenCalledWith('amazon-fireos', projectRoot,
cfg, targets[0], null, opts);
+            done();
+        });
+    });
+
+    it('throws if package.json file returns null', function (done) {
+
+        var targets = ['C:\\Projects\\cordova-projects\\cordova-android'];
+
+        var call_into_create_mock = jasmine.createSpy();
+        platform.__set__('call_into_create', call_into_create_mock);
+
+        platform.__set__('getPackageJsonContent', function (p) {
+            return null;
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
+            var packagePath = path.join(targets[0], 'package');
+            expect(error.message).toBe('The provided path does not seem to contain a ' +
'Cordova platform: ' + targets[0]);
+            done();
+        });
+    });
+
+    it('throws if the name in package.json file is not a recognized platform', function (done)
{
+
+        var targets = ['C:\\Projects\\cordova-projects\\cordova-android'];
+
+        // These are the only 'recognized' platforms
+        platform.__set__('platforms', {
+            "ios": {
+                "hostos": ["darwin"],
+                "parser": "./metadata/ios_parser",
+                "url": "https://git-wip-us.apache.org/repos/asf?p=cordova-ios.git",
+                "version": "3.7.0"
+            }
+        });
+
+        var call_into_create_mock = jasmine.createSpy();
+        platform.__set__('call_into_create', call_into_create_mock);
+
+        platform.__set__('getPackageJsonContent', function () {
+            return {
+                'name': 'cordova-android',
+                'version': '3.7.0-dev',
+                'description': 'cordova-android release',
+                'main': 'bin/create'
+            };
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
+            var packagePath = path.join(targets[0], 'package');
+            expect(error.message).toBe('The provided path does not seem to contain a ' +
'Cordova platform: ' + targets[0]);
+            done();
+        });
+    });
+
+    it('uses lazy_loading when the target is specified by name', function (done) {
+
+        var targets = ['android'];
+
+        var call_into_create_mock = jasmine.createSpy();
+        platform.__set__('call_into_create', call_into_create_mock);
+
+        var based_on_config_called = false;
+        platform.__set__('lazy_load', {
+            based_on_config: function (projectRoot, t, opts) {
+                based_on_config_called = true;
+                return Q(libDir);
+            }
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).then(function () {
+            expect(based_on_config_called).toBeTruthy();
+            expect(call_into_create_mock).toHaveBeenCalledWith('android', projectRoot, cfg,
libDir, null, opts);
+            done();
+        });
+    });
+
+    it('throws if lazy_loading fails', function (done) {
+
+        var targets = ['android'];
+
+        // Error out during 'lazy load'
+        var errorMessage = 'Cordova library "' + targets[0] + '" not recognized.';
+        platform.__set__('lazy_load', {
+            based_on_config: function (projectRoot, t, opts) {
+                return Q.reject(new Error(errorMessage));
+            }
+        });
+
+        var call_into_create_mock = jasmine.createSpy();
+        platform.__set__('call_into_create', call_into_create_mock);
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
+            expect(error.message).toBe('Unable to fetch platform ' + targets[0] + ': ' +
'Error: ' + errorMessage);
+            expect(call_into_create_mock).not.toHaveBeenCalled();
+            done();
+        });
+    });
+
+    it('allows targets of the form "platform@version" where "version" is a url', function
(done) {
+
+        var targets = ['wp8@https://git-wip-us.apache.org/repos/asf?p=cordova-wp8.git;a=snapshot;h=3.7.0;sf=tgz'];
+
+        var call_into_create_mock = jasmine.createSpy();
+        platform.__set__('call_into_create', call_into_create_mock);
+
+        var based_on_config_called = false;
+        platform.__set__('lazy_load', {
+            based_on_config: function (projectRoot, t, opts) {
+                based_on_config_called = true;
+                return Q(libDir);
+            }
+        });
+
+        platform.add(hooksRunnerMock, projectRoot, targets, opts).then(function () {
+            expect(based_on_config_called).toBeTruthy();
+            expect(call_into_create_mock).toHaveBeenCalledWith('wp8', projectRoot, cfg, libDir,
null, opts);
+            done();
+        });
+    });
+});

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/9079bb85/cordova-lib/src/cordova/platform.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/platform.js b/cordova-lib/src/cordova/platform.js
index 5c754e3..563bc8e 100644
--- a/cordova-lib/src/cordova/platform.js
+++ b/cordova-lib/src/cordova/platform.js
@@ -21,8 +21,6 @@
           indent:4, unused:vars, latedef:nofunc
 */
 
-
-
 var config            = require('./config'),
     cordova           = require('./cordova'),
     cordova_util      = require('./util'),
@@ -55,7 +53,7 @@ function add(hooksRunner, projectRoot, targets, opts) {
         return Q.reject(new CordovaError(msg));
     }
 
-    for(var i= 0 ; i< targets.length; i++){
+    for (var i= 0 ; i < targets.length; i++) {
         if ( !hostSupports(targets[i]) ) {
             msg = 'WARNING: Applications for platform ' + targets[i] +
                   ' can not be built on this OS - ' + process.platform + '.';
@@ -70,7 +68,6 @@ function add(hooksRunner, projectRoot, targets, opts) {
     opts = opts || {};
     opts.searchpath = opts.searchpath || config_json.plugin_search_path;
 
-
     // The "platforms" dir is safe to delete, it's almost equivalent to
     // cordova platform rm <list of all platforms>
     if ( !fs.existsSync(platformsDir)) {
@@ -79,22 +76,21 @@ function add(hooksRunner, projectRoot, targets, opts) {
 
     return hooksRunner.fire('before_platform_add', opts)
     .then(function() {
-        return promiseutil.Q_chainmap(targets, function(t) {
+        return promiseutil.Q_chainmap(targets, function (t) {
             // For each platform, download it and call its "create" script.
-
             var p;  // The promise to be returned by this function.
             var platform = t.split('@')[0];
             // If t is not a platform or platform@version, it must be a dir.
             // In this case get platform name from package.json in that dir and
             // skip lazy-load.
             if( !(platform in platforms) ) {
-                var pPath = path.resolve(t);
+                var pPath = resolvePath(t);
                 var pkg;
                 // Prep the message in advance, we might need it in several places.
                 msg = 'The provided path does not seem to contain a ' +
                       'Cordova platform: ' + t;
                 try {
-                    pkg = require(path.join(pPath, 'package'));
+                    pkg = getPackageJsonContent(pPath);
                 } catch(e) {
                     throw new CordovaError(msg + '\n' + e.message);
                 }
@@ -134,6 +130,14 @@ function add(hooksRunner, projectRoot, targets, opts) {
     });
 }
 
+function resolvePath(pPath){
+    return path.resolve(pPath);
+}
+
+function getPackageJsonContent(pPath) {
+    return require(path.join(pPath, 'package'));
+}
+
 function remove(hooksRunner, projectRoot, targets, opts) {
     if (!targets || !targets.length) {
         return Q.reject(new CordovaError('No platform[s] specified. Please specify platform[s]
to remove. See `'+cordova_util.binname+' platform list`.'));


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message