UNPKG

7.43 kBJavaScriptView Raw
1/*
2 * Copyright 2013 Amadeus s.a.s.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16var Q = require('q');
17
18var JsonConsole = require("../reports/json-console.js");
19var JsonReport = require("../reports/json-report.js");
20var ConsoleReport = require("../reports/console-report.js");
21var JsonLogReport = require("../reports/json-log-report.js");
22var ConsoleLogger = require("../logging/console-logger.js");
23
24var attester = require("../attester");
25var config = attester.config;
26
27var writeReports = require("../reports/write-reports.js");
28
29/**
30 * This module handles the reports creation and communication with attester.
31 * There are two types of reports
32 * - global, these are the one reporting any activity on attester
33 * - campaign, reports specific to a certain campaign
34 *
35 * The module listen to events raised by attester to initialize reports and listen to the results
36 */
37
38var globalReports = [];
39// The console logger is a global report that wants to be cleaned before exiting
40var consoleLogger;
41
42exports.__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 * Write the reports for the given campaign. This generates all the files specified in the campaign
50 * configuration so it can be asynchronous.
51 *
52 * The callback is called with a success boolean, true if there where no remarkable errors or failures
53 */
54exports.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
80function 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
88function 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
113function 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// Create all global reports, generic for attester
127
128function createGlobals() {
129 if (config["json-console"]) {
130 var jsonConsole = new JsonConsole(process.stdout, config.heartbeats);
131 process.stdout.on("error", function () {
132 // the process listening on this stream was closed
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
147function 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// Receive a result event either from attester itself or from one of the campaigns
164// This method dispatches events to the interested reports
165
166function receiveResult(event) {
167 globalReports.forEach(function (report) {
168 report.addResult(event);
169 });
170}
171
172function 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
182exports.__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};