b9f4cd66b9a54928209bee8386b06e274e9e5bd2
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview TAP reporter
3  * @author Jonathan Kingston
4  */
5 "use strict";
6
7 var yaml = require("js-yaml");
8
9 //------------------------------------------------------------------------------
10 // Helper Functions
11 //------------------------------------------------------------------------------
12
13 /**
14  * Returns a canonical error level string based upon the error message passed in.
15  * @param {object} message Individual error message provided by eslint
16  * @returns {String} Error level string
17  */
18 function getMessageType(message) {
19     if (message.fatal || message.severity === 2) {
20         return "error";
21     } else {
22         return "warning";
23     }
24 }
25
26 /**
27  * Takes in a JavaScript object and outputs a TAP diagnostics string
28  * @param {object} diagnostic JavaScript object to be embedded as YAML into output.
29  * @returns {string} diagnostics string with YAML embedded - TAP version 13 compliant
30  */
31 function outputDiagnostics(diagnostic) {
32     var prefix = "  ";
33     var output = prefix + "---\n";
34     output += prefix + yaml.safeDump(diagnostic).split("\n").join("\n" + prefix);
35     output += "...\n";
36     return output;
37 }
38
39 //------------------------------------------------------------------------------
40 // Public Interface
41 //------------------------------------------------------------------------------
42
43 module.exports = function(results) {
44     var output = "TAP version 13\n1.." + results.length + "\n";
45
46     results.forEach(function(result, id) {
47         var messages = result.messages;
48         var testResult = "ok";
49         var diagnostics = {};
50
51         if (messages.length > 0) {
52             testResult = "not ok";
53
54             messages.forEach(function(message) {
55                 var diagnostic = {
56                     message: message.message,
57                     severity: getMessageType(message),
58                     data: {
59                         line: message.line || 0,
60                         column: message.column || 0,
61                         ruleId: message.ruleId || ""
62                     }
63                 };
64
65                 // If we have multiple messages place them under a messages key
66                 // The first error will be logged as message key
67                 // This is to adhere to TAP 13 loosely defined specification of having a message key
68                 if ("message" in diagnostics) {
69                     if ("messages" in diagnostics) {
70                         diagnostics.messages.push(diagnostic);
71                     } else {
72                         diagnostics.messages = [diagnostic];
73                     }
74                 } else {
75                     diagnostics = diagnostic;
76                 }
77             });
78         }
79
80         output += testResult + " " + (id + 1) + " - " + result.filePath + "\n";
81
82         // If we have an error include diagnostics
83         if (messages.length > 0) {
84             output += outputDiagnostics(diagnostics);
85         }
86
87     });
88
89     return output;
90 };