-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
150 lines (123 loc) · 4.01 KB
/
index.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
var util = require('util');
var spawn = require('child_process').spawn;
var path = require('path');
var http = require('http');
var config_osx = {
host: "localhost",
port: 1337
};
var OSXReporter = function(helper, logger, config) {
var log = logger.create('reporter.osx'),
lastResult;
extend(config_osx, config.osxReporter);
// Start local server that will send messages to Notification Center
var center = spawn(require.resolve('node-osx-notifier'), [config_osx.port, config_osx.host]);
log.info("OSX Notification Center reporter started at http://%s:%s", config_osx.host, config_osx.port);
center.on('exit', function(code) {
log.info('node-osx-notifier exited with code ' + code);
});
this.adapters = [];
this.onBrowserComplete = function(browser) {
report(browser.lastResult, browser);
};
this.onRunComplete = function(browsers, results) {
if (browsers.length <= 1 || results.disconnected) { return; }
report(results);
};
function showNotification(result) {
var show = false;
// If this is the first run `lastResult` will be undefined and we show
// the notification to confirm that the process is running.
if (lastResult === undefined) {
lastResult = result;
return true;
}
switch (config_osx.notificationMode) {
case 'always':
show = true;
break;
case 'change':
show = result !== lastResult;
break;
case 'failChange':
if (result === 'fail' || (result === 'pass' && lastResult === 'fail')) {
show = true;
}
break;
case 'failOnly':
show = result === 'fail';
break;
default: show = true;
}
lastResult = result;
return show;
}
function report(results, browser) {
var str_request, title, message, uri;
var time = helper.formatTimeInterval(results.totalTime);
if (results.disconnected || results.error) {
str_request = 'fail';
title = util.format('ERROR - %s', browser.name);
message = 'Test error';
}
else if (results.failed) {
str_request = 'fail';
if (browser) {
title = util.format('FAILED - %s', browser.name);
message = util.format('%d/%d tests failed in %s.', results.failed, results.total, time);
} else {
title = util.format('TOTAL FAILED: %s', results.failed);
message = util.format('%d/%d tests failed', results.failed, results.success + results.failed);
}
} else {
str_request = 'pass';
if (browser) {
title = util.format('PASSED - %s', browser.name);
message = util.format('%d tests passed in %s.', results.success, time);
} else {
title = util.format('TOTAL PASSED: %s', results.success);
message = util.format('%d tests passed.', results.success);
}
}
if (showNotification(str_request)) {
uri = '/' + str_request + "?title=" + encodeURIComponent(title) + "&message=" + encodeURIComponent(message);
Object.keys(config_osx).forEach(function(key) {
if (key !== 'host' && key !== 'port') {
var value = typeof config_osx[key] === 'function' ? config_osx[key](results, browser) : config_osx[key];
uri += '&' + key + '=' + encodeURIComponent(value);
}
});
}
var options = {
host: config_osx.host,
port: config_osx.port,
path: uri,
method: 'GET'
};
var req = http.request(options, null);
req.on('error', function(err) {
log.error('error: ' + err.message);
});
req.end();
}
};
if (process.platform !== 'darwin') {
// overwrite reporter with void function for non-OSX
OSXReporter = function(helper, logger) {};
}
OSXReporter.$inject = ['helper', 'logger', 'config'];
// PUBLISH DI MODULE
module.exports = {
'reporter:osx': ['type', OSXReporter]
};
function extend(obj) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
}
return obj;
}