From 8d8600b442a5c98959317b665a5b5e157620ccb4 Mon Sep 17 00:00:00 2001 From: Norman Breau Date: Thu, 18 Jun 2020 09:58:14 -0300 Subject: [PATCH] fix: Plugin install fails when preview sdk is installed (#985) --- bin/templates/cordova/lib/android_sdk.js | 19 ++++++++----------- spec/unit/android_sdk.spec.js | 17 ++++++++++++----- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/bin/templates/cordova/lib/android_sdk.js b/bin/templates/cordova/lib/android_sdk.js index 2ab843de..963d7f1d 100755 --- a/bin/templates/cordova/lib/android_sdk.js +++ b/bin/templates/cordova/lib/android_sdk.js @@ -29,17 +29,14 @@ var suffix_number_regex = /(\d+)$/; // the number at the end, the more recent the target, the closer to the // start of the array. function sort_by_largest_numerical_suffix (a, b) { - var suffix_a = a.match(suffix_number_regex); - var suffix_b = b.match(suffix_number_regex); - if (suffix_a && suffix_b) { - // If the two targets being compared have suffixes, return less than - // zero, or greater than zero, based on which suffix is larger. - return (parseInt(suffix_a[1]) > parseInt(suffix_b[1]) ? -1 : 1); - } else { - // If no suffix numbers were detected, leave the order as-is between - // elements a and b. - return 0; - } + let suffix_a = a.match(suffix_number_regex); + let suffix_b = b.match(suffix_number_regex); + // If no number is detected (eg: preview version like android-R), + // designate a suffix of 0 so it gets moved to the end + suffix_a = suffix_a || ['0', '0']; + suffix_b = suffix_b || ['0', '0']; + // Return < zero, or > zero, based on which suffix is larger. + return (parseInt(suffix_a[1]) > parseInt(suffix_b[1]) ? -1 : 1); } module.exports.print_newest_available_sdk_target = function () { diff --git a/spec/unit/android_sdk.spec.js b/spec/unit/android_sdk.spec.js index 6cf77c21..346bca9b 100644 --- a/spec/unit/android_sdk.spec.js +++ b/spec/unit/android_sdk.spec.js @@ -33,14 +33,21 @@ describe('android_sdk', () => { describe('sort_by_largest_numerical_suffix', () => { it('should return the newest version first', () => { - const ids = ['android-24', 'android-19', 'android-27', 'android-23']; - const sortedIds = ['android-27', 'android-24', 'android-23', 'android-19']; + const ids = ['android-P', 'android-24', 'android-19', 'android-27', 'android-23']; + const sortedIds = ['android-27', 'android-24', 'android-23', 'android-19', 'android-P']; expect(ids.sort(android_sdk.__get__('sort_by_largest_numerical_suffix'))).toEqual(sortedIds); }); - it('should return 0 (no sort) if one of the versions has no number', () => { - const ids = ['android-27', 'android-P']; - expect(android_sdk.__get__('sort_by_largest_numerical_suffix')(ids[0], ids[1])).toBe(0); + describe('should return release version over preview versions', () => { + it('Test #001', () => { + const ids = ['android-27', 'android-P']; + expect(android_sdk.__get__('sort_by_largest_numerical_suffix')(ids[0], ids[1])).toBe(-1); + }); + + it('Test #002', () => { + const ids = ['android-P', 'android-27']; + expect(android_sdk.__get__('sort_by_largest_numerical_suffix')(ids[0], ids[1])).toBe(1); + }); }); });