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

Commit 0a176cc

Browse files
committed
Add some tests and clean up error handling for non-string bins
1 parent 76f6f42 commit 0a176cc

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

read-json.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -403,17 +403,16 @@ function checkBinReferences_ (file, data, warn, cb) {
403403
keys.forEach(function (key) {
404404
var dirName = path.dirname(file)
405405
var relName = data.bin[key]
406-
try {
407-
var binPath = path.resolve(dirName, relName)
408-
fs.stat(binPath, (err) => handleExists(relName, !err))
409-
} catch (error) {
410-
if (error.message === 'Arguments to path.resolve must be strings' || error.message.indexOf('Path must be a string') === 0) {
411-
warn('Bin filename for ' + key + ' is not a string: ' + util.inspect(relName))
412-
handleExists(relName, true)
413-
} else {
414-
cb(error)
415-
}
406+
if (typeof relName !== 'string') {
407+
var msg = 'Bin filename for ' + key +
408+
' is not a string: ' + util.inspect(relName)
409+
warn(msg)
410+
delete data.bin[key]
411+
handleExists(relName, true)
412+
return
416413
}
414+
var binPath = path.resolve(dirName, relName)
415+
fs.stat(binPath, (err) => handleExists(relName, !err))
417416
})
418417
}
419418

test/bin-non-string.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ var p = path.resolve(__dirname, 'fixtures/badbinnonstring.json')
55

66
tap.test('non-string bin entries', function (t) {
77
var logmsgs = []
8-
readJson(p, function (msg) { logmsgs.push([].slice.call(arguments)) }, function (er, data) {
9-
t.comment(logmsgs.map(function (msg) { return 'Warning: ' + msg.join(' ') }).join('\n'))
8+
const warn = (...msg) => logmsgs.push(msg)
9+
readJson(p, warn, function (er, data) {
10+
t.comment(logmsgs.map(msg => 'Warning: ' + msg.join(' ')).join('\n'))
1011
t.like(er, null, 'no error from readJson')
12+
t.same(data.bin, {})
1113
t.end()
1214
})
1315
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "bob-the-cat",
3+
"version": "a live bobcat",
4+
"description": "instead of semver, package contained live bobcat"
5+
}

test/semver.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const t = require('tap')
2+
const readJson = require('../')
3+
const path = require('path')
4+
const file = path.resolve(__dirname, 'fixtures/invalid-version/package.json')
5+
const logs = []
6+
const warn = (...msg) => logs.push(msg)
7+
readJson(file, warn, false, (er, data) => {
8+
t.match(er, {
9+
message: 'Invalid version: "a live bobcat"'
10+
})
11+
t.notOk(data)
12+
t.end()
13+
})

0 commit comments

Comments
 (0)