-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestexamples
executable file
·163 lines (123 loc) · 3.96 KB
/
testexamples
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
/*
This will iterate through each one of the examples running the unit tests on them
*/
var fs=require('fs');
var cp=require('child_process');
var path=require('path');
var optimist=require('optimist');
var colors=require('colors');
var common=require('hopjs-common');
common.requireOnDemand(['hopjs-remote'],function(){
optimist=optimist.describe("url","url to use for testing");
optimist=optimist.describe("log","log file");
optimist=optimist.describe("hopjs","path to hopjs command to use");
var args = optimist.argv;
var url = args.url||"http://localhost:3000/";
var results={};
var testExample=function(example,onComplete){
var dir = path.join("./examples",example);
console.log("Testing example",example);
cp.exec("(cd "+dir+" && rm -rf node_modules && npm install .)",function(err,stdout,stderr){
if(err) return onComplete("Error installing package:"+err);
var exited=false;
var testNumber=0;
var appRunning=true;
console.log("Starting application");
var app = cp.spawn("node",["./app.js"],{ cwd: dir });
app.on("exit",function(code,data){
console.log("Example application exited with:",code);
appRunning=false;
});
var appOut="";
app.on("close",function(code,data){
//console.log("A",arguments);
});
app.stderr.on("data",function(data){
appOut+=data;
});
app.stdout.on("data",function(data){
appOut+=data;
});
setTimeout(function(){
if(!appRunning){
console.error(appOut);
return onComplete("Application quit before being tested");
}
var hopjsPath = args.hopjs||path.join(__dirname,"node_modules","hopjs-remote","bin","hopjs");
var unitTest = cp.exec(hopjsPath+" --url "+url+" --unitTest --testDetails");
var unitTestErr="";
var unitTestOut="";
unitTest.on("close",function(code,data){
if(args.log){
fs.appendFileSync(args.log,"\n"+example+": ########################################################\n");
fs.appendFileSync(args.log,"\n"+example+": APP OUT ---------------------------------------------\n");
fs.appendFileSync(args.log,appOut);
fs.appendFileSync(args.log,"\n"+example+": TEST STDOUT ---------------------------------------------\n");
fs.appendFileSync(args.log,unitTestOut);
fs.appendFileSync(args.log,"\n"+example+": TEST STDERR ---------------------------------------------\n");
fs.appendFileSync(args.log,unitTestErr);
}
app.kill();
console.log("Unit test exited with",code);
results[example]={ exitCode:code }
var m = /^Pass.*$/gm.exec(unitTestOut);
if(m){
results[example].results=m[0];
}
app.kill("SIGKILL");
cp.exec("kill -9 "+app.pid);
var waitForAppExit=function(){
if(appRunning){
setTimeout(waitForAppExit,200);
} else {
return onComplete();
}
}
waitForAppExit();
});
unitTest.stderr.on("data",function(data){
console.log(data.toString());
});
unitTest.stdout.on("data",function(data){
console.log(data);
unitTestOut+=data;
var m = /^#([0-9]+)/gm.exec(data);
if(m){
testNumber=m[1];
}
});
},3000);
});
}
fs.readdir("./examples",function(err,examples){
var run=function(){
if(examples.length>0){
var example = examples.shift();
testExample(example,function(err,res){
if(err){
console.error(err,example);
process.exit(-1);
}
run();
})
} else {
console.log("--------------------------");
var str="";
for(var i in results){
console.log(i+":");
str+="#"+i+":\n";
if(results[i].results){
console.log("\t",results[i].results);
str+="\t"+results[i].results.toString()+"\n";
} else {
console.log("\t exited with",results[i].exitCode)
str+=("\t exited with "+results[i].exitCode+"\n");
}
}
fs.writeFileSync("testresults.md",str);
process.exit();
}
}
run();
});
});