lint: Run eslint --fix on bin/ and tests/

This commit is contained in:
Richard Hansen 2020-11-23 13:21:51 -05:00 committed by John McLear
parent 0625739cb8
commit b8d07a42eb
78 changed files with 4319 additions and 4599 deletions

View file

@ -1,6 +1,6 @@
/* global __dirname, __filename, afterEach, beforeEach, describe, it, process, require */
function m(mod) { return __dirname + '/../../../src/' + mod; }
function m(mod) { return `${__dirname}/../../../src/${mod}`; }
const assert = require('assert').strict;
const common = require('../common');
@ -10,13 +10,13 @@ const sinon = require(m('node_modules/sinon'));
const logger = common.logger;
describe(__filename, function() {
describe(__filename, function () {
const hookName = 'testHook';
const hookFnName = 'testPluginFileName:testHookFunctionName';
let testHooks; // Convenience shorthand for plugins.hooks[hookName].
let hook; // Convenience shorthand for plugins.hooks[hookName][0].
beforeEach(async function() {
beforeEach(async function () {
// Make sure these are not already set so that we don't accidentally step on someone else's
// toes:
assert(plugins.hooks[hookName] == null);
@ -30,24 +30,22 @@ describe(__filename, function() {
testHooks = plugins.hooks[hookName];
});
afterEach(async function() {
afterEach(async function () {
sinon.restore();
delete plugins.hooks[hookName];
delete hooks.deprecationNotices[hookName];
delete hooks.exportedForTestingOnly.deprecationWarned[hookFnName];
});
const makeHook = (ret) => {
return {
hook_name: hookName,
// Many tests will likely want to change this. Unfortunately, we can't use a convenience
// wrapper like `(...args) => hookFn(..args)` because the hooks look at Function.length and
// change behavior depending on the number of parameters.
hook_fn: (hn, ctx, cb) => cb(ret),
hook_fn_name: hookFnName,
part: {plugin: 'testPluginName'},
};
};
const makeHook = (ret) => ({
hook_name: hookName,
// Many tests will likely want to change this. Unfortunately, we can't use a convenience
// wrapper like `(...args) => hookFn(..args)` because the hooks look at Function.length and
// change behavior depending on the number of parameters.
hook_fn: (hn, ctx, cb) => cb(ret),
hook_fn_name: hookFnName,
part: {plugin: 'testPluginName'},
});
// Hook functions that should work for both synchronous and asynchronous hooks.
const supportedSyncHookFunctions = [
@ -95,30 +93,30 @@ describe(__filename, function() {
},
];
describe('callHookFnSync', function() {
describe('callHookFnSync', function () {
const callHookFnSync = hooks.exportedForTestingOnly.callHookFnSync; // Convenience shorthand.
describe('basic behavior', function() {
it('passes hook name', async function() {
describe('basic behavior', function () {
it('passes hook name', async function () {
hook.hook_fn = (hn) => { assert.equal(hn, hookName); };
callHookFnSync(hook);
});
it('passes context', async function() {
it('passes context', async function () {
for (const val of ['value', null, undefined]) {
hook.hook_fn = (hn, ctx) => { assert.equal(ctx, val); };
callHookFnSync(hook, val);
}
});
it('returns the value provided to the callback', async function() {
it('returns the value provided to the callback', async function () {
for (const val of ['value', null, undefined]) {
hook.hook_fn = (hn, ctx, cb) => { cb(ctx); };
assert.equal(callHookFnSync(hook, val), val);
}
});
it('returns the value returned by the hook function', async function() {
it('returns the value returned by the hook function', async function () {
for (const val of ['value', null, undefined]) {
// Must not have the cb parameter otherwise returning undefined will error.
hook.hook_fn = (hn, ctx) => ctx;
@ -126,17 +124,17 @@ describe(__filename, function() {
}
});
it('does not catch exceptions', async function() {
it('does not catch exceptions', async function () {
hook.hook_fn = () => { throw new Error('test exception'); };
assert.throws(() => callHookFnSync(hook), {message: 'test exception'});
});
it('callback returns undefined', async function() {
it('callback returns undefined', async function () {
hook.hook_fn = (hn, ctx, cb) => { assert.equal(cb('foo'), undefined); };
callHookFnSync(hook);
});
it('checks for deprecation', async function() {
it('checks for deprecation', async function () {
sinon.stub(console, 'warn');
hooks.deprecationNotices[hookName] = 'test deprecation';
callHookFnSync(hook);
@ -146,9 +144,9 @@ describe(__filename, function() {
});
});
describe('supported hook function styles', function() {
describe('supported hook function styles', function () {
for (const tc of supportedSyncHookFunctions) {
it(tc.name, async function() {
it(tc.name, async function () {
sinon.stub(console, 'warn');
sinon.stub(console, 'error');
hook.hook_fn = tc.fn;
@ -164,7 +162,7 @@ describe(__filename, function() {
}
});
describe('bad hook function behavior (other than double settle)', function() {
describe('bad hook function behavior (other than double settle)', function () {
const promise1 = Promise.resolve('val1');
const promise2 = Promise.resolve('val2');
@ -192,7 +190,7 @@ describe(__filename, function() {
];
for (const tc of testCases) {
it(tc.name, async function() {
it(tc.name, async function () {
sinon.stub(console, 'error');
hook.hook_fn = tc.fn;
assert.equal(callHookFnSync(hook), tc.wantVal);
@ -204,8 +202,8 @@ describe(__filename, function() {
// Test various ways a hook might attempt to settle twice. (Examples: call the callback a second
// time, or call the callback and then return a value.)
describe('bad hook function behavior (double settle)', function() {
beforeEach(function() {
describe('bad hook function behavior (double settle)', function () {
beforeEach(function () {
sinon.stub(console, 'error');
});
@ -243,7 +241,7 @@ describe(__filename, function() {
// settle would complicate the tests, and it is sufficient to test only double settles.
if (step1.async && step2.async) continue;
it(`${step1.name} then ${step2.name} (diff. outcomes) -> log+throw`, async function() {
it(`${step1.name} then ${step2.name} (diff. outcomes) -> log+throw`, async function () {
hook.hook_fn = (hn, ctx, cb) => {
step1.fn(cb, new Error(ctx.ret1), ctx.ret1);
return step2.fn(cb, new Error(ctx.ret2), ctx.ret2);
@ -306,7 +304,7 @@ describe(__filename, function() {
// doesn't, so skip those cases.
if (step1.rejects !== step2.rejects) continue;
it(`${step1.name} then ${step2.name} (same outcome) -> only log`, async function() {
it(`${step1.name} then ${step2.name} (same outcome) -> only log`, async function () {
const err = new Error('val');
hook.hook_fn = (hn, ctx, cb) => {
step1.fn(cb, err, 'val');
@ -329,66 +327,66 @@ describe(__filename, function() {
});
});
describe('hooks.callAll', function() {
describe('basic behavior', function() {
it('calls all in order', async function() {
describe('hooks.callAll', function () {
describe('basic behavior', function () {
it('calls all in order', async function () {
testHooks.length = 0;
testHooks.push(makeHook(1), makeHook(2), makeHook(3));
assert.deepEqual(hooks.callAll(hookName), [1, 2, 3]);
});
it('passes hook name', async function() {
it('passes hook name', async function () {
hook.hook_fn = (hn) => { assert.equal(hn, hookName); };
hooks.callAll(hookName);
});
it('undefined context -> {}', async function() {
it('undefined context -> {}', async function () {
hook.hook_fn = (hn, ctx) => { assert.deepEqual(ctx, {}); };
hooks.callAll(hookName);
});
it('null context -> {}', async function() {
it('null context -> {}', async function () {
hook.hook_fn = (hn, ctx) => { assert.deepEqual(ctx, {}); };
hooks.callAll(hookName, null);
});
it('context unmodified', async function() {
it('context unmodified', async function () {
const wantContext = {};
hook.hook_fn = (hn, ctx) => { assert.equal(ctx, wantContext); };
hooks.callAll(hookName, wantContext);
});
});
describe('result processing', function() {
it('no registered hooks (undefined) -> []', async function() {
describe('result processing', function () {
it('no registered hooks (undefined) -> []', async function () {
delete plugins.hooks.testHook;
assert.deepEqual(hooks.callAll(hookName), []);
});
it('no registered hooks (empty list) -> []', async function() {
it('no registered hooks (empty list) -> []', async function () {
testHooks.length = 0;
assert.deepEqual(hooks.callAll(hookName), []);
});
it('flattens one level', async function() {
it('flattens one level', async function () {
testHooks.length = 0;
testHooks.push(makeHook(1), makeHook([2]), makeHook([[3]]));
assert.deepEqual(hooks.callAll(hookName), [1, 2, [3]]);
});
it('filters out undefined', async function() {
it('filters out undefined', async function () {
testHooks.length = 0;
testHooks.push(makeHook(), makeHook([2]), makeHook([[3]]));
assert.deepEqual(hooks.callAll(hookName), [2, [3]]);
});
it('preserves null', async function() {
it('preserves null', async function () {
testHooks.length = 0;
testHooks.push(makeHook(null), makeHook([2]), makeHook([[3]]));
assert.deepEqual(hooks.callAll(hookName), [null, 2, [3]]);
});
it('all undefined -> []', async function() {
it('all undefined -> []', async function () {
testHooks.length = 0;
testHooks.push(makeHook(), makeHook());
assert.deepEqual(hooks.callAll(hookName), []);
@ -396,23 +394,23 @@ describe(__filename, function() {
});
});
describe('callHookFnAsync', function() {
describe('callHookFnAsync', function () {
const callHookFnAsync = hooks.exportedForTestingOnly.callHookFnAsync; // Convenience shorthand.
describe('basic behavior', function() {
it('passes hook name', async function() {
describe('basic behavior', function () {
it('passes hook name', async function () {
hook.hook_fn = (hn) => { assert.equal(hn, hookName); };
await callHookFnAsync(hook);
});
it('passes context', async function() {
it('passes context', async function () {
for (const val of ['value', null, undefined]) {
hook.hook_fn = (hn, ctx) => { assert.equal(ctx, val); };
await callHookFnAsync(hook, val);
}
});
it('returns the value provided to the callback', async function() {
it('returns the value provided to the callback', async function () {
for (const val of ['value', null, undefined]) {
hook.hook_fn = (hn, ctx, cb) => { cb(ctx); };
assert.equal(await callHookFnAsync(hook, val), val);
@ -420,7 +418,7 @@ describe(__filename, function() {
}
});
it('returns the value returned by the hook function', async function() {
it('returns the value returned by the hook function', async function () {
for (const val of ['value', null, undefined]) {
// Must not have the cb parameter otherwise returning undefined will never resolve.
hook.hook_fn = (hn, ctx) => ctx;
@ -429,27 +427,27 @@ describe(__filename, function() {
}
});
it('rejects if it throws an exception', async function() {
it('rejects if it throws an exception', async function () {
hook.hook_fn = () => { throw new Error('test exception'); };
await assert.rejects(callHookFnAsync(hook), {message: 'test exception'});
});
it('rejects if rejected Promise passed to callback', async function() {
it('rejects if rejected Promise passed to callback', async function () {
hook.hook_fn = (hn, ctx, cb) => cb(Promise.reject(new Error('test exception')));
await assert.rejects(callHookFnAsync(hook), {message: 'test exception'});
});
it('rejects if rejected Promise returned', async function() {
it('rejects if rejected Promise returned', async function () {
hook.hook_fn = (hn, ctx, cb) => Promise.reject(new Error('test exception'));
await assert.rejects(callHookFnAsync(hook), {message: 'test exception'});
});
it('callback returns undefined', async function() {
it('callback returns undefined', async function () {
hook.hook_fn = (hn, ctx, cb) => { assert.equal(cb('foo'), undefined); };
await callHookFnAsync(hook);
});
it('checks for deprecation', async function() {
it('checks for deprecation', async function () {
sinon.stub(console, 'warn');
hooks.deprecationNotices[hookName] = 'test deprecation';
await callHookFnAsync(hook);
@ -459,7 +457,7 @@ describe(__filename, function() {
});
});
describe('supported hook function styles', function() {
describe('supported hook function styles', function () {
const supportedHookFunctions = supportedSyncHookFunctions.concat([
{
name: 'legacy async cb',
@ -541,7 +539,7 @@ describe(__filename, function() {
]);
for (const tc of supportedSyncHookFunctions.concat(supportedHookFunctions)) {
it(tc.name, async function() {
it(tc.name, async function () {
sinon.stub(console, 'warn');
sinon.stub(console, 'error');
hook.hook_fn = tc.fn;
@ -559,8 +557,8 @@ describe(__filename, function() {
// Test various ways a hook might attempt to settle twice. (Examples: call the callback a second
// time, or call the callback and then return a value.)
describe('bad hook function behavior (double settle)', function() {
beforeEach(function() {
describe('bad hook function behavior (double settle)', function () {
beforeEach(function () {
sinon.stub(console, 'error');
});
@ -688,7 +686,7 @@ describe(__filename, function() {
// There can't be a second step if the first step is to return or throw.
if (step1.name.startsWith('return ') || step1.name === 'throw') continue;
for (const step2 of behaviors) {
it(`${step1.name} then ${step2.name} (diff. outcomes) -> log+throw`, async function() {
it(`${step1.name} then ${step2.name} (diff. outcomes) -> log+throw`, async function () {
hook.hook_fn = (hn, ctx, cb) => {
step1.fn(cb, new Error(ctx.ret1), ctx.ret1);
return step2.fn(cb, new Error(ctx.ret2), ctx.ret2);
@ -729,8 +727,8 @@ describe(__filename, function() {
}
}
assert.equal(console.error.callCount, 1,
'Got errors:\n' +
console.error.getCalls().map((call) => call.args[0]).join('\n'));
`Got errors:\n${
console.error.getCalls().map((call) => call.args[0]).join('\n')}`);
assert.match(console.error.getCall(0).args[0], /DOUBLE SETTLE BUG/);
assert(asyncErr instanceof Error);
assert.match(asyncErr.message, /DOUBLE SETTLE BUG/);
@ -741,7 +739,7 @@ describe(__filename, function() {
// doesn't, so skip those cases.
if (step1.rejects !== step2.rejects) continue;
it(`${step1.name} then ${step2.name} (same outcome) -> only log`, async function() {
it(`${step1.name} then ${step2.name} (same outcome) -> only log`, async function () {
const err = new Error('val');
hook.hook_fn = (hn, ctx, cb) => {
step1.fn(cb, err, 'val');
@ -764,9 +762,9 @@ describe(__filename, function() {
});
});
describe('hooks.aCallAll', function() {
describe('basic behavior', function() {
it('calls all asynchronously, returns values in order', async function() {
describe('hooks.aCallAll', function () {
describe('basic behavior', function () {
it('calls all asynchronously, returns values in order', async function () {
testHooks.length = 0; // Delete the boilerplate hook -- this test doesn't use it.
let nextIndex = 0;
const hookPromises = [];
@ -800,35 +798,35 @@ describe(__filename, function() {
assert.deepEqual(await p, [0, 1]);
});
it('passes hook name', async function() {
it('passes hook name', async function () {
hook.hook_fn = async (hn) => { assert.equal(hn, hookName); };
await hooks.aCallAll(hookName);
});
it('undefined context -> {}', async function() {
it('undefined context -> {}', async function () {
hook.hook_fn = async (hn, ctx) => { assert.deepEqual(ctx, {}); };
await hooks.aCallAll(hookName);
});
it('null context -> {}', async function() {
it('null context -> {}', async function () {
hook.hook_fn = async (hn, ctx) => { assert.deepEqual(ctx, {}); };
await hooks.aCallAll(hookName, null);
});
it('context unmodified', async function() {
it('context unmodified', async function () {
const wantContext = {};
hook.hook_fn = async (hn, ctx) => { assert.equal(ctx, wantContext); };
await hooks.aCallAll(hookName, wantContext);
});
});
describe('aCallAll callback', function() {
it('exception in callback rejects', async function() {
describe('aCallAll callback', function () {
it('exception in callback rejects', async function () {
const p = hooks.aCallAll(hookName, {}, () => { throw new Error('test exception'); });
await assert.rejects(p, {message: 'test exception'});
});
it('propagates error on exception', async function() {
it('propagates error on exception', async function () {
hook.hook_fn = () => { throw new Error('test exception'); };
await hooks.aCallAll(hookName, {}, (err) => {
assert(err instanceof Error);
@ -836,54 +834,54 @@ describe(__filename, function() {
});
});
it('propagages null error on success', async function() {
it('propagages null error on success', async function () {
await hooks.aCallAll(hookName, {}, (err) => {
assert(err == null, `got non-null error: ${err}`);
});
});
it('propagages results on success', async function() {
it('propagages results on success', async function () {
hook.hook_fn = () => 'val';
await hooks.aCallAll(hookName, {}, (err, results) => {
assert.deepEqual(results, ['val']);
});
});
it('returns callback return value', async function() {
it('returns callback return value', async function () {
assert.equal(await hooks.aCallAll(hookName, {}, () => 'val'), 'val');
});
});
describe('result processing', function() {
it('no registered hooks (undefined) -> []', async function() {
describe('result processing', function () {
it('no registered hooks (undefined) -> []', async function () {
delete plugins.hooks[hookName];
assert.deepEqual(await hooks.aCallAll(hookName), []);
});
it('no registered hooks (empty list) -> []', async function() {
it('no registered hooks (empty list) -> []', async function () {
testHooks.length = 0;
assert.deepEqual(await hooks.aCallAll(hookName), []);
});
it('flattens one level', async function() {
it('flattens one level', async function () {
testHooks.length = 0;
testHooks.push(makeHook(1), makeHook([2]), makeHook([[3]]));
assert.deepEqual(await hooks.aCallAll(hookName), [1, 2, [3]]);
});
it('filters out undefined', async function() {
it('filters out undefined', async function () {
testHooks.length = 0;
testHooks.push(makeHook(), makeHook([2]), makeHook([[3]]), makeHook(Promise.resolve()));
assert.deepEqual(await hooks.aCallAll(hookName), [2, [3]]);
});
it('preserves null', async function() {
it('preserves null', async function () {
testHooks.length = 0;
testHooks.push(makeHook(null), makeHook([2]), makeHook(Promise.resolve(null)));
assert.deepEqual(await hooks.aCallAll(hookName), [null, 2, null]);
});
it('all undefined -> []', async function() {
it('all undefined -> []', async function () {
testHooks.length = 0;
testHooks.push(makeHook(), makeHook(Promise.resolve()));
assert.deepEqual(await hooks.aCallAll(hookName), []);