diff --git a/bin/templates/cordova/lib/android_sdk.js b/bin/templates/cordova/lib/android_sdk.js index 79eeb1af..a1a806a6 100755 --- a/bin/templates/cordova/lib/android_sdk.js +++ b/bin/templates/cordova/lib/android_sdk.js @@ -1,5 +1,3 @@ - - /* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file @@ -95,7 +93,7 @@ module.exports.list_targets = function() { // If there's an error, like avdmanager could not be found, we can try // as a last resort, to run `android`, in case this is a super old // SDK installation. - if (err && err.code == 'ENOENT') { + if (err && (err.code == 'ENOENT' || (err.stderr && err.stderr.match(/not recognized/)))) { return module.exports.list_targets_with_android(); } else throw err; }) diff --git a/bin/templates/cordova/lib/emulator.js b/bin/templates/cordova/lib/emulator.js index c60dcb5c..22209aa0 100644 --- a/bin/templates/cordova/lib/emulator.js +++ b/bin/templates/cordova/lib/emulator.js @@ -172,17 +172,7 @@ module.exports.list_images_using_android = function() { */ module.exports.list_images = function() { if (forgivingWhichSync('avdmanager')) { - return module.exports.list_images_using_avdmanager() - .catch(function(err) { - // try to use `avdmanager` in case `android` reports it is no longer available. - // this likely means the target machine is using a newer version of - // the android sdk, and possibly `avdmanager` is available. - if (err && err.code == 'ENOENT') { - return module.exports.list_images_using_android(); - } else { - throw err; - } - }); + return module.exports.list_images_using_avdmanager(); } else if (forgivingWhichSync('android')) { return module.exports.list_images_using_android(); } else { diff --git a/spec/unit/android_sdk.spec.js b/spec/unit/android_sdk.spec.js index 9434443e..04322ce8 100644 --- a/spec/unit/android_sdk.spec.js +++ b/spec/unit/android_sdk.spec.js @@ -98,9 +98,26 @@ describe("android_sdk", function () { done(); }); }); + it("should parse Android SDK installed target information with `android` command if list_targets_with_avdmanager fails with not-recognized error (Windows)", function(done) { + var deferred = Q.defer(); + spyOn(android_sdk, "list_targets_with_avdmanager").and.returnValue(deferred.promise); + deferred.reject({ + code: 1, + stderr: "'avdmanager' is not recognized as an internal or external commmand,\r\noperable program or batch file.\r\n" + }); + var twoferred = Q.defer(); + twoferred.resolve(["target1"]); + var avdmanager_spy = spyOn(android_sdk, "list_targets_with_android").and.returnValue(twoferred.promise); + return android_sdk.list_targets() + .then(function(targets) { + expect(avdmanager_spy).toHaveBeenCalled(); + expect(targets[0]).toEqual("target1"); + done(); + }); + }); it("should throw an error if no Android targets were found.", function(done) { var deferred = Q.defer(); - spyOn(android_sdk, "list_targets_with_android").and.returnValue(deferred.promise); + spyOn(android_sdk, "list_targets_with_avdmanager").and.returnValue(deferred.promise); deferred.resolve([]); return android_sdk.list_targets() .then(function(targets) { diff --git a/spec/unit/emulator.spec.js b/spec/unit/emulator.spec.js index 88122559..8aef5efc 100644 --- a/spec/unit/emulator.spec.js +++ b/spec/unit/emulator.spec.js @@ -88,23 +88,14 @@ describe("emulator", function () { emu.list_images(); expect(avdmanager_spy).toHaveBeenCalled(); }); - it("should catch if `avdmanager` exits with non-zero code, and delegate to `android` if it can find it", function() { + it("should delegate to `android` if `avdmanager` cant be found and `android` can", function() { spyOn(shelljs, "which").and.callFake(function(cmd) { if (cmd == "avdmanager") { - return true; - } else { return false; + } else { + return true; } }); - spyOn(emu, "list_images_using_avdmanager").and.returnValue({ - catch:function(cb) { - cb({ - code: "ENOENT" - }); - } - }); - - // Fake out the old promise to feign a failed `android` command var android_spy = spyOn(emu, "list_images_using_android"); emu.list_images(); expect(android_spy).toHaveBeenCalled();