Skip to content

Commit

Permalink
Bugfix: prefix aggregation issues (#94)
Browse files Browse the repository at this point in the history
* Fix prefix aggregation issues

* Add tests

* Add more tests
  • Loading branch information
darkgl0w authored Jul 17, 2020
1 parent c1d31a7 commit b8266ec
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 3 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function findPlugins (dir, options, accumulator = [], prefix) {

const file = path.join(dir, dirent.name)
if (dirent.isDirectory()) {
directoryPromises.push(findPlugins(file, options, accumulator, (prefix ? prefix + '/' : '') + dirent.name))
directoryPromises.push(findPlugins(file, options, accumulator, (prefix ? prefix + '/' : '/') + dirent.name))
continue
}

Expand Down Expand Up @@ -125,6 +125,7 @@ async function loadPlugin (file, type, directoryPrefix, options) {
plugin.autoConfig = undefined
}

pluginOptions.prefix = (pluginOptions.prefix && pluginOptions.prefix.endsWith('/')) ? pluginOptions.prefix.slice(0, -1) : pluginOptions.prefix
const prefixOverride = plugin.prefixOverride !== undefined ? plugin.prefixOverride : content.prefixOverride !== undefined ? content.prefixOverride : undefined
const prefix = (plugin.autoPrefix !== undefined ? plugin.autoPrefix : content.autoPrefix !== undefined ? content.autoPrefix : undefined) || directoryPrefix
if (prefixOverride !== undefined) {
Expand Down
20 changes: 19 additions & 1 deletion test/commonjs/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const t = require('tap')
const Fastify = require('fastify')

t.plan(59)
t.plan(65)

const app = Fastify()

Expand Down Expand Up @@ -184,4 +184,22 @@ app.ready(function (err) {
t.equal(res.statusCode, 200)
t.deepEqual(JSON.parse(res.payload), { works: true })
})

app.inject({
url: '/one/'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.deepEqual(JSON.parse(res.payload), { works: true })
})

app.inject({
url: '/one/two/three'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
t.deepEqual(JSON.parse(res.payload), { works: true })
})
})
5 changes: 5 additions & 0 deletions test/commonjs/basic/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ module.exports = function (fastify, opts, next) {
options: { prefix: '/defaultPrefix' }
})

fastify.register(autoLoad, {
dir: path.join(__dirname, 'one'),
options: { prefix: 'one/' }
})

fastify.register(autoLoad, {
dir: path.join(__dirname, 'index'),
options: { prefix: 'index/' }
Expand Down
5 changes: 5 additions & 0 deletions test/commonjs/basic/one/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = async (server, opts) => {
server.get('/', async (req, reply) => {
reply.send({ works: true })
})
}
5 changes: 5 additions & 0 deletions test/commonjs/basic/one/two/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = async (server, opts) => {
server.get('/three', async (req, reply) => {
reply.send({ works: true })
})
}
18 changes: 17 additions & 1 deletion test/module/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import t from 'tap'
import fastify from 'fastify'
import basicApp from './basic/app.js'

t.plan(53)
t.plan(59)

const app = fastify()

Expand Down Expand Up @@ -167,4 +167,20 @@ app.ready(function (err) {
t.equal(res.statusCode, 200)
t.deepEqual(JSON.parse(res.payload), { no: 'prefix' })
})

app.inject({
url: '/one/'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.deepEqual(JSON.parse(res.payload), { works: true })
})

app.inject({
url: '/one/two/three'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.deepEqual(JSON.parse(res.payload), { works: true })
})
})
5 changes: 5 additions & 0 deletions test/module/basic/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default function (fastify, opts, next) {
options: { prefix: '/defaultPrefix' }
})

fastify.register(autoLoad, {
dir: path.join(__dirname, 'one'),
options: { prefix: 'one/' }
})

const skipDir = path.join(__dirname, 'skip')
fs.mkdir(path.join(skipDir, 'empty'), () => {
fastify.register(autoLoad, {
Expand Down
5 changes: 5 additions & 0 deletions test/module/basic/one/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default async (server, opts) => {
server.get('/', async (req, reply) => {
reply.send({ works: true })
})
}
5 changes: 5 additions & 0 deletions test/module/basic/one/two/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default async (server, opts) => {
server.get('/three', async (req, reply) => {
reply.send({ works: true })
})
}

0 comments on commit b8266ec

Please sign in to comment.