-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from testdouble/silent-default
Shore up logging behavior
- Loading branch information
Showing
29 changed files
with
281 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,7 @@ | ||
engines: | ||
duplication: | ||
enabled: true | ||
config: | ||
languages: | ||
- javascript | ||
fixme: | ||
enabled: true | ||
version: 2 | ||
|
||
ratings: | ||
paths: | ||
- lib/** | ||
- test/** | ||
|
||
exclude_paths: | ||
- example/**/* | ||
- node_modules/**/* | ||
exclude_patterns: | ||
- example/ | ||
- node_modules/ | ||
- test/ | ||
- "**/*.test.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var log = require('./log') | ||
|
||
module.exports = function deriveLogLevel (userOptions) { | ||
if (!userOptions) return | ||
|
||
if (userOptions.logLevel) return userOptions.logLevel | ||
|
||
if (userOptions.verbose) return log.verbose | ||
|
||
if (userOptions.dryRun) return log.info | ||
|
||
if (userOptions.silent || userOptions.quiet) return log.silent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var subject = require('./derive-log-level') | ||
var log = require('./log') | ||
|
||
module.exports = { | ||
'does not default': function () { | ||
assert.equal(subject(), undefined) | ||
}, | ||
|
||
'explicit logLevel takes precedence': function () { | ||
assert.equal(subject({ | ||
silent: true, | ||
dryRun: true, | ||
verbose: true, | ||
logLevel: 'warn' }), 'warn') | ||
}, | ||
|
||
'passes through unrecognized values': function () { | ||
assert.equal(subject({ logLevel: 'worn' }), 'worn') | ||
}, | ||
|
||
'verbose preempts dry-run and silent': function () { | ||
assert.equal(subject({ silent: true, dryRun: true, verbose: true }), log.verbose) | ||
}, | ||
|
||
'dry-run preempts silent': function () { | ||
assert.equal(subject({ silent: true, dryRun: true }), log.info) | ||
}, | ||
|
||
'silent is read last': function () { | ||
assert.equal(subject({ silent: true }), log.silent) | ||
}, | ||
|
||
'quiet is alias for silent': function () { | ||
assert.equal(subject({ quiet: true }), log.silent) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const util = require('util') | ||
|
||
const constant = val => () => val | ||
|
||
const formatError = args => | ||
args.map(arg => arg instanceof Error ? arg.message : arg) | ||
|
||
const loggerWithPrefix = (prefix, writer) => (...args) => | ||
writer()(prefix, util.format(...formatError(args)) | ||
.replace(/(\r?\n)(?=[\s\S]+)/g, `$1${prefix} `)) | ||
|
||
const silentLogger = (...args) => { | ||
output += util.format(...args) + '\n' | ||
} | ||
|
||
let level | ||
let output | ||
|
||
module.exports = { | ||
get level () { | ||
return level.toString() | ||
}, | ||
|
||
set level (l) { | ||
level = module.exports[String(l).toLowerCase()] | ||
}, | ||
|
||
read: () => output, | ||
|
||
reset: () => { | ||
output = '' | ||
module.exports.level = 'info' | ||
} | ||
} | ||
|
||
;[ | ||
['verbose', '>'], | ||
['info', '>'], | ||
['warn', 'WARN'], | ||
['error', 'ERR!'], | ||
['silent'] | ||
].forEach(([name, prefix], index) => { | ||
const logger = loggerWithPrefix(`scripty ${prefix}`, () => | ||
level <= logger ? console.error : silentLogger) | ||
|
||
logger.valueOf = constant(index + 1) | ||
logger.toString = constant(name) | ||
|
||
module.exports[name] = logger | ||
}) | ||
|
||
module.exports.reset() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
var subject = require('./log') | ||
|
||
module.exports = { | ||
beforeEach: function () { | ||
td.replace(console, 'error') | ||
subject.reset() | ||
}, | ||
writesToStderr: function () { | ||
subject.info('foo') | ||
|
||
td.verify(console.error('scripty >', 'foo')) | ||
}, | ||
setTheLogLevel: { | ||
verbose: function () { | ||
subject.level = subject.verbose | ||
subject.verbose('ity') | ||
td.verify(console.error('scripty >', 'ity')) | ||
}, | ||
info: function () { | ||
subject.level = subject.info | ||
td.when(console.error('ity')).thenThrow(new Error('Should not log verbose calls at INFO level')) | ||
subject.verbose('ity') | ||
subject.info('mation') | ||
td.verify(console.error('scripty >', 'mation')) | ||
}, | ||
warn: function () { | ||
subject.level = subject.warn | ||
td.when(console.error('mation')).thenThrow(new Error('Should not log info calls at WARN level')) | ||
subject.info('mation') | ||
subject.warn('ing') | ||
td.verify(console.error('scripty WARN', 'ing')) | ||
}, | ||
error: function () { | ||
subject.level = subject.error | ||
td.when(console.error('ing')).thenThrow(new Error('Should not log warn calls at ERROR level')) | ||
subject.warn('ing') | ||
subject.error('fail') | ||
td.verify(console.error('scripty ERR!', 'fail')) | ||
}, | ||
silent: function () { | ||
subject.level = subject.silent | ||
td.when(console.error('fail')).thenThrow(new Error('Should not log error calls at SILENT level')) | ||
subject.error('fail') | ||
} | ||
}, | ||
modeSwitchCapturesLogs: function () { | ||
subject.level = subject.silent | ||
|
||
subject.info('bar') | ||
subject.info('baz', 'noz') | ||
|
||
td.verify(console.error(), { ignoreExtraArgs: true, times: 0 }) | ||
assert.equal(subject.read(), 'scripty > bar\nscripty > baz noz\n') | ||
}, | ||
resetResetsMode: function () { | ||
subject.level = subject.silent | ||
|
||
subject.reset() | ||
|
||
subject.info('biz') | ||
td.verify(console.error('scripty >', 'biz')) | ||
}, | ||
resetResetsLog: function () { | ||
subject.level = subject.silent | ||
|
||
subject.info('lalalal') | ||
|
||
subject.reset() | ||
|
||
assert.equal(subject.read(), '') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,21 @@ | ||
var _ = require('lodash') | ||
var globFirst = require('./glob-first') | ||
var path = require('path') | ||
var async = require('async') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var globFirst = require('./glob-first') | ||
var log = require('../log') | ||
|
||
var isExecutable = require('./is-executable') | ||
|
||
module.exports = function (patterns, cb) { | ||
globFirst(patterns, function (er, results) { | ||
module.exports = (patterns, cb) => | ||
globFirst(patterns, (er, results) => { | ||
if (er) return cb(er) | ||
async.map(results, function (result, cb) { | ||
isExecutable(result, function (er, itIsExecutable) { | ||
if (itIsExecutable) { | ||
cb(er, path.resolve(result)) | ||
} else { | ||
console.warn( | ||
'Warning: scripty - ignoring script "' + result + '" because it' + | ||
' was not executable. Run `chmod +x "' + result + '" if you want' + | ||
' scripty to run it.' | ||
) | ||
cb(er, undefined) | ||
} | ||
}) | ||
}, function (er, results) { | ||
cb(er, _.compact(results)) | ||
}) | ||
|
||
async.map(results, | ||
(result, cb) => fs.access(result, fs.constants.R_OK | fs.constants.X_OK, | ||
er => cb(null, er | ||
? log.warn(`Ignoring script '${result}' because it was not readable/executable.\n` + | ||
`Run \`chmod u+rx '${result}'\` if you want scripty to run it.`) | ||
: path.resolve(result)) | ||
), | ||
(er, results) => cb(er, _.compact(results)) | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.