-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparamedic.js
305 lines (275 loc) · 11.1 KB
/
paramedic.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
#!/usr/bin/env node
var http = require('http'),
localtunnel = require('localtunnel'),
parseArgs = require('minimist'),
shell = require('shelljs'),
fs = require('fs'),
request = require('request'),
tmp = require('tmp'),
path = require('path');
var PORT = 8008;
var TIMEOUT = 10 * 60 * 1000; // 10 minutes in msec - this will become a param
function ParamedicRunner(_platformId,_plugins,_callback,bJustBuild,nPort,msTimeout,browserify,bSilent,bVerbose,platformPath) {
this.tunneledUrl = "";
this.port = nPort;
this.justBuild = bJustBuild;
this.plugins = _plugins;
this.platformId = _platformId;
this.callback = _callback;
this.tempFolder = null;
this.timeout = msTimeout;
this.verbose = bVerbose;
this.platformPath = platformPath;
if(browserify) {
this.browserify = "--browserify";
} else {
this.browserify = '';
}
if(bSilent) {
var logOutput = this.logOutput = [];
this.logMessage = function(msg) {
logOutput.push(msg);
};
}
else {
this.logMessage = function(msg) {
console.log(msg);
};
}
}
ParamedicRunner.prototype = {
run: function() {
var cordovaResult = shell.exec('cordova --version', {silent:!this.verbose});
if(cordovaResult.code) {
this.logMessage(cordovaResult.output);
// this would be fatal
process.exit(cordovaResult.code);
}
// limit runtime to TIMEOUT msecs
var self = this;
setTimeout(function(){
self.logMessage("This test seems to be blocked :: timeout exceeded. Exiting ...");
self.cleanUpAndExitWithCode(1);
},self.timeout);
this.createTempProject();
this.installPlugins();
this.startServer();
},
createTempProject: function() {
this.tempFolder = tmp.dirSync();
tmp.setGracefulCleanup();
this.logMessage("cordova-paramedic: creating temp project at " + this.tempFolder.name);
shell.exec('cordova create ' + this.tempFolder.name,{silent:!this.verbose});
shell.cd(this.tempFolder.name);
},
installSinglePlugin: function(plugin) {
this.logMessage("cordova-paramedic: installing " + plugin);
var pluginPath = path.resolve(this.storedCWD, plugin);
var plugAddCmd = shell.exec('cordova plugin add ' + pluginPath, {silent:!this.verbose});
if(plugAddCmd.code !== 0) {
this.logMessage('Failed to install plugin : ' + plugin);
this.cleanUpAndExitWithCode(1);
}
},
installPlugins: function() {
for(var n = 0; n < this.plugins.length; n++) {
var plugin = this.plugins[n];
this.installSinglePlugin(plugin);
if(!this.justBuild) {
this.installSinglePlugin(path.join(plugin,"tests"));
}
}
if(!this.justBuild) {
this.logMessage("cordova-paramedic: installing plugin-test-framework");
var plugAddCmd = shell.exec('cordova plugin add https://github.com/apache/cordova-plugin-test-framework',
{silent:!this.verbose});
if(plugAddCmd.code !== 0) {
this.logMessage('cordova-plugin-test-framework');
this.cleanUpAndExitWithCode(1);
}
}
},
cleanUpAndExitWithCode: function(exitCode,resultsObj) {
shell.cd(this.storedCWD);
// the TMP_FOLDER.removeCallback() call is throwing an exception, so we explicitly delete it here
shell.exec('rm -rf ' + this.tempFolder.name);
var logStr = this.logOutput ? this.logOutput.join("\n") : null;
this.callback(exitCode,resultsObj,logStr);
},
writeMedicLogUrl: function(url) {
this.logMessage("cordova-paramedic: writing medic log url to project");
var obj = {logurl:url};
fs.writeFileSync(path.join("www","medic.json"),JSON.stringify(obj));
},
setConfigStartPage: function() {
this.logMessage("cordova-paramedic: setting app start page to test page");
var fileName = 'config.xml';
var configStr = fs.readFileSync(fileName).toString();
if(configStr) {
configStr = configStr.replace("src=\"index.html\"","src=\"cdvtests/index.html\"");
fs.writeFileSync(fileName, configStr);
}
else {
this.logMessage("Oops, could not find config.xml");
}
},
startServer: function() {
if(this.justBuild) {
this.addAndRunPlatform();
return;
}
/// else ....
this.logMessage("cordova-paramedic: starting local medic server " + this.platformId);
var self = this;
var server = http.createServer(this.requestListener.bind(this));
server.listen(this.port, '127.0.0.1',function onServerConnect() {
switch(self.platformId) {
case "android" :
self.writeMedicLogUrl("http://10.0.2.2:" + self.port);
self.addAndRunPlatform();
break;
case "wp8" :
//localtunnel(PORT, tunnelCallback);
request.get('http://google.com/', function(e, res, data) {
if(e) {
self.logMessage("failed to detect ip address");
self.cleanUpAndExitWithCode(1);
}
else {
var ip = res.req.connection.localAddress ||
res.req.socket.localAddress;
self.logMessage("Using ip : " + ip);
self.writeMedicLogUrl("http://" + ip + ":" + self.port);
self.addAndRunPlatform();
}
});
break;
case "ios" : // intentional fallthrough
case "browser" :
case "windows" :
default :
self.writeMedicLogUrl("http://127.0.0.1:" + self.port);
self.addAndRunPlatform();
break;
}
});
},
requestListener: function(request, response) {
var self = this;
if (request.method == 'PUT' || request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
// Too much POST data, kill the connection!
if (body.length > 1e6) {
req.connection.destroy();
}
});
request.on('end', function (res) {
if(body.indexOf("mobilespec") == 2){ // {\"mobilespec\":{...}}
try {
//logMessage("body = " + body);
var results = JSON.parse(body);
self.logMessage("Results: ran " +
results.mobilespec.specs +
" specs with " +
results.mobilespec.failures +
" failures");
if(results.mobilespec.failures > 0) {
self.cleanUpAndExitWithCode(1,results);
}
else {
self.cleanUpAndExitWithCode(0,results);
}
}
catch(err) {
self.logMessage("parse error :: " + err);
self.cleanUpAndExitWithCode(1);
}
}
else {
self.logMessage("console-log:" + body);
}
});
}
else {
self.logMessage(request.method);
response.writeHead(200, { 'Content-Type': 'text/plain'});
response.write("Hello"); // sanity check to make sure server is running
response.end();
}
},
addAndRunPlatform: function() {
var self = this;
var plat = this.platformPath || this.platformId;
this.logMessage("cordova-paramedic: adding platform : " + plat);
shell.exec('cordova platform add ' + plat,{silent:!this.verbose});
shell.exec('cordova prepare '+ this.browserify,{silent:!this.verbose});
if(this.justBuild) {
this.logMessage("building ...");
shell.exec('cordova build ' + this.platformId.split("@")[0],
{async:true,silent:!this.verbose},
function(code,output){
if(code !== 0) {
self.logMessage("Error: cordova build returned error code " + code);
self.logMessage("output: " + output);
self.cleanUpAndExitWithCode(1);
}
else {
self.cleanUpAndExitWithCode(0);
}
}
);
}
else {
this.setConfigStartPage();
shell.exec('cordova emulate ' + this.platformId.split("@")[0] + " --phone",
{async:true,silent:!this.verbose},
function(code,output){
if(code !== 0) {
self.logMessage("Error: cordova emulate return error code " + code);
self.logMessage("output: " + output);
self.cleanUpAndExitWithCode(1);
}
}
);
}
},
tunnelCallback: function(err, tunnel) {
if (err){
this.logMessage("failed to create tunnel url, check your internet connectivity.");
this.cleanUpAndExitWithCode(1);
}
else {
// the assigned public url for your tunnel
// i.e. https://abcdefgjhij.localtunnel.me
this.tunneledUrl = tunnel.url;
this.logMessage("cordova-paramedic: tunneledURL = " + tunneledUrl);
this.writeMedicLogUrl(tunneledUrl);
this.addAndRunPlatform();
}
}
};
var storedCWD = null;
exports.run = function(_platformId,_plugins,_callback,bJustBuild,nPort,msTimeout,bBrowserify,bSilent,bVerbose,platformPath) {
storedCWD = storedCWD || process.cwd();
if(!_plugins) {
_plugins = process.cwd();
}
if(_platformId && _plugins) {
// make it an array if it's not
var plugins = Array.isArray(_plugins) ? _plugins : [_plugins];
// if we are passed a callback, we will use it,
// otherwise just make a quick and dirty one
var callback = ( _callback && _callback.apply ) ? _callback : function(resCode,resObj) {
process.exit(resCode);
};
var runner = new ParamedicRunner(_platformId, plugins, callback, !!bJustBuild,
nPort || PORT, msTimeout || TIMEOUT, !!bBrowserify, !!bSilent, !!bVerbose, platformPath);
runner.storedCWD = storedCWD;
return runner.run();
}
else {
console.error("Error : Missing platformId and/or plugins");
}
};