Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

android_sdk.spec.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. const superspawn = require('cordova-common').superspawn;
  18. const fs = require('fs');
  19. const path = require('path');
  20. const rewire = require('rewire');
  21. describe('android_sdk', () => {
  22. let android_sdk;
  23. beforeEach(() => {
  24. android_sdk = rewire('../../bin/templates/cordova/lib/android_sdk');
  25. });
  26. describe('sort_by_largest_numerical_suffix', () => {
  27. it('should return the newest version first', () => {
  28. const ids = ['android-24', 'android-19', 'android-27', 'android-23'];
  29. const sortedIds = ['android-27', 'android-24', 'android-23', 'android-19'];
  30. expect(ids.sort(android_sdk.__get__('sort_by_largest_numerical_suffix'))).toEqual(sortedIds);
  31. });
  32. it('should return 0 (no sort) if one of the versions has no number', () => {
  33. const ids = ['android-27', 'android-P'];
  34. expect(android_sdk.__get__('sort_by_largest_numerical_suffix')(ids[0], ids[1])).toBe(0);
  35. });
  36. });
  37. describe('print_newest_available_sdk_target', () => {
  38. it('should log the newest version', () => {
  39. const sortedIds = ['android-27', 'android-24', 'android-23', 'android-19'];
  40. spyOn(android_sdk, 'list_targets').and.returnValue(Promise.resolve(sortedIds));
  41. spyOn(sortedIds, 'sort');
  42. spyOn(console, 'log');
  43. return android_sdk.print_newest_available_sdk_target().then(() => {
  44. expect(sortedIds.sort).toHaveBeenCalledWith(android_sdk.__get__('sort_by_largest_numerical_suffix'));
  45. expect(console.log).toHaveBeenCalledWith(sortedIds[0]);
  46. });
  47. });
  48. });
  49. describe('list_targets_with_android', () => {
  50. 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', () => {
  51. spyOn(superspawn, 'spawn').and.returnValue(new Promise(() => {}, () => {}));
  52. android_sdk.list_targets_with_android();
  53. expect(superspawn.spawn).toHaveBeenCalledWith('android', ['list', 'target']);
  54. });
  55. it('should parse and return results from `android list targets` command', () => {
  56. const testTargets = fs.readFileSync(path.join('spec', 'fixtures', 'sdk25.2-android_list_targets.txt'), 'utf-8');
  57. spyOn(superspawn, 'spawn').and.returnValue(Promise.resolve(testTargets));
  58. return android_sdk.list_targets_with_android().then(list => {
  59. [ 'Google Inc.:Google APIs:23',
  60. 'Google Inc.:Google APIs:22',
  61. 'Google Inc.:Google APIs:21',
  62. 'android-25',
  63. 'android-24',
  64. 'android-N',
  65. 'android-23',
  66. 'android-MNC',
  67. 'android-22',
  68. 'android-21',
  69. 'android-20' ].forEach((target) => expect(list).toContain(target));
  70. });
  71. });
  72. });
  73. describe('list_targets_with_avdmanager', () => {
  74. it('should parse and return results from `avdmanager list target` command', () => {
  75. const testTargets = fs.readFileSync(path.join('spec', 'fixtures', 'sdk25.3-avdmanager_list_target.txt'), 'utf-8');
  76. spyOn(superspawn, 'spawn').and.returnValue(Promise.resolve(testTargets));
  77. return android_sdk.list_targets_with_avdmanager().then(list => {
  78. expect(list).toContain('android-25');
  79. });
  80. });
  81. });
  82. describe('list_targets', () => {
  83. it('should parse Android SDK installed target information with `avdmanager` command first', () => {
  84. const avdmanager_spy = spyOn(android_sdk, 'list_targets_with_avdmanager').and.returnValue(new Promise(() => {}, () => {}));
  85. android_sdk.list_targets();
  86. expect(avdmanager_spy).toHaveBeenCalled();
  87. });
  88. it('should parse Android SDK installed target information with `android` command if list_targets_with_avdmanager fails with ENOENT', () => {
  89. spyOn(android_sdk, 'list_targets_with_avdmanager').and.returnValue(Promise.reject({ code: 'ENOENT' }));
  90. const avdmanager_spy = spyOn(android_sdk, 'list_targets_with_android').and.returnValue(Promise.resolve(['target1']));
  91. return android_sdk.list_targets().then(targets => {
  92. expect(avdmanager_spy).toHaveBeenCalled();
  93. expect(targets[0]).toEqual('target1');
  94. });
  95. });
  96. it('should parse Android SDK installed target information with `android` command if list_targets_with_avdmanager fails with not-recognized error (Windows)', () => {
  97. spyOn(android_sdk, 'list_targets_with_avdmanager').and.returnValue(Promise.reject({
  98. code: 1,
  99. stderr: "'avdmanager' is not recognized as an internal or external commmand,\r\noperable program or batch file.\r\n"
  100. }));
  101. const avdmanager_spy = spyOn(android_sdk, 'list_targets_with_android').and.returnValue(Promise.resolve(['target1']));
  102. return android_sdk.list_targets().then(targets => {
  103. expect(avdmanager_spy).toHaveBeenCalled();
  104. expect(targets[0]).toEqual('target1');
  105. });
  106. });
  107. it('should throw an error if `avdmanager` command fails with an unknown error', () => {
  108. const errorMsg = 'Some random error';
  109. spyOn(android_sdk, 'list_targets_with_avdmanager').and.returnValue(Promise.reject(errorMsg));
  110. return android_sdk.list_targets().then(
  111. () => fail('Unexpectedly resolved'),
  112. err => {
  113. expect(err).toBe(errorMsg);
  114. }
  115. );
  116. });
  117. it('should throw an error if no Android targets were found.', () => {
  118. spyOn(android_sdk, 'list_targets_with_avdmanager').and.returnValue(Promise.resolve([]));
  119. return android_sdk.list_targets().then(
  120. () => fail('Unexpectedly resolved'),
  121. err => {
  122. expect(err).toBeDefined();
  123. expect(err.message).toContain('No android targets (SDKs) installed!');
  124. }
  125. );
  126. });
  127. });
  128. });