-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathupdate-tests.js
482 lines (390 loc) · 15.3 KB
/
update-tests.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
/*!
unpdate-test.js
run $node unpdate-test.js
Downloads github repo of microfomats tests then:
* updates mocha tests
* updates data.js file for testrunner
Copyright (C) 2015 Glenn Jones. All Rights Reserved.
MIT License: https://raw.github.com/glennjones/microformat-shiv/master/license.txt
*/
var path = require('path'),
request = require('request'),
fs = require('fs-extra'),
download = require('download-github-repo');
var repo = 'microformats/tests', // glennjones/tests or microformats/tests
tempDir = path.resolve(__dirname,'temp-tests'),
standardsDir = 'standards-tests',
moduleDir = 'module-tests',
interfaceDir = 'interface-tests',
standardsDirResolve = path.resolve(__dirname,'test', standardsDir),
moduleDirResolve = path.resolve(__dirname,'test', moduleDir),
interfaceDirResolve = path.resolve(__dirname,'test', interfaceDir),
coverageTestPagePath = path.resolve(__dirname,'test/coverage.html'),
serverTestPagePath = path.resolve(__dirname,'test/ci.html'),
testJSPath = path.resolve(__dirname,'test/static/javascript/data.js'),
livingStandardPath = path.resolve(__dirname,'lib/living-standard.js');
// write living standard date
writeLivingStandard( repo, livingStandardPath )
download(repo, tempDir, function(err, data){
// remove current mocha js test files
clearDirectory(function(err){
if(err){
console.err(err);
}else{
var fileList = getFileList(path.resolve(tempDir,'tests')),
testStructure = getGetTestStructure( fileList ),
version = getTestSuiteVersion(),
standardTestPathArr = [],
dataCollection = [];
// loop array of test found
testStructure.forEach(function(item){
// get data for each test
getDataFromFiles( item, function(err, data){
if(data){
// build mocha tests
var test = buildMochaJSString( data, item, version, repo ),
filePath = shortenFilePath( item[0] + '-' + item[1] + '-' + item[2].replace('.json','') + '.js' );
standardTestPathArr.push( filePath );
writeFile(path.resolve(standardsDirResolve,filePath), test);
// add to data collection
data.name = shortenFilePath( item[0] + '-' + item[1] + '-' + item[2].replace('.json',''));
dataCollection.push( data );
// log each test as it written
console.log(path.resolve(standardsDirResolve,filePath));
}else{
console.log(err);
}
});
});
// build client and server test pages
writeFile(coverageTestPagePath, buildTestPage( standardTestPathArr, version, repo, false, true, ['standards','interface'], ''));
writeFile(serverTestPagePath, buildTestPage( standardTestPathArr, version, repo, false, false, ['standards','interface'], ''));
// build individual test pages
writeFile(interfaceDirResolve + '/index.html', buildTestPage( standardTestPathArr, version, repo, true, false, ['interface'], '../'));
writeFile(moduleDirResolve + '/index.html', buildTestPage( standardTestPathArr, version, repo, true, false, ['module'], '../'));
writeFile(standardsDirResolve + '/index.html', buildTestPage( standardTestPathArr, version, repo, true, false, ['standards'], '../'));
// build json data for testrunner
writeFile(testJSPath, 'var testData = ' + JSON.stringify({
date: new Date(),
repo: repo,
version: version,
data: dataCollection
}));
fs.removeSync(tempDir);
console.log('done');
}
});
});
/**
* get a list of file paths from a directory
*
* @param {String} dir
* @param {Array} dir
* @return {Array}
*/
function getFileList (dir, filesArr){
filesArr = filesArr || [];
var files = fs.readdirSync(dir);
for (var i in files){
var name = dir + '/' + files[i];
if (fs.statSync(name).isDirectory()){
getFileList(name, filesArr);
} else {
filesArr.push(name);
}
}
return filesArr;
}
// get format directories
function getGetTestStructure( fileList ){
var out = [];
fileList.forEach(function(item){
item = item.replace( path.resolve(tempDir,'tests') + '/', '' );
if(item.indexOf('.html') === -1){
if(item.indexOf('/') > -1){
var items = item.split('/');
out.push(items);
}
}
});
return out;
}
/**
* gets the test suite version number
*
* @return {String}
*/
function getTestSuiteVersion(){
var pack = fs.readFileSync(path.resolve(tempDir,'package.json'), {encoding: 'utf8'});
if(pack){
pack = JSON.parse(pack)
if(pack && pack.version){
return pack.version;
}
}
return '';
}
/**
* gets JSON and HTML for a individual test from files in repo
*
* @param {Array} testStructure
* @param {Function} callback
*/
function getDataFromFiles( testStructure, callback ){
var jsonFile = 'tests/' + testStructure[0] + '/' + testStructure[1] + '/' + testStructure[2],
htmlFile = jsonFile.replace('.json','.html'),
json = fs.readFileSync(path.resolve(tempDir,jsonFile), {encoding: 'utf8'}),
html = fs.readFileSync(path.resolve(tempDir,htmlFile), {encoding: 'utf8'});
if(json && html){
callback(null, { 'json':json, 'html': html});
}else{
callback('error loading files: ' + jsonFile, null);
}
}
/**
* creates javascript string for a mocha test
*
* @param {Object} testData
* @param {Array} testStructure
* @param {String} version
* @param {String} repo
* @return {String}
*/
function buildMochaJSString( testData, testStructure, version, repo ){
var out = '',
fileName = testStructure[0] + '/' + testStructure[1] + '/' + testStructure[2].replace('.json',''),
date = new Date().toString();
out += '/*\r\nMicroformats Test Suite - Downloaded from github repo: ' + repo + ' version v' + version + ' \r\n';
out += 'Mocha integration test from: ' + fileName + '\r\nThe test was built on ' + date + '\r\n*/\r\n\r\n';
out += "assert = chai.assert;\r\n\r\n\r\n";
out += "describe('" + testStructure[1] + "', function() {\r\n";
out += " var htmlFragment = " + JSON.stringify(testData.html) + ";\r\n";
out += " var expected = " + JSON.stringify(JSON.parse(testData.json)) + ";\r\n\r\n";
out += " it('" + testStructure[2].replace('.json','') + "', function(){\r\n";
out += " var doc, dom, node, options, parser, found;\r\n";
out += " dom = new DOMParser();\r\n";
out += " doc = dom.parseFromString( htmlFragment, 'text/html' );\r\n";
out += " options ={\r\n";
out += " 'document': doc,\r\n";
out += " 'node': doc,\r\n";
out += " 'baseUrl': 'http://example.com',\r\n";
out += " 'dateFormat': 'html5'\r\n";
out += " };\r\n";
out += " found = Microformats.get( options );\r\n";
out += " assert.deepEqual(found, expected);\r\n";
out += " });\r\n";
out += "});\r\n";
return out;
}
/**
* creates html string for test page
*
* @param {Object} testData
* @param {String} version
* @param {String} repo
* @param {Boolean} clientJS
* @param {Boolean} coverage
* @return {String}
*/
function buildTestPage( standardTestPathArr, version, repo, clientJS, coverage, testDirectories, dirPath ){
var date = new Date().toString(),
out = '';
out += '<html><head><title>Mocha</title>\r\n';
out += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\r\n';
if(clientJS === true && coverage === false){
out += '<link rel="stylesheet" href="' + dirPath + 'static/css/mocha.css" />\r\n';
out += '<script src="' + dirPath + 'static/javascript/chai.js"></script>\r\n';
out += '<script src="' + dirPath + 'static/javascript/mocha.js"></script>\r\n';
}else{
out += '<link rel="stylesheet" href="' + dirPath + '../node_modules/mocha/mocha.css" />\r\n';
out += '<script src="' + dirPath + '../node_modules/chai/chai.js"></script>\r\n';
out += '<script src="' + dirPath + '../node_modules/mocha/mocha.js"></script>\r\n';
// this file is need for testing early browser make sure it not in files used by firefox
out += '<script src="' + dirPath + '../thirdparty/es5-shim.min.js"></script>\r\n';
}
out += '<link rel="stylesheet" href="' + dirPath + 'static/css/mocha-custom.css" />\r\n\r\n';
out += '<script src="' + dirPath + 'static/javascript/DOMParser.js"></script>\r\n\r\n';
// load either full version of shiv or just the modules that make it
if(testDirectories.indexOf('module') > -1){
out += '<script data-cover src="' + dirPath + '../lib/utilities.js"></script>\r\n';
out += '<script data-cover src="' + dirPath + '../lib/domutils.js"></script>\r\n';
out += '<script data-cover src="' + dirPath + '../lib/url.js"></script>\r\n';
out += '<script data-cover src="' + dirPath + '../lib/html.js"></script>\r\n';
out += '<script data-cover src="' + dirPath + '../lib/text.js"></script>\r\n';
out += '<script data-cover src="' + dirPath + '../lib/dates.js"></script>\r\n';
out += '<script data-cover src="' + dirPath + '../lib/isodate.js"></script>\r\n\r\n';
}else{
out += '<script data-cover src="' + dirPath + '../microformat-shiv.js"></script>\r\n\r\n';
}
// this is need for firefox marionette tests runner
if(clientJS === true && coverage === false){
out += '<script>\r\n';
out += 'var uncaughtError;\r\n\r\n';
out += 'window.addEventListener("error", function(error) {\r\n';
out += 'uncaughtError = error;\r\n';
out += '});\r\n\r\n';
out += 'var consoleWarn = console.warn;\r\n';
out += 'var caughtWarnings = [];\r\n\r\n';
out += 'console.warn = function() {\r\n';
out += 'var args = Array.slice(arguments);\r\n';
out += 'caughtWarnings.push(args);\r\n';
out += 'consoleWarn.apply(console, args);\r\n';
out += '};\r\n';
out += '</script>\r\n\r\n';
}
out += '<script>\r\n';
out += 'chai.config.includeStack = true;\r\n';
out += 'mocha.setup({ui: \'bdd\', timeout: 10000});\r\n';
out += '</script>\r\n\r\n';
// add standards tests
if(testDirectories.indexOf('standards') > -1){
standardTestPathArr.forEach(function(item){
out += '<script src="' + dirPath + standardsDir + '/' + item + '"></script>\r\n';
});
}
// add interface tests
if(testDirectories.indexOf('interface') > -1){
out += '\r\n';
out += '<script src="' + dirPath + 'interface-tests/get-test.js"></script>\r\n';
out += '<script src="' + dirPath + 'interface-tests/getParent-test.js"></script>\r\n';
out += '<script src="' + dirPath + 'interface-tests/count-test.js"></script>\r\n';
out += '<script src="' + dirPath + 'interface-tests/isMicroformat-test.js"></script>\r\n';
out += '<script src="' + dirPath + 'interface-tests/hasMicroformats-test.js"></script>\r\n\r\n';
out += '<script src="' + dirPath + 'interface-tests/experimental-test.js"></script>\r\n\r\n';
}
// add module tests
if(testDirectories.indexOf('module') > -1){
out += '\r\n';
out += '<script src="' + dirPath + 'module-tests/dates-test.js"></script>\r\n';
out += '<script src="' + dirPath + 'module-tests/domUtils-test.js"></script>\r\n';
out += '<script src="' + dirPath + 'module-tests/html-test.js"></script>\r\n';
out += '<script src="' + dirPath + 'module-tests/isodate-test.js"></script>\r\n';
out += '<script src="' + dirPath + 'module-tests/text-test.js"></script>\r\n\r\n';
out += '<script src="' + dirPath + 'module-tests/url-test.js"></script>\r\n\r\n';
out += '<script src="' + dirPath + 'module-tests/utilities-test.js"></script>\r\n\r\n';
}
out += '</head><body>\r\n';
out += '<h3 class="capitalize">Microformats-shiv: ' + testDirectories.join(', ') + ' tests</h3>\r\n';
if(testDirectories.indexOf('standards') > -1){
out += '<p>Standards tests built on ' + date + '. Downloaded from github repo: ' + repo + ' version v' + version + '</p>\r\n';
}
out += '<div id="mocha"></div>\r\n';
out += '</body>\r\n';
if(clientJS === true){
// this is need for firefox marionette tests runner
out += '<script>\r\n';
out += 'describe("Uncaught Error Check", function() {\r\n';
out += 'it("should load the tests without errors", function() {\r\n';
out += 'chai.expect(uncaughtError && uncaughtError.message).to.be.undefined;\r\n';
out += '});\r\n';
out += '});\r\n\r\n';
out += 'describe("Unexpected Warnings Check", function() {\r\n';
out += 'it("should long only the warnings we expect", function() {\r\n';
out += 'chai.expect(caughtWarnings.length).to.eql(0);\r\n';
out += '});\r\n';
out += '});\r\n\r\n';
// mocha.run creates div for firefox marionette
out += 'mocha.run(function () {\r\n';
out += 'var completeNode = document.createElement("p");\r\n';
out += 'completeNode.setAttribute("id", "complete");\r\n';
out += 'completeNode.appendChild(document.createTextNode("Complete"));\r\n';
out += 'document.getElementById("mocha").appendChild(completeNode);\r\n';
out += '});\r\n\r\n';
out += '</script>\r\n';
}
if(coverage === true){
// mocha-blanket.js injects mocha.run
out += '\r\n<script src="' + dirPath + '../node_modules/poncho/node_modules/blanket/dist/qunit/blanket.min.js"> </script>\r\n';
out += '<script src="' + dirPath + '../node_modules/poncho/node_modules/blanket/src/adapters/mocha-blanket.js"></script>\r\n\r\n';
}else{
if(clientJS === false){
// standard mocha.run or mochaPhantomJS.run
out += '<script>window.onload= function(){\r\n';
out += 'if (window.mochaPhantomJS) {\r\n';
out += 'mochaPhantomJS.run();\r\n';
out += '}else{\r\n';
out += 'mocha.run();\r\n';
out += '}\r\n';
out += '};</script>\r\n';
}
}
out += '</body></html>\r\n';
return out;
}
/**
* shortens filename removing 'microformats-'
*
* @param {String} filepath
* @return {string}
*/
function shortenFilePath( filepath ){
return 'mf-' + filepath.replace('microformats-mixed','mixed').replace('microformats-v1','v1').replace('microformats-v2','v2');
}
/**
* delete all files with prefix mf- from 'standardsDirResolve' directory
*
* @param {Function} callback
*/
function clearDirectory( callback ){
var fileList = getFileList (standardsDirResolve);
fileList.forEach(function(filePath){
if(filePath.indexOf('/mf-') > -1){
fs.removeSync(filePath);
}
});
callback(null);
}
/**
* write a file
*
* @param {String} repo
* @param {String} content
*/
function writeFile(path, content){
fs.writeFile(path, content, 'utf8', function(err) {
if(err) {
console.log(err);
} else {
console.log('The file: ' + path + ' was saved');
}
});
}
/**
* get the last commit date from github repo
*
* @param {String} repo
* @param {Function} callback
*/
function getLastCommitDate( repo, callback ){
var options = {
url: 'https://api.github.com/repos/' + repo + '/commits?per_page=1',
headers: {
'User-Agent': 'request'
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
var date = null,
json = JSON.parse(body);
if(json && json.length && json[0].commit && json[0].commit.author ){
date = json[0].commit.author.date;
}
callback(null, date);
}else{
console.log(error, response, body);
callback('fail to get last commit date', null);
}
});
}
/**
* write the living standard .js file
*
* @param {String} repo
* @param {String} livingStandardPath
*/
function writeLivingStandard( repo, livingStandardPath ){
getLastCommitDate( repo, function( err, date ){
var out = ' modules.livingStandard = \'' + date + '\';';
writeFile(livingStandardPath, out);
});
}