This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
forked from hsch/node-goog
-
Notifications
You must be signed in to change notification settings - Fork 16
/
runClosureLibraryJSUnitsManual.js
executable file
·174 lines (151 loc) · 5.32 KB
/
runClosureLibraryJSUnitsManual.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
#!/usr/bin/env node
/**
* @private
* @type {nclosure.core}
* @const
*/
var ng_ = require('nclosure').nclosure();
goog.require('goog.testing.jsunit');
goog.require('nclosure.tests.utils');
var fs_ = require('fs');
var allTestFiles;
var tmpdir = ng_.getPath(process.cwd(), 'tests/tmpclosuretests/');
var runningTmpFileCount = 0;
var results = [];
var maxTests = -1;
var maxParallels = 8;
var testsToRun;
var start = Date.now();
function setUpPage() {
// TODO: This assumes the nctest command is running in
// the nclosure directory.
allTestFiles = nclosure.tests.utils.readDirRecursiveSync
('third_party/closure-library/closure/goog/', '_test[\d\w_]*\.(html|js)');
if (maxTests > 0 && maxTests < allTestFiles.length)
allTestFiles = allTestFiles.slice(0, maxTests);
// This line allows the tests to be limited for testing purposes
// allTestFiles = allTestFiles.slice(10, 15);
testsToRun = allTestFiles.length;
asyncTestCase.stepTimeout = testsToRun * 500;
};
function tearDownPage() { nclosure.tests.rmRfDir(tmpdir); };
function testClousreTests() {
assertTrue('Could not find test files', allTestFiles.length > 0);
asyncTestCase.waitForAsync();
copyAndParseAllTestFiles(function(err) {
if (err) {
console.error(err.stack);
asyncTestCase.continueTesting();
return;
}
var allFilesInTmp = fs_.readdirSync(tmpdir);
assertEquals('Expected:\n\t' + allTestFiles.join('\n\t') +
'\nGot:\n\t' + allFilesInTmp.join('\n\t'),
testsToRun, allFilesInTmp.length);
var commands = goog.array.map(allFilesInTmp,
function(f) { return 'nctest ' + ng_.getPath(tmpdir, f); });
nclosure.tests.utils.paralleliseExecs(
commands,runTestCallback_, onCompleted_, maxParallels);
});
};
function copyAndParseAllTestFiles(oncomplete) {
var impl = function(err) {
if (err) return oncomplete(err);
copyAndParseAllTestFilesImpl(oncomplete)
};
if (path_.existsSync(tmpdir)) {
nclosure.tests.utils.rmRfDir(tmpdir, function(err) {
if (err) return oncomplete(err);
fs_.mkdir(tmpdir, 0777, impl);
});
} else {
fs_.mkdir(tmpdir, 0777, impl);
}
}
function copyAndParseAllTestFilesImpl(oncomplete) {
var max = allTestFiles.length;
var remaining = max;
for (var i = 0; i < max; i++) {
copyAndParseFile(allTestFiles[i], function(err) {
if (err) return oncomplete(err);
if (--remaining === 0) { oncomplete(); }
});
};
};
function copyAndParseFile(file, oncomplete) {
fs_.readFile(file, 'utf-8', function(err, contents) {
if (err) return oncomplete(err);
var fileName = file.substring(file.lastIndexOf('/') + 1);
var isHtml = fileName.indexOf('.js') < 0;
var toFile = (++runningTmpFileCount) + '_' +
(isHtml ? fileName + '.js' : fileName);
toFile = ng_.getPath(tmpdir, toFile);
if (isHtml) contents = convertToJS_(contents);
fs_.writeFile(toFile, contents, 'utf-8', function(err) {
oncomplete(err);
});
});
};
function convertToJS_(html) {
var blocks = [];
var idx = html.indexOf('<script');
while (idx >= 0) {
idx = html.indexOf('>', idx);
var endIdx = html.indexOf('</script>', idx);
blocks.push(html.substring(idx + 1, endIdx));
html = html.substring(endIdx + 9);
idx = html.indexOf('<script')
}
return blocks.join('\n');
};
function runTestCallback_(command, err, stdout, stderr) {
var name = command.substring(command.lastIndexOf('/') + 1, command.indexOf('.'));
var r = {success:false,file:name,name:name.substring(name.indexOf('_') + 1)};
if (err) r.exception = err;
if (stdout.indexOf(', 0 failed') < 0) {
if (stderr) { r.message = stderr; }
else {
var underlineIdx = stdout.indexOf('------');
r.message = stdout.substring(stdout.indexOf('\n', underlineIdx) + 1).
replace(/\[0\;3\dm/g, '').
replace(/\n/g, '\<br \/\>');
}
} else { r.success = true; }
if (stdout) {
var summaryLine = goog.array.find(stdout.split('\n'),
function(l) { return l.indexOf(' passed, ') >= 0; });
if (summaryLine) r.summary = summaryLine.substring(7);
};
results.push(r);
};
function onCompleted_() {
assertEquals('Did not run as many tests as expected.',
testsToRun, results.length);
var failures = 0, successes = 0;
var reportFile = [
'<table border="1"><tr>',
'<th>Test</th><th>Results</th><th>Summary</th><th>' +
'Details (Mouse Over for Code)</th>'
];
for (var i = 0, len = results.length; i < len; i++) {
var r = results[i];
if (!r.success) { failures++; }
else { successes++; }
reportFile.push('<tr><td>' + r.name + '</td>' +
'<td>' + (!r.success ? 'Fail' : 'Success') + '</td>' +
'<td> ' + r.summary + '</td>' +
'<td>' + (r.exception ? r.exception : (r.message || 'n/a') + '</td></tr>'));
};
var took = Date.now() - start;
var summary = 'Closure Library Tests Results ' +
'(Success: ' + successes + '/' + testsToRun +
') - Took: ' + took + 'ms.';
fs_.writeFileSync('tests/closure_lib_tsts.html',
'<html><body><h1>' + summary + '</h1>' +
reportFile.join('\n') +
'</table></body></html>', 'utf-8');
console.error(summary +
'\nFor full details see tests/closure_lib_tsts.html');
asyncTestCase.continueTesting();
};
var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();