-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
print-routes options and print-plugins (#637)
* Add printRoutes flags This adds flags for method, commonPrefix and includeHooks options in the printRoutes method. Closes #635 * Add print-plugins command It runs the printPlugins method and prints the results. No arguments. Closes #636 * Fix flakey tests Better flakey CI skip test logic Add timeout Try awaiting in the same async function? Skip in CI Re-enable in darwin Return close promise Fix tests
- Loading branch information
Showing
16 changed files
with
321 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
module.exports = function (fastify, options, next) { | ||
fastify.decorate('test', true) | ||
fastify.get('/hello-world', function (req, reply) { | ||
req.log.trace('trace') | ||
req.log.debug('debug') | ||
reply.send({ hello: 'world' }) | ||
}) | ||
fastify.post('/help', function (req, reply) { | ||
reply.send({ hello: 'world' }) | ||
}) | ||
next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Usage: fastify print-plugins <file> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
Usage: fastify print-routes <file> | ||
|
||
OPTS | ||
|
||
--method <METHOD> | ||
print debugging safe internal router tree for a given method | ||
|
||
--common-prefix | ||
print uncompressed radix tree | ||
|
||
--include-hooks | ||
display all properties from the route.store object for each displayed route |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#! /usr/bin/env node | ||
|
||
'use strict' | ||
|
||
const parseArgs = require('./args') | ||
const log = require('./log') | ||
const { | ||
exit, | ||
requireFastifyForModule, | ||
requireServerPluginFromPath, | ||
showHelpForCommand | ||
} = require('./util') | ||
|
||
let Fastify = null | ||
|
||
function loadModules (opts) { | ||
try { | ||
Fastify = requireFastifyForModule(opts._[0]).module | ||
} catch (e) { | ||
module.exports.stop(e) | ||
} | ||
} | ||
|
||
function printPlugins (args) { | ||
const opts = parseArgs(args) | ||
if (opts.help) { | ||
return showHelpForCommand('print-plugins') | ||
} | ||
|
||
if (opts._.length !== 1) { | ||
console.error('Missing the required file parameter\n') | ||
return showHelpForCommand('print-plugins') | ||
} | ||
|
||
// we start crashing on unhandledRejection | ||
require('make-promises-safe') | ||
|
||
loadModules(opts) | ||
|
||
return runFastify(opts) | ||
} | ||
|
||
async function runFastify (opts) { | ||
require('dotenv').config() | ||
|
||
let file = null | ||
|
||
try { | ||
file = await requireServerPluginFromPath(opts._[0]) | ||
} catch (e) { | ||
return module.exports.stop(e) | ||
} | ||
|
||
const fastify = Fastify(opts.options) | ||
|
||
const pluginOptions = {} | ||
if (opts.prefix) { | ||
pluginOptions.prefix = opts.prefix | ||
} | ||
|
||
await fastify.register(file, pluginOptions) | ||
await fastify.ready() | ||
log('debug', fastify.printPlugins()) | ||
|
||
return fastify | ||
} | ||
|
||
function stop (message) { | ||
exit(message) | ||
} | ||
|
||
function cli (args) { | ||
return printPlugins(args).then(fastify => { | ||
if (fastify) return fastify.close() | ||
}) | ||
} | ||
|
||
module.exports = { cli, stop, printPlugins } | ||
|
||
if (require.main === module) { | ||
cli(process.argv.slice(2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ test-env: [ | |
TS_NODE_FILES=true, | ||
TS_NODE_PROJECT=./test/tsconfig.json | ||
] | ||
timeout: 120 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ test-env: [ | |
TS_NODE_FILES=true, | ||
TS_NODE_PROJECT=./test/tsconfig.json | ||
] | ||
timeout: 120 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
'use strict' | ||
|
||
const proxyquire = require('proxyquire') | ||
const tap = require('tap') | ||
const sinon = require('sinon') | ||
const util = require('node:util') | ||
const exec = util.promisify(require('node:child_process').exec) | ||
|
||
const printPlugins = require('../print-plugins') | ||
|
||
const test = tap.test | ||
|
||
test('should print plugins', async t => { | ||
t.plan(3) | ||
|
||
const spy = sinon.spy() | ||
const command = proxyquire('../print-plugins', { | ||
'./log': spy | ||
}) | ||
const fastify = await command.printPlugins(['./examples/plugin.js']) | ||
|
||
await fastify.close() | ||
t.ok(spy.called) | ||
t.same(spy.args[0][0], 'debug') | ||
t.match(spy.args[0][1], /bound root \d+ ms\n├── bound _after \d+ ms\n├─┬ function \(fastify, options, next\) { -- fastify\.decorate\('test', true\) \d+ ms\n│ ├── bound _after \d+ ms\n│ ├── bound _after \d+ ms\n│ └── bound _after \d+ ms\n└── bound _after \d+ ms\n/) | ||
}) | ||
|
||
// This never exits in CI for some reason | ||
test('should plugins routes via cli', { skip: process.env.CI }, async t => { | ||
t.plan(1) | ||
const { stdout } = await exec('node cli.js print-plugins ./examples/plugin.js', { encoding: 'utf-8', timeout: 10000 }) | ||
t.match( | ||
stdout, | ||
/bound root \d+ ms\n├── bound _after \d+ ms\n├─┬ function \(fastify, options, next\) { -- fastify\.decorate\('test', true\) \d+ ms\n│ ├── bound _after \d+ ms\n│ ├── bound _after \d+ ms\n│ └── bound _after \d+ ms\n└── bound _after \d+ ms\n\n/ | ||
) | ||
}) | ||
|
||
test('should warn on file not found', t => { | ||
t.plan(1) | ||
|
||
const oldStop = printPlugins.stop | ||
t.teardown(() => { printPlugins.stop = oldStop }) | ||
printPlugins.stop = function (message) { | ||
t.ok(/not-found.js doesn't exist within/.test(message), message) | ||
} | ||
|
||
const argv = ['./data/not-found.js'] | ||
printPlugins.printPlugins(argv) | ||
}) | ||
|
||
test('should throw on package not found', t => { | ||
t.plan(1) | ||
|
||
const oldStop = printPlugins.stop | ||
t.teardown(() => { printPlugins.stop = oldStop }) | ||
printPlugins.stop = function (err) { | ||
t.ok(/Cannot find module 'unknown-package'/.test(err.message), err.message) | ||
} | ||
|
||
const argv = ['./test/data/package-not-found.js'] | ||
printPlugins.printPlugins(argv) | ||
}) | ||
|
||
test('should throw on parsing error', t => { | ||
t.plan(1) | ||
|
||
const oldStop = printPlugins.stop | ||
t.teardown(() => { printPlugins.stop = oldStop }) | ||
printPlugins.stop = function (err) { | ||
t.equal(err.constructor, SyntaxError) | ||
} | ||
|
||
const argv = ['./test/data/parsing-error.js'] | ||
printPlugins.printPlugins(argv) | ||
}) | ||
|
||
test('should exit without error on help', t => { | ||
const exit = process.exit | ||
process.exit = sinon.spy() | ||
|
||
t.teardown(() => { | ||
process.exit = exit | ||
}) | ||
|
||
const argv = ['-h', 'true'] | ||
printPlugins.printPlugins(argv) | ||
|
||
t.ok(process.exit.called) | ||
t.equal(process.exit.lastCall.args[0], undefined) | ||
|
||
t.end() | ||
}) | ||
|
||
test('should print plugins of server with an async/await plugin', async t => { | ||
const nodeMajorVersion = process.versions.node.split('.').map(x => parseInt(x, 10))[0] | ||
if (nodeMajorVersion < 7) { | ||
t.pass('Skip because Node version < 7') | ||
return t.end() | ||
} | ||
|
||
t.plan(3) | ||
|
||
const spy = sinon.spy() | ||
const command = proxyquire('../print-plugins', { | ||
'./log': spy | ||
}) | ||
const argv = ['./examples/async-await-plugin.js'] | ||
const fastify = await command.printPlugins(argv) | ||
|
||
await fastify.close() | ||
t.ok(spy.called) | ||
t.same(spy.args[0][0], 'debug') | ||
t.match(spy.args[0][1], /bound root \d+ ms\n├── bound _after \d+ ms\n├─┬ async function \(fastify, options\) { -- fastify\.get\('\/', async function \(req, reply\) { \d+ ms\n│ ├── bound _after \d+ ms\n│ └── bound _after \d+ ms\n└── bound _after \d+ ms\n/) | ||
}) |
Oops, something went wrong.