From c839fba9ce2f776a5c71ccb6ea48f459ae88ce4e Mon Sep 17 00:00:00 2001 From: Evan Shortiss Date: Sun, 10 Feb 2019 22:02:52 -0800 Subject: [PATCH 1/4] feat: improve error output if 'root' is missing --- index.js | 4 ++++ test/static.test.js | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index cbdc7642..4fa72dbb 100644 --- a/index.js +++ b/index.js @@ -179,6 +179,10 @@ function checkRootPathForErrors (rootPath) { try { pathStat = statSync(rootPath) } catch (e) { + if (e.code === 'ENOENT') { + return new Error('"root" should point to an existing directory') + } + return e } diff --git a/test/static.test.js b/test/static.test.js index 4ebd9074..ab36f641 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -4,7 +4,7 @@ const path = require('path') const fs = require('fs') const url = require('url') const http = require('http') - +const os = require('os') const t = require('tap') const simple = require('simple-get') const Fastify = require('fastify') @@ -593,6 +593,16 @@ t.test('prefix default', t => { t.doesNotThrow(() => fastify.register(fastifyStatic, pluginOptions)) }) +t.test('root not found error', t => { + t.plan(1) + const pluginOptions = { root: path.join(os.tmpdir(), 'not-going-to-exist') } + const fastify = Fastify({ logger: false }) + fastify.register(fastifyStatic, pluginOptions) + fastify.listen(0, err => { + t.equal(err.message, '"root" should point to an existing directory') + }) +}) + t.test('send options', t => { t.plan(11) const pluginOptions = { From 64ae6311b809daf0963900e8643ede3c350631af Mon Sep 17 00:00:00 2001 From: Evan Shortiss Date: Sun, 10 Feb 2019 22:05:31 -0800 Subject: [PATCH 2/4] fix: simplify root missing test --- test/static.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/static.test.js b/test/static.test.js index ab36f641..206c4c1a 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -4,7 +4,6 @@ const path = require('path') const fs = require('fs') const url = require('url') const http = require('http') -const os = require('os') const t = require('tap') const simple = require('simple-get') const Fastify = require('fastify') @@ -595,7 +594,7 @@ t.test('prefix default', t => { t.test('root not found error', t => { t.plan(1) - const pluginOptions = { root: path.join(os.tmpdir(), 'not-going-to-exist') } + const pluginOptions = { root: path.join(__dirname, 'does-not-exist') } const fastify = Fastify({ logger: false }) fastify.register(fastifyStatic, pluginOptions) fastify.listen(0, err => { From 18aaac081e679652e2f9f9862182ff1737c233f3 Mon Sep 17 00:00:00 2001 From: Evan Shortiss Date: Mon, 11 Feb 2019 10:59:51 -0800 Subject: [PATCH 3/4] fix: address PR feedback --- index.js | 2 +- test/static.test.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4fa72dbb..1b874b2c 100644 --- a/index.js +++ b/index.js @@ -180,7 +180,7 @@ function checkRootPathForErrors (rootPath) { pathStat = statSync(rootPath) } catch (e) { if (e.code === 'ENOENT') { - return new Error('"root" should point to an existing directory') + return new Error(`"root" option "${rootPath}" must exist`) } return e diff --git a/test/static.test.js b/test/static.test.js index 206c4c1a..da1d7683 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -594,11 +594,12 @@ t.test('prefix default', t => { t.test('root not found error', t => { t.plan(1) - const pluginOptions = { root: path.join(__dirname, 'does-not-exist') } + const rootPath = path.join(__dirname, 'does-not-exist') + const pluginOptions = { root: rootPath } const fastify = Fastify({ logger: false }) fastify.register(fastifyStatic, pluginOptions) fastify.listen(0, err => { - t.equal(err.message, '"root" should point to an existing directory') + t.equal(err.message, `"root" option "${rootPath}" must exist`) }) }) From 17b9f5875f3d449565a63aa328011946fb589bde Mon Sep 17 00:00:00 2001 From: Evan Shortiss Date: Mon, 11 Feb 2019 11:00:31 -0800 Subject: [PATCH 4/4] fix: change 'option' to 'path' --- index.js | 2 +- test/static.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1b874b2c..84bca23c 100644 --- a/index.js +++ b/index.js @@ -180,7 +180,7 @@ function checkRootPathForErrors (rootPath) { pathStat = statSync(rootPath) } catch (e) { if (e.code === 'ENOENT') { - return new Error(`"root" option "${rootPath}" must exist`) + return new Error(`"root" path "${rootPath}" must exist`) } return e diff --git a/test/static.test.js b/test/static.test.js index da1d7683..63ecabf1 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -599,7 +599,7 @@ t.test('root not found error', t => { const fastify = Fastify({ logger: false }) fastify.register(fastifyStatic, pluginOptions) fastify.listen(0, err => { - t.equal(err.message, `"root" option "${rootPath}" must exist`) + t.equal(err.message, `"root" path "${rootPath}" must exist`) }) })