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

create.spec.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 rewire = require('rewire');
  18. var create = rewire('../../bin/lib/create');
  19. var check_reqs = require('../../bin/templates/cordova/lib/check_reqs');
  20. var fs = require('fs');
  21. var path = require('path');
  22. var Q = require('q');
  23. var shell = require('shelljs');
  24. describe('create', function () {
  25. describe('validatePackageName helper method', function () {
  26. describe('happy path (valid package names)', function () {
  27. var valid = [
  28. 'org.apache.mobilespec',
  29. 'com.example',
  30. 'com.floors42.package',
  31. 'ball8.ball8.ball8ball'
  32. ];
  33. valid.forEach(function (package_name) {
  34. it('Test#001 : should accept ' + package_name, () => {
  35. return create.validatePackageName(package_name);
  36. });
  37. });
  38. });
  39. describe('failure cases (invalid package names)', function () {
  40. function expectPackageNameToBeRejected (name) {
  41. return create.validatePackageName(name).then(() => {
  42. fail('Expected promise to be rejected');
  43. }, err => {
  44. expect(err).toEqual(jasmine.any(Error));
  45. expect(err.message).toContain('Error validating package name');
  46. });
  47. }
  48. it('should reject empty package names', () => {
  49. return expectPackageNameToBeRejected('');
  50. });
  51. it('should reject package names containing "class"', () => {
  52. return expectPackageNameToBeRejected('com.class.is.bad');
  53. });
  54. it('should reject package names that do not start with a latin letter', () => {
  55. return expectPackageNameToBeRejected('_un.der.score');
  56. });
  57. it('should reject package names with terms that do not start with a latin letter', () => {
  58. return expectPackageNameToBeRejected('un._der.score');
  59. });
  60. it('should reject package names containing non-alphanumeric or underscore characters', () => {
  61. return expectPackageNameToBeRejected('th!$.!$.b@d');
  62. });
  63. it('should reject package names that do not contain enough dots', () => {
  64. return expectPackageNameToBeRejected('therearenodotshere');
  65. });
  66. it('should reject package names that end with a dot', () => {
  67. return expectPackageNameToBeRejected('this.is.a.complete.sentence.');
  68. });
  69. });
  70. });
  71. describe('validateProjectName helper method', function () {
  72. describe('happy path (valid project names)', function () {
  73. var valid = [
  74. 'mobilespec',
  75. 'package_name',
  76. 'PackageName',
  77. 'CordovaLib'
  78. ];
  79. valid.forEach(function (project_name) {
  80. it('Test#003 : should accept ' + project_name, () => {
  81. return create.validateProjectName(project_name);
  82. });
  83. });
  84. });
  85. describe('failure cases (invalid project names)', function () {
  86. it('should reject empty project names', () => {
  87. return create.validateProjectName('').then(() => {
  88. fail('Expected promise to be rejected');
  89. }, err => {
  90. expect(err).toEqual(jasmine.any(Error));
  91. expect(err.message).toContain('Project name cannot be empty');
  92. });
  93. });
  94. it('should reject "CordovaActivity" as a project name', () => {
  95. return create.validateProjectName('CordovaActivity').then(() => {
  96. fail('Expected promise to be rejected');
  97. }, err => {
  98. expect(err).toEqual(jasmine.any(Error));
  99. expect(err.message).toContain('Project name cannot be CordovaActivity');
  100. });
  101. });
  102. it('should reject project names that begin with a number', () => {
  103. return create.validateProjectName('1337').then(() => {
  104. fail('Expected promise to be rejected');
  105. }, err => {
  106. expect(err).toEqual(jasmine.any(Error));
  107. expect(err.message).toContain('Project name must not begin with a number');
  108. });
  109. });
  110. });
  111. });
  112. describe('main method', function () {
  113. var config_mock;
  114. var events_mock;
  115. var Manifest_mock = function () {};
  116. var revert_manifest_mock;
  117. var project_path = path.join('some', 'path');
  118. var app_path = path.join(project_path, 'app', 'src', 'main');
  119. var default_templates = path.join(__dirname, '..', '..', 'bin', 'templates', 'project');
  120. var fake_android_target = 'android-1337';
  121. beforeEach(function () {
  122. Manifest_mock.prototype = jasmine.createSpyObj('AndroidManifest instance mock', ['setPackageId', 'getActivity', 'setName', 'write']);
  123. Manifest_mock.prototype.setPackageId.and.returnValue(new Manifest_mock());
  124. Manifest_mock.prototype.getActivity.and.returnValue(new Manifest_mock());
  125. Manifest_mock.prototype.setName.and.returnValue(new Manifest_mock());
  126. spyOn(create, 'validatePackageName').and.returnValue(Q());
  127. spyOn(create, 'validateProjectName').and.returnValue(Q());
  128. spyOn(create, 'setShellFatal').and.callFake(function (noop, cb) { cb(); });
  129. spyOn(create, 'copyJsAndLibrary');
  130. spyOn(create, 'copyScripts');
  131. spyOn(create, 'copyBuildRules');
  132. spyOn(create, 'writeProjectProperties');
  133. spyOn(create, 'prepBuildFiles');
  134. revert_manifest_mock = create.__set__('AndroidManifest', Manifest_mock);
  135. spyOn(fs, 'existsSync').and.returnValue(false);
  136. spyOn(shell, 'cp');
  137. spyOn(shell, 'mkdir');
  138. spyOn(shell, 'sed');
  139. config_mock = jasmine.createSpyObj('ConfigParser mock instance', ['packageName', 'android_packageName', 'name', 'android_activityName']);
  140. events_mock = jasmine.createSpyObj('EventEmitter mock instance', ['emit']);
  141. spyOn(check_reqs, 'get_target').and.returnValue(fake_android_target);
  142. });
  143. afterEach(function () {
  144. revert_manifest_mock();
  145. });
  146. describe('parameter values and defaults', function () {
  147. it('should have a default package name of my.cordova.project', () => {
  148. config_mock.packageName.and.returnValue(undefined);
  149. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  150. expect(create.validatePackageName).toHaveBeenCalledWith('my.cordova.project');
  151. });
  152. });
  153. it('should use the ConfigParser-provided package name, if exists', () => {
  154. config_mock.packageName.and.returnValue('org.apache.cordova');
  155. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  156. expect(create.validatePackageName).toHaveBeenCalledWith('org.apache.cordova');
  157. });
  158. });
  159. it('should have a default project name of CordovaExample', () => {
  160. config_mock.name.and.returnValue(undefined);
  161. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  162. expect(create.validateProjectName).toHaveBeenCalledWith('CordovaExample');
  163. });
  164. });
  165. it('should use the ConfigParser-provided project name, if exists', () => {
  166. config_mock.name.and.returnValue('MySweetAppName');
  167. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  168. expect(create.validateProjectName).toHaveBeenCalledWith('MySweetAppName');
  169. });
  170. });
  171. it('should replace any non-word characters (including unicode and spaces) in the ConfigParser-provided project name with underscores', () => {
  172. config_mock.name.and.returnValue('応応応応 hello 用用用用');
  173. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  174. expect(create.validateProjectName).toHaveBeenCalledWith('_____hello_____');
  175. });
  176. });
  177. it('should have a default activity name of MainActivity', () => {
  178. config_mock.android_activityName.and.returnValue(undefined);
  179. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  180. expect(Manifest_mock.prototype.setName).toHaveBeenCalledWith('MainActivity');
  181. });
  182. });
  183. it('should use the activityName provided via options parameter, if exists', () => {
  184. config_mock.android_activityName.and.returnValue(undefined);
  185. return create.create(project_path, config_mock, { activityName: 'AwesomeActivity' }, events_mock).then(() => {
  186. expect(Manifest_mock.prototype.setName).toHaveBeenCalledWith('AwesomeActivity');
  187. });
  188. });
  189. it('should use the ConfigParser-provided activity name, if exists', () => {
  190. config_mock.android_activityName.and.returnValue('AmazingActivity');
  191. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  192. expect(Manifest_mock.prototype.setName).toHaveBeenCalledWith('AmazingActivity');
  193. });
  194. });
  195. });
  196. describe('failure', function () {
  197. it('should fail if the target path already exists', () => {
  198. fs.existsSync.and.returnValue(true);
  199. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  200. fail('Expected promise to be rejected');
  201. }, err => {
  202. expect(err).toEqual(jasmine.any(Error));
  203. expect(err.message).toContain('Project already exists!');
  204. });
  205. });
  206. it('should fail if validateProjectName rejects', () => {
  207. const fakeError = new Error();
  208. create.validateProjectName.and.callFake(() => Promise.reject(fakeError));
  209. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  210. fail('Expected promise to be rejected');
  211. }, err => {
  212. expect(err).toBe(fakeError);
  213. });
  214. });
  215. });
  216. describe('happy path', function () {
  217. it('should copy project templates from a specified custom template', () => {
  218. return create.create(project_path, config_mock, { customTemplate: '/template/path' }, events_mock).then(() => {
  219. expect(shell.cp).toHaveBeenCalledWith('-r', path.join('/template/path', 'assets'), app_path);
  220. expect(shell.cp).toHaveBeenCalledWith('-r', path.join('/template/path', 'res'), app_path);
  221. expect(shell.cp).toHaveBeenCalledWith(path.join('/template/path', 'gitignore'), path.join(project_path, '.gitignore'));
  222. });
  223. });
  224. it('should copy project templates from the default templates location if no custom template is provided', () => {
  225. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  226. expect(shell.cp).toHaveBeenCalledWith('-r', path.join(default_templates, 'assets'), app_path);
  227. expect(shell.cp).toHaveBeenCalledWith('-r', path.join(default_templates, 'res'), app_path);
  228. expect(shell.cp).toHaveBeenCalledWith(path.join(default_templates, 'gitignore'), path.join(project_path, '.gitignore'));
  229. });
  230. });
  231. it('should copy JS and library assets', () => {
  232. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  233. expect(create.copyJsAndLibrary).toHaveBeenCalled();
  234. });
  235. });
  236. it('should create a java src directory based on the provided project package name', () => {
  237. config_mock.packageName.and.returnValue('org.apache.cordova');
  238. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  239. expect(shell.mkdir).toHaveBeenCalledWith('-p', path.join(app_path, 'java', 'org', 'apache', 'cordova'));
  240. });
  241. });
  242. it('should copy, rename and interpolate the template Activity java class with the project-specific activity name and package name', () => {
  243. config_mock.packageName.and.returnValue('org.apache.cordova');
  244. config_mock.android_activityName.and.returnValue('CEEDEEVEE');
  245. var activity_path = path.join(app_path, 'java', 'org', 'apache', 'cordova', 'CEEDEEVEE.java');
  246. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  247. expect(shell.cp).toHaveBeenCalledWith('-f', path.join(default_templates, 'Activity.java'), activity_path);
  248. expect(shell.sed).toHaveBeenCalledWith('-i', /__ACTIVITY__/, 'CEEDEEVEE', activity_path);
  249. expect(shell.sed).toHaveBeenCalledWith('-i', /__ID__/, 'org.apache.cordova', activity_path);
  250. });
  251. });
  252. it('should interpolate the project name into strings.xml', () => {
  253. config_mock.name.and.returnValue('IncredibleApp');
  254. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  255. expect(shell.sed).toHaveBeenCalledWith('-i', /__NAME__/, 'IncredibleApp', path.join(app_path, 'res', 'values', 'strings.xml'));
  256. });
  257. });
  258. it('should copy template scripts into generated project', () => {
  259. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  260. expect(create.copyScripts).toHaveBeenCalledWith(project_path);
  261. });
  262. });
  263. it('should copy build rules / gradle files into generated project', () => {
  264. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  265. expect(create.copyBuildRules).toHaveBeenCalledWith(project_path);
  266. });
  267. });
  268. it('should write project.properties file with project details and target API', () => {
  269. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  270. expect(create.writeProjectProperties).toHaveBeenCalledWith(project_path, fake_android_target);
  271. });
  272. });
  273. it('should prepare build files', () => {
  274. return create.create(project_path, config_mock, {}, events_mock).then(() => {
  275. expect(create.prepBuildFiles).toHaveBeenCalledWith(project_path);
  276. });
  277. });
  278. });
  279. });
  280. });