You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by er...@apache.org on 2020/04/11 05:04:58 UTC
[cordova-electron] branch master updated: breaking: restructure the
platform lib code (#142)
This is an automated email from the ASF dual-hosted git repository.
erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-electron.git
The following commit(s) were added to refs/heads/master by this push:
new c35713f breaking: restructure the platform lib code (#142)
c35713f is described below
commit c35713fa839e8afef999a8de6a554977fa828089
Author: エリス <er...@users.noreply.github.com>
AuthorDate: Sat Apr 11 14:04:51 2020 +0900
breaking: restructure the platform lib code (#142)
* refactor (create.js): relocate to package root
* refactor (update.js): relocate to package root
* refactor (parser.js): relocate to package root
* refactor (handler.js): relocate to package root
* refactor (util.js): relocate to package root
* refactor (SettingJsonParser.js): relocate to package root
* refactor (ManifestJsonParser.js): relocate to package root
* refactor (PackageJsonParser.js): relocate to package root
* refactor (check_reqs.js): relocate to package root
* refactor (run.js): relocate to package root
* refactor (clean.js): relocate to package root
* refactor (prepare.js): relocate to package root
* refactor (Api.js): relocate to package root
* added Api.js template mapper to package Api.js
* refactor (build.js): relocate to package root
* updated build run to throw error instead of console.log
* refactor (run): fix run command
---
bin/templates/cordova/Api.js | 366 +--------------------
{bin/templates/cordova => lib}/Api.js | 16 +-
.../cordova/lib => lib}/ManifestJsonParser.js | 0
.../cordova/lib => lib}/PackageJsonParser.js | 0
.../cordova/lib => lib}/SettingJsonParser.js | 0
{bin/templates/cordova/lib => lib}/build.js | 4 +-
{bin/templates/cordova/lib => lib}/build/base.json | 0
.../cordova/lib => lib}/build/darwin.json | 0
.../templates/cordova/lib => lib}/build/linux.json | 0
.../templates/cordova/lib => lib}/build/win32.json | 0
{bin/templates/cordova/lib => lib}/check_reqs.js | 0
{bin/templates/cordova/lib => lib}/clean.js | 0
{bin/lib => lib}/create.js | 10 +-
{bin/templates/cordova => lib}/handler.js | 0
{bin/templates/cordova => lib}/parser.js | 0
{bin/templates/cordova/lib => lib}/prepare.js | 0
{bin/templates/cordova/lib => lib}/run.js | 2 +-
{bin/lib => lib}/update.js | 0
{bin/templates/cordova/lib => lib}/util.js | 0
package.json | 2 +-
tests/spec/unit/{ => lib}/Api.spec.js | 26 +-
.../cordova => }/lib/ManifestJsonParser.spec.js | 12 +-
.../cordova => }/lib/PackageJsonParser.spec.js | 13 +-
.../cordova => }/lib/SettingJsonParser.spec.js | 10 +-
.../unit/{templates/cordova => }/lib/build.spec.js | 43 ++-
tests/spec/unit/lib/clean.spec.js | 102 ++++++
tests/spec/unit/{ => lib}/create.spec.js | 7 +-
tests/spec/unit/{ => lib}/handler.spec.js | 5 +-
tests/spec/unit/{templates => lib}/parser.spec.js | 5 +-
.../{templates/cordova => }/lib/prepare.spec.js | 13 +-
.../unit/{templates/cordova => }/lib/run.spec.js | 18 +-
tests/spec/unit/lib/update.spec.js | 6 +-
.../unit/{templates/cordova => }/lib/util.spec.js | 6 +-
.../spec/unit/templates/cordova/lib/clean.spec.js | 127 -------
34 files changed, 219 insertions(+), 574 deletions(-)
diff --git a/bin/templates/cordova/Api.js b/bin/templates/cordova/Api.js
index bdd033f..2d2cd99 100644
--- a/bin/templates/cordova/Api.js
+++ b/bin/templates/cordova/Api.js
@@ -17,364 +17,14 @@
under the License.
*/
-/*
- this file is found by cordova-lib when you attempt to
- 'cordova platform add PATH' where path is this repo.
-*/
-const path = require('path');
-const fs = require('fs-extra');
-const {
- ActionStack,
- ConfigChanges: { PlatformMunger },
- CordovaError,
- CordovaLogger,
- events: selfEvents,
- PlatformJson,
- PluginInfoProvider
-} = require('cordova-common');
-const Parser = require('./parser');
-
-function setupEvents (externalEventEmitter) {
- if (externalEventEmitter) {
- // This will make the platform internal events visible outside
- selfEvents.forwardEventsTo(externalEventEmitter);
- return externalEventEmitter;
- }
-
- // There is no logger if external emitter is not present,
- // so attach a console logger
- CordovaLogger.get().subscribe(selfEvents);
- return selfEvents;
-}
-
-class Api {
- constructor (platform, platformRootDir, events) {
- this.platform = 'electron';
-
- // MyApp/platforms/electron
- this.root = platformRootDir || path.resolve(__dirname, '..');
- this.events = setupEvents(events);
- this.parser = new Parser(this.root);
- this.handler = require('./handler');
-
- this.locations = {
- platformRootDir,
- root: this.root,
- www: path.join(this.root, 'www'),
- res: path.join(this.root, 'res'),
- platformWww: path.join(this.root, 'platform_www'),
- configXml: path.join(this.root, 'config.xml'),
- defaultConfigXml: path.join(this.root, 'cordova/defaults.xml'),
- build: path.join(this.root, 'build'),
- buildRes: path.join(this.root, 'build-res'),
- cache: path.join(this.root, 'cache'),
- // NOTE: Due to platformApi spec we need to return relative paths here
- cordovaJs: 'bin/templates/project/assets/www/cordova.js',
- cordovaJsSrc: 'cordova-js-src'
- };
-
- this._platformJson = PlatformJson.load(this.root, this.platform);
- this._pluginInfoProvider = new PluginInfoProvider();
- this._munger = new PlatformMunger(this.platform, this.root, this._platformJson, this._pluginInfoProvider);
- }
-
- getPlatformInfo () {
- return {
- locations: this.locations,
- root: this.root,
- name: this.platform,
- version: Api.version(),
- projectConfig: this.config
- };
- }
-
- prepare (cordovaProject, options) {
- return require('./lib/prepare').prepare.call(this, cordovaProject, options);
- }
-
- addPlugin (pluginInfo, installOptions) {
- if (!pluginInfo) {
- return Promise.reject(new Error('Missing plugin info parameter. The first parameter should contain a valid PluginInfo instance.'));
- }
-
- installOptions = installOptions || {};
- installOptions.variables = installOptions.variables || {};
- // CB-10108 platformVersion option is required for proper plugin installation
- installOptions.platformVersion = installOptions.platformVersion ||
- this.getPlatformInfo().version;
-
- const actions = new ActionStack();
-
- let platform = this.platform;
- if (!pluginInfo.getPlatformsArray().includes(platform)) { // if `cordova-electron` is not defined in plugin.xml, `browser` is used instead.
- platform = 'browser';
- }
-
- // gather all files needs to be handled during install
- pluginInfo.getFilesAndFrameworks(platform)
- .concat(pluginInfo.getAssets(platform))
- .concat(pluginInfo.getJsModules(platform))
- .forEach((item) => {
- actions.push(actions.createAction(
- this._getInstaller(item.itemType),
- [item, pluginInfo.dir, pluginInfo.id, installOptions],
- this._getUninstaller(item.itemType),
- [item, pluginInfo.dir, pluginInfo.id, installOptions]));
- });
-
- // run through the action stack
- return actions.process(platform, this.root)
- .then(() => {
- // Add PACKAGE_NAME variable into vars
- if (!installOptions.variables.PACKAGE_NAME) {
- installOptions.variables.PACKAGE_NAME = this.handler.package_name(this.root);
- }
-
- this._munger
- // Ignore passed `is_top_level` option since platform itself doesn't know
- // anything about managing dependencies - it's responsibility of caller.
- .add_plugin_changes(pluginInfo, installOptions.variables, /* is_top_level= */true, /* should_increment= */true)
- .save_all();
-
- const targetDir = installOptions.usePlatformWww ? this.getPlatformInfo().locations.platformWww : this.getPlatformInfo().locations.www;
-
- this._addModulesInfo(platform, pluginInfo, targetDir);
- });
- }
-
- removePlugin (pluginInfo, uninstallOptions) {
- if (!pluginInfo) {
- return Promise.reject(new Error('Missing plugin info parameter. The first parameter should contain a valid PluginInfo instance.'));
- }
-
- uninstallOptions = uninstallOptions || {};
- // CB-10108 platformVersion option is required for proper plugin installation
- uninstallOptions.platformVersion = uninstallOptions.platformVersion || this.getPlatformInfo().version;
-
- const actions = new ActionStack();
-
- let platform = this.platform;
- if (!pluginInfo.getPlatformsArray().includes(platform)) { // if `cordova-electron` is not defined in plugin.xml, `browser` is used instead.
- platform = 'browser';
- }
-
- // queue up plugin files
- pluginInfo.getFilesAndFrameworks(platform)
- .concat(pluginInfo.getAssets(platform))
- .concat(pluginInfo.getJsModules(platform))
- .forEach((item) => {
- actions.push(actions.createAction(
- this._getUninstaller(item.itemType), [item, pluginInfo.dir, pluginInfo.id, uninstallOptions],
- this._getInstaller(item.itemType), [item, pluginInfo.dir, pluginInfo.id, uninstallOptions]));
- });
-
- // run through the action stack
- return actions.process(platform, this.root)
- .then(() => {
- this._munger
- // Ignore passed `is_top_level` option since platform itself doesn't know
- // anything about managing dependencies - it's responsibility of caller.
- .remove_plugin_changes(pluginInfo, /* is_top_level= */true)
- .save_all();
-
- const targetDir = uninstallOptions.usePlatformWww
- ? this.getPlatformInfo().locations.platformWww
- : this.getPlatformInfo().locations.www;
-
- this._removeModulesInfo(pluginInfo, targetDir);
- // Remove stale plugin directory
- // @todo this should be done by plugin files uninstaller
- fs.removeSync(path.resolve(this.root, 'Plugins', pluginInfo.id));
- });
- }
-
- _getInstaller (type) {
- return (item, plugin_dir, plugin_id, options, project) => {
- const installer = this.handler[type];
-
- if (!installer) {
- this.events.emit('warn', `Unrecognized type "${type}"`);
- } else {
- const wwwDest = options.usePlatformWww
- ? this.getPlatformInfo().locations.platformWww
- : this.handler.www_dir(this.root);
- if (type === 'asset') {
- installer.install(item, plugin_dir, wwwDest);
- } else if (type === 'js-module') {
- installer.install(item, plugin_dir, plugin_id, wwwDest);
- } else {
- installer.install(item, plugin_dir, this.root, plugin_id, options, project);
- }
- }
- };
- }
+const { resolve } = require('path');
- _getUninstaller (type) {
- return (item, plugin_dir, plugin_id, options, project) => {
- const installer = this.handler[type];
+// Setting up some global defaults to share accross all files
+global.cdvPlatformPath = resolve(__dirname, '..');
+global.cdvProjectPath = resolve(__dirname, '../../..');
- if (!installer) {
- this.events.emit('warn', `electron plugin uninstall: unrecognized type, skipping : ${type}`);
- } else {
- const wwwDest = options.usePlatformWww
- ? this.getPlatformInfo().locations.platformWww
- : this.handler.www_dir(this.root);
-
- if (['asset', 'js-module'].indexOf(type) > -1) {
- return installer.uninstall(item, wwwDest, plugin_id);
- } else {
- return installer.uninstall(item, this.root, plugin_id, options, project);
- }
- }
- };
- }
-
- /**
- * Removes the specified modules from list of installed modules and updates
- * platform_json and cordova_plugins.js on disk.
- *
- * @param {PluginInfo} plugin PluginInfo instance for plugin, which modules
- * needs to be added.
- * @param {String} targetDir The directory, where updated cordova_plugins.js
- * should be written to.
- */
- _addModulesInfo (platform, plugin, targetDir) {
- const installedModules = this._platformJson.root.modules || [];
-
- const installedPaths = installedModules.map((installedModule) => installedModule.file);
-
- const modulesToInstall = plugin.getJsModules(platform)
- .filter((moduleToInstall) => installedPaths.indexOf(moduleToInstall.file) === -1)
- .map((moduleToInstall) => {
- const moduleName = `${plugin.id}.${moduleToInstall.name || moduleToInstall.src.match(/([^\/]+)\.js/)[1]}`;
- const obj = {
- file: ['plugins', plugin.id, moduleToInstall.src].join('/'),
- id: moduleName,
- pluginId: plugin.id
- };
-
- if (moduleToInstall.clobbers.length > 0) {
- obj.clobbers = moduleToInstall.clobbers.map((o) => o.target);
- }
-
- if (moduleToInstall.merges.length > 0) {
- obj.merges = moduleToInstall.merges.map((o) => o.target);
- }
-
- if (moduleToInstall.runs) {
- obj.runs = true;
- }
-
- return obj;
- });
-
- this._platformJson.root.modules = installedModules.concat(modulesToInstall);
- if (!this._platformJson.root.plugin_metadata) {
- this._platformJson.root.plugin_metadata = {};
- }
-
- this._platformJson.root.plugin_metadata[plugin.id] = plugin.version;
- this._writePluginModules(targetDir);
- this._platformJson.save();
- }
-
- /**
- * Fetches all installed modules, generates cordova_plugins contents and writes
- * it to file.
- *
- * @param {String} targetDir Directory, where write cordova_plugins.js to.
- * Ususally it is either <platform>/www or <platform>/platform_www
- * directories.
- */
- _writePluginModules (targetDir) {
- // Write out moduleObjects as JSON wrapped in a cordova module to cordova_plugins.js
- const final_contents = `cordova.define('cordova/plugin_list', function (require, exports, module) {
- module.exports = ${JSON.stringify(this._platformJson.root.modules, null, ' ')};
-
- module.exports.metadata =
- // TOP OF METADATA
- ${JSON.stringify(this._platformJson.root.plugin_metadata || {}, null, ' ')}
- // BOTTOM OF METADATA
- });`;
-
- fs.ensureDirSync(targetDir);
- fs.writeFileSync(path.join(targetDir, 'cordova_plugins.js'), final_contents, 'utf-8');
- }
-
- /**
- * Removes the specified modules from list of installed modules and updates
- * platform_json and cordova_plugins.js on disk.
- *
- * @param {PluginInfo} plugin PluginInfo instance for plugin, which modules
- * needs to be removed.
- * @param {String} targetDir The directory, where updated cordova_plugins.js
- * should be written to.
- */
- _removeModulesInfo (plugin, targetDir) {
- const installedModules = this._platformJson.root.modules || [];
- const modulesToRemove = plugin.getJsModules(this.platform)
- .map((jsModule) => ['plugins', plugin.id, jsModule.src].join('/')); /* eslint no-useless-escape : 0 */
-
- const updatedModules = installedModules
- .filter((installedModule) => modulesToRemove.indexOf(installedModule.file) === -1);
-
- this._platformJson.root.modules = updatedModules;
-
- if (this._platformJson.root.plugin_metadata) {
- delete this._platformJson.root.plugin_metadata[plugin.id];
- }
-
- this._writePluginModules(targetDir);
- this._platformJson.save();
- }
-
- build (buildOptions) {
- return require('./lib/build').run.call(this, buildOptions, this);
- }
-
- run (runOptions) {
- return require('./lib/run').run(runOptions);
- }
-
- clean (cleanOptions) {
- return require('./lib/clean').run(cleanOptions);
- }
-
- requirements () {
- return require('./lib/check_reqs').run();
- }
-
- static version () {
- let platformPkg = null;
-
- try {
- // coming from user project
- platformPkg = require(require.resolve('cordova-electron/package.json'));
- } catch (e) {
- // coming from repo test & coho
- platformPkg = require('../../../package.json');
- }
-
- return platformPkg.version;
- }
+try {
+ module.exports = require('cordova-electron');
+} catch (error) {
+ module.exports = require('../../../lib/Api');
}
-// @todo create projectInstance and fulfill promise with it.
-Api.updatePlatform = () => Promise.resolve();
-
-Api.createPlatform = (dest, config, options, events) => {
- if (!config) throw new CordovaError('An Electron platform can not be created with a missing config argument.');
-
- events = setupEvents(events);
-
- const name = config.name();
- const id = config.packageName();
-
- try {
- // we create the project using our scripts in this platform
- return require('../../lib/create').createProject(dest, id, name, options).then(() => new Api(null, dest, events));
- } catch (e) {
- events.emit('error', 'createPlatform is not callable from the electron project API.');
- }
-};
-
-module.exports = Api;
diff --git a/bin/templates/cordova/Api.js b/lib/Api.js
similarity index 96%
copy from bin/templates/cordova/Api.js
copy to lib/Api.js
index bdd033f..0cecc9f 100644
--- a/bin/templates/cordova/Api.js
+++ b/lib/Api.js
@@ -52,7 +52,7 @@ class Api {
this.platform = 'electron';
// MyApp/platforms/electron
- this.root = platformRootDir || path.resolve(__dirname, '..');
+ this.root = platformRootDir || path.resolve(__dirname, '../bin/templates');
this.events = setupEvents(events);
this.parser = new Parser(this.root);
this.handler = require('./handler');
@@ -89,7 +89,7 @@ class Api {
}
prepare (cordovaProject, options) {
- return require('./lib/prepare').prepare.call(this, cordovaProject, options);
+ return require('./prepare').prepare.call(this, cordovaProject, options);
}
addPlugin (pluginInfo, installOptions) {
@@ -329,19 +329,19 @@ class Api {
}
build (buildOptions) {
- return require('./lib/build').run.call(this, buildOptions, this);
+ return require('./build').run.call(this, buildOptions, this);
}
run (runOptions) {
- return require('./lib/run').run(runOptions);
+ return require('./run').run(runOptions);
}
clean (cleanOptions) {
- return require('./lib/clean').run(cleanOptions);
+ return require('./clean').run(cleanOptions);
}
requirements () {
- return require('./lib/check_reqs').run();
+ return require('./check_reqs').run();
}
static version () {
@@ -352,7 +352,7 @@ class Api {
platformPkg = require(require.resolve('cordova-electron/package.json'));
} catch (e) {
// coming from repo test & coho
- platformPkg = require('../../../package.json');
+ platformPkg = require('../package.json');
}
return platformPkg.version;
@@ -371,7 +371,7 @@ Api.createPlatform = (dest, config, options, events) => {
try {
// we create the project using our scripts in this platform
- return require('../../lib/create').createProject(dest, id, name, options).then(() => new Api(null, dest, events));
+ return require('./create').createProject(dest, id, name, options).then(() => new Api(null, dest, events));
} catch (e) {
events.emit('error', 'createPlatform is not callable from the electron project API.');
}
diff --git a/bin/templates/cordova/lib/ManifestJsonParser.js b/lib/ManifestJsonParser.js
similarity index 100%
rename from bin/templates/cordova/lib/ManifestJsonParser.js
rename to lib/ManifestJsonParser.js
diff --git a/bin/templates/cordova/lib/PackageJsonParser.js b/lib/PackageJsonParser.js
similarity index 100%
rename from bin/templates/cordova/lib/PackageJsonParser.js
rename to lib/PackageJsonParser.js
diff --git a/bin/templates/cordova/lib/SettingJsonParser.js b/lib/SettingJsonParser.js
similarity index 100%
rename from bin/templates/cordova/lib/SettingJsonParser.js
rename to lib/SettingJsonParser.js
diff --git a/bin/templates/cordova/lib/build.js b/lib/build.js
similarity index 99%
rename from bin/templates/cordova/lib/build.js
rename to lib/build.js
index 174db0f..56a3925 100644
--- a/bin/templates/cordova/lib/build.js
+++ b/lib/build.js
@@ -358,8 +358,8 @@ module.exports.run = (buildOptions, api) => require('./check_reqs')
.configure()
.build()
)
- .catch((error) => {
- console.log(error);
+ .catch(error => {
+ throw error;
});
module.exports.help = (argv) => {
diff --git a/bin/templates/cordova/lib/build/base.json b/lib/build/base.json
similarity index 100%
rename from bin/templates/cordova/lib/build/base.json
rename to lib/build/base.json
diff --git a/bin/templates/cordova/lib/build/darwin.json b/lib/build/darwin.json
similarity index 100%
rename from bin/templates/cordova/lib/build/darwin.json
rename to lib/build/darwin.json
diff --git a/bin/templates/cordova/lib/build/linux.json b/lib/build/linux.json
similarity index 100%
rename from bin/templates/cordova/lib/build/linux.json
rename to lib/build/linux.json
diff --git a/bin/templates/cordova/lib/build/win32.json b/lib/build/win32.json
similarity index 100%
rename from bin/templates/cordova/lib/build/win32.json
rename to lib/build/win32.json
diff --git a/bin/templates/cordova/lib/check_reqs.js b/lib/check_reqs.js
similarity index 100%
rename from bin/templates/cordova/lib/check_reqs.js
rename to lib/check_reqs.js
diff --git a/bin/templates/cordova/lib/clean.js b/lib/clean.js
similarity index 100%
rename from bin/templates/cordova/lib/clean.js
rename to lib/clean.js
diff --git a/bin/lib/create.js b/lib/create.js
similarity index 83%
rename from bin/lib/create.js
rename to lib/create.js
index 1e52de5..3485a41 100644
--- a/bin/lib/create.js
+++ b/lib/create.js
@@ -19,9 +19,9 @@
const fs = require('fs-extra');
const path = require('path');
-const ROOT = path.join(__dirname, '..', '..');
+const rootDir = path.resolve(__dirname, '..');
const events = require('cordova-common').events;
-const check_reqs = require('./../templates/cordova/lib/check_reqs.js');
+const check_reqs = require(path.join(rootDir, 'lib/check_reqs'));
// exported method to create a project, returns a promise that resolves with null
module.exports.createProject = (platform_dir, package_name, project_name, options) => {
@@ -49,11 +49,11 @@ module.exports.createProject = (platform_dir, package_name, project_name, option
fs.ensureDirSync(platform_dir);
// copy templates directory to the platform directory recursively
- fs.copySync(path.join(ROOT, 'bin/templates'), path.join(platform_dir), { overwrite: false });
+ fs.copySync(path.join(rootDir, 'bin/templates'), path.join(platform_dir), { overwrite: false });
// recreate our node_modules structure in the new project
- if (fs.existsSync(path.join(ROOT, 'node_modules'))) {
- fs.copySync(path.join(ROOT, 'node_modules'), path.join(platform_dir, 'cordova', 'node_modules'), { overwrite: false });
+ if (fs.existsSync(path.join(rootDir, 'node_modules'))) {
+ fs.copySync(path.join(rootDir, 'node_modules'), path.join(platform_dir, 'cordova', 'node_modules'), { overwrite: false });
}
return Promise.resolve();
diff --git a/bin/templates/cordova/handler.js b/lib/handler.js
similarity index 100%
rename from bin/templates/cordova/handler.js
rename to lib/handler.js
diff --git a/bin/templates/cordova/parser.js b/lib/parser.js
similarity index 100%
rename from bin/templates/cordova/parser.js
rename to lib/parser.js
diff --git a/bin/templates/cordova/lib/prepare.js b/lib/prepare.js
similarity index 100%
rename from bin/templates/cordova/lib/prepare.js
rename to lib/prepare.js
diff --git a/bin/templates/cordova/lib/run.js b/lib/run.js
similarity index 93%
rename from bin/templates/cordova/lib/run.js
rename to lib/run.js
index 398d47c..b639b9e 100644
--- a/bin/templates/cordova/lib/run.js
+++ b/lib/run.js
@@ -22,7 +22,7 @@ const execa = require('execa');
const path = require('path');
module.exports.run = (args) => {
- const pathToMain = path.resolve(__dirname, '..', '..', 'www', 'cdv-electron-main.js');
+ const pathToMain = path.join(global.cdvPlatformPath, 'www/cdv-electron-main.js');
const child = execa(electron, [pathToMain]);
child.on('close', (code) => {
diff --git a/bin/lib/update.js b/lib/update.js
similarity index 100%
rename from bin/lib/update.js
rename to lib/update.js
diff --git a/bin/templates/cordova/lib/util.js b/lib/util.js
similarity index 100%
rename from bin/templates/cordova/lib/util.js
rename to lib/util.js
diff --git a/package.json b/package.json
index b868b6c..6050872 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
"name": "cordova-electron",
"version": "2.0.0-dev",
"description": "electron apps as a target for cordova developers",
- "main": "bin/templates/cordova/Api.js",
+ "main": "lib/Api.js",
"repository": "github:apache/cordova-electron",
"bugs": "https://github.com/apache/cordova-electron/issues",
"kewords": [
diff --git a/tests/spec/unit/Api.spec.js b/tests/spec/unit/lib/Api.spec.js
similarity index 95%
rename from tests/spec/unit/Api.spec.js
rename to tests/spec/unit/lib/Api.spec.js
index 32a4a8e..d157ce7 100644
--- a/tests/spec/unit/Api.spec.js
+++ b/tests/spec/unit/lib/Api.spec.js
@@ -22,19 +22,19 @@ const path = require('path');
const rewire = require('rewire');
const { events, PluginInfo, ConfigParser } = require('cordova-common');
-const templateDir = path.resolve(__dirname, '..', '..', '..', 'bin', 'templates');
+const rootDir = path.resolve(__dirname, '../../../..');
+const fixturesDir = path.join(rootDir, 'tests/spec/fixtures');
+const tmpDir = path.join(rootDir, 'temp');
+const testProjectDir = path.join(tmpDir, 'testapp');
-const create = require(path.join(templateDir, '../lib/create'));
-const Api = rewire(path.join(templateDir, 'cordova', 'Api'));
+const create = require(path.join(rootDir, 'lib/create'));
+const Api = rewire(path.join(rootDir, 'lib/Api'));
-const tmpDir = path.join(__dirname, '../../../temp');
const apiRequire = Api.__get__('require');
-const FIXTURES = path.join(__dirname, '..', 'fixtures');
-const pluginFixture = path.join(FIXTURES, 'testplugin');
-const pluginFixtureEmptyJSModule = path.join(FIXTURES, 'testplugin-empty-jsmodule');
-const pluginNotElectronFixture = path.join(FIXTURES, 'test-non-electron-plugin');
-const pluginBrowserFixture = path.join(FIXTURES, 'test-browser-plugin');
-const testProjectDir = path.join(tmpDir, 'testapp');
+const pluginFixture = path.join(fixturesDir, 'testplugin');
+const pluginFixtureEmptyJSModule = path.join(fixturesDir, 'testplugin-empty-jsmodule');
+const pluginNotElectronFixture = path.join(fixturesDir, 'test-non-electron-plugin');
+const pluginBrowserFixture = path.join(fixturesDir, 'test-browser-plugin');
function dirExists (dir) {
return fs.existsSync(dir) && fs.statSync(dir).isDirectory();
@@ -65,7 +65,7 @@ describe('Api class', () => {
beforeAll(() => {
fs.ensureDirSync(tmpDir);
- fs.copySync(path.resolve(FIXTURES, 'testapp'), path.resolve(tmpDir, 'testapp'));
+ fs.copySync(path.resolve(fixturesDir, 'testapp'), path.resolve(tmpDir, 'testapp'));
apiEvents = Api.__get__('selfEvents');
apiEvents.addListener('verbose', (data) => { });
@@ -427,7 +427,7 @@ describe('Api prototype methods', () => {
beforeEach(() => {
fs.removeSync(tmpDir);
- config = new ConfigParser(path.join(FIXTURES, 'test-config-empty.xml'));
+ config = new ConfigParser(path.join(fixturesDir, 'test-config-empty.xml'));
});
afterEach(() => {
@@ -476,7 +476,7 @@ describe('Api prototype methods', () => {
it('should get version from package.json.', () => {
const dummyRequire = path => {
- expect(path).toEqual('../../../package.json');
+ expect(path).toEqual('../package.json');
return { version: '1.0.0' };
};
diff --git a/tests/spec/unit/templates/cordova/lib/ManifestJsonParser.spec.js b/tests/spec/unit/lib/ManifestJsonParser.spec.js
similarity index 93%
rename from tests/spec/unit/templates/cordova/lib/ManifestJsonParser.spec.js
rename to tests/spec/unit/lib/ManifestJsonParser.spec.js
index 4ee70ee..0f0d64e 100644
--- a/tests/spec/unit/templates/cordova/lib/ManifestJsonParser.spec.js
+++ b/tests/spec/unit/lib/ManifestJsonParser.spec.js
@@ -21,14 +21,16 @@ const rewire = require('rewire');
const path = require('path');
const fs = require('fs-extra');
const { ConfigParser } = require('cordova-common');
-const ManifestJsonParser = rewire('../../../../../../bin/templates/cordova/lib/ManifestJsonParser');
-const FIXTURES = path.join(__dirname, '..', '..', '..', '..', 'fixtures');
+const rootDir = path.resolve(__dirname, '../../../..');
+const fixturesDir = path.join(rootDir, 'tests/spec/fixtures');
+
+const ManifestJsonParser = rewire(path.join(rootDir, 'lib/ManifestJsonParser'));
// Create a real config object before mocking out everything.
-const cfg1 = new ConfigParser(path.join(FIXTURES, 'test-config-1.xml'));
-const cfg2 = new ConfigParser(path.join(FIXTURES, 'test-config-2.xml'));
-const cfgEmpty = new ConfigParser(path.join(FIXTURES, 'test-config-empty.xml'));
+const cfg1 = new ConfigParser(path.join(fixturesDir, 'test-config-1.xml'));
+const cfg2 = new ConfigParser(path.join(fixturesDir, 'test-config-2.xml'));
+const cfgEmpty = new ConfigParser(path.join(fixturesDir, 'test-config-empty.xml'));
const locations = {
buildRes: path.join('mock', 'build-res'),
diff --git a/tests/spec/unit/templates/cordova/lib/PackageJsonParser.spec.js b/tests/spec/unit/lib/PackageJsonParser.spec.js
similarity index 95%
rename from tests/spec/unit/templates/cordova/lib/PackageJsonParser.spec.js
rename to tests/spec/unit/lib/PackageJsonParser.spec.js
index c3975c1..c555052 100644
--- a/tests/spec/unit/templates/cordova/lib/PackageJsonParser.spec.js
+++ b/tests/spec/unit/lib/PackageJsonParser.spec.js
@@ -20,16 +20,17 @@
const path = require('path');
const fs = require('fs-extra');
const rewire = require('rewire');
-
-const PackageJsonParser = rewire('../../../../../../bin/templates/cordova/lib/PackageJsonParser');
const { ConfigParser, events } = require('cordova-common');
-const FIXTURES = path.join(__dirname, '..', '..', '..', '..', 'fixtures');
+const rootDir = path.resolve(__dirname, '../../../..');
+const fixturesDir = path.join(rootDir, 'tests/spec/fixtures');
+
+const PackageJsonParser = rewire(path.join(rootDir, 'lib/PackageJsonParser'));
// Create a real config object before mocking out everything.
-const cfg = new ConfigParser(path.join(FIXTURES, 'test-config-1.xml'));
-const cfgEmpty = new ConfigParser(path.join(FIXTURES, 'test-config-empty.xml'));
-const cfgNoAuthorCustomEmail = new ConfigParser(path.join(FIXTURES, 'test-config-no-author-custom-email.xml'));
+const cfg = new ConfigParser(path.join(fixturesDir, 'test-config-1.xml'));
+const cfgEmpty = new ConfigParser(path.join(fixturesDir, 'test-config-empty.xml'));
+const cfgNoAuthorCustomEmail = new ConfigParser(path.join(fixturesDir, 'test-config-no-author-custom-email.xml'));
const defaultMockProjectPackageJson = {
name: 'io.cordova.electronTest',
diff --git a/tests/spec/unit/templates/cordova/lib/SettingJsonParser.spec.js b/tests/spec/unit/lib/SettingJsonParser.spec.js
similarity index 95%
rename from tests/spec/unit/templates/cordova/lib/SettingJsonParser.spec.js
rename to tests/spec/unit/lib/SettingJsonParser.spec.js
index 1f4bfcd..533630c 100644
--- a/tests/spec/unit/templates/cordova/lib/SettingJsonParser.spec.js
+++ b/tests/spec/unit/lib/SettingJsonParser.spec.js
@@ -19,21 +19,21 @@
const rewire = require('rewire');
const path = require('path');
-
const ConfigParser = require('cordova-common').ConfigParser;
-const FIXTURES = path.join(__dirname, '..', '..', '..', '..', 'fixtures');
+const rootDir = path.resolve(__dirname, '../../../..');
+const fixturesDir = path.join(rootDir, 'tests/spec/fixtures');
// Create a real config object before mocking out everything.
-const cfg = new ConfigParser(path.join(FIXTURES, 'test-config-1.xml'));
-const cfgEmpty = new ConfigParser(path.join(FIXTURES, 'test-config-empty.xml'));
+const cfg = new ConfigParser(path.join(fixturesDir, 'test-config-1.xml'));
+const cfgEmpty = new ConfigParser(path.join(fixturesDir, 'test-config-empty.xml'));
describe('Testing SettingJsonParser.js:', () => {
let SettingJsonParser;
let locations;
beforeEach(() => {
- SettingJsonParser = rewire('../../../../../../bin/templates/cordova/lib/SettingJsonParser');
+ SettingJsonParser = rewire(path.join(rootDir, 'lib/SettingJsonParser'));
locations = {
buildRes: path.join('mock', 'build-res'),
diff --git a/tests/spec/unit/templates/cordova/lib/build.spec.js b/tests/spec/unit/lib/build.spec.js
similarity index 98%
rename from tests/spec/unit/templates/cordova/lib/build.spec.js
rename to tests/spec/unit/lib/build.spec.js
index 6b24b98..fa68d88 100644
--- a/tests/spec/unit/templates/cordova/lib/build.spec.js
+++ b/tests/spec/unit/lib/build.spec.js
@@ -20,13 +20,17 @@
const rewire = require('rewire');
const path = require('path');
-const Api = rewire('../../../../../../bin/templates/cordova/Api');
+
+const rootDir = path.resolve(__dirname, '../../../..');
+
+const Api = rewire(path.join(rootDir, 'lib/Api'));
+const check_reqs = require(path.join(rootDir, 'lib/check_reqs'));
describe('Testing build.js:', () => {
let build;
beforeEach(() => {
- build = rewire('../../../../../../bin/templates/cordova/lib/build');
+ build = rewire(path.join(rootDir, 'lib/build'));
});
describe('Build class', () => {
@@ -1713,40 +1717,35 @@ describe('Testing build.js:', () => {
});
});
- it('should have failed requirement and console log error.', () => {
+ it('should have failed requirement check and thrown error.', () => {
const api = new Api(null);
const buildOptions = { debug: false, buildConfig: 'LOAD_MY_FAKE_DATA', argv: [] };
-
- // create spies
- const logSpy = jasmine.createSpy('emit');
- build.__set__('console', { log: logSpy });
-
- build.__set__('require', (file) => {
- // if (file === 'LOAD_MY_FAKE_DATA') return buildConfig;
- if (file === './check_reqs') return { run: () => Promise.reject(new Error('Error')) };
- return require(file);
- });
-
- build.run(buildOptions, api).then(() => {
- expect(logSpy).toHaveBeenCalled();
- });
+ const errorMsg = 'error';
+ spyOn(check_reqs, 'run').and.callFake(() => Promise.reject(new Error(errorMsg)));
+
+ return build.run(buildOptions, api).then(
+ () => fail('Unexpectedly resolved'),
+ error => {
+ expect(check_reqs.run).toHaveBeenCalled();
+ expect(error.message).toBe(errorMsg);
+ }
+ );
});
});
describe('Module exports help', () => {
it('should display help usage.', () => {
const help = build.__get__('module.exports.help');
-
const argv = { binPath: 'bin' };
- const logSpy = jasmine.createSpy('log');
- build.__set__('console', { log: logSpy });
+ // create spies
+ spyOn(console, 'log');
help(argv);
- expect(logSpy).toHaveBeenCalled();
+ expect(console.log).toHaveBeenCalled();
- const actual = logSpy.calls.argsFor(0)[0];
+ const actual = console.log.calls.argsFor(0)[0];
expect(actual).toContain('--debug');
expect(actual).toContain('--release');
expect(actual).toContain('--nobuild');
diff --git a/tests/spec/unit/lib/clean.spec.js b/tests/spec/unit/lib/clean.spec.js
new file mode 100644
index 0000000..2ea5bd6
--- /dev/null
+++ b/tests/spec/unit/lib/clean.spec.js
@@ -0,0 +1,102 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+*/
+
+const path = require('path');
+const fs = require('fs-extra');
+
+const rootDir = path.resolve(__dirname, '../../../..');
+
+const clean = require(path.join(rootDir, 'lib/clean'));
+const check_reqs = require(path.join(rootDir, 'lib/check_reqs'));
+
+describe('Clean', () => {
+ describe('run export method', () => {
+ it('should stop process when requirement check fails.', () => {
+ // set spies
+ spyOn(console, 'error');
+ spyOn(check_reqs, 'run').and.returnValue(false);
+ spyOn(process, 'exit');
+
+ // run test
+ clean.run();
+
+ const expectedLog = 'Please make sure you meet the software requirements in order to clean an electron cordova project';
+
+ expect(console.error).toHaveBeenCalledWith(expectedLog);
+ expect(process.exit).toHaveBeenCalledWith(2);
+ });
+
+ it('should not find previous build dir and not attempt to remove.', () => {
+ spyOn(check_reqs, 'run').and.returnValue(true);
+ spyOn(fs, 'existsSync').and.returnValue(false);
+ spyOn(fs, 'removeSync');
+
+ clean.run();
+
+ expect(fs.existsSync).toHaveBeenCalled();
+ expect(fs.removeSync).not.toHaveBeenCalled();
+ });
+
+ it('should find previous build dir and attempt to remove.', () => {
+ spyOn(check_reqs, 'run').and.returnValue(true);
+ spyOn(fs, 'existsSync').and.returnValue(true);
+ spyOn(fs, 'removeSync');
+
+ clean.run();
+
+ expect(fs.existsSync).toHaveBeenCalled();
+ expect(fs.removeSync).toHaveBeenCalled();
+ });
+
+ it('should find previous build dir and fail to remove.', () => {
+ spyOn(console, 'log');
+ spyOn(check_reqs, 'run').and.returnValue(true);
+ spyOn(fs, 'existsSync').and.returnValue(true);
+ spyOn(fs, 'removeSync').and.callFake(() => {
+ throw new Error('Fake Error');
+ });
+
+ clean.run();
+
+ expect(console.log).toHaveBeenCalledWith(
+ jasmine.stringMatching(/could not remove/)
+ );
+ });
+ });
+
+ describe('cleanProject export method', () => {
+ it('should console out that it will execute run command.', () => {
+ spyOn(console, 'log');
+
+ clean.cleanProject();
+
+ expect(console.log).toHaveBeenCalledWith('lib/clean will soon only export a `run` command, please update to not call `cleanProject`.');
+ });
+ });
+
+ describe('help export method', () => {
+ it('should console out clean usage.', () => {
+ spyOn(console, 'log');
+
+ clean.help({ binPath: 'foobar' });
+
+ expect(console.log).toHaveBeenCalledWith('Usage: foobar');
+ });
+ });
+});
diff --git a/tests/spec/unit/create.spec.js b/tests/spec/unit/lib/create.spec.js
similarity index 95%
rename from tests/spec/unit/create.spec.js
rename to tests/spec/unit/lib/create.spec.js
index 36fc91f..fca94b2 100644
--- a/tests/spec/unit/create.spec.js
+++ b/tests/spec/unit/lib/create.spec.js
@@ -21,9 +21,10 @@ const fs = require('fs-extra');
const path = require('path');
const rewire = require('rewire');
-const cordova_bin = path.join(__dirname, '../../../bin');// is this the same on all platforms?
-const tmpDir = path.join(__dirname, '../../../temp');
-const create = rewire(path.join(cordova_bin, 'lib', 'create'));
+const rootDir = path.resolve(__dirname, '../../../..');
+const tmpDir = path.join(rootDir, 'temp');
+
+const create = rewire(path.join(rootDir, 'lib/create'));
function createAndValidateProjectDirName (projectname, projectid, { copyNodeModules = false } = {}) {
// remove existing folder
diff --git a/tests/spec/unit/handler.spec.js b/tests/spec/unit/lib/handler.spec.js
similarity index 98%
rename from tests/spec/unit/handler.spec.js
rename to tests/spec/unit/lib/handler.spec.js
index 60bc511..e823f0f 100644
--- a/tests/spec/unit/handler.spec.js
+++ b/tests/spec/unit/lib/handler.spec.js
@@ -21,7 +21,10 @@ const path = require('path');
const fs = require('fs-extra');
const { events } = require('cordova-common');
const rewire = require('rewire');
-const handler = rewire('../../../bin/templates/cordova/handler');
+
+const rootDir = path.resolve(__dirname, '../../../..');
+
+const handler = rewire(path.join(rootDir, 'lib/handler'));
describe('Handler export', () => {
describe('www_dir method', () => {
diff --git a/tests/spec/unit/templates/parser.spec.js b/tests/spec/unit/lib/parser.spec.js
similarity index 98%
rename from tests/spec/unit/templates/parser.spec.js
rename to tests/spec/unit/lib/parser.spec.js
index 4c4fadc..90e8818 100644
--- a/tests/spec/unit/templates/parser.spec.js
+++ b/tests/spec/unit/lib/parser.spec.js
@@ -21,7 +21,10 @@ const path = require('path');
const fs = require('fs-extra');
const rewire = require('rewire');
const { CordovaError, events } = require('cordova-common');
-const Parser = rewire('../../../../bin/templates/cordova/parser');
+
+const rootDir = path.resolve(__dirname, '../../../..');
+
+const Parser = rewire(path.join(rootDir, 'lib/parser'));
const mockProjectPath = 'mock_project_path';
diff --git a/tests/spec/unit/templates/cordova/lib/prepare.spec.js b/tests/spec/unit/lib/prepare.spec.js
similarity index 99%
rename from tests/spec/unit/templates/cordova/lib/prepare.spec.js
rename to tests/spec/unit/lib/prepare.spec.js
index e0adba5..c97f6e9 100644
--- a/tests/spec/unit/templates/cordova/lib/prepare.spec.js
+++ b/tests/spec/unit/lib/prepare.spec.js
@@ -20,7 +20,10 @@
const rewire = require('rewire');
const path = require('path');
const CordovaError = require('cordova-common').CordovaError;
-const Api = require(path.resolve(__dirname, '..', '..', '..', '..', '..', '..', 'bin', 'templates', 'cordova', 'Api'));
+
+const rootDir = path.resolve(__dirname, '../../../..');
+
+const Api = require(path.join(rootDir, 'bin/templates/cordova/Api'));
let prepare;
/**
@@ -128,7 +131,7 @@ function createSpies () {
updateSplashScreensSpy = jasmine.createSpy('updateSplashScreensSpy');
emitSpy = jasmine.createSpy('emitSpy');
- prepare = rewire(path.resolve(__dirname, '..', '..', '..', '..', '..', '..', 'bin', 'templates', 'cordova', 'lib', 'prepare'));
+ prepare = rewire(path.join(rootDir, 'lib/prepare'));
prepare.__set__('events', {
emit: emitSpy
@@ -1418,7 +1421,7 @@ describe('Testing prepare.js:', () => {
let locations;
beforeEach(() => {
- prepare = rewire(path.resolve(__dirname, '..', '..', '..', '..', '..', '..', 'bin', 'templates', 'cordova', 'lib', 'prepare'));
+ prepare = rewire(path.join(rootDir, 'lib/prepare'));
cordovaProject = Object.assign({}, cordovaProjectDefault);
locations = Object.assign({}, locationsDefault);
@@ -1593,7 +1596,7 @@ describe('Testing prepare.js:', () => {
let cordovaProject;
beforeEach(() => {
- prepare = rewire(path.resolve(__dirname, '..', '..', '..', '..', '..', '..', 'bin', 'templates', 'cordova', 'lib', 'prepare'));
+ prepare = rewire(path.join(rootDir, 'lib/prepare'));
cordovaProject = Object.assign({}, cordovaProjectDefault);
mapResources = prepare.__get__('mapResources');
@@ -1639,7 +1642,7 @@ describe('Testing prepare.js:', () => {
let cordovaProject;
beforeEach(() => {
- prepare = rewire(path.resolve(__dirname, '..', '..', '..', '..', '..', '..', 'bin', 'templates', 'cordova', 'lib', 'prepare'));
+ prepare = rewire(path.join(rootDir, 'lib/prepare'));
cordovaProject = Object.assign({}, cordovaProjectDefault);
copyResources = prepare.__get__('copyResources');
diff --git a/tests/spec/unit/templates/cordova/lib/run.spec.js b/tests/spec/unit/lib/run.spec.js
similarity index 77%
rename from tests/spec/unit/templates/cordova/lib/run.spec.js
rename to tests/spec/unit/lib/run.spec.js
index 1fb853d..983a4bc 100644
--- a/tests/spec/unit/templates/cordova/lib/run.spec.js
+++ b/tests/spec/unit/lib/run.spec.js
@@ -19,14 +19,17 @@
const rewire = require('rewire');
const path = require('path');
-const run = rewire('../../../../../../bin/templates/cordova/lib/run');
+
+const rootDir = path.resolve(__dirname, '../../../..');
+
+const run = rewire(path.join(rootDir, 'lib/run'));
describe('Run', () => {
describe('run export method', () => {
it('should run electron with cdv-electron-main.js.', () => {
const execaSpy = jasmine.createSpy('execa');
const onSpy = jasmine.createSpy('on');
- const expectedPathToMain = path.resolve(__dirname, '..', '..', '..', '..', '..', '..', 'bin', 'templates', 'www', 'cdv-electron-main.js');
+ const expectedPathToMain = path.join(rootDir, 'bin/templates/www/cdv-electron-main.js');
run.__set__('electron', 'electron-require');
spyOn(process, 'exit');
@@ -49,16 +52,13 @@ describe('Run', () => {
describe('help export method', () => {
it('should console out run usage.', () => {
- const logSpy = jasmine.createSpy('log');
- run.__set__('console', {
- log: logSpy
- });
+ spyOn(console, 'log');
run.help({ binPath: 'foobar' });
- expect(logSpy.calls.argsFor(0)[0]).toContain('Usage');
- expect(logSpy.calls.argsFor(0)[0]).toContain('foobar');
- expect(logSpy.calls.argsFor(0)[0]).toContain('nobuild');
+ expect(console.log.calls.argsFor(0)[0]).toContain('Usage');
+ expect(console.log.calls.argsFor(0)[0]).toContain('foobar');
+ expect(console.log.calls.argsFor(0)[0]).toContain('nobuild');
});
});
});
diff --git a/tests/spec/unit/lib/update.spec.js b/tests/spec/unit/lib/update.spec.js
index 41be855..b8fb0fb 100644
--- a/tests/spec/unit/lib/update.spec.js
+++ b/tests/spec/unit/lib/update.spec.js
@@ -17,8 +17,12 @@
under the License.
*/
+const path = require('path');
const rewire = require('rewire');
-const update = rewire('../../../../bin/lib/update');
+
+const rootDir = path.resolve(__dirname, '../../../..');
+
+const update = rewire(path.join(rootDir, 'lib/update'));
describe('Update', () => {
describe('run export method', () => {
diff --git a/tests/spec/unit/templates/cordova/lib/util.spec.js b/tests/spec/unit/lib/util.spec.js
similarity index 90%
rename from tests/spec/unit/templates/cordova/lib/util.spec.js
rename to tests/spec/unit/lib/util.spec.js
index 3825ddf..6331d5b 100644
--- a/tests/spec/unit/templates/cordova/lib/util.spec.js
+++ b/tests/spec/unit/lib/util.spec.js
@@ -17,7 +17,11 @@
under the License.
*/
-const util = require('../../../../../../bin/templates/cordova/lib/util');
+const path = require('path');
+
+const rootDir = path.resolve(__dirname, '../../../..');
+
+const util = require(path.join(rootDir, 'lib/util'));
describe('Testing util.js:', () => {
describe('deepMerge method', () => {
diff --git a/tests/spec/unit/templates/cordova/lib/clean.spec.js b/tests/spec/unit/templates/cordova/lib/clean.spec.js
deleted file mode 100644
index f060119..0000000
--- a/tests/spec/unit/templates/cordova/lib/clean.spec.js
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
-*/
-
-const rewire = require('rewire');
-const clean = rewire('../../../../../../bin/templates/cordova/lib/clean');
-
-describe('Clean', () => {
- describe('run export method', () => {
- it('should stop process when requirement check fails.', () => {
- // create spies
- const logSpy = jasmine.createSpy('log');
-
- // set spies
- clean.__set__('console', { error: logSpy });
- clean.__set__('check_reqs', {
- run: jasmine.createSpy('run').and.returnValue(false)
- });
-
- spyOn(process, 'exit');
-
- // run test
- clean.run();
-
- const logArgs = logSpy.calls.argsFor(0)[0];
- const expectedLog = 'Please make sure you meet the software requirements in order to clean an electron cordova project';
-
- expect(logArgs).toContain(expectedLog);
- expect(process.exit).toHaveBeenCalledWith(2);
- });
-
- it('should not find previous build dir and not attempt to remove.', () => {
- clean.__set__('check_reqs', {
- run: jasmine.createSpy('run').and.returnValue(true)
- });
-
- const existsSyncSpy = jasmine.createSpy('existsSync').and.returnValue(false);
- const removeSyncSpy = jasmine.createSpy('removeSync');
- clean.__set__('fs', {
- existsSync: existsSyncSpy,
- removeSync: removeSyncSpy
- });
-
- clean.run();
-
- expect(existsSyncSpy).toHaveBeenCalled();
- expect(removeSyncSpy).not.toHaveBeenCalled();
- });
-
- it('should find previous build dir and attempt to remove.', () => {
- clean.__set__('check_reqs', {
- run: jasmine.createSpy('run').and.returnValue(true)
- });
-
- const existsSyncSpy = jasmine.createSpy('existsSync').and.returnValue(true);
- const removeSyncSpy = jasmine.createSpy('removeSync');
- clean.__set__('fs', {
- existsSync: existsSyncSpy,
- removeSync: removeSyncSpy
- });
-
- clean.run();
-
- expect(existsSyncSpy).toHaveBeenCalled();
- expect(removeSyncSpy).toHaveBeenCalled();
- });
-
- it('should find previous build dir and fail to remove.', () => {
- clean.__set__('check_reqs', {
- run: jasmine.createSpy('run').and.returnValue(true)
- });
-
- clean.__set__('fs', {
- existsSync: jasmine.createSpy('existsSync').and.returnValue(true),
- removeSync: () => {
- throw new Error('Fake Error');
- }
- });
-
- expect(() => {
- clean.run();
- }).toThrow();
- });
- });
-
- describe('cleanProject export method', () => {
- it('should console out that it will execute run command.', () => {
- const logSpy = jasmine.createSpy('log');
- clean.__set__('console', {
- log: logSpy
- });
-
- clean.cleanProject();
-
- expect(logSpy.calls.argsFor(0)[0]).toContain('lib/clean will soon only export a `run` command, please update to not call `cleanProject`.');
- });
- });
-
- describe('help export method', () => {
- it('should console out clean usage.', () => {
- const logSpy = jasmine.createSpy('log');
- clean.__set__('console', {
- log: logSpy
- });
-
- clean.help({ binPath: 'foobar' });
-
- expect(logSpy.calls.argsFor(0)[0]).toContain('Usage');
- expect(logSpy.calls.argsFor(0)[0]).toContain('foobar');
- });
- });
-});
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org