This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
612 lines (533 loc) · 18.6 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
var EventEmitter = require("events").EventEmitter;
// simple child process mocking
var oldspawn = require('child_process').spawn;
var spawned;
var spawnerror = false;
require('child_process').spawn = function (name, args) {
spawned = {name: name, args: args};
var emitter = new EventEmitter();
emitter.stdout = {pipe: function () {}};
emitter.stderr = {pipe: function () {}};
process.nextTick(function () {
if (spawnerror) emitter.emit('end', 127);
else emitter.emit('exit', 0);
});
return emitter;
};
var assert = require("assert");
var tsm;
if (process.env.TSM_COV) tsm = require("./lib-cov/index");
else tsm = require('./index');
var path = require('path');
var EventEmitter = require('events').EventEmitter;
var nock = require('nock');
var fs = require('fs');
if (!fs.exists) fs.exists = path.exists;
var ncp = require('ncp').ncp;
var rimraf = require('rimraf');
function assertInPath (expected, actual) {
// fix slashes in path for windows
if (process.platform === 'win32')
expected = expected.split('/').join('\\');
assert(actual.indexOf(expected) != -1, "correct path");
}
suite('gitCheck', function () {
var hash0 = "e721f1820f65368794aee29e2da8ebc03de804fd";
var hash1 = "e2a219dceb3026e73be3afab4591aa3ba627d58b";
test('matches properly', function () {
assert(tsm.gitCheck('e721', hash0) === true);
assert(tsm.gitCheck('e2', hash0) === false);
assert(tsm.gitCheck('e2', hash1) === true);
});
test('handles invalid input', function () {
assert(tsm.gitCheck(false, hash0) === false);
assert(tsm.gitCheck('1', hash0) === false);
assert(tsm.gitCheck({}, hash0) === false);
});
});
suite('getBranches', function () {
test('functional', function (done) {
var scope = nock('http://builds.appcelerator.com.s3.amazonaws.com')
.get('/mobile/branches.json')
.replyWithFile(200, __dirname + "/fixtures/branches.json");
tsm.getBranches(function (error, data) {
try {
assert(error === null);
assert(Array.isArray(data));
assert(data.indexOf('master') != -1, "master is in branch list");
assert(data.indexOf('2_0_X') != -1, "2_0_X is in branch list");
scope.done();
done();
} catch (e) { done(e); }
});
});
test('handles 404', function (done) {
var scope = nock('http://builds.appcelerator.com.s3.amazonaws.com')
.get('/mobile/branches.json')
.reply(404);
tsm.getBranches(function (error, data) {
try {
assert(error instanceof Error, "got error");
scope.done();
done();
} catch (e) { done(e); }
});
});
});
suite('parseDate', function () {
var date0 = "20120827132447";
test('parses properly', function () {
var date = tsm.parseDate(date0);
// Months are zero indexed so we're really testing if the month is August
assert(date.getMonth() === 7, "check month");
assert(date.getDate() === 27, "check day of month");
assert(date.getHours() === 13, "check hour");
});
});
suite('parseBuildList', function () {
var list = [
{
"sha1": "2a60ec1e0b693047e6fa9112fe93944c5432a3c3",
"build_url": "http://jenkins.appcelerator.org/job/titanium_mobile_master/1277/",
"git_revision": "c63b0d947da94e2cdfb3fb06e95106cb803c3f22",
"filename": "mobilesdk-2.1.0.v20120827132447-win32.zip",
"git_branch": "master",
"build_type": "mobile",
"size": 26961576
},
{
"sha1": "7be939ed222691536b8b32ad0782e4729a9d447c",
"build_url": "http://jenkins.appcelerator.org/job/titanium_mobile_master/1278/",
"git_revision": "79e9c73d5070fc4306d37bc1cf8cbabb2ad67ae8",
"filename": "mobilesdk-2.2.0.v20120827143312-osx.zip",
"git_branch": "master",
"build_type": "mobile",
"size": 80186079
},
{
"sha1": "a0c6a6cbf334752cf9e2f23be37ee11e63af0a1b",
"build_url": "http://jenkins.appcelerator.org/job/titanium_mobile_master/1278/",
"git_revision": "79e9c73d5070fc4306d37bc1cf8cbabb2ad67ae8",
"filename": "mobilesdk-2.2.0.v20120827143312-linux.zip",
"git_branch": "master",
"build_type": "mobile",
"size": 26960385
},
{
"sha1": "92ffcd16454b38d8eca203a4b36352ec140504ec",
"build_url": "http://jenkins.appcelerator.org/job/titanium_mobile_master/1278/",
"git_revision": "79e9c73d5070fc4306d37bc1cf8cbabb2ad67ae8",
"filename": "mobilesdk-2.2.0.v20120827143312-win32.zip",
"git_branch": "master",
"build_type": "mobile",
"size": 26961706
}
];
test('parses out correct os', function () {
var ret = tsm.parseBuildList(null, 'osx', list);
ret.forEach(function (build) {
assert(build.filename.indexOf('win32') === -1, 'no win32 buids');
assert(build.filename.indexOf('linux') === -1, 'no linux buids');
});
});
test('matches git hashes properly', function () {
var ret = tsm.parseBuildList('79e9', 'linux', list);
assert(ret.length === 1, "only one result");
assert(
ret[0].sha1 === "a0c6a6cbf334752cf9e2f23be37ee11e63af0a1b",
"correct build matched"
);
});
test('matches versions properly', function () {
var ret = tsm.parseBuildList('2.1', 'win32', list);
assert(ret.length === 1, "only one result");
assert(
ret[0].sha1 === "2a60ec1e0b693047e6fa9112fe93944c5432a3c3",
"correct build matched"
);
});
test('handles malformed filename', function () {
var malformed = [
{
"sha1": "a0c6a6cbf334752cf9e2f23be37ee11e63af0a1b",
"build_url": "http://jenkins.appcelerator.org/job/titanium_mobile_master/1278/",
"git_revision": "79e9c73d5070fc4306d37bc1cf8cbabb2ad67ae8",
"filename": "mobilessdf92jfjlllssssssssssfoooooo312-linux.zip",
"git_branch": "master",
"build_type": "mobile",
"size": 26960385
}
];
// just want to make sure it doesn't throw
var ret = tsm.parseBuildList(undefined, 'linux', malformed);
});
});
suite('getBuilds', function () {
test('functional: retrieval of branch master', function (done) {
var scope = nock('http://builds.appcelerator.com.s3.amazonaws.com')
.get('/mobile/master/index.json')
.replyWithFile(200, __dirname + '/fixtures/master-index.json');
tsm.getBuilds('master', function (error, data) {
try {
assert(error === null, "no error");
assert(Array.isArray(data), "return is an array");
assert(data.length > 4, "more than 4 items");
data.forEach(function (item) {
assert(item.sha1);
assert(item.filename);
assert(typeof item.size === "number");
});
done();
} catch (e) { done(e); }
});
});
test('handles 404', function (done) {
var scope = nock('http://builds.appcelerator.com.s3.amazonaws.com')
.get('/mobile/master/index.json')
.reply(404);
tsm.getBuilds('master', function (error, data) {
try {
assert(error instanceof Error, "expected error");
scope.done();
done();
} catch (e) { done(e); }
});
});
test('handles malformed json', function (done) {
var scope = nock('http://builds.appcelerator.com.s3.amazonaws.com')
.get('/mobile/master/index.json')
.reply(200, "}}}[[[[");
tsm.getBuilds('master', function (error, data) {
try {
assert(error instanceof SyntaxError, "expected error");
scope.done();
done();
} catch (e) { done(e); }
});
});
});
suite('getAllBuilds', function () {
test('functional', function (done) {
var scope = nock('http://builds.appcelerator.com.s3.amazonaws.com')
.get('/mobile/branches.json')
.replyWithFile(200, __dirname + "/fixtures/branches.json");
var branches = ["master", "1_4_X", "1_5_X", "1_6_X", "1_7_X", "1_8_X", "2_0_X", "2_1_X"];
branches.forEach(function (b) {
scope
.get('/mobile/'+b+'/index.json')
.replyWithFile(200, __dirname + '/fixtures/' + b + '-index.json');
});
tsm.getAllBuilds('2.1.x', 'linux', function (error, builds) {
try {
if (error) throw error;
assert(Array.isArray(builds), "return is an array");
assert(builds.length > 4, "more than 4 items");
for (var i = 0; i < builds.length; i++) {
var b = builds[i];
assert(b.sha1, "build has a sha1");
assert(b.date.getDay, "build has a date object");
assert(b.zip, "build has a zip url");
}
scope.done();
done();
} catch (e) { done(e); }
});
});
});
suite('parseVersionFile', function () {
var file = "version=2.1.0\n"+
"module_apiversion=2\n"+
"timestamp=05/02/12 14:18\n" +
"githash=cde5b27";
var malformedFile = "======asdf== ==\n\n\nsfoij3===!!!!!!!";
test('parses', function () {
var res = tsm.parseVersionFile(file);
assert(res.version === '2.1.0');
assert(res.timestamp === '05/02/12 14:18');
assert(res.githash === 'cde5b27');
});
test("doesn't die with invalid input", function () {
var res = tsm.parseVersionFile(malformedFile);
});
});
suite('examineDir', function () {
test('handles a dir w/o version.txt', function (done) {
var dir = path.join(__dirname, 'fixtures', '1', '.DS_Store');
tsm.examineDir(dir, function (error, data) {
if (error instanceof Error) done();
else done(new Error("expected error"));
});
});
test('handles dir with malformed version.txt', function (done) {
var dir = path.join(__dirname, 'fixtures', '1', 'malformed');
tsm.examineDir(dir, function (error, data) {
if (error instanceof SyntaxError) done();
else done(new Error("expected error"));
});
});
test('handles a valid dir', function (done) {
var dir = path.join(__dirname, 'fixtures', '1', '2.1.0');
tsm.examineDir(dir, function (error, data) {
try {
if (error) throw error;
assert(data.version === '2.1.0');
assert(data.timestamp === '05/02/12 14:18');
assert(data.githash === 'cde5b27');
done();
} catch (e) { done(e); }
});
});
});
suite('findInstalled', function () {
test('functional', function (done) {
var dir = path.join(__dirname, 'fixtures', '1');
var e = tsm.findInstalled(dir, undefined, function (error, builds) {
try {
assert(builds.length === 2, "2 results");
assert(builds[1].version === '2.1.0');
assert(builds[1].githash === 'cde5b27');
assert(builds[1].date.getDay, "has a valid date object");
done();
} catch (e) { done(e); }
});
assert(e instanceof EventEmitter);
});
});
suite('mergeBuilds', function () {
var installed = [
{ githash: '0a43607',
version: '2.1.1',
date: new Date("Tue, 17 Jul 2012 02:46:00 GMT") },
{ githash: 'c63b0d9',
version: '2.1.0',
date: new Date("Fri, 27 Jul 2012 18:01:00 GMT") },
{ githash: '61078b0',
version: '2.2.0',
date: new Date("Fri, 10 Aug 2012 23:41:00 GMT") }
];
var available = [
{
"sha1": "2a60ec1e0b693047e6fa9112fe93944c5432a3c3",
"build_url": "http://jenkins.appcelerator.org/job/titanium_mobile_master/1277/",
"git_revision": "c63b0d947da94e2cdfb3fb06e95106cb803c3f22",
"githash": "c63b0d9",
"filename": "mobilesdk-2.1.0.v20120827132447-osx.zip",
"git_branch": "master",
"build_type": "mobile",
"date": new Date(),
"size": 26961576
},
{
"sha1": "7be939ed222691536b8b32ad0782e4729a9d447c",
"build_url": "http://jenkins.appcelerator.org/job/titanium_mobile_master/1278/",
"git_revision": "79e9c73d5070fc4306d37bc1cf8cbabb2ad67ae8",
"githash": "79e9c73",
"filename": "mobilesdk-2.2.0.v20120827143312-osx.zip",
"git_branch": "master",
"build_type": "mobile",
"date": new Date(),
"size": 80186079
},
{
"sha1": "a0c6a6cbf334752cf9e2f23be37ee11e63af0a1b",
"build_url": "http://jenkins.appcelerator.org/job/titanium_mobile_master/1278/",
"git_revision": "39bc239d5070fc4306d37bc1cf8cbabb2ad67ae8",
"githash": "39bc239",
"filename": "mobilesdk-2.2.0.v20120827143312-osx.zip",
"git_branch": "master",
"build_type": "mobile",
"date": new Date(),
"size": 26960385
}
];
test('merges correctly', function () {
var merged = tsm.mergeBuilds(available, installed);
assert(merged.length === 5);
// pull out the one that should've been merged
var mergedEntry = merged.filter(function (item) {
return item.githash === 'c63b0d9';
});
assert(mergedEntry.length === 1);
assert(mergedEntry[0].installed === true, "merged entry marked as installed");
var availableEntry = merged.filter(function (item) {
return item.githash === '79e9c73';
});
assert(availableEntry.length === 1);
assert(availableEntry[0].installed === false);
// pull out the one that should've been merged
var installedEntry = merged.filter(function (item) {
return item.githash === '61078b0';
});
assert(installedEntry.length === 1);
assert(installedEntry[0].installed === true);
});
});
suite('list', function () {
test('handles missing / malformed os parameter', function () {
try {
tsm.list({available: true, os: 7}, function (error, data) {});
} catch (e) {
assert(e instanceof TypeError);
}
try {
tsm.list({available: true}, function (error, data) {});
} catch (e) {
assert(e instanceof TypeError);
}
});
test('functional: installed', function (done) {
tsm.list({
installed: true,
os: 'osx',
dir: __dirname + "/fixtures/1"
}, function (error, data) {
try {
assert(error === null);
assert(data.length === 2);
assert(data[1].githash === 'cde5b27');
assert(data[1].installed === true);
assert(data[0].installed === true);
done();
} catch (e) { done(e); }
});
});
test('functional: installed and available, input: 2', function (done) {
var scope = nock('http://builds.appcelerator.com.s3.amazonaws.com')
.get('/mobile/branches.json')
.replyWithFile(200, __dirname + "/fixtures/branches.json");
var branches = ["master", "1_4_X", "1_5_X", "1_6_X", "1_7_X", "1_8_X", "2_0_X", "2_1_X"];
branches.forEach(function (b) {
scope
.get('/mobile/'+b+'/index.json')
.replyWithFile(200, __dirname + '/fixtures/' + b + '-index.json');
});
tsm.list({
installed: true,
available: true,
os: 'osx',
dir: __dirname + "/fixtures/1",
input: '2'
}, function (error, data) {
try {
assert(error === null, "no error");
var installed = data.filter(function (item) {
return item.installed;
});
assert(installed.length > 0, "at least one marked as installed");
data.forEach(function (item) {
assert(item.installed !== undefined);
assert(item.githash);
assert(typeof item.date.getDay === 'function');
});
scope.done();
done();
} catch (e) { done(e); }
});
});
test('handles missing / malformed dir argument', function () {
try {
tsm.list({os: 'osx', installed: true}, function (error, data) {});
} catch (e) {
assert(e instanceof TypeError);
}
try {
tsm.list({os: 'osx', installed: true, dir: 7}, function (error, data) {});
} catch (e) {
assert(e instanceof TypeError);
}
});
});
suite('unzip', function () {
test('extracts', function (done) {
var zip = path.join(__dirname, "fixtures", "test.zip");
var output = path.join(__dirname, "fixtures", "2");
var p = path.join(__dirname, "fixtures", "2", "index.js");
tsm.unzip(zip, output, function (error) {
if (error) done(error);
fs.exists(p, function (exists) {
if (!exists) done(new Error("file was not extracted properly"));
fs.unlink(p, done);
});
});
});
});
suite('install', function () {
test('functional', function (done) {
var zip = __dirname + "/fixtures/test.zip";
var output = __dirname + "/fixtures/2/";
var zipurl = "/mobile/master/mobilesdk-2.2.0.v20120828153312-osx.zip";
var path = __dirname + "/1.8.2/version.txt";
var scope = nock('http://builds.appcelerator.com.s3.amazonaws.com')
.get('/mobile/branches.json')
.replyWithFile(200, __dirname + "/fixtures/branches-simple.json")
.get('/mobile/master/index.json')
.replyWithFile(200, __dirname + '/fixtures/master-index.json')
.get(zipurl)
.replyWithFile(200, __dirname + '/fixtures/sdk.zip');
var emitter = tsm.install({
dir: output,
input: '2',
os: 'osx'
}, function (error) {
try {
assert(error === null);
scope.done();
fs.exists(path, function (exists) {
if (!exists) done(new Error("file was not extracted properly"));
rimraf(__dirname + "/1.8.2", done);
});
} catch (e) { done(e); }
});
});
});
suite('delete', function () {
test('functional', function (done) {
var src = __dirname + "/fixtures/1/";
var sdkdir = __dirname + "/fixtures/2";
ncp(src, sdkdir, function (err) {
if (err) return done(err);
tsm.remove({dir: sdkdir, input: "1.8.x"}, function (error) {
if (error) return done(error);
fs.exists(sdkdir + "/1.8.2", function (exists) {
if (exists) return done(new Error("sdk wasn't deleted"));
fs.exists(sdkdir + "/2.1.0", function (exists) {
if (!exists) return done(new Error("wrong sdk deleted"));
rimraf(sdkdir, function () {
fs.mkdir(sdkdir, done);
});
});
});
});
});
});
});
suite('builder', function () {
test('functional: iphone', function (done) {
var sdkdir = __dirname + "/fixtures/1/";
var options = {dir: sdkdir, input: '1.8', os: 'iphone', args: ['foo', 'bar']};
tsm.builder(options, function (error) {
try {
assert(spawned.name === "python");
assertInPath("fixtures/1/1.8.2/iphone/builder.py", spawned.args[0]);
assert(spawned.args[1] === "foo");
assert(spawned.args[2] === "bar");
done();
} catch (e) { done(e); }
});
});
});
suite('titanium', function () {
test('functional', function (done) {
var sdkdir = __dirname + "/fixtures/1/";
var options = {dir: sdkdir, input: '1.8', args: ['foo', 'bar']};
tsm.titanium(options, function (error) {
try {
assert(spawned.name === "python");
assertInPath("fixtures/1/1.8.2/titanium.py", spawned.args[0]);
assert(spawned.args[1] === "foo");
assert(spawned.args[2] === "bar");
done();
} catch (e) { done(e); }
});
});
});