Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Add some tests and clean up error handling for non-string bins
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 13, 2019
1 parent 76f6f42 commit 0a176cc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
19 changes: 9 additions & 10 deletions read-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,16 @@ function checkBinReferences_ (file, data, warn, cb) {
keys.forEach(function (key) {
var dirName = path.dirname(file)
var relName = data.bin[key]
try {
var binPath = path.resolve(dirName, relName)
fs.stat(binPath, (err) => handleExists(relName, !err))
} catch (error) {
if (error.message === 'Arguments to path.resolve must be strings' || error.message.indexOf('Path must be a string') === 0) {
warn('Bin filename for ' + key + ' is not a string: ' + util.inspect(relName))
handleExists(relName, true)
} else {
cb(error)
}
if (typeof relName !== 'string') {
var msg = 'Bin filename for ' + key +
' is not a string: ' + util.inspect(relName)
warn(msg)
delete data.bin[key]
handleExists(relName, true)
return
}
var binPath = path.resolve(dirName, relName)
fs.stat(binPath, (err) => handleExists(relName, !err))
})
}

Expand Down
6 changes: 4 additions & 2 deletions test/bin-non-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ var p = path.resolve(__dirname, 'fixtures/badbinnonstring.json')

tap.test('non-string bin entries', function (t) {
var logmsgs = []
readJson(p, function (msg) { logmsgs.push([].slice.call(arguments)) }, function (er, data) {
t.comment(logmsgs.map(function (msg) { return 'Warning: ' + msg.join(' ') }).join('\n'))
const warn = (...msg) => logmsgs.push(msg)
readJson(p, warn, function (er, data) {
t.comment(logmsgs.map(msg => 'Warning: ' + msg.join(' ')).join('\n'))
t.like(er, null, 'no error from readJson')
t.same(data.bin, {})
t.end()
})
})
5 changes: 5 additions & 0 deletions test/fixtures/invalid-version/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "bob-the-cat",
"version": "a live bobcat",
"description": "instead of semver, package contained live bobcat"
}
13 changes: 13 additions & 0 deletions test/semver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const t = require('tap')
const readJson = require('../')
const path = require('path')
const file = path.resolve(__dirname, 'fixtures/invalid-version/package.json')
const logs = []
const warn = (...msg) => logs.push(msg)
readJson(file, warn, false, (er, data) => {
t.match(er, {
message: 'Invalid version: "a live bobcat"'
})
t.notOk(data)
t.end()
})

0 comments on commit 0a176cc

Please sign in to comment.