2017-03-15 22:47:51 +08:00
/ * *
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 .
* /
2018-09-02 10:44:37 +08:00
const fs = require ( 'fs' ) ;
const path = require ( 'path' ) ;
const rewire = require ( 'rewire' ) ;
describe ( 'android_sdk' , ( ) => {
let android _sdk ;
2020-01-07 06:15:22 +08:00
let execaSpy ;
2018-09-02 10:44:37 +08:00
beforeEach ( ( ) => {
android _sdk = rewire ( '../../bin/templates/cordova/lib/android_sdk' ) ;
2020-01-07 06:15:22 +08:00
execaSpy = jasmine . createSpy ( 'execa' ) ;
android _sdk . _ _set _ _ ( 'execa' , execaSpy ) ;
2018-09-02 10:44:37 +08:00
} ) ;
describe ( 'sort_by_largest_numerical_suffix' , ( ) => {
it ( 'should return the newest version first' , ( ) => {
2020-06-18 20:58:14 +08:00
const ids = [ 'android-P' , 'android-24' , 'android-19' , 'android-27' , 'android-23' ] ;
const sortedIds = [ 'android-27' , 'android-24' , 'android-23' , 'android-19' , 'android-P' ] ;
2018-09-02 10:44:37 +08:00
expect ( ids . sort ( android _sdk . _ _get _ _ ( 'sort_by_largest_numerical_suffix' ) ) ) . toEqual ( sortedIds ) ;
} ) ;
2020-06-18 20:58:14 +08:00
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 ) ;
} ) ;
2018-09-02 10:44:37 +08:00
} ) ;
} ) ;
describe ( 'print_newest_available_sdk_target' , ( ) => {
it ( 'should log the newest version' , ( ) => {
const sortedIds = [ 'android-27' , 'android-24' , 'android-23' , 'android-19' ] ;
spyOn ( android _sdk , 'list_targets' ) . and . returnValue ( Promise . resolve ( sortedIds ) ) ;
spyOn ( sortedIds , 'sort' ) ;
2019-07-17 06:18:12 +08:00
spyOn ( console , 'log' ) ;
2018-09-02 10:44:37 +08:00
return android _sdk . print _newest _available _sdk _target ( ) . then ( ( ) => {
expect ( sortedIds . sort ) . toHaveBeenCalledWith ( android _sdk . _ _get _ _ ( 'sort_by_largest_numerical_suffix' ) ) ;
2019-07-17 06:18:12 +08:00
expect ( console . log ) . toHaveBeenCalledWith ( sortedIds [ 0 ] ) ;
2018-09-02 10:44:37 +08:00
} ) ;
} ) ;
} ) ;
describe ( 'list_targets_with_android' , ( ) => {
it ( 'should invoke `android` with the `list target` command and _not_ the `list targets` command, as the plural form is not supported in some Android SDK Tools versions' , ( ) => {
2020-01-07 06:15:22 +08:00
execaSpy . and . returnValue ( Promise . resolve ( { stdout : '' } ) ) ;
2017-04-07 04:48:58 +08:00
android _sdk . list _targets _with _android ( ) ;
2020-01-07 06:15:22 +08:00
expect ( execaSpy ) . toHaveBeenCalledWith ( 'android' , [ 'list' , 'target' ] ) ;
2017-04-07 04:48:58 +08:00
} ) ;
2018-09-02 10:44:37 +08:00
it ( 'should parse and return results from `android list targets` command' , ( ) => {
const testTargets = fs . readFileSync ( path . join ( 'spec' , 'fixtures' , 'sdk25.2-android_list_targets.txt' ) , 'utf-8' ) ;
2020-01-07 06:15:22 +08:00
execaSpy . and . returnValue ( Promise . resolve ( { stdout : testTargets } ) ) ;
2018-09-02 10:44:37 +08:00
return android _sdk . list _targets _with _android ( ) . then ( list => {
2020-01-31 21:02:48 +08:00
[ 'Google Inc.:Google APIs:23' ,
2017-06-14 02:42:20 +08:00
'Google Inc.:Google APIs:22' ,
'Google Inc.:Google APIs:21' ,
'android-25' ,
'android-24' ,
'android-N' ,
'android-23' ,
'android-MNC' ,
'android-22' ,
'android-21' ,
2020-01-31 21:02:48 +08:00
'android-20' ] . forEach ( ( target ) => expect ( list ) . toContain ( target ) ) ;
2017-03-15 22:47:51 +08:00
} ) ;
} ) ;
} ) ;
2018-09-02 10:44:37 +08:00
describe ( 'list_targets_with_avdmanager' , ( ) => {
it ( 'should parse and return results from `avdmanager list target` command' , ( ) => {
const testTargets = fs . readFileSync ( path . join ( 'spec' , 'fixtures' , 'sdk25.3-avdmanager_list_target.txt' ) , 'utf-8' ) ;
2020-01-07 06:15:22 +08:00
execaSpy . and . returnValue ( Promise . resolve ( { stdout : testTargets } ) ) ;
2018-09-02 10:44:37 +08:00
return android _sdk . list _targets _with _avdmanager ( ) . then ( list => {
2017-06-14 02:42:20 +08:00
expect ( list ) . toContain ( 'android-25' ) ;
2017-03-15 22:47:51 +08:00
} ) ;
} ) ;
} ) ;
2018-09-02 10:44:37 +08:00
describe ( 'list_targets' , ( ) => {
it ( 'should parse Android SDK installed target information with `avdmanager` command first' , ( ) => {
const avdmanager _spy = spyOn ( android _sdk , 'list_targets_with_avdmanager' ) . and . returnValue ( new Promise ( ( ) => { } , ( ) => { } ) ) ;
2017-03-15 22:47:51 +08:00
android _sdk . list _targets ( ) ;
2017-04-07 04:13:38 +08:00
expect ( avdmanager _spy ) . toHaveBeenCalled ( ) ;
2017-03-15 22:47:51 +08:00
} ) ;
2018-09-02 10:44:37 +08:00
it ( 'should parse Android SDK installed target information with `android` command if list_targets_with_avdmanager fails with ENOENT' , ( ) => {
spyOn ( android _sdk , 'list_targets_with_avdmanager' ) . and . returnValue ( Promise . reject ( { code : 'ENOENT' } ) ) ;
const avdmanager _spy = spyOn ( android _sdk , 'list_targets_with_android' ) . and . returnValue ( Promise . resolve ( [ 'target1' ] ) ) ;
return android _sdk . list _targets ( ) . then ( targets => {
2017-04-07 04:13:38 +08:00
expect ( avdmanager _spy ) . toHaveBeenCalled ( ) ;
2017-06-14 02:42:20 +08:00
expect ( targets [ 0 ] ) . toEqual ( 'target1' ) ;
2017-03-15 22:47:51 +08:00
} ) ;
} ) ;
2018-09-02 10:44:37 +08:00
it ( 'should parse Android SDK installed target information with `android` command if list_targets_with_avdmanager fails with not-recognized error (Windows)' , ( ) => {
spyOn ( android _sdk , 'list_targets_with_avdmanager' ) . and . returnValue ( Promise . reject ( {
2017-04-07 06:08:22 +08:00
code : 1 ,
stderr : "'avdmanager' is not recognized as an internal or external commmand,\r\noperable program or batch file.\r\n"
2018-09-02 10:44:37 +08:00
} ) ) ;
const avdmanager _spy = spyOn ( android _sdk , 'list_targets_with_android' ) . and . returnValue ( Promise . resolve ( [ 'target1' ] ) ) ;
return android _sdk . list _targets ( ) . then ( targets => {
2017-04-07 06:08:22 +08:00
expect ( avdmanager _spy ) . toHaveBeenCalled ( ) ;
2017-06-14 02:42:20 +08:00
expect ( targets [ 0 ] ) . toEqual ( 'target1' ) ;
2017-04-07 06:08:22 +08:00
} ) ;
} ) ;
2018-09-02 10:44:37 +08:00
it ( 'should throw an error if `avdmanager` command fails with an unknown error' , ( ) => {
const errorMsg = 'Some random error' ;
spyOn ( android _sdk , 'list_targets_with_avdmanager' ) . and . returnValue ( Promise . reject ( errorMsg ) ) ;
return android _sdk . list _targets ( ) . then (
( ) => fail ( 'Unexpectedly resolved' ) ,
err => {
expect ( err ) . toBe ( errorMsg ) ;
}
) ;
} ) ;
it ( 'should throw an error if no Android targets were found.' , ( ) => {
spyOn ( android _sdk , 'list_targets_with_avdmanager' ) . and . returnValue ( Promise . resolve ( [ ] ) ) ;
return android _sdk . list _targets ( ) . then (
( ) => fail ( 'Unexpectedly resolved' ) ,
err => {
expect ( err ) . toBeDefined ( ) ;
expect ( err . message ) . toContain ( 'No android targets (SDKs) installed!' ) ;
}
) ;
2017-03-15 22:47:51 +08:00
} ) ;
} ) ;
} ) ;