Skip to content
This repository has been archived by the owner on Mar 31, 2020. It is now read-only.

Commit

Permalink
Add prettyPrintSummary()
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenyOrekhov committed Mar 2, 2017
1 parent c84938e commit 557b38d
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"remark-preset-lint-recommended": "^2.0.0",
"stylelint": "^7.9.0",
"stylelint-config-standard": "^16.0.0",
"w3cjs": "^0.3.0"
"w3cjs": "^0.3.0",
"wordwrap": "^1.0.0"
},
"files": [
"bin/",
Expand Down
12 changes: 7 additions & 5 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
const fs = require("fs");

const Bluebird = require("bluebird");
const R = require("ramda");

const lints = require("./lints");
const report = require("./report");
const prettyPrintWarnings = require("./pretty-print-warnings");
const prettyPrintReport = require("./pretty-print-report");

Bluebird.promisifyAll(fs);

Expand All @@ -22,10 +23,11 @@ module.exports = function cli({
.then(JSON.parse)
.then((config) => lints(config, lintersDirectory))
.then(report)
.then(prettyPrintWarnings)
.tap(log)
.then(function setAndReturnExitCode(output) {
process.exitCode = Number(output.length > 0);
.tap(
R.pipe(prettyPrintReport, log)
)
.then(function setAndReturnExitCode({numbers}) {
process.exitCode = Number(numbers.totalWarnings > 0);

return process.exitCode;
})
Expand Down
18 changes: 18 additions & 0 deletions src/pretty-print-report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*jslint node, es6, maxlen: 80 */

"use strict";

const R = require("ramda");

const prettyPrintWarnings = require("./pretty-print-warnings");
const prettyPrintSummary = require("./pretty-print-summary");

module.exports = function prettyPrintReport(report) {
return R.pipe(
R.reject(R.isEmpty),
R.join("\n")
)([
prettyPrintWarnings(report),
prettyPrintSummary(report)
]);
};
21 changes: 21 additions & 0 deletions src/pretty-print-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*jslint node, es6, maxlen: 80 */

"use strict";

const wordwrap = require("wordwrap");

const maxLineLength = 80;

const wrapTo80 = wordwrap(maxLineLength);

module.exports = function prettyPrintSummary({numbers, usedLinters}) {
return usedLinters.length > 0
? wrapTo80(`Used linters: ${usedLinters.join(", ")}
Total files checked: ${numbers.totalFiles}
Files with warnings: ${numbers.filesWithWarnings}
Total warnings: ${numbers.totalWarnings}
`)
: "Nothing to check\n";
};
32 changes: 31 additions & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ test("cli()", function (t) {
`test/stubs/bad.js (JSLint)
line 0 column 11
Expected ';' and instead saw '(end)'. (expected_a_b)
Used linters: JSLint
Total files checked: 1
Files with warnings: 1
Total warnings: 1
`,
"should log warnings"
"should log warnings and summary"
);
}

Expand Down Expand Up @@ -54,3 +61,26 @@ test("cli()", function (t) {
process.exitCode = 0;
});
});

test("cli()", function (t) {
t.plan(3);

function log(result) {
return t.strictSame(
result,
"Nothing to check\n",
"should log summary"
);
}

return cli({
rcFile: "test/stubs/.lints-nothing-to-check.json",
log,
lintersDirectory: "./linters/"
})
// eslint-disable-next-line promise/always-return
.then(function (exitCode) {
t.strictSame(exitCode, 0);
t.strictSame(process.exitCode, 0);
});
});
82 changes: 82 additions & 0 deletions test/pretty-print-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*jslint node, es6, maxlen: 80 */
/*eslint "func-names": "off" */

"use strict";

const {test} = require("tap");

const prettyPrintSummary = require("../src/pretty-print-summary");

test("prettyPrintSummary()", function (t) {
const result = prettyPrintSummary({
numbers: {
totalFiles: 15,
filesWithWarnings: 4,
totalWarnings: 8
},
usedLinters: ["linterA", "linterB"]
});

t.strictSame(
result,
`Used linters: linterA, linterB
Total files checked: 15
Files with warnings: 4
Total warnings: 8
`,
"should pretty print summary"
);

t.end();
});

test("prettyPrintSummary()", function (t) {
const resultWithoutFiles = prettyPrintSummary({usedLinters: []});

t.strictSame(
resultWithoutFiles,
"Nothing to check\n",
"should pretty print if there were no used linters"
);

t.end();
});

test("prettyPrintSummary()", function (t) {
const result = prettyPrintSummary({
numbers: {
totalFiles: 15,
filesWithWarnings: 4,
totalWarnings: 8
},
usedLinters: [
"linterNameA",
"linterNameB",
"linterNameC",
"longLinterNameD",
"linterNameE",
"linterNameF",
"linterNameG",
"linterNameH",
"linterNameI",
"linterNameJ"
]
});

t.strictSame(
result,
`Used linters: linterNameA, linterNameB, linterNameC, longLinterNameD,
linterNameE, linterNameF, linterNameG, linterNameH, linterNameI, linterNameJ
Total files checked: 15
Files with warnings: 4
Total warnings: 8
`,
"should wrap lines to 80 characters"
);

t.end();
});
7 changes: 7 additions & 0 deletions test/stubs/.lints-nothing-to-check.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"jslint": {
"files": [
"nonexistent"
]
}
}

0 comments on commit 557b38d

Please sign in to comment.