-
Notifications
You must be signed in to change notification settings - Fork 20
/
update-test.js
175 lines (136 loc) · 4.71 KB
/
update-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
var path = require('path'),
fs = require('fs-extra'),
download = require('download-github-repo');
var repo = 'microformats/tests', // glennjones/tests or microformats/tests
tempDir = path.resolve(__dirname,'temp-tests'),
testDir = path.resolve(__dirname,'test'),
testJSPath = path.resolve(__dirname,'static/javascript/data.js');
download(repo, tempDir, function(err, data){
clearDirectory(function(err){
if(err){
console.err(err);
}else{
var fileList = getFileList(path.resolve(tempDir,'tests')),
testStructure = getGetTestStructure( fileList ),
version = getTestSuiteVersion(),
dataCollection = [];
// loop array of test found
testStructure.forEach(function(item){
getDataFromFiles( item, function(err, data){
if(data){
// build mocha tests
var test = buildTest( data, item, version, repo ),
filePath = shortenFilePath( item[0] + '-' + item[1] + '-' + item[2].replace('.json','') + '.js' );
writeFile(path.resolve(testDir,filePath), test);
console.log(path.resolve(testDir,filePath));
// add to data collection
data.name = shortenFilePath( item[0] + '-' + item[1] + '-' + item[2].replace('.json',''));
dataCollection.push( data );
}else{
console.log(err);
}
});
});
// 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
function getFileList (dir, files_){
files_ = files_ || [];
var files = fs.readdirSync(dir);
for (var i in files){
var name = dir + '/' + files[i];
if (fs.statSync(name).isDirectory()){
getFileList(name, files_);
} else {
files_.push(name);
}
}
return files_;
}
// 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
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 '';
}
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);
}
}
function buildTest( 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 += "var chai = require('chai'),\r\n assert = chai.assert,\r\n helper = require('../test/helper.js');\r\n\r\n\r\n";
out += "describe('" + testStructure[1] + "', function() {\r\n";
out += " var htmlFragment = " + JSON.stringify(testData.html) + ";\r\n";
out += " var found = helper.parseHTML(htmlFragment,'http://example.com/');\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 += " assert.deepEqual(found, expected);\r\n";
out += " });\r\n";
out += "});\r\n";
return out;
}
//
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 current test directory
function clearDirectory( callback ){
var fileList = getFileList (testDir);
fileList.forEach(function(filePath){
if(filePath.indexOf('/mf-') > -1){
fs.removeSync(filePath);
}
});
callback(null);
}
// write a file
function writeFile(path, content){
fs.writeFile(path, content, 'utf8', function(err) {
if(err) {
console.log(err);
} else {
console.log('The file: ' + path + ' was saved');
}
});
}