Repository: cordova-lib Updated Branches: refs/heads/master 465185b88 -> a20d770ab
CB-12592 : added requireNoCache function and replaced instances of delete.require cache This closes #536 Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/a20d770a Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/a20d770a Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/a20d770a Branch: refs/heads/master Commit: a20d770ab5d95f3cb2222654e07888887bce8ae3 Parents: 465185b Author: Audrey So <audre...@apache.org> Authored: Tue Mar 21 11:13:28 2017 -0700 Committer: Steve Gill <stevengil...@gmail.com> Committed: Wed Apr 5 13:29:34 2017 -0500 ---------------------------------------------------------------------- .../spec-cordova/pkgJson-restore.spec.js | 161 +++++++------------ cordova-lib/spec-cordova/pkgJson.spec.js | 135 ++++++---------- cordova-lib/spec-cordova/platform.spec.ios.js | 12 +- cordova-lib/src/cordova/platform.js | 20 +-- cordova-lib/src/cordova/plugin.js | 9 +- cordova-lib/src/cordova/util.js | 7 + 6 files changed, 129 insertions(+), 215 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a20d770a/cordova-lib/spec-cordova/pkgJson-restore.spec.js ---------------------------------------------------------------------- diff --git a/cordova-lib/spec-cordova/pkgJson-restore.spec.js b/cordova-lib/spec-cordova/pkgJson-restore.spec.js index cf42693..43880eb 100644 --- a/cordova-lib/spec-cordova/pkgJson-restore.spec.js +++ b/cordova-lib/spec-cordova/pkgJson-restore.spec.js @@ -22,6 +22,7 @@ events = require('cordova-common').events, ConfigParser = require('cordova-common').ConfigParser, cordova = require('../src/cordova/cordova'), + cordova_util = require('../src/cordova/util'), TIMEOUT = 60 * 1000; /** Testing will check if "cordova prepare" is restoring platforms and plugins as expected. @@ -47,7 +48,7 @@ describe('tests platform/spec restore with --save', function () { }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -67,7 +68,7 @@ describe('tests platform/spec restore with --save', function () { it('Test#000 : tests that the spec (~,^) is added and updated as expected in config.xml', function(done) { var cwd = process.cwd(); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var pkgJson; var androidPlatform = 'android'; var configXmlPath = path.join(cwd, 'config.xml'); @@ -80,8 +81,7 @@ describe('tests platform/spec restore with --save', function () { // Add platform with save, fetch return cordova.raw.platform('add', androidPlatform, {'save':true , 'fetch':true}); }).then(function() { - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Added platform properly to pkg.json expect(pkgJson.cordova.platforms).toBeDefined(); expect(pkgJson.cordova.platforms.indexOf(androidPlatform)).toBeGreaterThan(-1); @@ -106,8 +106,7 @@ describe('tests platform/spec restore with --save', function () { // Run cordova prepare return cordova.raw.prepare(); }).then(function() { - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // No changes to pkg.json spec for android. expect(pkgJson.dependencies['cordova-'+androidPlatform].charAt(0)).toEqual('^'); expect(pkgJson.cordova.platforms.indexOf(androidPlatform)).toBeGreaterThan(-1); @@ -164,26 +163,22 @@ describe('tests platform/spec restore with --save', function () { expect(engNames).toEqual([bPlatform]); expect(engSpec).toEqual([ 'https://github.com/apache/cordova-browser' ]); // Check that platform was added to pkg.json successfully. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); expect(pkgJson.cordova.platforms.indexOf('browser')).toBeDefined(); expect(pkgJson.dependencies['cordova-browser']).toEqual('git+https://github.com/apache/cordova-browser.git'); // Check that platform was added to platforms list successfully. - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); expect(platformsJson[bPlatform]).toBeDefined(); }).then(function() { // Remove platform without --save. return cordova.raw.platform('rm', bPlatform, {'fetch':true}); }).then(function() { // Platform in pkg.json should still be there. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); expect(pkgJson.cordova.platforms.indexOf('browser')).toBeDefined(); expect(pkgJson.dependencies['cordova-browser']).toEqual('git+https://github.com/apache/cordova-browser.git'); // Platform in platforms.json should not be there. - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); expect(platformsJson[bPlatform]).toBeUndefined(); }).then(function() { // Run cordova prepare @@ -201,13 +196,11 @@ describe('tests platform/spec restore with --save', function () { expect(engNames).toEqual([ 'browser' ]); expect(engSpec).toEqual([ 'git+https://github.com/apache/cordova-browser.git' ]); // No change to pkg.json. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); expect(pkgJson.cordova.platforms.indexOf('browser')).toBeDefined(); expect(pkgJson.dependencies['cordova-browser']).toEqual('git+https://github.com/apache/cordova-browser.git'); // Check that platform was restored to platform.json list successfully. - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); expect(platformsJson[bPlatform]).toBeDefined(); }).fail(function(err) { expect(err).toBeUndefined(); @@ -239,9 +232,8 @@ describe('tests platform/spec restore with --save', function () { configPlugin = cfg.getPlugin(configPlugins); expect(configPlugin.spec).toEqual('https://github.com/apache/cordova-plugin-splashscreen'); expect(configPlugin.name).toEqual('cordova-plugin-splashscreen'); - // Plugin was added to pkg.json successfully in plugin list and dependencies. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + // Plugin was added to pkg.json successfully in plugin list and dependencies. + pkgJson = cordova_util.requireNoCache(pkgJsonPath); expect(pkgJson.dependencies).toEqual({ 'cordova-plugin-splashscreen': 'git+https://github.com/apache/cordova-plugin-splashscreen.git' }); expect(pkgJson.cordova.plugins['cordova-plugin-splashscreen']).toBeDefined(); // Plugin was added to installed plugin list successfully. @@ -257,8 +249,7 @@ describe('tests platform/spec restore with --save', function () { expect(configPlugin.spec).toEqual('https://github.com/apache/cordova-plugin-splashscreen'); expect(configPlugin.name).toEqual('cordova-plugin-splashscreen'); // Plugin still in pkg.json plugin list and dependencies. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); expect(pkgJson.dependencies).toEqual({ 'cordova-plugin-splashscreen': 'git+https://github.com/apache/cordova-plugin-splashscreen.git' }); expect(pkgJson.cordova.plugins['cordova-plugin-splashscreen']).toBeDefined(); // Plugin was removed from the installed plugin list successfully. @@ -277,8 +268,7 @@ describe('tests platform/spec restore with --save', function () { expect(configPlugin.spec).toEqual('git+https://github.com/apache/cordova-plugin-splashscreen.git'); expect(configPlugin.name).toEqual('cordova-plugin-splashscreen'); // Pkg.json splashscreen dependency has no changes. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); expect (pkgJson.dependencies['cordova-plugin-splashscreen']).toEqual('git+https://github.com/apache/cordova-plugin-splashscreen.git'); expect(pkgJson.cordova.plugins['cordova-plugin-splashscreen']).toBeDefined(); // Plugin was restored and added to installed plugin list successfully. @@ -307,7 +297,7 @@ describe('tests platform/spec restore with --save', function () { }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -349,10 +339,8 @@ describe('tests platform/spec restore with --save', function () { return cordova.raw.platform('add', [helpers.testPlatform], {'save':true}); }).then(function() { // Delete any previous caches of require(package.json) and (platformsJson) - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); // Check the platform add of only helpers.testPlatform was successful in package.json. expect(pkgJson.cordova.platforms).toBeDefined(); expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1); @@ -369,10 +357,8 @@ describe('tests platform/spec restore with --save', function () { return cordova.raw.platform('rm', secondPlatformAdded); }).then(function() { // Delete any previous caches of require(pkgJson) and (platformsJson) - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check that the platform that was added with --save is still in package.json. expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1); // Check that both platforms were removed from the platforms.json. @@ -383,8 +369,7 @@ describe('tests platform/spec restore with --save', function () { return cordova.raw.prepare(); }).then(function() { // Delete any previous caches of platformsJson - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); // Expect "helpers.testPlatform" to be in the installed platforms list. expect(platformsJson[helpers.testPlatform]).toBeDefined(); // Expect that 'browser' will not be in platforms.json and has not been restored. @@ -414,7 +399,7 @@ describe('files should not be modified if their platforms are identical', functi }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -453,8 +438,7 @@ describe('files should not be modified if their platforms are identical', functi return elem.name; }); configEngArray = engNames.slice(); - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Expect android to be in both pkg.json and config.xml. expect(pkgJson.cordova.platforms.indexOf('android')).toBeGreaterThan(-1); expect(configEngArray.indexOf('android')).toBeGreaterThan(-1); @@ -485,7 +469,7 @@ describe('update pkg.json to include platforms in config.xml', function () { }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -510,7 +494,7 @@ describe('update pkg.json to include platforms in config.xml', function () { var cfg = new ConfigParser(configXmlPath); var engines = cfg.getEngines(); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var pkgJson = require(pkgJsonPath); var engNames = engines.map(function(elem) { return elem.name; @@ -531,8 +515,7 @@ describe('update pkg.json to include platforms in config.xml', function () { emptyPlatformList().then(function() { return cordova.raw.prepare(); }).then(function() { - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Expect 'browser' to be added to pkg.json. expect(pkgJson.cordova.platforms.indexOf(browserPlatform)).toBeGreaterThan(-1); // Expect 'android' to still be there in pkg.json. @@ -570,7 +553,7 @@ describe('update empty package.json to match config.xml', function () { }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -591,7 +574,7 @@ describe('update empty package.json to match config.xml', function () { var cwd = process.cwd(); var configXmlPath = path.join(cwd, 'config.xml'); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var cfg1 = new ConfigParser(configXmlPath); var engines = cfg1.getEngines(); var pkgJson = require(pkgJsonPath); @@ -616,8 +599,7 @@ describe('update empty package.json to match config.xml', function () { }); configEngArray = engNames.slice(); // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Expect no change to config.xml. expect(configEngArray.indexOf('android')).toBeGreaterThan(-1); // Expect cordova key and 'android' platform to be added to pkg.json. @@ -649,7 +631,7 @@ describe('update config.xml to include platforms in pkg.json', function () { }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -673,7 +655,7 @@ describe('update config.xml to include platforms in pkg.json', function () { var cfg1 = new ConfigParser(configXmlPath); var engines = cfg1.getEngines(); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var pkgJson = require(pkgJsonPath); var engNames = engines.map(function(elem) { return elem.name; @@ -691,8 +673,7 @@ describe('update config.xml to include platforms in pkg.json', function () { return cordova.raw.prepare(); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cfg2 = new ConfigParser(configXmlPath); engines = cfg2.getEngines(); engNames = engines.map(function(elem) { @@ -738,7 +719,7 @@ describe('update config.xml to use the variable found in pkg.json', function () }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -758,7 +739,7 @@ describe('update config.xml to use the variable found in pkg.json', function () var cwd = process.cwd(); var configXmlPath = path.join(cwd, 'config.xml'); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var cfg1 = new ConfigParser(configXmlPath); var configPlugins = cfg1.getPluginIdList(); var configPlugin = cfg1.getPlugin(configPlugins); @@ -781,8 +762,7 @@ describe('update config.xml to use the variable found in pkg.json', function () return cordova.raw.prepare(); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cfg2 = new ConfigParser(configXmlPath); configPlugins = cfg2.getPluginIdList(); configPlugin = cfg2.getPlugin(configPlugins); @@ -821,7 +801,7 @@ describe('update pkg.json to include plugin and variable found in config.xml', f }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -843,7 +823,7 @@ describe('update pkg.json to include plugin and variable found in config.xml', f var configXmlPath = path.join(cwd, 'config.xml'); var pkgJsonPath = path.join(cwd,'package.json'); // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var cfg1 = new ConfigParser(configXmlPath); var configPlugins = cfg1.getPluginIdList(); var configPlugin = cfg1.getPlugin(configPlugins); @@ -867,8 +847,7 @@ describe('update pkg.json to include plugin and variable found in config.xml', f return cordova.raw.prepare(); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cfg2 = new ConfigParser(configXmlPath); configPlugins = cfg2.getPluginIdList(); configPlugin = cfg2.getPlugin(configPlugins); @@ -908,7 +887,7 @@ describe('update pkg.json AND config.xml to include all plugins and merge unique }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -930,7 +909,7 @@ describe('update pkg.json AND config.xml to include all plugins and merge unique var cwd = process.cwd(); var configXmlPath = path.join(cwd, 'config.xml'); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var cfg1 = new ConfigParser(configXmlPath); var configPlugins = cfg1.getPluginIdList(); var pkgJson = require(pkgJsonPath); @@ -970,8 +949,7 @@ describe('update pkg.json AND config.xml to include all plugins and merge unique return cordova.raw.prepare(); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cfg2 = new ConfigParser(configXmlPath); configPlugins = cfg2.getPluginIdList(); // Check to make sure that variables were added as expected. @@ -1031,7 +1009,7 @@ describe('update pkg.json AND config.xml to include all plugins/merge variables }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -1053,7 +1031,7 @@ describe('update pkg.json AND config.xml to include all plugins/merge variables var cwd = process.cwd(); var configXmlPath = path.join(cwd, 'config.xml'); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var cfg1 = new ConfigParser(configXmlPath); var configPlugins = cfg1.getPluginIdList(); var pkgJson = require(pkgJsonPath); @@ -1100,8 +1078,7 @@ describe('update pkg.json AND config.xml to include all plugins/merge variables return cordova.raw.prepare(); }).then(function() { // Delete any previous caches of require(package.json) - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cfg2 = new ConfigParser(configXmlPath); configPlugins = cfg2.getPluginIdList(); @@ -1173,7 +1150,7 @@ describe('update config.xml to include the plugin that is in pkg.json', function }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -1194,7 +1171,7 @@ describe('update config.xml to include the plugin that is in pkg.json', function var cwd = process.cwd(); var configXmlPath = path.join(cwd, 'config.xml'); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var cfg = new ConfigParser(configXmlPath); var configPlugins = cfg.getPluginIdList(); var configPluginSpecs = cfg.getPlugin(configPlugins); @@ -1219,8 +1196,7 @@ describe('update config.xml to include the plugin that is in pkg.json', function return cordova.raw.prepare(); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cfg2 = new ConfigParser(configXmlPath); configPlugins = cfg2.getPluginIdList(); configPluginSpecs = cfg2.getPlugin(configPlugins); @@ -1325,8 +1301,7 @@ describe('platforms and plugins should be restored with config.xml even without configEngArray = engNames.slice(); expect(configEngArray.length === 2); // Delete previouc caches of (pkg.json). - delete require.cache[require.resolve(platformsFolderPath1)]; - platformsJson = require(platformsFolderPath1); + platformsJson = cordova_util.requireNoCache(platformsFolderPath1); // Browser should be installed expect(platformsJson).toBeDefined(); expect(platformsJson[androidPlatform]).not.toBeDefined(); @@ -1335,8 +1310,7 @@ describe('platforms and plugins should be restored with config.xml even without return cordova.raw.platform('rm', [browserPlatform]); }).then(function () { // Android should not be in the installed list (only browser). - delete require.cache[require.resolve(platformsFolderPath1)]; - platformsJson = require(platformsFolderPath1); + platformsJson = cordova_util.requireNoCache(platformsFolderPath1); expect(platformsJson).toBeDefined(); expect(platformsJson[browserPlatform]).toBeUndefined(); expect(platformsJson[androidPlatform]).not.toBeDefined(); @@ -1355,8 +1329,7 @@ describe('platforms and plugins should be restored with config.xml even without expect(configEngArray.indexOf(browserPlatform)).toBeGreaterThan(-1); expect(configEngArray.length === 2); // Expect that android and browser were restored. - delete require.cache[require.resolve(platformsFolderPath1)]; - platformsJson = require(platformsFolderPath1); + platformsJson = cordova_util.requireNoCache(platformsFolderPath1); expect(platformsJson[androidPlatform]).toBeDefined(); expect(platformsJson[browserPlatform]).toBeDefined(); }).then(function () { @@ -1418,7 +1391,7 @@ describe('tests platform/spec restore with --save', function () { }); afterEach(function() { - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -1448,7 +1421,7 @@ describe('tests platform/spec restore with --save', function () { it('Test#001 : should restore platform that has been removed from project', function(done) { var cwd = process.cwd(); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(pkgJsonPath); var pkgJson; var platformsFolderPath = path.join(cwd,'platforms/platforms.json'); var platformsJson; @@ -1457,11 +1430,9 @@ describe('tests platform/spec restore with --save', function () { // Add the testing platform with --save. return cordova.raw.platform('add', 'android', {'save':true}); }).then(function() { - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Require platformsFolderPath - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); // Check the platform add was successful in package.json. expect(pkgJson.cordova.platforms).toBeDefined(); expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1); @@ -1474,10 +1445,8 @@ describe('tests platform/spec restore with --save', function () { return cordova.raw.platform('rm', [helpers.testPlatform]); }).then(function() { // Delete any previous caches of require(package.json) and (platforms.json) - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check that the platform removed without --save is still in platforms key. expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1); // Check that the platform was removed from the platforms.json @@ -1487,8 +1456,7 @@ describe('tests platform/spec restore with --save', function () { return cordova.raw.prepare(); }).then(function() { // Delete any previous caches of platforms.json. - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); // Expect "helpers.testPlatform" to be in the installed platforms list in platforms.json. expect(platformsJson[helpers.testPlatform]).toBeDefined(); }).fail(function(err) { @@ -1518,10 +1486,8 @@ describe('tests platform/spec restore with --save', function () { return cordova.raw.platform('add',secondPlatformAdded, {'save':true}); }).then(function() { // Delete any previous caches of require(package.json) and (platforms.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); // Check the platform add of both platforms (to pkg.Json) was successful. expect(pkgJson.cordova.platforms).toBeDefined(); expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toEqual(0); @@ -1539,10 +1505,8 @@ describe('tests platform/spec restore with --save', function () { return cordova.raw.platform('rm', secondPlatformAdded); }).then(function() { // Delete any previous caches of require(package.json) and (platformsJson). - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check that ONLY the platform removed without --save is still in (pkg.json) platforms key. expect(pkgJson.cordova.platforms.indexOf(secondPlatformAdded)).toEqual(0); expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toEqual(-1); @@ -1554,8 +1518,7 @@ describe('tests platform/spec restore with --save', function () { return cordova.raw.prepare(); }).then(function() { // Delete any previous caches of platformsJson. - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); // Expect "helpers.testPlatform" to be in the installed platforms list in platforms.json expect(platformsJson[helpers.testPlatform]).toBeUndefined(); // Expect 'browser' not to be in platforms.json and has not been restored. http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a20d770a/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 362c216..a3dbd5c 100644 --- a/cordova-lib/spec-cordova/pkgJson.spec.js +++ b/cordova-lib/spec-cordova/pkgJson.spec.js @@ -23,6 +23,7 @@ var helpers = require('./helpers'), ConfigParser = require('cordova-common').ConfigParser, cordova = require('../src/cordova/cordova'), TIMEOUT = 30 * 1000, + cordova_util = require('../src/cordova/util'), semver = require('semver'); function includeFunc(container, value) { @@ -79,8 +80,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('add', pluginId+'@1.1.2', {'save':true, 'fetch':true}) .then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check that the plugin and spec add was successful to pkg.json. expect(pkgJson).toBeDefined(); expect(pkgJson.cordova.plugins).toBeDefined(); @@ -97,8 +97,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('rm', pluginId, {'save':true, 'fetch':true}); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Checking that the plugin removed is in not in the pkg.json. expect(pkgJson.cordova.plugins[pluginId]).toBeUndefined(); // Spec should be removed from dependencies. @@ -121,8 +120,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('add', pluginId); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check the plugin add was successful for the first plugin that had --save. expect(pkgJson).not.toBeUndefined(); expect(pkgJson.cordova.plugins['cordova-plugin-camera']).toBeDefined(); @@ -143,8 +141,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('add', pluginId, {'save':true}) .then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check the platform add was successful. expect(pkgJson).not.toBeUndefined(); expect(pkgJson.cordova.plugins).toBeDefined(); @@ -154,8 +151,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('rm', 'cordova-plugin-device'); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // The plugin should still be in package.json. expect(pkgJson.cordova.plugins[pluginId]).toBeDefined(); }).fail(function(err) { @@ -174,8 +170,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('add', pluginId, {'save':true, 'cli_variables': {'someKey':'someValue'}}) .then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check the plugin add was successful and that variables have been added too. expect(pkgJson).not.toBeUndefined(); expect(pkgJson.cordova.plugins).toBeDefined(); @@ -186,8 +181,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('rm', pluginId, {'save':true}); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Checking that the plugin and variables were removed successfully. expect(pkgJson.cordova.plugins[pluginId]).toBeUndefined(); }).fail(function(err) { @@ -198,10 +192,8 @@ describe('plugin end-to-end', function() { // CB-12170 : Test is commented out because not promisified correctly in cordova-create script xit('Test#005 : should successfully add and remove multiple plugins with save & fetch', function(done) { var pkgJsonPath = path.join(process.cwd(),'package.json'); - var pkgJson; // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + var pkgJson = cordova_util.requireNoCache(pkgJsonPath); expect(pkgJsonPath).toExist(); @@ -209,8 +201,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('add', [pluginId,'cordova-plugin-device-motion'], {'save':true, 'fetch':true}) .then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check that the plugin add was successful. expect(pkgJson).not.toBeUndefined(); expect(pkgJson.cordova.plugins).not.toBeUndefined(); @@ -223,8 +214,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('rm', [pluginId,'cordova-plugin-device-motion'], {'save':true, 'fetch':true}); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Checking that the plugin removed is in not in the platforms. expect(pkgJson.cordova.plugins[pluginId]).toBeUndefined(); expect(pkgJson.cordova.plugins['cordova-plugin-device-motion']).toBeUndefined(); @@ -243,9 +233,8 @@ describe('plugin end-to-end', function() { var iosDirectory = path.join(cwd, 'platforms/ios/cordova/version'); var configXmlPath = path.join(cwd, 'config.xml'); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + var pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cfg = new ConfigParser(configXmlPath); - var pkgJson = require(pkgJsonPath); var engines = cfg.getEngines(); var engNames; var engSpec; @@ -263,8 +252,7 @@ describe('plugin end-to-end', function() { return cordova.raw.platform('add', ['ios'], {'save':true, 'fetch':true}) .then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Pkg.json has ios. expect(pkgJson.cordova.platforms).toEqual([iosPlatform]); // Config.xml and ios/cordova/version check. @@ -278,8 +266,7 @@ describe('plugin end-to-end', function() { }); expect(engNames).toEqual([ 'ios' ]); // delete previous caches of iosVersion; - delete require.cache[require.resolve(iosDirectory)]; - iosVersion = require(iosDirectory); + iosVersion = cordova_util.requireNoCache(iosDirectory); engSpec = engines.map(function(elem) { // Check that config and ios/cordova/version versions "satify" each other. expect(semver.satisfies(iosVersion.version, elem.spec)).toEqual(true); @@ -293,13 +280,11 @@ describe('plugin end-to-end', function() { configPlugins = cfg3.getPluginIdList(); configPlugin = cfg3.getPlugin(configPlugins); // Delete previous caches of pluginPkgJson. - delete require.cache[require.resolve(pluginPkgJsonDir)]; - pluginPkgJsonVersion = require(pluginPkgJsonDir); + pluginPkgJsonVersion = cordova_util.requireNoCache(pluginPkgJsonDir); // Check that version in plugin pkg.json and config version "satisfy" each other. expect(semver.satisfies(pluginPkgJsonVersion.version, configPlugin.spec)).toEqual(true); // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check that pkg.json and plugin pkg.json versions "satisfy". expect(semver.satisfies(pluginPkgJsonVersion.version, pkgJson.dependencies['cordova-ios'])); }).fail(function(err) { @@ -322,13 +307,12 @@ describe('plugin end-to-end', function() { var engNames; var engSpec; - delete require.cache[require.resolve(pkgJsonPath)]; + cordova_util.requireNoCache(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); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Pkg.json has browser. expect(pkgJson.cordova.platforms).toEqual(['browser']); @@ -352,8 +336,7 @@ describe('plugin end-to-end', function() { return cordova.raw.plugin('add', pluginPath, {'save':true, 'fetch':true}); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Pkg.json has geolocation plugin. expect(pkgJson.cordova.plugins['cordova-lib-test-plugin']).toBeDefined(); @@ -397,7 +380,7 @@ describe('platform end-to-end with --save', function () { afterEach(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -438,8 +421,7 @@ describe('platform end-to-end with --save', function () { return cordova.raw.platform('rm', [helpers.testPlatform], {'save':true}); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Checking that the platform removed is in not in the platforms key. expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toEqual(-1); }).fail(function(err) { @@ -450,15 +432,14 @@ describe('platform end-to-end with --save', function () { it('Test#007 : should not remove platforms from package.json when removing without --save', function(done) { var pkgJsonPath = path.join(process.cwd(),'package.json'); expect(pkgJsonPath).toExist(); - delete require.cache[require.resolve(pkgJsonPath)]; - var pkgJson = require(pkgJsonPath); + var pkgJson = cordova_util.requireNoCache(pkgJsonPath); + emptyPlatformList().then(function() { // Add the testing platform with --save. return cordova.raw.platform('add', [helpers.testPlatform], {'save':true}); }).then(function() { // Check the platform add was successful. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); expect(pkgJson.cordova.platforms).not.toBeUndefined(); expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1); }).then(fullPlatformList) // Platform should still be in platform ls. @@ -467,8 +448,7 @@ describe('platform end-to-end with --save', function () { return cordova.raw.platform('rm', [helpers.testPlatform]); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check that the platform removed without --save is still in platforms key. expect(pkgJson.cordova.platforms.indexOf(helpers.testPlatform)).toBeGreaterThan(-1); }).then(emptyPlatformList) @@ -479,19 +459,16 @@ describe('platform end-to-end with --save', function () { it('Test#008 : should not add platform to package.json when adding without --save', function(done) { var pkgJsonPath = path.join(process.cwd(),'package.json'); - var pkgJson; expect(pkgJsonPath).toExist(); // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + var pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Pkg.json "platforms" should be empty and helpers.testPlatform should not exist in pkg.json. expect(pkgJson.cordova).toBeUndefined(); // Add platform without --save. cordova.raw.platform('add',[helpers.testPlatform]) .then(function() { // Check the platform add was successful, reload, skipping cache. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // PkgJson.cordova should not be defined and helpers.testPlatform should NOT have been added. expect(pkgJson.cordova).toBeUndefined(); }).then(fullPlatformList) @@ -514,8 +491,7 @@ describe('platform end-to-end with --save', function () { return cordova.raw.platform('add', [helpers.testPlatform], {'save':true}); }).then(function() { // Check the platform add was successful, reload, skipping cache. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Beware empty/missing cordova object. var pkgJsonCordova = pkgJson.cordova || {platforms:[]}; // Check that only the platform added with --save was added to package.json. @@ -533,8 +509,7 @@ describe('platform end-to-end with --save', function () { expect(pkgJsonPath).toExist(); var pkgJson; // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cwd = process.cwd(); var configXmlPath = path.join(cwd, 'config.xml'); var cfg = new ConfigParser(configXmlPath); @@ -553,8 +528,7 @@ describe('platform end-to-end with --save', function () { return cordova.raw.platform('add', ['android@6.1.0', 'browser@4.1.0'], {'save':true, 'fetch':true}); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check the platform add was successful in platforms list and in dependencies. expect(pkgJson.cordova.platforms).toBeDefined(); expect(pkgJson.cordova.platforms.indexOf('android')).toEqual(0); @@ -581,8 +555,7 @@ describe('platform end-to-end with --save', function () { return cordova.raw.platform('rm', ['android', 'browser'], {'save':true, 'fetch':true}); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Checking that the platform removed is in not in the platforms key. expect(pkgJson.cordova.platforms.indexOf('android')).toEqual(-1); expect(pkgJson.cordova.platforms.indexOf('browser')).toEqual(-1); @@ -624,7 +597,7 @@ describe('During add, if pkg.json has a platform/plugin spec, use that one.', fu afterEach(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -649,9 +622,8 @@ describe('During add, if pkg.json has a platform/plugin spec, use that one.', fu var platformsFolderPath = path.join(cwd,'platforms/platforms.json'); var configXmlPath = path.join(cwd, 'config.xml'); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + var pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cfg = new ConfigParser(configXmlPath); - var pkgJson = require(pkgJsonPath); var engines = cfg.getEngines(); var engNames; var engSpec; @@ -672,12 +644,10 @@ describe('During add, if pkg.json has a platform/plugin spec, use that one.', fu return cordova.raw.platform('add', [iosPlatform], {'save':true , 'fetch':true}); }).then(function() { // Require platformsFolderPath, ios and spec should be in there. - delete require.cache[require.resolve(platformsFolderPath)]; - platformsJson = require(platformsFolderPath); + platformsJson = cordova_util.requireNoCache(platformsFolderPath); // Delete any previous caches of require(package.json). // ios has been added. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // No change to pkg.json platforms or spec for ios. expect(pkgJson.cordova.platforms).toEqual([iosPlatform]); // Config.xml and ios/cordova/version check. @@ -692,8 +662,7 @@ describe('During add, if pkg.json has a platform/plugin spec, use that one.', fu }); engSpec = engines.map(function(elem) { // Check that config and ios/cordova/version versions "satify" each other. - delete require.cache[require.resolve(iosDirectory)]; - iosVersion = require(iosDirectory); + iosVersion = cordova_util.requireNoCache(iosDirectory); expect(semver.satisfies(iosVersion.version, elem.spec)).toEqual(true); // Check that config and platforms.json "satisfy". expect(semver.satisfies(platformsJson[iosPlatform], elem.spec)).toEqual(true); @@ -708,11 +677,9 @@ describe('During add, if pkg.json has a platform/plugin spec, use that one.', fu // Add splashscreen plugin with --save --fetch. return cordova.raw.plugin('add', 'cordova-plugin-splashscreen', {'save':true, 'fetch':true}); }).then(function() { - delete require.cache[require.resolve(pluginPkgJsonDir)]; - pluginPkgJsonVersion = require(pluginPkgJsonDir); + pluginPkgJsonVersion = cordova_util.requireNoCache(pluginPkgJsonDir); // Check that pkg.json version and plugin pkg.json version "satisfy" each other. - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); expect(semver.satisfies(pluginPkgJsonVersion.version, pkgJson.dependencies['cordova-plugin-splashscreen'])).toEqual(true); }).fail(function(err) { expect(err).toBeUndefined(); @@ -781,8 +748,7 @@ describe('During add, if config.xml has a platform/plugin spec and pkg.json does return cordova.raw.platform('add', [iosPlatform], {'save':true , 'fetch':true}); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // pkg.json has new platform. expect(pkgJson.cordova.platforms).toEqual([iosPlatform]); // Config.xml and ios/cordova/version check. @@ -796,8 +762,7 @@ describe('During add, if config.xml has a platform/plugin spec and pkg.json does // Config.xml has ios platform. expect(engNames).toEqual([ 'ios' ]); engSpec = engines.map(function(elem) { - delete require.cache[require.resolve(iosDirectory)]; - iosVersion = require(iosDirectory); + iosVersion = cordova_util.requireNoCache(iosDirectory); // Config and ios/cordova/version versions "satisfy" each other. expect(semver.satisfies(iosVersion.version, elem.spec)).toEqual(true); }); @@ -812,8 +777,7 @@ describe('During add, if config.xml has a platform/plugin spec and pkg.json does expect(configPlugins.length).toEqual(1); // Splashscreen plugin and spec added. expect(configPlugin.name).toEqual('cordova-plugin-splashscreen'); - delete require.cache[require.resolve(pluginPkgJsonDir)]; - pluginPkgJsonVersion = require(pluginPkgJsonDir); + pluginPkgJsonVersion = cordova_util.requireNoCache(pluginPkgJsonDir); // Check that version in plugin pkg.json and config version "satisfy" each other. expect(semver.satisfies(pluginPkgJsonVersion.version, configPlugin.spec)).toEqual(true); }).fail(function(err) { @@ -841,7 +805,7 @@ describe('During add, if add specifies a platform spec, use that one regardless afterEach(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(path.join(process.cwd(),'package.json'))]; + cordova_util.requireNoCache(path.join(process.cwd(),'package.json')); process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows. shell.rm('-rf', tmpDir); }); @@ -865,9 +829,8 @@ describe('During add, if add specifies a platform spec, use that one regardless var iosDirectory = path.join(cwd, 'platforms/ios/cordova/version'); var configXmlPath = path.join(cwd, 'config.xml'); var pkgJsonPath = path.join(cwd,'package.json'); - delete require.cache[require.resolve(pkgJsonPath)]; + var pkgJson = cordova_util.requireNoCache(pkgJsonPath); var cfg = new ConfigParser(configXmlPath); - var pkgJson = require(pkgJsonPath); var engines = cfg.getEngines(); var engNames; var configPlugins = cfg.getPluginIdList(); @@ -886,8 +849,7 @@ describe('During add, if add specifies a platform spec, use that one regardless return cordova.raw.platform('add', ['ios@4.3.0'], {'save':true , 'fetch':true}); }).then(function() { // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Pkg.json has ios. expect(pkgJson.cordova.platforms).toEqual([iosPlatform]); // Config.xml and ios/cordova/version check. @@ -901,8 +863,7 @@ describe('During add, if add specifies a platform spec, use that one regardless // Config.xml has ios platform. expect(engNames).toEqual([ 'ios' ]); // delete previous caches of iosVersion; - delete require.cache[require.resolve(iosDirectory)]; - iosVersion = require(iosDirectory); + iosVersion = cordova_util.requireNoCache(iosDirectory); // Check that pkg.json and ios/cordova/version versions "satisfy" each other. expect(semver.satisfies(iosVersion.version, pkgJson.dependencies['cordova-ios'])).toEqual(true); }).then(function() { @@ -915,13 +876,11 @@ describe('During add, if add specifies a platform spec, use that one regardless configPlugin = cfg3.getPlugin(configPlugins); // Delete previous caches of pluginPkgJson. - delete require.cache[require.resolve(pluginPkgJsonDir)]; - pluginPkgJsonVersion = require(pluginPkgJsonDir); + pluginPkgJsonVersion = cordova_util.requireNoCache(pluginPkgJsonDir); // Check that version in plugin pkg.json and config version "satisfy" each other. expect(semver.satisfies(pluginPkgJsonVersion.version, configPlugin.spec)).toEqual(true); // Delete any previous caches of require(package.json). - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); // Check that pkg.json and plugin pkg.json versions "satisfy". expect(semver.satisfies(pluginPkgJsonVersion.version, pkgJson.dependencies['cordova-ios'])); }).fail(function(err) { http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a20d770a/cordova-lib/spec-cordova/platform.spec.ios.js ---------------------------------------------------------------------- diff --git a/cordova-lib/spec-cordova/platform.spec.ios.js b/cordova-lib/spec-cordova/platform.spec.ios.js index d46fc6a..6934a8f 100644 --- a/cordova-lib/spec-cordova/platform.spec.ios.js +++ b/cordova-lib/spec-cordova/platform.spec.ios.js @@ -21,6 +21,7 @@ var helpers = require('./helpers'), path = require('path'), fs = require('fs'), shell = require('shelljs'), + cordova_util = ('../src/cordova/util'), cordova = require('../src/cordova/cordova'); @@ -89,8 +90,7 @@ describe('cocoapod plugin add and rm end-to-end', function () { expect(podfileContent.indexOf(CWStatusBarNotification) !== -1); expect(numberOfTimesAFNetworkingIsInPodfile).toEqual(1); - delete require.cache[require.resolve(podsJSON)]; - var podsJSONContent = require(podsJSON); + var podsJSONContent = cordova_util.requireNoCache(podsJSON); var countPropertyOfAFNetworkingInPodsJSON = podsJSONContent[AFNetworking].count; var specPropertyOfAFNetworkingInPodsJSON = podsJSONContent[AFNetworking].spec; @@ -108,8 +108,7 @@ describe('cocoapod plugin add and rm end-to-end', function () { expect(podfileContent.indexOf(CWStatusBarNotification) === -1); expect(podfileContent.indexOf(AFNetworking) !== -1); - delete require.cache[require.resolve(podsJSON)]; - var podsJSONContent = require(podsJSON); + var podsJSONContent = cordova_util.requireNoCache(podsJSON); expect(podsJSONContent[AFNetworking]); expect(podsJSONContent[CWStatusBarNotification] === undefined); @@ -118,14 +117,13 @@ describe('cocoapod plugin add and rm end-to-end', function () { }) .then(function() { //expect no pods - delete require.cache[require.resolve(podfile)]; + cordova_util.requireNoCache(podfile); var podfileContent = fs.readFileSync(podfile, {'encoding' : 'utf8'}); expect(podfileContent.indexOf(CWStatusBarNotification) === -1); expect(podfileContent.indexOf(AFNetworking) === -1); - delete require.cache[require.resolve(podsJSON)]; - var podsJSONContent = require(podsJSON); + var podsJSONContent = cordova_util.requireNoCache(podsJSON); expect(podsJSONContent[AFNetworking] === undefined); expect(podsJSONContent[CWStatusBarNotification] === undefined); http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a20d770a/cordova-lib/src/cordova/platform.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/cordova/platform.js b/cordova-lib/src/cordova/platform.js index bc616ef..aa84948 100644 --- a/cordova-lib/src/cordova/platform.js +++ b/cordova-lib/src/cordova/platform.js @@ -113,8 +113,7 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) { } 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')); + pkgJson = cordova_util.requireNoCache(path.join(projectRoot, 'package.json')); } // If there is no spec specified, try to get spec from package.json @@ -125,7 +124,6 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) { } else if (pkgJson.dependencies[platform]) { spec = pkgJson.dependencies[platform]; } - 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); @@ -153,9 +151,6 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) { } return downloadPlatform(projectRoot, platform, spec, opts); }).then(function(platDetails) { - if(fs.existsSync(path.join(projectRoot, 'package.json'))) { - delete require.cache[require.resolve(path.join(projectRoot, 'package.json'))]; - } platform = platDetails.platform; var platformPath = path.join(projectRoot, 'platforms', platform); var platformAlreadyAdded = fs.existsSync(platformPath); @@ -208,8 +203,7 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) { // basically a valid NodeJS script but intended to be used as a regular // executable script. if (path.basename(apiEntryPoint) === 'Api.js') { - delete require.cache[apiEntryPoint]; - PlatformApi = require(apiEntryPoint); + PlatformApi = cordova_util.requireNoCache(apiEntryPoint); events.emit('verbose', 'PlatformApi successfully found for platform ' + platform); } } catch (e) { @@ -281,8 +275,7 @@ function addHelper(cmd, hooksRunner, projectRoot, targets, opts) { var pkgJsonPath = path.join(projectRoot, 'package.json'); var modifiedPkgJson = false; if(fs.existsSync(pkgJsonPath)) { - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(path.join(pkgJsonPath)); } else { // TODO: Create package.json in cordova@7 } @@ -410,11 +403,9 @@ function getPlatformDetailsFromDir(dir, platformIfKnown){ try { var pkgPath = path.join(libDir, 'package.json'); - delete require.cache[pkgPath]; - var pkg = require(pkgPath); + var pkg = cordova_util.requireNoCache(pkgPath); 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)); @@ -465,8 +456,7 @@ function remove(hooksRunner, projectRoot, targets, opts) { var pkgJsonPath = path.join(projectRoot,'package.json'); // If statement to see if pkgJsonPath exists in the filesystem if(fs.existsSync(pkgJsonPath)) { - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); } if(opts.save || autosave){ targets.forEach(function(target) { http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a20d770a/cordova-lib/src/cordova/plugin.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/cordova/plugin.js b/cordova-lib/src/cordova/plugin.js index 0cfb3d7..377e97b 100644 --- a/cordova-lib/src/cordova/plugin.js +++ b/cordova-lib/src/cordova/plugin.js @@ -238,8 +238,7 @@ module.exports = function plugin(command, targets, opts) { // If statement to see if pkgJsonPath exists in the filesystem if(fs.existsSync(pkgJsonPath)) { // Delete any previous caches of require(package.json) - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); } else { // Create package.json in cordova@7 } @@ -323,8 +322,7 @@ module.exports = function plugin(command, targets, opts) { // If statement to see if pkgJsonPath exists in the filesystem if(fs.existsSync(pkgJsonPath)) { //delete any previous caches of require(package.json) - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); } else { // Create package.json in cordova@7 } @@ -392,8 +390,7 @@ function determinePluginTarget(projectRoot, cfg, target, fetchOptions) { // Require project pkgJson. var pkgJsonPath = path.join(projectRoot, 'package.json'); if(fs.existsSync(pkgJsonPath)) { - delete require.cache[require.resolve(pkgJsonPath)]; - pkgJson = require(pkgJsonPath); + pkgJson = cordova_util.requireNoCache(pkgJsonPath); } // If no parsedSpec.version, use the one from pkg.json or config.xml. http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/a20d770a/cordova-lib/src/cordova/util.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/cordova/util.js b/cordova-lib/src/cordova/util.js index 67552c8..2b1aa7f 100644 --- a/cordova-lib/src/cordova/util.js +++ b/cordova-lib/src/cordova/util.js @@ -75,7 +75,14 @@ exports.isUrl = isUrl; exports.getLatestMatchingNpmVersion = getLatestMatchingNpmVersion; exports.getAvailableNpmVersions = getAvailableNpmVersions; exports.getInstalledPlatformsWithVersions = getInstalledPlatformsWithVersions; +exports.requireNoCache = requireNoCache; +function requireNoCache(pkgJsonPath) { + delete require.cache[require.resolve(pkgJsonPath)]; + var returnVal = require(pkgJsonPath); + delete require.cache[require.resolve(pkgJsonPath)]; + return returnVal; +} function isUrl(value) { var u = value && url.parse(value); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For additional commands, e-mail: commits-h...@cordova.apache.org