added travis and appveyor
[cordova-create.git] / spec / create.spec.js
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
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 var helpers = require('./helpers'),
21 path = require('path'),
22 shell = require('shelljs'),
23 Q = require('q'),
24 events = require('cordova-common').events,
25 ConfigParser = require('cordova-common').ConfigParser,
26 create = require('../index'),
27 CordovaLogger = require('cordova-common').CordovaLogger.get().setLevel('error');
28
29 var tmpDir = helpers.tmpDir('create_test');
30 var appName = 'TestBase';
31 var appId = 'org.testing';
32 var project = path.join(tmpDir, appName);
33
34 var configSubDirPkgJson = {
35 lib: {
36 www: {
37 template: true,
38 url: path.join(__dirname, 'templates', 'withsubdirectory_package_json'),
39 version: ''
40 }
41 }
42 };
43
44 var configConfigInWww = {
45 lib: {
46 www: {
47 template: true,
48 url: path.join(__dirname, 'templates', 'config_in_www'),
49 version: ''
50 }
51 }
52 };
53
54 var configGit = {
55 lib: {
56 www: {
57 url: 'https://github.com/apache/cordova-app-hello-world',
58 template: true,
59 version: 'not_versioned'
60 }
61 }
62 };
63
64 var configNPM = {
65 lib: {
66 www: {
67 template: true,
68 url: 'cordova-app-hello-world',
69 version: ''
70 }
71 }
72 };
73
74 describe('cordova create checks for valid-identifier', function(done) {
75 it('should reject reserved words from start of id', function(done) {
76 create('projectPath', 'int.bob', 'appName')
77 .fail(function(err) {
78 expect(err.message).toBe('App id contains a reserved word, or is not a valid identifier.');
79 })
80 .fin(done);
81 });
82
83 it('should reject reserved words from end of id', function(done) {
84 create('projectPath', 'bob.class', 'appName')
85 .fail(function(err) {
86 expect(err.message).toBe('App id contains a reserved word, or is not a valid identifier.');
87 })
88 .fin(done);
89 });
90 });
91
92
93 describe('create end-to-end', function() {
94 //this.timeout(240000);
95
96 beforeEach(function() {
97 shell.rm('-rf', project);
98 shell.mkdir('-p', tmpDir);
99 });
100
101
102 afterEach(function() {
103 process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
104 shell.rm('-rf', tmpDir);
105 });
106
107 function checkProject() {
108 console.log('hi');
109 // Check if top level dirs exist.
110 var dirs = ['hooks', 'platforms', 'plugins', 'www'];
111 dirs.forEach(function(d) {
112 expect(path.join(project, d)).toExist();
113 });
114
115 expect(path.join(project, 'hooks', 'README.md')).toExist();
116
117 // Check if www files exist.
118 expect(path.join(project, 'www', 'index.html')).toExist();
119
120 // Check that config.xml was updated.
121 var configXml = new ConfigParser(path.join(project, 'config.xml'));
122 expect(configXml.packageName()).toEqual(appId);
123
124 // TODO (kamrik): check somehow that we got the right config.xml from the fixture and not some place else.
125 // expect(configXml.name()).toEqual('TestBase');
126 }
127
128 function checkConfigXml() {
129 // Check if top level dirs exist.
130 var dirs = ['hooks', 'platforms', 'plugins', 'www'];
131 dirs.forEach(function(d) {
132 expect(path.join(project, d)).toExist();
133 });
134 expect(path.join(project, 'hooks', 'README.md')).toExist();
135
136 //index.js and template subdir folder should not exist (inner files should be copied to the project folder)
137 expect(path.join(project, 'index.js')).not.toExist();
138 expect(path.join(project, 'template')).not.toExist();
139
140 // Check if www files exist.
141 expect(path.join(project, 'www', 'index.html')).toExist();
142 var configXml = new ConfigParser(path.join(project, 'config.xml'));
143 expect(configXml.packageName()).toEqual(appId);
144 expect(configXml.version()).toEqual('1.0.0');
145
146 // Check that config.xml does not exist inside of www
147 expect(path.join(project, 'www', 'config.xml')).not.toExist();
148
149 // Check that we got no package.json
150 expect(path.join(project, 'package.json')).not.toExist();
151
152 // Check that we got the right config.xml from the template and not stock
153 expect(configXml.description()).toEqual('this is the correct config.xml');
154 }
155
156 function checkSubDir() {
157 // Check if top level dirs exist.
158 var dirs = ['hooks', 'platforms', 'plugins', 'www'];
159 dirs.forEach(function(d) {
160 expect(path.join(project, d)).toExist();
161 });
162 expect(path.join(project, 'hooks', 'README.md')).toExist();
163
164 //index.js and template subdir folder should not exist (inner files should be copied to the project folder)
165 expect(path.join(project, 'index.js')).not.toExist();
166 expect(path.join(project, 'template')).not.toExist();
167
168 // Check if config files exist.
169 expect(path.join(project, 'www', 'index.html')).toExist();
170
171 // Check that config.xml was updated.
172 var configXml = new ConfigParser(path.join(project, 'config.xml'));
173 expect(configXml.packageName()).toEqual(appId);
174 expect(configXml.version()).toEqual('1.0.0');
175
176
177 // Check that we got package.json (the correct one)
178 var pkjson = require(path.join(project, 'package.json'));
179 expect(pkjson.name).toEqual(appName.toLowerCase());
180 expect(pkjson.valid).toEqual('true');
181
182 // Check that we got the right config.xml
183 expect(configXml.description()).toEqual('this is the correct config.xml');
184 }
185
186 var results;
187 events.on('results', function(res) { results = res; });
188
189 it('should successfully run with Git URL', function(done) {
190 // Call cordova create with no args, should return help.
191 Q()
192 .then(function() {
193 console.log('creating proj');
194 // Create a real project
195 return create(project, appId, appName, configGit);
196 })
197 .then(checkProject)
198 .fail(function(err) {
199 console.log(err && err.stack);
200 expect(err).toBeUndefined();
201 })
202 .fin(done);
203 }, 60000);
204
205 it('should successfully run with NPM package', function(done) {
206 // Call cordova create with no args, should return help.
207 Q()
208 .then(function() {
209 // Create a real project
210 console.log('creating proj2');
211 console.log(project, appId, appName, configNPM);
212 return create(project, appId, appName, configNPM);
213 })
214 .then(checkProject)
215 .fail(function(err) {
216 console.log(err && err.stack);
217 expect(err).toBeUndefined();
218 })
219 .fin(done);
220 }, 10000);
221
222 it('should successfully run with template not having a package.json at toplevel', function(done) {
223 // Call cordova create with no args, should return help.
224 var config = {
225 lib: {
226 www: {
227 template: true,
228 url: path.join(__dirname, 'templates', 'nopackage_json'),
229 version: ''
230 }
231 }
232 };
233 Q()
234 .then(function() {
235 // Create a real project
236 return create(project, appId, appName, config);
237 })
238 .then(checkProject)
239 .then(function(){
240 // Check that we got the right config.xml
241 var configXml = new ConfigParser(path.join(project, 'config.xml'));
242 expect(configXml.description()).toEqual('this is the very correct config.xml');
243 })
244 .fail(function(err) {
245 console.log(err && err.stack);
246 expect(err).toBeUndefined();
247 })
248 .fin(done);
249 });
250
251 it('should successfully run with template having package.json and no sub directory', function(done) {
252 // Call cordova create with no args, should return help.
253 var config = {
254 lib: {
255 www: {
256 template: true,
257 url: path.join(__dirname, 'templates', 'withpackage_json'),
258 version: ''
259 }
260 }
261 };
262 Q()
263 .then(function() {
264 // Create a real project
265 return create(project, appId, appName, config);
266 })
267 .then(checkProject)
268 .fail(function(err) {
269 console.log(err && err.stack);
270 expect(err).toBeUndefined();
271 })
272 .fin(done);
273 });
274
275 it('should successfully run with template having package.json, and subdirectory, and no package.json in subdirectory', function(done) {
276 // Call cordova create with no args, should return help.
277 var config = {
278 lib: {
279 www: {
280 template: true,
281 url: path.join(__dirname, 'templates', 'withsubdirectory'),
282 version: ''
283 }
284 }
285 };
286 Q()
287 .then(function() {
288 // Create a real project
289 return create(project, appId, appName, config);
290 })
291 .then(checkProject)
292 .fail(function(err) {
293 console.log(err && err.stack);
294 expect(err).toBeUndefined();
295 })
296 .fin(done);
297 });
298
299
300 it('should successfully run with template having package.json, and subdirectory, and package.json in subdirectory', function(done) {
301 // Call cordova create with no args, should return help.
302 var config = configSubDirPkgJson;
303 Q()
304 .then(function() {
305 // Create a real project
306 project = project + '1';
307 return create(project, appId, appName, config);
308 })
309 .then(checkSubDir)
310 .fail(function(err) {
311 console.log(err && err.stack);
312 expect(err).toBeUndefined();
313 })
314 .fin(done);
315 });
316
317 it('should successfully run config.xml in the www folder and move it outside', function(done) {
318 // Call cordova create with no args, should return help.
319 var config = configConfigInWww;
320 Q()
321 .then(function() {
322 // Create a real project
323 project = project + '2';
324 return create(project, appId, appName, config);
325 })
326 .then(checkConfigXml)
327 .fail(function(err) {
328 console.log(err && err.stack);
329 expect(err).toBeUndefined();
330 })
331 .fin(done);
332 });
333
334 it('should successfully run with www folder as the template', function(done) {
335 var config = {
336 lib: {
337 www: {
338 template: true,
339 url: path.join(__dirname, 'templates', 'config_in_www', 'www'),
340 version: ''
341 }
342 }
343 };
344 Q()
345 .then(function() {
346 project = project + '3';
347 return create(project, appId, appName, config);
348 })
349 .then(checkConfigXml)
350 .fail(function(err) {
351 console.log(err && err.stack);
352 expect(err).toBeUndefined();
353 })
354 .fin(done);
355 });
356
357 });