This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
testrunner.js
229 lines (197 loc) · 6.43 KB
/
testrunner.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
/*globals global require __dirname BT process CoreTest */
require('./buildtools'); // adds the BT namespace as a global
var fs = require('fs'),
path = require('path'),
util = require('util'),
assert = require('assert'),
vows = require('vows');
var datastore = require('./datastore');
var jsFiles = datastore.get('orderedJavaScriptFiles').map(function(file) {
return file.get('sourcePath');
});
// Don't include the app itself.
global.SC = global.SproutCore; // Already loaded by the buildtools
// global.SproutCore = SC;
// global.FAST_LAYOUT_FUNCTION = false;
global.YES = true;
global.NO = false;
// Load the code we want to test.
// console.log(jsFiles);
jsFiles.forEach(function(path) { require(path); });
// Simulate becoming "ready"
SC.didBecomeReady();
// Start the proxy server.
// var http = require('http'),
// PROXY_LISTEN = 4020,
// PROXY_HOST = '127.0.0.1', PROXY_PORT = 9000,
// PROXY_PREFIX_FROM = '/datasource/', PROXY_PREFIX_TO = '/';
//
// var server = http.createServer(function(request, response) {
// var body = '';
//
// request.addListener('data', function(chunk) {
// body += chunk;
// });
//
// request.addListener('end', function() {
// var proxyClient, proxyRequest,
// url = request.url;
//
// if (PROXY_PREFIX_FROM.length > 0 && url.indexOf(PROXY_PREFIX_FROM) < 0) {
// console.error("Don't know how to proxy: " + url);
// response.writeHead(404);
// response.end();
// return; // don't proxy this
// } else {
// url = url.replace(PROXY_PREFIX_FROM, PROXY_PREFIX_TO);
// }
//
// // console.log("PROXYING http://localhost:"+PROXY_LISTEN + request.url + " TO http://" + PROXY_HOST + ":" + PROXY_PORT + url);
// proxyClient = http.createClient(PROXY_PORT, PROXY_HOST);
//
// proxyClient.addListener('error', function(err) {
// console.error('ERROR: "' + err.message + '" for proxy request on ' + PROXY_HOST + ':' + PROXY_PORT);
// response.writeHead(404);
// response.end();
// });
//
// request.headers.host = PROXY_HOST;
// request.headers['content-length'] = body.length;
// request.headers['X-Forwarded-Host'] = request.headers.host + ':' + PROXY_LISTEN;
// if (PROXY_PORT != 80) request.headers.host += ':' + PROXY_PORT;
//
// proxyRequest = proxyClient.request(request.method, url, request.headers);
//
// if (body.length > 0) { proxyRequest.write(body); }
//
// proxyRequest.addListener('response', function(proxyResponse) {
// response.writeHead(proxyResponse.statusCode, proxyResponse.headers);
// proxyResponse.addListener('data', function(chunk) {
// response.write(chunk, 'binary');
// });
// proxyResponse.addListener('end', function() {
// response.end();
// });
// });
//
// proxyRequest.end();
// });
//
// }).listen(PROXY_LISTEN);
// console.log("PROXY: http://"+PROXY_HOST+":"+PROXY_LISTEN + PROXY_PREFIX_FROM + '*' + " \u2192 http://" + PROXY_HOST + ":" + PROXY_PORT + PROXY_PREFIX_TO + '*');
// Load and process our tests.
var tests = process.argv.slice(2),
suites = tests.length;
if (suites === 0) {
console.log("ERROR: Nothing to test. Please pass files to test, like this:\n\n node testrunner.js tests/path/to/test1.js tests/path/to/test2.js\n");
// server.close(); // nothing to test
}
require('./arraytests');
var testsuite;
var testsuites;
global.suite = function(name, options) {
// console.log('suite', name);
if (testsuite.name) {
testsuites.push(testsuite);
testsuite = { tests: [] };
}
testsuite.name = name;
testsuite.options = options || {};
};
global.test = function(name, testfunction) {
// console.log('test', name);
testsuite.tests.push({
name: name,
test: testfunction
});
};
global.notest = function(name, callback, nowait) {};
global.ok = function(value, message) {
assert.ok(value, message);
};
global.equals = function(actual, expected, message) {
assert.equal(actual, expected, message);
};
global.same = function(actual, expected, message) {
assert.ok(CoreTest.equiv(actual, expected), message);
};
global.expect = function(amt) {
testsuite.expect = amt;
};
global.should_throw = function(callback, expected, msg) {
var actual = false ;
try {
callback();
} catch(e) {
actual = (typeof expected === "string") ? e.message : e;
}
if (expected===false) {
ok(actual===false, CoreTest.fmt("%@ expected no exception, actual %@", msg, actual));
} else if (expected===Error || expected===null || expected===true) {
ok(!!actual, CoreTest.fmt("%@ expected exception, actual %@", msg, actual));
} else {
equals(actual, expected, msg);
}
};
function buildVowsSuite() {
var suite = vows.describe("QUnit");
testsuites.push(testsuite);
testsuites.forEach(function(ts) {
var tests = { topic: true };
ts.tests.forEach(function(test) {
tests[test.name] = function() {
if (ts.options.setup) ts.options.setup();
test.test();
if (ts.options.teardown) ts.options.teardown();
};
});
var batch = {};
batch[ts.name] = tests;
suite.addBatch(batch);
});
return suite;
}
function runVowsTest(aPath) {
try {
if (aPath.indexOf('qunit') !== -1) {
// Capture qunit test.
testsuite = { tests: [] };
testsuites = [];
require(aPath);
// console.log(testsuite);
buildVowsSuite().run(null, function(results) {
suites--;
// if (suites === 0) server.close(); // the process will exit now
});
} else {
// Run vows test directly.
require(aPath).run(null, function(results) {
suites--;
// if (suites === 0) server.close(); // the process will exit now
});
}
} catch (e) { suites--; console.log(e); }
}
function processPath(aPath) {
var stat = fs.statSync(aPath);
if (stat.isFile()) {
if (aPath.slice(-3) === '.js') runVowsTest(aPath);
} else {
fs.readdirSync(aPath).forEach(function(filename) {
var testpath = path.join(aPath, filename);
stat = fs.statSync(testpath);
if (stat.isFile() && testpath.slice(-3) === '.js') {
runVowsTest(testpath);
} else if (stat.isDirectory()) {
if (testpath.indexOf('test_suites') >= 0) return;
processPath(testpath);
} else {
// console.log("the file is something strange");
}
});
}
}
// console.log(tests);
tests.forEach(function(testpath) {
if (testpath) processPath(path.join(__dirname, testpath));
});