You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by st...@apache.org on 2017/02/22 00:07:38 UTC

[07/32] cordova-lib git commit: CB-12021 : updated platform.js to fix failing tests and modified test 25 to check for config.xml too

CB-12021 : updated platform.js to fix failing tests and modified test 25 to check for config.xml too


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

Branch: refs/heads/master
Commit: a5b0441d5572a9c37b235e33bd6d7af3ce9ffc03
Parents: e5efb1a
Author: Audrey So <au...@apache.org>
Authored: Wed Feb 15 18:03:25 2017 -0800
Committer: Audrey So <au...@apache.org>
Committed: Wed Feb 15 18:03:25 2017 -0800

----------------------------------------------------------------------
 cordova-lib/spec-cordova/pkgJson.spec.js | 49 ++++++++++++++++++++-----
 cordova-lib/src/cordova/platform.js      | 52 ++++++++++-----------------
 2 files changed, 58 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a5b0441d/cordova-lib/spec-cordova/pkgJson.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/pkgJson.spec.js b/cordova-lib/spec-cordova/pkgJson.spec.js
index 397f9b6..f003a26 100644
--- a/cordova-lib/spec-cordova/pkgJson.spec.js
+++ b/cordova-lib/spec-cordova/pkgJson.spec.js
@@ -308,18 +308,36 @@ describe('plugin end-to-end', function() {
         var pluginPath = path.join(testRunRoot,'spec-cordova/fixtures/plugins/cordova-lib-test-plugin');
         var pkgJsonPath = path.join(cwd,'package.json');
         var pkgJson;
-        delete require.cache[require.resolve(pkgJsonPath)];
+        var configXmlPath = path.join(cwd, 'config.xml');
+        var cfg = new ConfigParser(configXmlPath);
+        var engines = cfg.getEngines();
+        var engNames;
+        var engSpec;
 
+        delete require.cache[require.resolve(pkgJsonPath)];
         // Run cordova platform add local path --save --fetch.
         return cordova.raw.platform('add', platformPath, {'save':true, 'fetch':true})
         .then(function() {
             // Delete any previous caches of require(package.json).
             delete require.cache[require.resolve(pkgJsonPath)];
             pkgJson = require(pkgJsonPath);
-            // Pkg.json has ios.
+            // Pkg.json has browser.
             expect(pkgJson.cordova.platforms).toEqual(['browser']);
-            // Pkg.json has platform local path spec.
-            expect(pkgJson.dependencies['cordova-browser'].includes(platformPath)).toEqual(true);
+
+            // Check that the value here exists
+            expect(pkgJson.dependencies['cordova-browser']).toBeDefined();
+
+            var cfg2 = new ConfigParser(configXmlPath);
+            engines = cfg2.getEngines();
+            // browser platform and spec have been added to config.xml.
+            engNames = engines.map(function(elem) {
+                return elem.name;
+            });
+            engSpec = engines.map(function(elem) {  
+                if (elem.name === 'browser') {
+                    expect(elem.spec.includes(platformPath)).toEqual(true);
+                }
+            });
         }).then(function() {
             // Run cordova plugin add local path --save --fetch.
             return cordova.raw.plugin('add', pluginPath, {'save':true, 'fetch':true});
@@ -329,8 +347,21 @@ describe('plugin end-to-end', function() {
             pkgJson = require(pkgJsonPath);
             // Pkg.json has geolocation plugin.
             expect(pkgJson.cordova.plugins['cordova-lib-test-plugin']).toBeDefined();
-            // Pkg.json has plugin local path spec.
-            expect(pkgJson.dependencies['cordova-lib-test-plugin'].includes(pluginPath)).toEqual(true);
+
+            // Check that the value here EXISTS
+            expect(pkgJson.dependencies['cordova-lib-test-plugin']).toBeDefined();
+
+            var cfg3 = new ConfigParser(configXmlPath);
+            engines = cfg3.getEngines();
+            // Check that browser and spec have been added to config.xml
+            engNames = engines.map(function(elem) {
+                return elem.name;
+            });
+            engSpec = engines.map(function(elem) {  
+                if (elem.name === 'browser') {
+                    expect(elem.spec.includes(platformPath)).toEqual(true);
+                }
+            });
         }).fail(function(err) {
             expect(err).toBeUndefined();
         }).fin(done);
@@ -487,7 +518,7 @@ describe('platform end-to-end with --save', function () {
         .fin(done);
     }, TIMEOUT);
 
-it('Test#010 : two platforms are added and removed correctly with --save --fetch', function(done) {
+    it('Test#010 : two platforms are added and removed correctly with --save --fetch', function(done) {
         var pkgJsonPath = path.join(process.cwd(),'package.json');
         expect(pkgJsonPath).toExist();
         var pkgJson;
@@ -915,7 +946,7 @@ describe('local path is added to config.xml without pkg.json', function () {
     });
 
     // Test#026: has NO pkg.json. Checks if local path is added to config.xml and has no errors.
-    it('Test#026 : if you add a platform with local path, pkg.json gets updated', function (done) {
+    it('Test#026 : if you add a platform with local path, config.xml gets updated', function (done) {
         var cwd = process.cwd();
         var configXmlPath = path.join(cwd, 'config.xml');
         var cfg = new ConfigParser(configXmlPath);
@@ -944,7 +975,7 @@ describe('local path is added to config.xml without pkg.json', function () {
     },60000);
 
     // Test#027: has NO pkg.json. Checks if local path is added to config.xml and has no errors.
-    it('Test#027 : if you add a plugin with local path, pkg.json gets updated', function (done) {
+    it('Test#027 : if you add a plugin with local path, config.xml gets updated', function (done) {
         var cwd = process.cwd();
         var pluginPath = path.join(testRunRoot,'spec-cordova/fixtures/plugins/cordova-lib-test-plugin');
         var configXmlPath = path.join(cwd, 'config.xml');

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a5b0441d/cordova-lib/src/cordova/platform.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/platform.js b/cordova-lib/src/cordova/platform.js
index 78dc8d7..437db3d 100644
--- a/cordova-lib/src/cordova/platform.js
+++ b/cordova-lib/src/cordova/platform.js
@@ -81,28 +81,16 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
 
     return hooksRunner.fire('before_platform_' + cmd, opts)
     .then(function() {
-        
         var platformsToSave = []; 
-        // If statement to see if pkgJsonPath exists in the filesystem
-        var pkgJson;
-        var pkgJsonPath = path.join(projectRoot, 'package.json');
-        // If statement to see if pkgJsonPath exists in the filesystem
-        if(fs.existsSync(pkgJsonPath)) {
-            pkgJson = require(pkgJsonPath);
-        } else {
-            // Create package.json in cordova@7
-        }
-        
+
         return promiseutil.Q_chainmap(targets, function(target) {
             // For each platform, download it and call its helper script.
             var parts = target.split('@');
             var platform = parts[0];
             var spec = parts[1];
             var pkgJson;
-            var pkgJsonPath = path.join(projectRoot, 'package.json');
             
             return Q.when().then(function() {
-                var prefixCordovaPlatform = 'cordova-'+platform;
                 if (!(platform in platforms)) {
                     spec = platform;
                     platform = null;
@@ -115,28 +103,21 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
                     events.emit('warn', 'wp8 has been deprecated. Please use windows instead.');
                 }
 
-                if(spec && pkgJson && pkgJson.dependencies && (pkgJson.dependencies[prefixCordovaPlatform] || pkgJson.dependencies[platform])) {
-                    if ((semver.satisfies(spec, pkgJson.dependencies[prefixCordovaPlatform])) || (semver.satisfies(spec, pkgJson.dependencies[platform]))) {
-                    } else {
-                        if (pkgJson.dependencies[prefixCordovaPlatform]) {
-                            pkgJson.dependencies[prefixCordovaPlatform] = '^'+spec;
-                        } else if (pkgJson.dependencies[platform]) {
-                            pkgJson.dependencies[platform] = '^'+spec;
-                        }
-                        fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 4), 'utf8');
-                    }
+                if(fs.existsSync(path.join(projectRoot,'package.json'))) {
+                    delete require.cache[require.resolve(path.join(projectRoot, 'package.json'))];
+                    pkgJson = require(path.join(projectRoot,'package.json'));
                 }
-
-                // If there is no spec specified during add, use the one from pkg.json.
-                if (spec === undefined && pkgJson && pkgJson.dependencies) {
-                    if (pkgJson.dependencies[prefixCordovaPlatform]) {
-                        spec = pkgJson.dependencies[prefixCordovaPlatform];
+                
+                // If there is no spec specified, try to get spec from package.json
+                // else, if there is no spec specified, try to get spec from config.xml
+                if (spec === undefined && pkgJson && pkgJson.dependencies && cmd === 'add') {
+                    if (pkgJson.dependencies['cordova-'+platform]) {
+                        spec = pkgJson.dependencies['cordova-'+platform];
                     } else if (pkgJson.dependencies[platform]) {
                         spec = pkgJson.dependencies[platform];
                     }
-                }
-
-                if (platform && !spec && cmd == 'add') {
+                    delete require.cache[require.resolve(path.join(projectRoot, 'package.json'))];
+                } else if (platform && spec === undefined && cmd === 'add') {
                     events.emit('verbose', 'No version supplied. Retrieving version from config.xml...');
                     spec = getVersionFromConfigFile(platform, cfg);
                 }
@@ -164,6 +145,8 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
             }).then(function(platDetails) {
                 if(fs.existsSync(path.join(projectRoot, 'package.json'))) {
                     delete require.cache[require.resolve(path.join(projectRoot, 'package.json'))];
+                    var pkgJson;
+                    pkgJson = require(path.join(projectRoot, 'package.json'));
                 }
                 platform = platDetails.platform;
                 var platformPath = path.join(projectRoot, 'platforms', platform);
@@ -257,6 +240,7 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
                     }
                 })
                 .then(function() {
+
                     var saveVersion = !spec || semver.validRange(spec, true);
 
                     // Save platform@spec into platforms.json, where 'spec' is a version or a soure location. If a
@@ -269,9 +253,7 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
                     if(opts.save || autosave){
                         // Similarly here, we save the source location if that was specified, otherwise the version that
                         // was installed. However, we save it with the "~" attribute (this allows for patch updates).
-                        if (spec.charAt(0) !== '~' && spec.charAt(0) !== '^') {
-                            spec = saveVersion ? '~' + platDetails.version : spec;
-                        }
+
                         spec = saveVersion ? '~' + platDetails.version : spec;
 
                         // Save target into config.xml, overriding already existing settings
@@ -293,6 +275,7 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) {
             if(fs.existsSync(pkgJsonPath)) {
                 delete require.cache[require.resolve(pkgJsonPath)]; 
                 pkgJson = require(pkgJsonPath);
+
             } else {
                 // TODO: Create package.json in cordova@7
             }
@@ -415,6 +398,7 @@ function getPlatformDetailsFromDir(dir, platformIfKnown){
 
         platform = platformFromName(pkg.name);
         version = pkg.version;
+        delete require.cache[pkgPath];
     } catch(e) {
         // Older platforms didn't have package.json.
         platform = platformIfKnown || platformFromName(path.basename(dir));


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