dcceda4bcb6feed4e22e0cfa1525dddc418a8d46
[aai/esr-gui.git] /
1 var fs = require('fs');
2
3 /* Note: because this plugin uses process.on('uncaughtException'), only one
4  * of these can exist at any given time. This plugin and anything else that
5  * uses process.on('uncaughtException') will conflict. */
6 exports.attachToRunner = function(runner, outputFile) {
7   var smokeOutput = { results : [] };
8   var runningTests = {};
9
10   var integraPlugin = {
11     beforeTest: function(test, callback) {
12       test.startTime = Date.now();
13       runningTests[test.name] = test;
14       callback();
15     },
16     afterTest: function(test, callback) {
17       smokeOutput.results.push({
18         status: test.status,
19         start: test.startTime,
20         end: Date.now(),
21         test_file: test.name,
22         exit_code: 0,
23         url: ""
24       });
25       delete runningTests[test.name];
26       callback();
27     },
28     beforeExit: function(obj, callback) {
29       fs.writeFile(outputFile, JSON.stringify(smokeOutput), function() {
30         callback();
31       });
32     }
33   };
34
35   // In case of exception, make sure we write file
36   process.on('uncaughtException', function(err) {
37     // Mark all currently running tests as failed
38     for (var testName in runningTests) {
39       smokeOutput.results.push({
40         status: "fail",
41         start: runningTests[testName].startTime,
42         end: Date.now(),
43         test_file: testName,
44         exit_code: 0,
45         url: ""
46       });
47     }
48
49     // write file
50     fs.writeFileSync(outputFile, JSON.stringify(smokeOutput));
51
52     // Standard NodeJS uncaught exception handler
53     console.error(err.stack);
54     process.exit(1);
55   });
56
57   runner.plugin(integraPlugin);
58   return integraPlugin;
59 };