diff --git a/__snapshots__/stats-spec.js.snap-shot b/__snapshots__/stats-spec.js.snap-shot new file mode 100644 index 0000000..a1c1198 --- /dev/null +++ b/__snapshots__/stats-spec.js.snap-shot @@ -0,0 +1,4 @@ +exports['formats stats message 1'] = "stats: foo 1.0.0 -> 2.0.0 success probability 50% 5 successes 5 failures" + +exports['formats stats message with singular failure 1'] = "stats: foo 1.0.0 -> 2.0.0 success probability 50% 1 success 1 failure" + diff --git a/package.json b/package.json index 2671569..be17554 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "chdir-promise": "0.4.0", "check-more-types": "2.24.0", "cli-color": "1.2.0", + "common-tags": "^1.4.0", "console.json": "0.2.1", "console.table": "0.8.0", "debug": "2.6.8", diff --git a/src/stats-spec.js b/src/stats-spec.js new file mode 100644 index 0000000..af7c54f --- /dev/null +++ b/src/stats-spec.js @@ -0,0 +1,30 @@ +// @ts-check +const snapShot = require('snap-shot') + +/* global describe, it */ +describe('stats', () => { + describe('formatStats', () => { + const {formatStats} = require('./stats') + it('formats stats message', () => { + const message = formatStats({}, { + name: 'foo', + from: '1.0.0', + to: '2.0.0', + success: 5, + failure: 5 + }) + snapShot(message) + }) + + it('formats stats message with singular failure', () => { + const message = formatStats({}, { + name: 'foo', + from: '1.0.0', + to: '2.0.0', + success: 1, + failure: 1 + }) + snapShot(message) + }) + }) +}) diff --git a/src/stats.js b/src/stats.js index 79ef516..ea453fe 100644 --- a/src/stats.js +++ b/src/stats.js @@ -3,6 +3,8 @@ var request = require('request') var Q = require('q') var colors = require('cli-color') var colorAvailable = process.stdout.isTTY +const pluralize = require('pluralize') +const {oneLine} = require('common-tags') var nextUpdateStatsUrl = require('../package.json')['next-update-stats'] || 'http://next-update.herokuapp.com' @@ -80,7 +82,7 @@ function colorProbability (probability, options) { return probabilityStr } -function printStats (options, stats) { +function formatStats (options, stats) { verify.object(stats, 'missing stats object') verify.unemptyString(stats.name, 'missing name') verify.unemptyString(stats.from, 'missing from version') @@ -90,14 +92,23 @@ function printStats (options, stats) { var total = stats.success + stats.failure var probability = (total > 0 ? stats.success / total : 0) var probabilityStr = colorProbability(probability, options) - console.log('stats:', stats.name, stats.from, '->', stats.to, - 'success probability', probabilityStr, - stats.success, 'success(es)', stats.failure, 'failure(s)') + return oneLine` + stats: ${stats.name} ${stats.from} -> ${stats.to} + success probability ${probabilityStr} + ${stats.success} ${pluralize('success', stats.success)} + ${stats.failure} ${pluralize('failure', stats.failure)} + ` +} + +function printStats (options, stats) { + const message = formatStats(options, stats) + console.log(message) } module.exports = { sendUpdateResult: sendUpdateResult, getSuccessStats: getSuccessStats, + formatStats: formatStats, printStats: printStats, colorProbability: colorProbability, url: nextUpdateStatsUrl