From ce2525d4d8b2b7c2af0b467f99490529c14beb1d Mon Sep 17 00:00:00 2001 From: daserge Date: Thu, 18 Feb 2016 21:27:40 +0300 Subject: [PATCH] CB-10628 Fix emulate android --target Added a test case --- bin/templates/cordova/lib/emulator.js | 7 ++--- bin/templates/cordova/lib/run.js | 18 ++++++++++--- package.json | 3 ++- spec/unit/run.spec.js | 38 +++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 spec/unit/run.spec.js diff --git a/bin/templates/cordova/lib/emulator.js b/bin/templates/cordova/lib/emulator.js index e2908972..96bb5c94 100644 --- a/bin/templates/cordova/lib/emulator.js +++ b/bin/templates/cordova/lib/emulator.js @@ -23,7 +23,6 @@ var retry = require('./retry'); var build = require('./build'); -var check_reqs = require('./check_reqs'); var path = require('path'); var Adb = require('./Adb'); var AndroidManifest = require('./AndroidManifest'); @@ -105,7 +104,8 @@ module.exports.best_image = function() { var closest = 9999; var best = images[0]; - var project_target = check_reqs.get_target().replace('android-', ''); + // Loading check_reqs at run-time to avoid test-time vs run-time directory structure difference issue + var project_target = require('./check_reqs').get_target().replace('android-', ''); for (var i in images) { var target = images[i].target; if(target) { @@ -165,7 +165,8 @@ module.exports.start = function(emulator_ID, boot_timeout) { return best.name; } - var androidCmd = check_reqs.getAbsoluteAndroidCmd(); + // Loading check_reqs at run-time to avoid test-time vs run-time directory structure difference issue + var androidCmd = require('./check_reqs').getAbsoluteAndroidCmd(); return Q.reject(new CordovaError('No emulator images (avds) found.\n' + '1. Download desired System Image by running: ' + androidCmd + ' sdk\n' + '2. Create an AVD by running: ' + androidCmd + ' avd\n' + diff --git a/bin/templates/cordova/lib/run.js b/bin/templates/cordova/lib/run.js index 48236dd7..3cc5c0d5 100644 --- a/bin/templates/cordova/lib/run.js +++ b/bin/templates/cordova/lib/run.js @@ -27,6 +27,19 @@ var path = require('path'), device = require('./device'), Q = require('q'); +function getInstallTarget(runOptions) { + var install_target; + if (runOptions.target) { + install_target = runOptions.target; + } else if (runOptions.device) { + install_target = '--device'; + } else if (runOptions.emulator) { + install_target = '--emulator'; + } + + return install_target; +} + /** * Runs the application on a device if available. If no device is found, it will * use a started emulator. If no started emulators are found it will attempt @@ -40,10 +53,7 @@ var path = require('path'), module.exports.run = function(runOptions) { var self = this; - - var install_target = runOptions.device ? '--device' : - runOptions.emulator ? '--emulator' : - runOptions.target; + var install_target = getInstallTarget(runOptions); return Q() .then(function() { diff --git a/package.json b/package.json index d3c59421..5af1fc7a 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "devDependencies": { "jasmine-node": "^1.14.5", "jshint": "^2.6.0", - "promise-matchers": "~0" + "promise-matchers": "~0", + "rewire": "^2.1.3" } } diff --git a/spec/unit/run.spec.js b/spec/unit/run.spec.js new file mode 100644 index 00000000..8c5ff99f --- /dev/null +++ b/spec/unit/run.spec.js @@ -0,0 +1,38 @@ +/** + 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. +*/ + +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("should select correct target based on the run opts", function() { + expect(getInstallTarget(targetOpts)).toBe("emu"); + expect(getInstallTarget(deviceOpts)).toBe("--device"); + expect(getInstallTarget(emulatorOpts)).toBe("--emulator"); + expect(getInstallTarget(emptyOpts)).toBeUndefined(); + }); + }); +});