1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var Q = require('q');
|
17 |
|
18 | var JsonConsole = require("../reports/json-console.js");
|
19 | var JsonReport = require("../reports/json-report.js");
|
20 | var ConsoleReport = require("../reports/console-report.js");
|
21 | var JsonLogReport = require("../reports/json-log-report.js");
|
22 | var ConsoleLogger = require("../logging/console-logger.js");
|
23 |
|
24 | var attester = require("../attester");
|
25 | var config = attester.config;
|
26 |
|
27 | var writeReports = require("../reports/write-reports.js");
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var globalReports = [];
|
39 |
|
40 | var consoleLogger;
|
41 |
|
42 | exports.__init__ = function () {
|
43 | attester.event.once("attester.config.available", createGlobals);
|
44 | attester.event.on("attester.result", receiveResult);
|
45 | attester.event.on("attester.campaign.created", createCampaignReports);
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | exports.writeReports = function (campaign, callback) {
|
55 | writeReports({
|
56 | test: campaign.config["test-reports"],
|
57 | coverage: campaign.config["coverage-reports"]
|
58 | }, {
|
59 | test: campaign.jsonReport,
|
60 | coverage: campaign.getCoverageResult()
|
61 | }, function (error) {
|
62 | if (error) {
|
63 | attester.event.emit("reports.error", "An error occurred while writing reports: " + error);
|
64 | return;
|
65 | }
|
66 | var ignoreErrors = attester.config['ignore-errors'];
|
67 | var ignoreFailures = attester.config['ignore-failures'];
|
68 | var stats = campaign.jsonReport.stats;
|
69 | var success = (ignoreErrors || stats.errors === 0) && (ignoreFailures || stats.failures === 0);
|
70 |
|
71 | logSummary(campaign.logger, stats);
|
72 | logErrorsSummary(campaign.logger, campaign.jsonReport.tasksErrors);
|
73 |
|
74 | attester.event.emit("reports.stats", stats);
|
75 |
|
76 | callback(success);
|
77 | });
|
78 | };
|
79 |
|
80 | function logSummary(logger, stats) {
|
81 | var msg = 'Tests run: ' + stats.testCases + ', ';
|
82 | var msgFailures = stats.failures ? ('Failures: ' + stats.failures + ', ').red.bold : 'Failures: 0, '.green;
|
83 | var msgErrors = stats.errors ? ('Errors: ' + stats.errors + ', ').red.bold : 'Errors: 0, '.green;
|
84 | var msgSkipped = stats.tasksIgnored ? ('Skipped: ' + stats.tasksIgnored).yellow.bold : 'Skipped: 0'.green;
|
85 | logger.logInfo(msg + msgFailures + msgErrors + msgSkipped);
|
86 | }
|
87 |
|
88 | function logErrorsSummary(logger, tasksErrors) {
|
89 | logger.logInfo("------------------------");
|
90 | var browsersWithErrors = Object.keys(tasksErrors);
|
91 | if (browsersWithErrors.length === 0) {
|
92 | printAsciiBanner(logger, "green");
|
93 | logger.logInfo("Everything okay!");
|
94 | logger.logInfo("------------------------");
|
95 | return;
|
96 | }
|
97 |
|
98 | printAsciiBanner(logger, "red", "bold");
|
99 | logger.logInfo("Summary of errors and failures:".yellow);
|
100 | browsersWithErrors.forEach(function (browserName) {
|
101 | var errors = tasksErrors[browserName];
|
102 | errors.sort(taskComparator);
|
103 |
|
104 | logger.logInfo((browserName + " (" + errors.length + "):").yellow);
|
105 | errors.forEach(function (item) {
|
106 | logger.logError(item.taskName + ": " + item.method);
|
107 | logger.logInfo(" " + item.errorMessage.white);
|
108 | });
|
109 | });
|
110 | logger.logInfo("------------------------");
|
111 | }
|
112 |
|
113 | function printAsciiBanner(logger, color, style) {
|
114 | color = color || "green";
|
115 | style = style || color;
|
116 | logger.logInfo(" _____ " [style][color]);
|
117 | logger.logInfo(" / ____| " [style][color]);
|
118 | logger.logInfo(" | (___ _ _ _ __ ___ _ __ ___ __ _ _ __ _ _ " [style][color]);
|
119 | logger.logInfo(" \\___ \\ | | | | | '_ ` _ \\ | '_ ` _ \\ / _` | | '__| | | | |" [style][color]);
|
120 | logger.logInfo(" ____) | | |_| | | | | | | | | | | | | | | (_| | | | | |_| |" [style][color]);
|
121 | logger.logInfo(" |_____/ \\__,_| |_| |_| |_| |_| |_| |_| \\__,_| |_| \\__, |" [style][color]);
|
122 | logger.logInfo(" __/ |" [style][color]);
|
123 | logger.logInfo(" |___/ " [style][color]);
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 | function createGlobals() {
|
129 | if (config["json-console"]) {
|
130 | var jsonConsole = new JsonConsole(process.stdout, config.heartbeats);
|
131 | process.stdout.on("error", function () {
|
132 |
|
133 | attester.event.emit("reports.error", "process.stdout raised an error in JsonConsole report");
|
134 | });
|
135 | process.stdout = process.stderr;
|
136 | console.log = console.warn;
|
137 | globalReports.push(jsonConsole);
|
138 | }
|
139 |
|
140 | attester.logger.logLevels["."] = config["log-level"];
|
141 | if (!consoleLogger) {
|
142 | consoleLogger = new ConsoleLogger(process.stdout);
|
143 | consoleLogger.attach(attester.logger);
|
144 | }
|
145 | }
|
146 |
|
147 | function createCampaignReports(campaign) {
|
148 | var jsonReport = new JsonReport();
|
149 | campaign.on("result", jsonReport.addResult.bind(jsonReport));
|
150 | campaign.jsonReport = jsonReport;
|
151 |
|
152 | var consoleReport = new ConsoleReport(campaign.logger, campaign, config["slow-test-threshold"]);
|
153 | campaign.on("result", consoleReport.addResult.bind(consoleReport));
|
154 |
|
155 | var logReports = campaign.config["test-reports"]["json-log-file"];
|
156 | logReports.forEach(function (logReport) {
|
157 | var report = new JsonLogReport(logReport);
|
158 | campaign.on("result", report.addResult.bind(report));
|
159 | });
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | function receiveResult(event) {
|
167 | globalReports.forEach(function (report) {
|
168 | report.addResult(event);
|
169 | });
|
170 | }
|
171 |
|
172 | function taskComparator(el1, el2) {
|
173 | if (el1.taskName > el2.taskName) {
|
174 | return 1;
|
175 | } else if (el1.taskName < el2.taskName) {
|
176 | return -1;
|
177 | } else {
|
178 | return 0;
|
179 | }
|
180 | }
|
181 |
|
182 | exports.__reset__ = function () {
|
183 | attester.event.off("attester.config.available", createGlobals);
|
184 | attester.event.off("attester.result", receiveResult);
|
185 | attester.event.off("attester.campaign.created", createCampaignReports);
|
186 | globalReports = [];
|
187 |
|
188 | attester.logger.logDebug("__reset__ reports done");
|
189 | return Q();
|
190 | };
|