From e056281bf4a6d0681bf707519e8a82150779df07 Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Thu, 15 Oct 2020 17:39:09 +0200 Subject: [PATCH] feat: delete version testing (#107) * feat: delete version testing * fix: deleting legacy test * fix: lint * fix: review --- package.json | 4 +-- plugin.js | 75 +++++++++++------------------------------------- test/esm/esm.mjs | 21 -------------- test/test.js | 68 ------------------------------------------- 4 files changed, 18 insertions(+), 150 deletions(-) diff --git a/package.json b/package.json index 9ab3a08..2c80bca 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,5 @@ "tsd": "^0.11.0", "typescript": "^3.8.2" }, - "dependencies": { - "semver": "^7.3.2" - } + "dependencies": {} } diff --git a/plugin.js b/plugin.js index 29cf836..7e41cf9 100644 --- a/plugin.js +++ b/plugin.js @@ -1,21 +1,21 @@ 'use strict' -const semver = require('semver') -const console = require('console') const extractPluginName = require('./stackParser') -const { join, dirname } = require('path') let count = 0 function plugin (fn, options = {}) { let autoName = false - if (typeof fn.default !== 'undefined') { // Support for 'export default' behaviour in transpiled ECMAScript module + if (typeof fn.default !== 'undefined') { + // Support for 'export default' behaviour in transpiled ECMAScript module fn = fn.default } if (typeof fn !== 'function') { - throw new TypeError(`fastify-plugin expects a function, instead got a '${typeof fn}'`) + throw new TypeError( + `fastify-plugin expects a function, instead got a '${typeof fn}'` + ) } fn[Symbol.for('skip-override')] = true @@ -23,25 +23,29 @@ function plugin (fn, options = {}) { const pluginName = (options && options.name) || checkName(fn) if (typeof options === 'string') { - checkVersion(options, pluginName) - options = {} + options = { + fastify: options + } } - if (typeof options !== 'object' || Array.isArray(options) || options === null) { + if ( + typeof options !== 'object' || + Array.isArray(options) || + options === null + ) { throw new TypeError('The options object should be an object') } + if (options.fastify !== undefined && typeof options.fastify !== 'string') { + throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof options.fastify}'`) + } + if (!options.name) { autoName = true options.name = pluginName + '-auto-' + count++ } fn[Symbol.for('fastify.display-name')] = options.name - - if (options.fastify) { - checkVersion(options.fastify, pluginName) - } - fn[Symbol.for('plugin-meta')] = options // Faux modules support @@ -77,50 +81,5 @@ function toCamelCase (name) { return newName } -function resolvePkgPath (mainFilename) { - return join(dirname(require.resolve('fastify', { paths: [mainFilename] })), 'package.json') -} - -function tryGetPath (p) { - var pkgPath - try { - pkgPath = resolvePkgPath(p) - } catch (_) { - } - return pkgPath -} - -function checkVersion (version, pluginName) { - if (typeof version !== 'string') { - throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof version}'`) - } - - // TODO refactor this check and move it inside Fastify itself. https://github.com/fastify/fastify/issues/2507 - var fastifyVersion - var pkgPath - if (require.main && require.main.filename) { - // We need to dynamically compute this to support yarn pnp - pkgPath = tryGetPath(require.main.filename) - } - if (!pkgPath && process.argv[1]) { - // We need this to support native ESM context - pkgPath = tryGetPath(process.argv[1]) - } - if (!pkgPath) { - // In bundlers, there is no require.main.filename so we go ahead and require directly - pkgPath = 'fastify/package.json' - } - - try { - fastifyVersion = semver.coerce(require(pkgPath).version) - } catch (_) { - console.info('fastify not found, proceeding anyway') - } - - if (fastifyVersion && !semver.satisfies(fastifyVersion, version)) { - throw new Error(`fastify-plugin: ${pluginName} - expected '${version}' fastify version, '${fastifyVersion}' is installed`) - } -} - plugin.default = plugin module.exports = plugin diff --git a/test/esm/esm.mjs b/test/esm/esm.mjs index b76d934..3240116 100644 --- a/test/esm/esm.mjs +++ b/test/esm/esm.mjs @@ -1,7 +1,4 @@ import t from 'tap' -import { readFileSync } from 'fs' -import { resolve, dirname } from 'path' -import { fileURLToPath } from 'url' import fp from '../../plugin.js' @@ -14,21 +11,3 @@ t.test('esm base support', async t => { t.end() }) - -t.test('esm support: should throw if the fastify version does not satisfies the plugin requested version', t => { - t.plan(1) - - function plugin (fastify, opts, next) { - next() - } - - const packageJson = JSON.parse(readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), '../../node_modules/fastify/package.json'))) - - const v = packageJson.version.replace(/-(rc|alpha)\.\d+/, '') - try { - fp(plugin, { fastify: '1000.1000.1000' }) - t.fail() - } catch (e) { - t.is(e.message, `fastify-plugin: plugin - expected '1000.1000.1000' fastify version, '${v}' is installed`) - } -}) diff --git a/test/test.js b/test/test.js index a7380bc..bc30539 100644 --- a/test/test.js +++ b/test/test.js @@ -5,7 +5,6 @@ const proxyquire = require('proxyquire') const test = t.test const fp = require('../plugin') const Fastify = require('fastify') -const { join, normalize } = require('path') test('fastify-plugin is a function', t => { t.plan(1) @@ -97,22 +96,6 @@ test('the options object should be an object', t => { } }) -test('should throw if the fastify version does not satisfies the plugin requested version', t => { - t.plan(1) - - function plugin (fastify, opts, next) { - next() - } - - const v = require('fastify/package.json').version.replace(/-(rc|alpha)\.\d+/, '') - try { - fp(plugin, { fastify: '1000.1000.1000' }) - t.fail() - } catch (e) { - t.is(e.message, `fastify-plugin: plugin - expected '1000.1000.1000' fastify version, '${v}' is installed`) - } -}) - test('should throw if the version number is not a string', t => { t.plan(1) @@ -124,25 +107,6 @@ test('should throw if the version number is not a string', t => { } }) -test('should not throw if fastify is not found', t => { - t.plan(1) - - const fp = proxyquire('./../plugin.js', { - [normalize(join(__dirname, '..', 'node_modules', 'fastify', 'package.json'))]: null, - console: { - info: function (msg) { - t.is(msg, 'fastify not found, proceeding anyway') - } - } - }) - - function plugin (fastify, opts, next) { - next() - } - - fp(plugin, { fastify: '>= 0' }) -}) - test('Should accept an option object', t => { t.plan(2) @@ -171,38 +135,6 @@ test('Should accept an option object and checks the version', t => { t.deepEqual(plugin[Symbol.for('plugin-meta')], opts) }) -test('should throw if the fastify version does not satisfies the plugin requested version', t => { - t.plan(1) - - function plugin (fastify, opts, next) { - next() - } - - const v = require('fastify/package.json').version.replace(/-(rc|alpha)\.\d+/, '') - try { - fp(plugin, { fastify: '1000.1000.1000' }) - t.fail() - } catch (e) { - t.is(e.message, `fastify-plugin: plugin - expected '1000.1000.1000' fastify version, '${v}' is installed`) - } -}) - -test('should throw if the fastify version does not satisfies the plugin requested version - plugin name', t => { - t.plan(1) - - function plugin (fastify, opts, next) { - next() - } - - const v = require('fastify/package.json').version.replace(/-(rc|alpha)\.\d+/, '') - try { - fp(plugin, { name: 'this-is-an-awesome-name', fastify: '1000.1000.1000' }) - t.fail() - } catch (e) { - t.is(e.message, `fastify-plugin: this-is-an-awesome-name - expected '1000.1000.1000' fastify version, '${v}' is installed`) - } -}) - test('should set anonymous function name to file it was called from with a counter', t => { const fp = proxyquire('../plugin.js', { stubs: {} })