Skip to content

Commit

Permalink
feat: delete version testing (#107)
Browse files Browse the repository at this point in the history
* feat: delete version testing

* fix: deleting legacy test

* fix: lint

* fix: review
  • Loading branch information
zekth authored Oct 15, 2020
1 parent 637ab99 commit e056281
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 150 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@
"tsd": "^0.11.0",
"typescript": "^3.8.2"
},
"dependencies": {
"semver": "^7.3.2"
}
"dependencies": {}
}
75 changes: 17 additions & 58 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
'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

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
Expand Down Expand Up @@ -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
21 changes: 0 additions & 21 deletions test/esm/esm.mjs
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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`)
}
})
68 changes: 0 additions & 68 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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: {} })

Expand Down

0 comments on commit e056281

Please sign in to comment.