Skip to content

Commit

Permalink
Merge pull request #17 from relekang/feat/printing-totals
Browse files Browse the repository at this point in the history
feat: Add totals to the text output
  • Loading branch information
relekang committed May 1, 2016
2 parents e18ae17 + 9afc571 commit ffe5674
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
17 changes: 16 additions & 1 deletion src/formatters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,24 @@ export function preFormatter(data) {
}))
}

export function generateStats(data) {
return {
errors: {
total: _.filter(data, { severity: 'error' }).length,
in: _.filter(data, { severity: 'error', isInDiff: true }).length,
out: _.filter(data, { severity: 'error', isInDiff: false }).length,
},
warnings: {
total: _.filter(data, { severity: 'warning' }).length,
in: _.filter(data, { severity: 'warning', isInDiff: true }).length,
out: _.filter(data, { severity: 'warning', isInDiff: false }).length,
},
}
}

export function formatOutput(format, data) {
if (!formatters.hasOwnProperty(format)) {
throw new Error(`Formatter with name '${format}' does not exist.`)
}
return formatters[format](preFormatter(data))
return formatters[format](preFormatter(data), generateStats(data))
}
11 changes: 10 additions & 1 deletion src/formatters/text.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import _ from 'lodash'
import chalk from 'chalk'

export default function text(result) {
function resultText(result) {
return _.map(result, file => {
const messages = _.map(file.messages, message =>
` ✖ ${chalk.gray(`${message.line}:${message.column}`)} ${message.message}\n`
).join('')
return `${chalk.underline(`File: ${file.filename}`)}\n${messages}`
}).join('')
}

function statsText(stats) {
return `${stats.errors.in} of ${stats.errors.total} errors and ` +
`${stats.warnings.in} of ${stats.warnings.total} warnings`
}

export default function text(result, stats) {
return `${resultText(result)}\n${statsText(stats)}`
}
18 changes: 17 additions & 1 deletion test/formatters/index_tests.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import test from 'ava'

import { preFormatter } from '../../src/formatters'
import { generateStats, preFormatter } from '../../src/formatters'

const input = [
{
line: '5',
column: '23',
severity: 'warning',
message: 'Extra semicolon. (semi)',
source: 'eslint.rules.semi',
file: '~/dev/lint-filter/src/checks.js',
isInDiff: false,
},
{
line: '7',
column: '23',
Expand Down Expand Up @@ -41,3 +50,10 @@ const output = [
test('preFormatter(data) should return formatted output', t => {
t.deepEqual(preFormatter(input), output)
})

test('generateStats(data) should return stats for data', t => {
const stats = generateStats(input)

t.deepEqual(stats.errors, { in: 1, out: 1, total: 2 })
t.deepEqual(stats.warnings, { in: 0, out: 1, total: 1 })
})
20 changes: 16 additions & 4 deletions test/formatters/text_tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import test from 'ava'
import chalk from 'chalk'

import formatter from '../../src/formatters/text'

chalk.enabled = true

const input = [
{
filename: '~/dev/lint-filter/src/index.js',
Expand All @@ -16,9 +19,18 @@ const input = [
],
},
]
const output = '\u001b[4mFile: ~/dev/lint-filter/src/index.js\u001b[24m\n' +
' ✖ \u001b[90m7:23\u001b[39m Extra semicolon. (semi)\n'

test.skip('text formatter should return checkstyle formatted output', t => {
t.is(formatter(input), output)
const stats = {
errors: { in: 1, out: 9, total: 10 },
warnings: { in: 3, out: 9, total: 11 },
}

const output = (
'\u001b[4mFile: ~/dev/lint-filter/src/index.js\u001b[24m\n' +
' ✖ \u001b[90m7:23\u001b[39m Extra semicolon. (semi)\n' +
'\n1 of 10 errors and 3 of 11 warnings'
)

test('text formatter should return formatted output', t => {
t.is(formatter(input, stats), output)
})

0 comments on commit ffe5674

Please sign in to comment.