forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphantom-harness.js
137 lines (117 loc) · 3.3 KB
/
phantom-harness.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
var slice = Array.prototype.slice;
var argv = slice.call(require("system").args);
// Hard to believe PhantomJS has no equivalent of Node's "path" module.
var fs = require("fs");
var splat = [fs.workingDirectory, argv[0]]
.join(fs.separator)
.split(fs.separator);
var harness = splat.pop();
if (harness !== "phantom-harness.js") {
console.error("wrong harness: " + harness);
phantom.exit(-1);
}
var cwd = splat.join(fs.separator);
fs.changeWorkingDirectory(cwd);
// Hard to believe PhantomJS has no option parsing module.
var port = 8080;
var debug = false;
var tests = [];
var rest = [];
while (argv.length > 0) {
var arg = argv.pop();
if (arg === "--port") {
port = +rest.pop();
} else if (arg === "--debug") {
debug = true;
} else if (arg === "--tests") {
while (rest.length > 0)
tests.push(rest.pop());
}
rest.push(arg);
}
// Dynamically enable the individual tests.
var indexHtml = fs.read("index.html").replace(
/^(\s*)ENABLE_TESTS_HERE/m,
function(placeholder, leadingSpace) {
return leadingSpace + tests.map(function(testID) {
return "harness.enableTest(" + JSON.stringify(testID) + ");";
}).join("\n" + leadingSpace);
}
);
var server = require("webserver").create();
server.listen(port, function(req, res) {
var file = req.url.replace(/^\/+/, "");
var content;
switch (file) {
case "jasmine.js":
case "react.js":
case "react-test.js":
file = "../build/" + file;
break;
case "phantomjs-shims.js":
case "worker.js":
file = "../src/test/" + file;
break;
case "jasmine.css":
file = "../vendor/jasmine/" + file;
break;
case "":
default:
file = "index.html";
content = indexHtml; // Prevents calling fs.read again.
break;
}
if (/\.css$/i.test(file)) {
res.setHeader("Content-Type", "text/css");
} else if (/\.js/i.test(file)) {
res.setHeader("Content-Type", "text/javascript");
} else {
res.setHeader("Content-Type", "text/html");
}
res.statusCode = 200;
res.write(content || fs.read(file));
res.close();
});
var url = "http://localhost:" + port;
var green = "\033[32m";
var cyan = "\033[36m";
var reset = "\033[0m";
if (debug) {
console.log(green);
console.log("PhantomJS received the " + cyan + "--debug" + green + " option.");
console.log("Load " + cyan + url + green + " in your browser to execute " +
"the test suite.");
console.log("Type " + cyan + "control-C" + green + " to terminate the " +
"PhantomJS process.");
console.log(reset);
// Leave PhantomJS running until killed with control-C...
} else {
var page = require("webpage").create();
var timeoutSecs = 60;
page.onCallback = function(data) {
switch (data.type) {
case "console":
console[data.method].apply(console, data.args);
break;
case "exit":
// PhantomJS crashes sometimes unless we call phantom.exit in its own
// event loop tick.
setTimeout(function() {
phantom.exit(data.code);
}, 10);
break;
}
};
page.open(url, function(status) {
if (status !== "success") {
console.error("failed to open " + url);
phantom.exit(-1);
}
setTimeout(function() {
console.error(
"PhantomJS tests timed out after " +
timeoutSecs + " seconds.");
phantom.exit(-1);
}, timeoutSecs * 1e3);
});
}