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

check_reqs.spec.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. var check_reqs = require('../../bin/templates/cordova/lib/check_reqs');
  18. var android_sdk = require('../../bin/templates/cordova/lib/android_sdk');
  19. var shelljs = require('shelljs');
  20. var fs = require('fs');
  21. var path = require('path');
  22. var Q = require('q');
  23. describe('check_reqs', function () {
  24. var original_env;
  25. beforeAll(function () {
  26. original_env = Object.create(process.env);
  27. });
  28. afterEach(function () {
  29. Object.keys(original_env).forEach(function (k) {
  30. process.env[k] = original_env[k];
  31. });
  32. });
  33. describe('check_android', function () {
  34. describe('find and set ANDROID_HOME when ANDROID_HOME and ANDROID_SDK_ROOT is not set', function () {
  35. beforeEach(function () {
  36. delete process.env.ANDROID_HOME;
  37. delete process.env.ANDROID_SDK_ROOT;
  38. });
  39. describe('even if no Android binaries are on the PATH', function () {
  40. beforeEach(function () {
  41. spyOn(shelljs, 'which').and.returnValue(null);
  42. spyOn(fs, 'existsSync').and.returnValue(true);
  43. });
  44. it('it should set ANDROID_HOME on Windows', () => {
  45. spyOn(check_reqs, 'isWindows').and.returnValue(true);
  46. process.env.LOCALAPPDATA = 'windows-local-app-data';
  47. process.env.ProgramFiles = 'windows-program-files';
  48. return check_reqs.check_android().then(function () {
  49. expect(process.env.ANDROID_HOME).toContain('windows-local-app-data');
  50. }).fin(function () {
  51. delete process.env.LOCALAPPDATA;
  52. delete process.env.ProgramFiles;
  53. });
  54. });
  55. it('it should set ANDROID_HOME on Darwin', () => {
  56. spyOn(check_reqs, 'isWindows').and.returnValue(false);
  57. spyOn(check_reqs, 'isDarwin').and.returnValue(true);
  58. process.env.HOME = 'home is where the heart is';
  59. return check_reqs.check_android().then(function () {
  60. expect(process.env.ANDROID_HOME).toContain('home is where the heart is');
  61. }).fin(function () {
  62. delete process.env.HOME;
  63. });
  64. });
  65. });
  66. describe('if some Android tooling exists on the PATH', function () {
  67. beforeEach(function () {
  68. spyOn(fs, 'realpathSync').and.callFake(function (path) {
  69. return path;
  70. });
  71. });
  72. it('should set ANDROID_HOME based on `android` command if command exists in a SDK-like directory structure', () => {
  73. spyOn(fs, 'existsSync').and.returnValue(true);
  74. spyOn(shelljs, 'which').and.callFake(function (cmd) {
  75. if (cmd === 'android') {
  76. return '/android/sdk/tools/android';
  77. } else {
  78. return null;
  79. }
  80. });
  81. return check_reqs.check_android().then(function () {
  82. expect(process.env.ANDROID_HOME).toEqual('/android/sdk');
  83. });
  84. });
  85. it('should error out if `android` command exists in a non-SDK-like directory structure', () => {
  86. spyOn(shelljs, 'which').and.callFake(function (cmd) {
  87. if (cmd === 'android') {
  88. return '/just/some/random/path/android';
  89. } else {
  90. return null;
  91. }
  92. });
  93. return check_reqs.check_android().then(() => {
  94. fail('Expected promise to be rejected');
  95. }, err => {
  96. expect(err).toEqual(jasmine.any(Error));
  97. expect(err.message).toContain('update your PATH to include valid path');
  98. });
  99. });
  100. it('should set ANDROID_HOME based on `adb` command if command exists in a SDK-like directory structure', () => {
  101. spyOn(fs, 'existsSync').and.returnValue(true);
  102. spyOn(shelljs, 'which').and.callFake(function (cmd) {
  103. if (cmd === 'adb') {
  104. return '/android/sdk/platform-tools/adb';
  105. } else {
  106. return null;
  107. }
  108. });
  109. return check_reqs.check_android().then(function () {
  110. expect(process.env.ANDROID_HOME).toEqual('/android/sdk');
  111. });
  112. });
  113. it('should error out if `adb` command exists in a non-SDK-like directory structure', () => {
  114. spyOn(shelljs, 'which').and.callFake(function (cmd) {
  115. if (cmd === 'adb') {
  116. return '/just/some/random/path/adb';
  117. } else {
  118. return null;
  119. }
  120. });
  121. return check_reqs.check_android().then(() => {
  122. fail('Expected promise to be rejected');
  123. }, err => {
  124. expect(err).toEqual(jasmine.any(Error));
  125. expect(err.message).toContain('update your PATH to include valid path');
  126. });
  127. });
  128. it('should set ANDROID_HOME based on `avdmanager` command if command exists in a SDK-like directory structure', () => {
  129. spyOn(fs, 'existsSync').and.returnValue(true);
  130. spyOn(shelljs, 'which').and.callFake(function (cmd) {
  131. if (cmd === 'avdmanager') {
  132. return '/android/sdk/tools/bin/avdmanager';
  133. } else {
  134. return null;
  135. }
  136. });
  137. return check_reqs.check_android().then(function () {
  138. expect(process.env.ANDROID_HOME).toEqual('/android/sdk');
  139. });
  140. });
  141. it('should error out if `avdmanager` command exists in a non-SDK-like directory structure', () => {
  142. spyOn(shelljs, 'which').and.callFake(function (cmd) {
  143. if (cmd === 'avdmanager') {
  144. return '/just/some/random/path/avdmanager';
  145. } else {
  146. return null;
  147. }
  148. });
  149. return check_reqs.check_android().then(() => {
  150. fail('Expected promise to be rejected');
  151. }, err => {
  152. expect(err).toEqual(jasmine.any(Error));
  153. expect(err.message).toContain('update your PATH to include valid path');
  154. });
  155. });
  156. });
  157. });
  158. describe('set PATH for various Android binaries if not available', function () {
  159. beforeEach(function () {
  160. spyOn(shelljs, 'which').and.returnValue(null);
  161. process.env.ANDROID_HOME = 'let the children play';
  162. spyOn(fs, 'existsSync').and.returnValue(true);
  163. });
  164. afterEach(function () {
  165. delete process.env.ANDROID_HOME;
  166. });
  167. it('should add tools/bin,tools,platform-tools to PATH if `avdmanager`,`android`,`adb` is not found', () => {
  168. return check_reqs.check_android().then(function () {
  169. expect(process.env.PATH).toContain('let the children play' + path.sep + 'tools');
  170. expect(process.env.PATH).toContain('let the children play' + path.sep + 'platform-tools');
  171. expect(process.env.PATH).toContain('let the children play' + path.sep + 'tools' + path.sep + 'bin');
  172. });
  173. });
  174. });
  175. });
  176. describe('get_target', function () {
  177. it('should retrieve target from framework project.properties file', function () {
  178. var target = check_reqs.get_target();
  179. expect(target).toBeDefined();
  180. expect(target).toContain('android-');
  181. });
  182. });
  183. describe('check_android_target', function () {
  184. it('should should return full list of supported targets if there is a match to ideal api level', () => {
  185. var deferred = Q.defer();
  186. spyOn(android_sdk, 'list_targets').and.returnValue(deferred.promise);
  187. var fake_targets = ['you are my fire', 'my one desire'];
  188. deferred.resolve(fake_targets);
  189. spyOn(check_reqs, 'get_target').and.returnValue('you are my fire');
  190. return check_reqs.check_android_target().then(function (targets) {
  191. expect(targets).toBeDefined();
  192. expect(targets).toEqual(fake_targets);
  193. });
  194. });
  195. it('should error out if there is no match between ideal api level and installed targets', () => {
  196. var deferred = Q.defer();
  197. spyOn(android_sdk, 'list_targets').and.returnValue(deferred.promise);
  198. var fake_targets = ['you are my fire', 'my one desire'];
  199. deferred.resolve(fake_targets);
  200. spyOn(check_reqs, 'get_target').and.returnValue('and i knowwwwwwwwwwww');
  201. return check_reqs.check_android_target().then(() => {
  202. fail('Expected promise to be rejected');
  203. }, err => {
  204. expect(err).toEqual(jasmine.any(Error));
  205. expect(err.message).toContain('Please install Android target');
  206. });
  207. });
  208. });
  209. });