Skip to content

Commit

Permalink
Throw on unresolved refs, reworks error generation, reaches 100% test…
Browse files Browse the repository at this point in the history
… coverage
  • Loading branch information
webketje committed Feb 21, 2024
1 parent a81fb1b commit 8e365fa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metalsmith/refs",
"version": "0.1.0",
"version": "0.0.0",
"description": "A metalsmith plugin to refer to other files and global metadata from a file's refs property",
"keywords": [
"reference",
Expand Down
24 changes: 12 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ const defaults = {
pattern: '**'
}

const ns = '@metalsmith/refs'

function error(msg) {
const err = new Error(msg)
err.name = ns
return err
}

/**
* Normalize plugin options
* @param {Options} [options]
Expand All @@ -36,7 +44,7 @@ const refProxyHandler = {
return true
},
deleteProperty(target, key) {
if (key === 'refs' || !(key in target)) return false
if (key == 'refs') throw error(`Cannot access a refs' own refs from host file.`)
return delete target[key]
},
ownKeys(target) {
Expand All @@ -51,7 +59,7 @@ const refProxyHandler = {
},
getOwnPropertyDescriptor(target, key) {
const value = target[key]
return value
return Reflect.has(target, key)
? {
value,
writable: true,
Expand Down Expand Up @@ -130,20 +138,12 @@ function refs(options) {
}
break
default:
done(
new Error(
'@metalsmith/refs - Unknown protocol "' +
protocol +
'" for file "' +
metalsmith.path(metalsmith.source(), file.id) +
'".'
)
)
done(error(`Unknown protocol "${protocol}" for file "${metalsmith.path(metalsmith.source(), path)}.`))
return
}

if (!resolved) {
debug.warn('Unable to resolve ref "%s" in file "%s"', `${protocol}:${lookup}`, path)
done(error(`Unable to resolve ref "${protocol}:${lookup}" in file "${path}"`))
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/unresolved-ref/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
refs:
someref: file:./inexistant.md
---
20 changes: 18 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ describe('@metalsmith/refs', function () {
})

it('should not crash the metalsmith build when using default options', async function () {
let { actual, expected, dir } = fixture('default')
const { actual, expected, dir } = fixture('default')
await Metalsmith(dir).env('DEBUG', process.env.DEBUG).use(plugin()).use(toJSON).build()
equals(actual, expected)
await Metalsmith(dir).env('DEBUG', process.env.DEBUG).use(plugin(null)).use(toJSON).build()
equals(actual, expected)
await Metalsmith(dir).env('DEBUG', process.env.DEBUG).use(plugin(true)).use(toJSON).build()
equals(actual, expected)
})
Expand All @@ -64,9 +66,11 @@ describe('@metalsmith/refs', function () {
Object.defineProperty(ref, 'added', { value: 'test' })
delete ref.temp
assert.strictEqual('refs' in ref, false)
assert.throws(() => delete ref.refs)
assert.strictEqual('about_title' in ref, true)
assert.strictEqual(ref.refs, undefined)
assert.strictEqual(ref.about_title, 'About')
assert.strictEqual(Object.getOwnPropertyDescriptor(ref, 'inexistant'), undefined)
assert.deepStrictEqual(Object.keys(ref), ['about_title', 'id', 'added'])
assert.throws(() => {
Object.defineProperty(ref, 'refs', { value: {} })
Expand Down Expand Up @@ -119,7 +123,19 @@ describe('@metalsmith/refs', function () {
const { dir } = fixture('invalid-protocol')
await Metalsmith(dir).env('DEBUG', process.env.DEBUG).use(plugin()).use(toJSON).build()
} catch (err) {
const expected = '@metalsmith/refs - Unknown protocol "invalid-protocol" for file'
assert.strictEqual(err.name, '@metalsmith/refs')
const expected = 'Unknown protocol "invalid-protocol" for file'
assert.strictEqual(err.message.slice(0, expected.length), expected)
}
})

it('should throw an error on unresolved refs', async function () {
try {
const { dir } = fixture('unresolved-ref')
await Metalsmith(dir).env('DEBUG', process.env.DEBUG).use(plugin()).use(toJSON).build()
} catch (err) {
assert.strictEqual(err.name, '@metalsmith/refs')
const expected = 'Unable to resolve ref "file:./inexistant.md" in file'
assert.strictEqual(err.message.slice(0, expected.length), expected)
}
})
Expand Down

0 comments on commit 8e365fa

Please sign in to comment.