1 | /**
|
2 | * @fileoverview TAP reporter
|
3 | * @author Jonathan Kingston
|
4 | */
|
5 | ;
|
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 | * The rules setup in the config will determine if an issue is an error.
|
16 | * Issues marked with message.fatal will be error level.
|
17 | * All other issues will be a warning.
|
18 | * @param {object} message Individual error message provided by eslint
|
19 | * @param {object} rules Rules setup in the config via: config.rules
|
20 | * @returns {String} Error level string
|
21 | */
|
22 | function getMessageType(message, rules) {
|
23 |
|
24 | // TODO: Get rule severity in a better way
|
25 | var severity = null;
|
26 |
|
27 | if (message.fatal) {
|
28 | return "error";
|
29 | }
|
30 |
|
31 | severity = rules[message.ruleId][0] || rules[message.ruleId];
|
32 |
|
33 | if (severity === 2) {
|
34 | return "error";
|
35 | }
|
36 |
|
37 | return "warning";
|
38 | }
|
39 |
|
40 | /**
|
41 | * Takes in a JavaScript object and outputs a TAP diagnostics string
|
42 | * @param {object} diagnostic JavaScript object to be embedded as YAML into output.
|
43 | * @returns {string} diagnostics string with YAML embedded - TAP version 13 compliant
|
44 | */
|
45 | function outputDiagnostics(diagnostic) {
|
46 | var prefix = " ";
|
47 | var output = prefix + "---\n";
|
48 | output += prefix + yaml.safeDump(diagnostic).split("\n").join("\n" + prefix);
|
49 | output += "...\n";
|
50 | return output;
|
51 | }
|
52 |
|
53 | //------------------------------------------------------------------------------
|
54 | // Public Interface
|
55 | //------------------------------------------------------------------------------
|
56 |
|
57 | module.exports = function(results, config) {
|
58 | var output = "TAP version 13\n1.." + results.length + "\n",
|
59 | rules = config.rules || {};
|
60 |
|
61 | results.forEach(function(result, id) {
|
62 | var messages = result.messages;
|
63 | var testResult = "ok";
|
64 | var diagnostics = {};
|
65 |
|
66 | if (messages.length > 0) {
|
67 | testResult = "not ok";
|
68 |
|
69 | messages.forEach(function(message) {
|
70 | var diagnostic = {
|
71 | message: message.message,
|
72 | severity: getMessageType(message, rules),
|
73 | data: {
|
74 | line: message.line || 0,
|
75 | column: message.column || 0,
|
76 | ruleId: message.ruleId || ""
|
77 | }
|
78 | };
|
79 |
|
80 | // If we have multiple messages place them under a messages key
|
81 | // The first error will be logged as message key
|
82 | // This is to adhere to TAP 13 loosely defined specification of having a message key
|
83 | if ("message" in diagnostics) {
|
84 | if ("messages" in diagnostics) {
|
85 | diagnostics.messages.push(diagnostic);
|
86 | } else {
|
87 | diagnostics.messages = [diagnostic];
|
88 | }
|
89 | } else {
|
90 | diagnostics = diagnostic;
|
91 | }
|
92 | });
|
93 | }
|
94 |
|
95 | output += testResult + " " + (id + 1) + " - " + result.filePath + "\n";
|
96 |
|
97 | // If we have an error include diagnostics
|
98 | if (messages.length > 0) {
|
99 | output += outputDiagnostics(diagnostics);
|
100 | }
|
101 |
|
102 | });
|
103 |
|
104 | return output;
|
105 | };
|