Skip to content

Commit

Permalink
Fix coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Aug 28, 2023
1 parent 065fc99 commit 60c43fc
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 17 deletions.
18 changes: 10 additions & 8 deletions lib/colors.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const { test } = require('tap')
const getColorizerPrivate = require('./colors')
const { colorizerFactory: getColorizerPublic } = require('../index')
const getColorizer = require('./colors')

const testDefaultColorizer = getColorizer => async t => {
const colorizer = getColorizer()
Expand Down Expand Up @@ -122,9 +121,12 @@ const testCustomColoringColorizer = getColorizer => async t => {
t.equal(colorized, '\u001B[37mUSERLVL\u001B[39m')
}

test('returns default colorizer - private export', testDefaultColorizer(getColorizerPrivate))
test('returns default colorizer - public export', testDefaultColorizer(getColorizerPublic))
test('returns colorizing colorizer - private export', testColoringColorizer(getColorizerPrivate))
test('returns colorizing colorizer - public export', testColoringColorizer(getColorizerPublic))
test('returns custom colorizing colorizer - private export', testCustomColoringColorizer(getColorizerPrivate))
test('returns custom colorizing colorizer - public export', testCustomColoringColorizer(getColorizerPublic))
test('returns default colorizer - private export', testDefaultColorizer(getColorizer))
test('returns colorizing colorizer - private export', testColoringColorizer(getColorizer))
test('returns custom colorizing colorizer - private export', testCustomColoringColorizer(getColorizer))

test('custom props defaults to standard levels', async t => {
const colorizer = getColorizer(true, [], true)
const colorized = colorizer('info')
t.equal(colorized, '\u001B[37mINFO\u001B[39m')
})
39 changes: 39 additions & 0 deletions lib/utils/build-safe-sonic-boom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,42 @@ tap.test('should stream.write works when error code is not "EPIPE"', async t =>
const dataFile = fs.readFileSync(dest)
t.equal(dataFile.toString(), 'will work')
})

tap.test('cover setupOnExit', async t => {
t.plan(3)
const { fd, dest } = file()
const stream = buildSafeSonicBoom({ sync: false, fd, mkdir: true })

t.teardown(() => rimraf(dest, noop))

stream.on('error', () => t.pass('error emitted'))
stream.emit('error', 'fake error description')

t.ok(stream.write('will work'))

await watchFileCreated(dest)

const dataFile = fs.readFileSync(dest)
t.equal(dataFile.toString(), 'will work')
})

function watchFileCreated (filename) {
return new Promise((resolve, reject) => {
const TIMEOUT = 2000
const INTERVAL = 100
const threshold = TIMEOUT / INTERVAL
let counter = 0
const interval = setInterval(() => {
// On some CI runs file is created but not filled
if (fs.existsSync(filename) && fs.statSync(filename).size !== 0) {
clearInterval(interval)
resolve()
} else if (counter <= threshold) {
counter++
} else {
clearInterval(interval)
reject(new Error(`${filename} was not created.`))
}
}, INTERVAL)
})
}
1 change: 1 addition & 0 deletions lib/utils/delete-log-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function deleteLogProperty (log, property) {

log = getPropertyValue(log, props)

/* istanbul ignore else */
if (log !== null && typeof log === 'object' && Object.prototype.hasOwnProperty.call(log, propToDelete)) {
delete log[propToDelete]
}
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/format-time.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ tap.test('passes through epoch if `translateTime` is `false`', async t => {
t.equal(formattedTime, epochMS)
})

tap.test('passes through epoch if date is invalid', async t => {
const input = 'this is not a date'
const formattedTime = formatTime(input, true)
t.equal(formattedTime, input)
})

tap.test('translates epoch milliseconds if `translateTime` is `true`', async t => {
const formattedTime = formatTime(epochMS, true)
t.equal(formattedTime, '17:30:00.000')
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/handle-custom-levels-names-opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ function handleCustomLevelsNamesOpts (cLevels) {
return cLevels
.split(',')
.reduce((agg, value, idx) => {
const [levelName, levelIdx = idx] = value.split(':')
agg[levelName.toLowerCase()] = levelIdx
const [levelName, levelNum = idx] = value.split(':')
agg[levelName.toLowerCase()] = levelNum
return agg
}, {})
} else if (Object.prototype.toString.call(cLevels) === '[object Object]') {
return Object
.keys(cLevels)
.reduce((agg, levelName, idx) => {
.reduce((agg, levelName) => {
agg[levelName.toLowerCase()] = cLevels[levelName]
return agg
}, {})
Expand Down
13 changes: 13 additions & 0 deletions lib/utils/handle-custom-levels-names-opts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
const tap = require('tap')
const handleCustomLevelsNamesOpts = require('./handle-custom-levels-names-opts')

tap.test('returns a empty object `{}` for undefined parameter', async t => {
const handledCustomLevelNames = handleCustomLevelsNamesOpts()
t.same(handledCustomLevelNames, {})
})

tap.test('returns a empty object `{}` for unknown parameter', async t => {
const handledCustomLevelNames = handleCustomLevelsNamesOpts(123)
t.same(handledCustomLevelNames, {})
Expand All @@ -29,3 +34,11 @@ tap.test('returns a filled object for object parameter', async t => {
error: 35
})
})

tap.test('defaults missing level num to first index', async t => {
const result = handleCustomLevelsNamesOpts('ok:10,info')
t.same(result, {
ok: 10,
info: 1
})
})
6 changes: 3 additions & 3 deletions lib/utils/handle-custom-levels-opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ function handleCustomLevelsOpts (cLevels) {
return cLevels
.split(',')
.reduce((agg, value, idx) => {
const [levelName, levelIdx = idx] = value.split(':')
agg[levelIdx] = levelName.toUpperCase()
const [levelName, levelNum = idx] = value.split(':')
agg[levelNum] = levelName.toUpperCase()
return agg
},
{ default: 'USERLVL' })
} else if (Object.prototype.toString.call(cLevels) === '[object Object]') {
return Object
.keys(cLevels)
.reduce((agg, levelName, idx) => {
.reduce((agg, levelName) => {
agg[cLevels[levelName]] = levelName.toUpperCase()
return agg
}, { default: 'USERLVL' })
Expand Down
14 changes: 14 additions & 0 deletions lib/utils/handle-custom-levels-opts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
const tap = require('tap')
const handleCustomLevelsOpts = require('./handle-custom-levels-opts')

tap.test('returns a empty object `{}` for undefined parameter', async t => {
const handledCustomLevel = handleCustomLevelsOpts()
t.same(handledCustomLevel, {})
})

tap.test('returns a empty object `{}` for unknown parameter', async t => {
const handledCustomLevel = handleCustomLevelsOpts(123)
t.same(handledCustomLevel, {})
Expand Down Expand Up @@ -31,3 +36,12 @@ tap.test('returns a filled object for object parameter', async t => {
default: 'USERLVL'
})
})

tap.test('defaults missing level num to first index', async t => {
const result = handleCustomLevelsOpts('ok:10,info')
t.same(result, {
10: 'OK',
1: 'INFO',
default: 'USERLVL'
})
})
4 changes: 2 additions & 2 deletions lib/utils/prettify-error-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ const prettifyObject = require('./prettify-object')
* @param {string[]} [input.errorProperties] A set of specific error object
* properties, that are not the value of `messageKey`, `type`, or `stack`, to
* include in the prettified result. The first entry in the list may be `'*'`
* to indicate that all sibiling properties should be prettified. Default: `[]`.
* to indicate that all sibling properties should be prettified. Default: `[]`.
*
* @returns {string} A sring that represents the prettified error log.
* @returns {string} A string that represents the prettified error log.
*/
function prettifyErrorLog ({
log,
Expand Down
39 changes: 39 additions & 0 deletions lib/utils/prettify-error-log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,42 @@ tap.test('returns string with custom eol', async t => {
const str = prettifyErrorLog({ log: err, eol: '\r\n' })
t.ok(str.startsWith(' Error: Something went wrong\r\n'))
})

tap.test('errorProperties', t => {
t.test('excludes all for wildcard', async t => {
const err = Error('boom')
err.foo = 'foo'
const str = prettifyErrorLog({ log: err, errorProperties: ['*'] })
t.ok(str.startsWith(' Error: boom'))
t.equal(str.includes('foo: "foo"'), false)
})

t.test('excludes only selected properties', async t => {
const err = Error('boom')
err.foo = 'foo'
const str = prettifyErrorLog({ log: err, errorProperties: ['foo'] })
t.ok(str.startsWith(' Error: boom'))
t.equal(str.includes('foo: foo'), true)
})

t.test('ignores specified properties if not present', async t => {
const err = Error('boom')
err.foo = 'foo'
const str = prettifyErrorLog({ log: err, errorProperties: ['foo', 'bar'] })
t.ok(str.startsWith(' Error: boom'))
t.equal(str.includes('foo: foo'), true)
t.equal(str.includes('bar'), false)
})

t.test('processes nested objects', async t => {
const err = Error('boom')
err.foo = { bar: 'bar', message: 'included' }
const str = prettifyErrorLog({ log: err, errorProperties: ['foo'] })
t.ok(str.startsWith(' Error: boom'))
t.equal(str.includes('foo: {'), true)
t.equal(str.includes('bar: "bar"'), true)
t.equal(str.includes('message: "included"'), true)
})

t.end()
})
12 changes: 12 additions & 0 deletions lib/utils/prettify-level.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ tap.test('returns colorized value for color colorizer', async t => {
const colorized = prettifyLevel({ log, colorizer })
t.equal(colorized, '\u001B[32mINFO\u001B[39m')
})

tap.test('passes output through provided prettifier', async t => {
const log = {
level: 30
}
const colorized = prettifyLevel({ log, prettifier })
t.equal(colorized, 'modified')

function prettifier () {
return 'modified'
}
})
22 changes: 22 additions & 0 deletions lib/utils/prettify-metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,25 @@ tap.test('works with all four present', async t => {
const str = prettifyMetadata({ log: { name: 'foo', pid: '1234', hostname: 'bar', caller: 'baz' } })
t.equal(str, '(foo/1234 on bar) <baz>')
})

tap.test('uses prettifiers from passed prettifiers object', async t => {
const prettifiers = {
name (input) {
return input.toUpperCase()
},
pid (input) {
return input + '__'
},
hostname (input) {
return input.toUpperCase()
},
caller (input) {
return input.toUpperCase()
}
}
const str = prettifyMetadata({
log: { pid: '1234', hostname: 'bar', caller: 'baz', name: 'joe' },
prettifiers
})
t.equal(str, '(JOE/1234__ on BAR) <BAZ>')
})
2 changes: 2 additions & 0 deletions lib/utils/prettify-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function prettifyObject ({
}) {
const keysToIgnore = [].concat(skipKeys)

/* istanbul ignore else */
if (excludeLoggerKeys === true) Array.prototype.push.apply(keysToIgnore, LOGGER_KEYS)

let result = ''
Expand All @@ -71,6 +72,7 @@ function prettifyObject ({

if (singleLine) {
// Stringify the entire object as a single JSON line
/* istanbul ignore else */
if (Object.keys(plain).length > 0) {
result += colorizer.greyMessage(stringifySafe(plain))
}
Expand Down
51 changes: 51 additions & 0 deletions lib/utils/prettify-object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,54 @@ tap.test('works with error props', async t => {
t.ok(str.includes(' "message": "Something went wrong",'))
t.ok(str.includes(' Error: Something went wrong'))
})

tap.test('customPrettifiers gets applied', async t => {
const customPrettifiers = {
foo: v => v.toUpperCase()
}
const str = prettifyObject({ input: { foo: 'foo' }, customPrettifiers })
t.equal(str.startsWith(' foo: FOO'), true)
})

tap.test('skips lines omitted by customPrettifiers', async t => {
const customPrettifiers = {
foo: () => { return undefined }
}
const str = prettifyObject({ input: { foo: 'foo', bar: 'bar' }, customPrettifiers })
t.equal(str.includes('bar: "bar"'), true)
t.equal(str.includes('foo: "foo"'), false)
})

tap.test('joined lines omits starting eol', async t => {
const str = prettifyObject({
input: { msg: 'doing work', calls: ['step 1', 'step 2', 'step 3'], level: 30 },
ident: '',
customPrettifiers: {
calls: val => '\n' + val.map(it => ' ' + it).join('\n')
}
})
t.equal(str, [
'msg: "doing work"',
'calls:',
' step 1',
' step 2',
' step 3',
''
].join('\n'))
})

tap.test('errors skips prettifiers', async t => {
const customPrettifiers = {
err: () => { return 'is_err' }
}
const str = prettifyObject({ input: { err: Error('boom') }, customPrettifiers })
t.equal(str.includes('err: is_err'), true)
})

tap.test('errors skips prettifying if no lines are present', async t => {
const customPrettifiers = {
err: () => { return undefined }
}
const str = prettifyObject({ input: { err: Error('boom') }, customPrettifiers })
t.equal(str, '')
})
10 changes: 10 additions & 0 deletions lib/utils/prettify-time.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,13 @@ tap.test('works with epoch as a number or string', (t) => {
t.same(asNumber, '[17:35:28.992]')
t.same(invalid, '[2 days ago]')
})

tap.test('uses custom prettifier', async t => {
const str = prettifyTime({
log: { time: 0 },
prettifier () {
return 'done'
}
})
t.equal(str, 'done')
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"lint": "standard | snazzy",
"test": "tap",
"test-types": "tsc && tsd",
"test:watch": "tap --no-coverage-report -w"
"test:watch": "tap --no-coverage-report -w",
"test:report": "tap --coverage-report=html"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 60c43fc

Please sign in to comment.