You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ra...@apache.org on 2018/06/20 11:24:54 UTC
[cordova-android] branch master updated (393dad6 -> 559b083)
This is an automated email from the ASF dual-hosted git repository.
raphinesse pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git.
from 393dad6 Lint JS files w/out extension too (#453)
new 8fb49ec Fix typo in variable name in retry.js
new 1a8154c Fix unsafe property access
new 2e88f7e Update Jasmine to support automatic promise handling
new 31bad29 Allow mocks to reject without a reason
new 559b083 Add unit tests for run and retryPromise
The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
bin/templates/cordova/lib/retry.js | 18 ++--
bin/templates/cordova/lib/run.js | 2 +-
package.json | 2 +-
spec/.eslintrc.yml | 4 +-
spec/unit/retry.spec.js | 53 ++++++++++
spec/unit/run.spec.js | 211 ++++++++++++++++++++++++++++++++++---
6 files changed, 265 insertions(+), 25 deletions(-)
create mode 100644 spec/unit/retry.spec.js
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org
[cordova-android] 04/05: Allow mocks to reject without a reason
Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git
commit 31bad290d583c75d37e3e3b6eb37515e29736a2f
Author: Raphael von der Grün <ra...@gmail.com>
AuthorDate: Wed Jun 20 12:20:38 2018 +0200
Allow mocks to reject without a reason
---
spec/.eslintrc.yml | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/spec/.eslintrc.yml b/spec/.eslintrc.yml
index 6afba65..77cb525 100644
--- a/spec/.eslintrc.yml
+++ b/spec/.eslintrc.yml
@@ -1,2 +1,4 @@
env:
- jasmine: true
\ No newline at end of file
+ jasmine: true
+rules:
+ prefer-promise-reject-errors: off
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org
[cordova-android] 03/05: Update Jasmine to support automatic
promise handling
Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git
commit 2e88f7ead23d8a166894be2af8c7589dcc2d56ca
Author: Gearoid M <Me...@users.noreply.github.com>
AuthorDate: Mon Jun 18 14:07:32 2018 +0900
Update Jasmine to support automatic promise handling
---
package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/package.json b/package.json
index d3dddf8..13893cd 100644
--- a/package.json
+++ b/package.json
@@ -57,7 +57,7 @@
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"istanbul": "^0.4.2",
- "jasmine": "~2.6.0",
+ "jasmine": "^3.1.0",
"npm-run-all": "^4.1.3",
"promise-matchers": "~0",
"rewire": "^2.1.3"
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org
[cordova-android] 02/05: Fix unsafe property access
Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git
commit 1a8154c90e5defb6a67b8947e4940666521355bb
Author: Gearoid M <Me...@users.noreply.github.com>
AuthorDate: Mon Jun 18 15:15:24 2018 +0900
Fix unsafe property access
---
bin/templates/cordova/lib/run.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bin/templates/cordova/lib/run.js b/bin/templates/cordova/lib/run.js
index 183fc92..37d91a6 100644
--- a/bin/templates/cordova/lib/run.js
+++ b/bin/templates/cordova/lib/run.js
@@ -106,7 +106,7 @@ module.exports.run = function (runOptions) {
// format than emulator.install expects.
// TODO: Update emulator/device.install to handle this change
return build.run.call(self, runOptions, resolvedTarget).then(function (buildResults) {
- if (resolvedTarget.isEmulator) {
+ if (resolvedTarget && resolvedTarget.isEmulator) {
return emulator.wait_for_boot(resolvedTarget.target).then(function () {
return emulator.install(resolvedTarget, buildResults);
});
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org
[cordova-android] 01/05: Fix typo in variable name in retry.js
Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git
commit 8fb49ec7ec6aeec6a94e4d0ae530d90ef63a7196
Author: Gearoid M <Me...@users.noreply.github.com>
AuthorDate: Mon Jun 18 15:15:24 2018 +0900
Fix typo in variable name in retry.js
---
bin/templates/cordova/lib/retry.js | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/bin/templates/cordova/lib/retry.js b/bin/templates/cordova/lib/retry.js
index 3ea8f14..b2b9a44 100644
--- a/bin/templates/cordova/lib/retry.js
+++ b/bin/templates/cordova/lib/retry.js
@@ -27,21 +27,20 @@ var events = require('cordova-common').events;
* Retry a promise-returning function a number of times, propagating its
* results on success or throwing its error on a failed final attempt.
*
- * @arg {Number} attemts_left - The number of times to retry the passed call.
+ * @arg {Number} attemptsLeft - The number of times to retry the passed call.
* @arg {Function} promiseFunction - A function that returns a promise.
* @arg {...} - Arguments to pass to promiseFunction.
*
* @returns {Promise}
*/
-module.exports.retryPromise = function (attemts_left, promiseFunction) {
+module.exports.retryPromise = function (attemptsLeft, promiseFunction) {
// NOTE:
- // get all trailing arguments, by skipping the first two (attemts_left and
+ // get all trailing arguments, by skipping the first two (attemptsLeft and
// promiseFunction) because they shouldn't get passed to promiseFunction
var promiseFunctionArguments = Array.prototype.slice.call(arguments, 2);
return promiseFunction.apply(undefined, promiseFunctionArguments).then(
-
// on success pass results through
function onFulfilled (value) {
return value;
@@ -49,17 +48,16 @@ module.exports.retryPromise = function (attemts_left, promiseFunction) {
// on rejection either retry, or throw the error
function onRejected (error) {
+ attemptsLeft -= 1;
- attemts_left -= 1;
-
- if (attemts_left < 1) {
+ if (attemptsLeft < 1) {
throw error;
}
- events.emit('verbose', 'A retried call failed. Retrying ' + attemts_left + ' more time(s).');
+ events.emit('verbose', 'A retried call failed. Retrying ' + attemptsLeft + ' more time(s).');
- // retry call self again with the same arguments, except attemts_left is now lower
- var fullArguments = [attemts_left, promiseFunction].concat(promiseFunctionArguments);
+ // retry call self again with the same arguments, except attemptsLeft is now lower
+ var fullArguments = [attemptsLeft, promiseFunction].concat(promiseFunctionArguments);
return module.exports.retryPromise.apply(undefined, fullArguments);
}
);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org
[cordova-android] 05/05: Add unit tests for run and retryPromise
Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git
commit 559b0833b42402a2b0fba6d6369fbc713e1063c1
Author: Gearoid M <Me...@users.noreply.github.com>
AuthorDate: Mon Jun 18 15:15:24 2018 +0900
Add unit tests for run and retryPromise
Co-authored-by: Raphael von der Grün <ra...@gmail.com>
---
spec/unit/retry.spec.js | 53 ++++++++++++
spec/unit/run.spec.js | 211 +++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 252 insertions(+), 12 deletions(-)
diff --git a/spec/unit/retry.spec.js b/spec/unit/retry.spec.js
new file mode 100644
index 0000000..cb5229d
--- /dev/null
+++ b/spec/unit/retry.spec.js
@@ -0,0 +1,53 @@
+/**
+ 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 retry = require('../../bin/templates/cordova/lib/retry');
+
+describe('retry', () => {
+ describe('retryPromise method', () => {
+ let promiseFn;
+
+ beforeEach(() => {
+ promiseFn = jasmine.createSpy().and.returnValue(Promise.resolve());
+ });
+
+ it('should pass all extra arguments to the promise', () => {
+ const args = ['test1', 'test2', 'test3'];
+
+ retry.retryPromise(0, promiseFn, ...args);
+ expect(promiseFn).toHaveBeenCalledWith(...args);
+ });
+
+ it('should retry the function up to specified number of times', () => {
+ const attempts = 3;
+ promiseFn.and.returnValue(Promise.reject());
+
+ return retry.retryPromise(attempts, promiseFn).then(
+ () => fail('Unexpectedly resolved'),
+ () => expect(promiseFn).toHaveBeenCalledTimes(attempts)
+ );
+ });
+
+ it('should not call the function again if it succeeds', () => {
+ return retry.retryPromise(42, promiseFn).then(() => {
+ expect(promiseFn).toHaveBeenCalledTimes(1);
+ });
+ });
+ });
+});
diff --git a/spec/unit/run.spec.js b/spec/unit/run.spec.js
index 881cc4a..425d85f 100644
--- a/spec/unit/run.spec.js
+++ b/spec/unit/run.spec.js
@@ -17,22 +17,209 @@
under the License.
*/
-var rewire = require('rewire');
-var run = rewire('../../bin/templates/cordova/lib/run');
-var getInstallTarget = run.__get__('getInstallTarget');
-
-describe('run', function () {
- describe('getInstallTarget', function () {
- var targetOpts = { target: 'emu' };
- var deviceOpts = { device: true };
- var emulatorOpts = { emulator: true };
- var emptyOpts = {};
-
- it('Test#001 : should select correct target based on the run opts', function () {
+const rewire = require('rewire');
+
+describe('run', () => {
+ let run;
+
+ beforeEach(() => {
+ run = rewire('../../bin/templates/cordova/lib/run');
+ });
+
+ describe('getInstallTarget', () => {
+ const targetOpts = { target: 'emu' };
+ const deviceOpts = { device: true };
+ const emulatorOpts = { emulator: true };
+ const emptyOpts = {};
+
+ it('Test#001 : should select correct target based on the run opts', () => {
+ const getInstallTarget = run.__get__('getInstallTarget');
expect(getInstallTarget(targetOpts)).toBe('emu');
expect(getInstallTarget(deviceOpts)).toBe('--device');
expect(getInstallTarget(emulatorOpts)).toBe('--emulator');
expect(getInstallTarget(emptyOpts)).toBeUndefined();
});
});
+
+ describe('run method', () => {
+ let buildSpyObj;
+ let deviceSpyObj;
+ let emulatorSpyObj;
+ let eventsSpyObj;
+ let getInstallTargetSpy;
+
+ beforeEach(() => {
+ buildSpyObj = jasmine.createSpyObj('buildSpy', ['run']);
+ deviceSpyObj = jasmine.createSpyObj('deviceSpy', ['install', 'list', 'resolveTarget']);
+ emulatorSpyObj = jasmine.createSpyObj('emulatorSpy', ['install', 'list_images', 'list_started', 'resolveTarget', 'start', 'wait_for_boot']);
+ eventsSpyObj = jasmine.createSpyObj('eventsSpy', ['emit']);
+ getInstallTargetSpy = jasmine.createSpy('getInstallTargetSpy');
+
+ buildSpyObj.run.and.returnValue(Promise.resolve());
+
+ run.__set__({
+ build: buildSpyObj,
+ device: deviceSpyObj,
+ emulator: emulatorSpyObj,
+ events: eventsSpyObj,
+ getInstallTarget: getInstallTargetSpy
+ });
+ });
+
+ it('should run on default device when no target arguments are specified', () => {
+ const deviceList = ['testDevice1', 'testDevice2'];
+
+ getInstallTargetSpy.and.returnValue(null);
+ deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
+
+ return run.run().then(() => {
+ expect(deviceSpyObj.resolveTarget).toHaveBeenCalledWith(deviceList[0]);
+ });
+ });
+
+ it('should run on emulator when no target arguments are specified, and no devices are found', () => {
+ const deviceList = [];
+
+ getInstallTargetSpy.and.returnValue(null);
+ deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
+ emulatorSpyObj.list_started.and.returnValue(Promise.resolve([]));
+
+ return run.run().then(() => {
+ expect(emulatorSpyObj.list_started).toHaveBeenCalled();
+ });
+ });
+
+ it('should run on default device when device is requested, but none specified', () => {
+ getInstallTargetSpy.and.returnValue('--device');
+
+ return run.run().then(() => {
+ // Default device is selected by calling device.resolveTarget(null)
+ expect(deviceSpyObj.resolveTarget).toHaveBeenCalledWith(null);
+ });
+ });
+
+ it('should run on a running emulator if one exists', () => {
+ const emulatorList = ['emulator1', 'emulator2'];
+
+ getInstallTargetSpy.and.returnValue('--emulator');
+ emulatorSpyObj.list_started.and.returnValue(Promise.resolve(emulatorList));
+
+ return run.run().then(() => {
+ expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(emulatorList[0]);
+ });
+ });
+
+ it('should start an emulator and run on that if none is running', () => {
+ const emulatorList = [];
+ const defaultEmulator = 'default-emu';
+
+ getInstallTargetSpy.and.returnValue('--emulator');
+ emulatorSpyObj.list_started.and.returnValue(Promise.resolve(emulatorList));
+ emulatorSpyObj.start.and.returnValue(Promise.resolve(defaultEmulator));
+
+ return run.run().then(() => {
+ expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(defaultEmulator);
+ });
+ });
+
+ it('should run on a named device if it is specified', () => {
+ const deviceList = ['device1', 'device2', 'device3'];
+ getInstallTargetSpy.and.returnValue(deviceList[1]);
+
+ deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
+
+ return run.run().then(() => {
+ expect(deviceSpyObj.resolveTarget).toHaveBeenCalledWith(deviceList[1]);
+ });
+ });
+
+ it('should run on a named emulator if it is specified', () => {
+ const startedEmulatorList = ['emu1', 'emu2', 'emu3'];
+ getInstallTargetSpy.and.returnValue(startedEmulatorList[2]);
+
+ deviceSpyObj.list.and.returnValue(Promise.resolve([]));
+ emulatorSpyObj.list_started.and.returnValue(Promise.resolve(startedEmulatorList));
+
+ return run.run().then(() => {
+ expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(startedEmulatorList[2]);
+ });
+ });
+
+ it('should start named emulator and then run on it if it is specified', () => {
+ const emulatorList = [
+ { name: 'emu1', id: 1 },
+ { name: 'emu2', id: 2 },
+ { name: 'emu3', id: 3 }
+ ];
+ getInstallTargetSpy.and.returnValue(emulatorList[2].name);
+
+ deviceSpyObj.list.and.returnValue(Promise.resolve([]));
+ emulatorSpyObj.list_started.and.returnValue(Promise.resolve([]));
+ emulatorSpyObj.list_images.and.returnValue(Promise.resolve(emulatorList));
+ emulatorSpyObj.start.and.returnValue(Promise.resolve(emulatorList[2].id));
+
+ return run.run().then(() => {
+ expect(emulatorSpyObj.start).toHaveBeenCalledWith(emulatorList[2].name);
+ expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(emulatorList[2].id);
+ });
+ });
+
+ it('should throw an error if target is specified but does not exist', () => {
+ const emulatorList = [{ name: 'emu1', id: 1 }];
+ const deviceList = ['device1'];
+ const target = 'nonexistentdevice';
+ getInstallTargetSpy.and.returnValue(target);
+
+ deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
+ emulatorSpyObj.list_started.and.returnValue(Promise.resolve([]));
+ emulatorSpyObj.list_images.and.returnValue(Promise.resolve(emulatorList));
+
+ return run.run().then(
+ () => fail('Expected error to be thrown'),
+ err => expect(err).toContain(target)
+ );
+ });
+
+ it('should build the app if a target has been found', () => {
+ getInstallTargetSpy.and.returnValue('--device');
+ deviceSpyObj.resolveTarget.and.returnValue({ target: 'device1', isEmulator: false });
+
+ return run.run().then(() => {
+ expect(buildSpyObj.run).toHaveBeenCalled();
+ });
+ });
+
+ it('should install on device after build', () => {
+ const deviceTarget = { target: 'device1', isEmulator: false };
+ getInstallTargetSpy.and.returnValue('--device');
+ deviceSpyObj.resolveTarget.and.returnValue(deviceTarget);
+
+ return run.run().then(() => {
+ expect(deviceSpyObj.install).toHaveBeenCalledWith(deviceTarget, undefined);
+ });
+ });
+
+ it('should install on emulator after build', () => {
+ const emulatorTarget = { target: 'emu1', isEmulator: true };
+ getInstallTargetSpy.and.returnValue('--emulator');
+ emulatorSpyObj.list_started.and.returnValue(Promise.resolve([emulatorTarget.target]));
+ emulatorSpyObj.resolveTarget.and.returnValue(emulatorTarget);
+ emulatorSpyObj.wait_for_boot.and.returnValue(Promise.resolve());
+
+ return run.run().then(() => {
+ expect(emulatorSpyObj.install).toHaveBeenCalledWith(emulatorTarget, undefined);
+ });
+ });
+ });
+
+ describe('help', () => {
+ it('should print out usage and help', () => {
+ const logSpy = jasmine.createSpy();
+ const procStub = { exit: _ => null, cwd: _ => '', argv: ['', ''] };
+ run.__set__({ console: { log: logSpy }, process: procStub });
+
+ run.help();
+ expect(logSpy).toHaveBeenCalledWith(jasmine.stringMatching(/^Usage:/));
+ });
+ });
});
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org