diff --git a/.eslintrc.js b/.eslintrc.js index 2cab26467..38393f4fd 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -10,6 +10,8 @@ module.exports = { 'comma-dangle': ['error', 'never'], 'consistent-return': 'off', 'func-names': 'off', + 'import/no-extraneous-dependencies': 'off', + 'import/no-unresolved': 'off', 'max-len': 'off', 'no-bitwise': 'off', 'no-continue': 'off', diff --git a/test/fixtures/blueprint-data.js b/test/fixtures/blueprint-data.js index 013dec488..c7d7ac26b 100644 --- a/test/fixtures/blueprint-data.js +++ b/test/fixtures/blueprint-data.js @@ -1,5 +1,3 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. module.exports = { './test/fixtures/multiple-examples.apib': { raw: 'FORMAT: 1A\n\n# Machines API\n\n# Group Machines\n\n# Machines collection [/machines/{id}]\n + Parameters\n - id (number, `1`)\n\n## Get Machines [GET]\n\n- Request (application/json)\n + Parameters\n - id (number, `2`)\n\n- Response 200 (application/json; charset=utf-8)\n\n [\n {\n "type": "bulldozer",\n "name": "willy"\n }\n ]\n\n- Request (application/json)\n + Parameters\n - id (number, `3`)\n\n- Response 200 (application/json; charset=utf-8)\n\n [\n {\n "type": "bulldozer",\n "name": "willy"\n }\n ]\n', diff --git a/test/fixtures/groupless-names.js b/test/fixtures/groupless-names.js index 3f4aab5d3..38c985d2f 100644 --- a/test/fixtures/groupless-names.js +++ b/test/fixtures/groupless-names.js @@ -1,11 +1,12 @@ -var hooks = require('hooks'); -var before = hooks.before; -var after = hooks.after; +const hooks = require('hooks'); -after(' > Machines collection > Get Machines', function(transaction){ - transaction['fail'] = 'failed in sandboxed hook'; +const before = hooks.before; +const after = hooks.after; + +after(' > Machines collection > Get Machines', (transaction) => { + transaction.fail = 'failed in sandboxed hook'; }); -before(' > Machines collection > Get Machines', function(transaction){ - transaction['fail'] = 'failed in sandboxed hook'; -}); \ No newline at end of file +before(' > Machines collection > Get Machines', (transaction) => { + transaction.fail = 'failed in sandboxed hook'; +}); diff --git a/test/fixtures/hooks-log.coffee b/test/fixtures/hooks-log.coffee new file mode 100644 index 000000000..de31eb9d6 --- /dev/null +++ b/test/fixtures/hooks-log.coffee @@ -0,0 +1,8 @@ +{before, after, log} = require 'hooks' + +before "Machines > Machines collection > Get Machines", (transaction) -> + log {err: 'Error object!'} + log true + +after "Machines > Machines collection > Get Machines", (transaction) -> + log "using hooks.log to debug" diff --git a/test/fixtures/hooks-log.js b/test/fixtures/hooks-log.js deleted file mode 100644 index f68f3feb7..000000000 --- a/test/fixtures/hooks-log.js +++ /dev/null @@ -1,15 +0,0 @@ -/* eslint-disable - import/no-extraneous-dependencies, - import/no-unresolved, - no-unused-vars, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -const { before, after, log } = require('hooks'); - -before('Machines > Machines collection > Get Machines', (transaction) => { - log({ err: 'Error object!' }); - return log(true); -}); - -after('Machines > Machines collection > Get Machines', transaction => log('using hooks.log to debug')); diff --git a/test/fixtures/multifile/multifile_hooks.coffee b/test/fixtures/multifile/multifile_hooks.coffee new file mode 100644 index 000000000..f680729d0 --- /dev/null +++ b/test/fixtures/multifile/multifile_hooks.coffee @@ -0,0 +1,10 @@ +{after} = require 'hooks' + +after "Name API > /name > GET", (transaction) -> + console.log "after name" + +after "Greeting API > /greeting > GET", (transaction) -> + console.log "after greeting" + +after "Message API > /message > GET", (transaction) -> + console.log "after message" diff --git a/test/fixtures/multifile/multifile_hooks.js b/test/fixtures/multifile/multifile_hooks.js deleted file mode 100644 index 15c50f3d4..000000000 --- a/test/fixtures/multifile/multifile_hooks.js +++ /dev/null @@ -1,15 +0,0 @@ -/* eslint-disable - import/no-extraneous-dependencies, - import/no-unresolved, - no-console, - no-unused-vars, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -const { after } = require('hooks'); - -after('Name API > /name > GET', transaction => console.log('after name')); - -after('Greeting API > /greeting > GET', transaction => console.log('after greeting')); - -after('Message API > /message > GET', transaction => console.log('after message')); diff --git a/test/fixtures/regression-152.coffee b/test/fixtures/regression-152.coffee new file mode 100644 index 000000000..c6076e0a7 --- /dev/null +++ b/test/fixtures/regression-152.coffee @@ -0,0 +1,17 @@ +hooks = require 'hooks' + +# New hooks helper function +hooks.beforeEach = (hookFn) -> + hooks.beforeAll (done) -> + for transactionKey, transaction of hooks.transactions or {} + hooks.beforeHooks[transaction.name] ?= [] + hooks.beforeHooks[transaction.name].unshift hookFn + done() + +hooks.beforeEach (transaction) -> + # add query parameter to each transaction here + paramToAdd = "api-key=23456" + if transaction.fullPath.indexOf('?') > -1 + transaction.fullPath += "&" + paramToAdd + else + transaction.fullPath += "?" + paramToAdd diff --git a/test/fixtures/regression-152.js b/test/fixtures/regression-152.js deleted file mode 100644 index 15292e3b0..000000000 --- a/test/fixtures/regression-152.js +++ /dev/null @@ -1,31 +0,0 @@ -/* eslint-disable - guard-for-in, - import/no-extraneous-dependencies, - import/no-unresolved, - no-return-assign, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -const hooks = require('hooks'); - -// New hooks helper function -hooks.beforeEach = hookFn => - hooks.beforeAll((done) => { - const object = hooks.transactions || {}; - for (const transactionKey in object) { - const transaction = object[transactionKey]; - if (hooks.beforeHooks[transaction.name] == null) { hooks.beforeHooks[transaction.name] = []; } - hooks.beforeHooks[transaction.name].unshift(hookFn); - } - return done(); - }) -; - -hooks.beforeEach((transaction) => { - // add query parameter to each transaction here - const paramToAdd = 'api-key=23456'; - if (transaction.fullPath.indexOf('?') > -1) { - return transaction.fullPath += `&${paramToAdd}`; - } - return transaction.fullPath += `?${paramToAdd}`; -}); diff --git a/test/fixtures/response/empty-body-hooks.js b/test/fixtures/response/empty-body-hooks.js index 72cee00de..5256ef7d3 100644 --- a/test/fixtures/response/empty-body-hooks.js +++ b/test/fixtures/response/empty-body-hooks.js @@ -1,7 +1,6 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); - -hooks.beforeEachValidation(function (transaction, done) { +hooks.beforeEachValidation((transaction, done) => { if (transaction.real.body) { transaction.fail = 'The response body must be empty'; } diff --git a/test/fixtures/sandboxed-hook.js b/test/fixtures/sandboxed-hook.js index 592c05bd2..c60257a9e 100644 --- a/test/fixtures/sandboxed-hook.js +++ b/test/fixtures/sandboxed-hook.js @@ -1,3 +1,3 @@ -after('Machines > Machines collection > Get Machines', function(transaction){ - transaction['fail'] = 'failed in sandboxed hook'; -}); \ No newline at end of file +after('Machines > Machines collection > Get Machines', (transaction) => { + transaction.fail = 'failed in sandboxed hook'; +}); diff --git a/test/fixtures/sandboxed-hooks-log.js b/test/fixtures/sandboxed-hooks-log.js index 65f0fb494..cffd91a72 100644 --- a/test/fixtures/sandboxed-hooks-log.js +++ b/test/fixtures/sandboxed-hooks-log.js @@ -1,7 +1,9 @@ -before("Machines > Machines collection > Get Machines", function (transaction) { +/* eslint-disable no-undef */ + +before('Machines > Machines collection > Get Machines', () => { log('shall not print, but be present in logs'); }); -after("Machines > Machines collection > Get Machines", function (transaction) { - log("using sandboxed hooks.log"); +after('Machines > Machines collection > Get Machines', () => { + log('using sandboxed hooks.log'); }); diff --git a/test/fixtures/sanitation/any-content-guard-pattern-matching.js b/test/fixtures/sanitation/any-content-guard-pattern-matching.js index 4afcc2d56..c753dff98 100644 --- a/test/fixtures/sanitation/any-content-guard-pattern-matching.js +++ b/test/fixtures/sanitation/any-content-guard-pattern-matching.js @@ -1,23 +1,22 @@ -var hooks = require('hooks'); -var assert = require('chai').assert; -var tokenPattern = /([0-9]|[a-f]){24,}/g; +const assert = require('chai').assert; +const hooks = require('hooks'); -hooks.beforeEach(function(transaction, done) { +const tokenPattern = /([0-9]|[a-f]){24,}/g; + +hooks.beforeEach((transaction, done) => { transaction.id = transaction.id.replace(tokenPattern, 'CENSORED'); transaction.origin.resourceName = transaction.origin.resourceName.replace(tokenPattern, 'CENSORED'); done(); }); -hooks.afterEach(function(transaction, done) { +hooks.afterEach((transaction, done) => { try { - - JSON.stringify(transaction.test, function(key, value) { + JSON.stringify(transaction.test, (key, value) => { if (typeof value === 'string') { assert.notMatch(value, tokenPattern); } return value; }); - } catch (error) { transaction.fail = 'Sensitive data would be sent to Dredd reporter'; transaction.test = { @@ -25,7 +24,7 @@ hooks.afterEach(function(transaction, done) { end: transaction.test.end, duration: transaction.test.duration, startedAt: transaction.test.startedAt, - message: transaction.fail, + message: transaction.fail }; } done(); diff --git a/test/fixtures/sanitation/any-content-pattern-matching.js b/test/fixtures/sanitation/any-content-pattern-matching.js index 570a09bea..1623a4f1b 100644 --- a/test/fixtures/sanitation/any-content-pattern-matching.js +++ b/test/fixtures/sanitation/any-content-pattern-matching.js @@ -1,14 +1,15 @@ -var hooks = require('hooks'); -var tokenPattern = /([0-9]|[a-f]){24,}/g; +const hooks = require('hooks'); -hooks.beforeEach(function(transaction, done) { +const tokenPattern = /([0-9]|[a-f]){24,}/g; + +hooks.beforeEach((transaction, done) => { transaction.id = transaction.id.replace(tokenPattern, 'CENSORED'); transaction.origin.resourceName = transaction.origin.resourceName.replace(tokenPattern, 'CENSORED'); done(); }); -hooks.afterEach(function(transaction, done) { - var test = JSON.stringify(transaction.test, function(key, value) { +hooks.afterEach((transaction, done) => { + const test = JSON.stringify(transaction.test, (key, value) => { if (value.replace) { return value.replace(tokenPattern, 'CENSORED'); } diff --git a/test/fixtures/sanitation/entire-request-body.js b/test/fixtures/sanitation/entire-request-body.js index 0d6776449..71defdabf 100644 --- a/test/fixtures/sanitation/entire-request-body.js +++ b/test/fixtures/sanitation/entire-request-body.js @@ -1,6 +1,6 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { +hooks.after('Resource > Update Resource', (transaction, done) => { transaction.test.request.body = ''; done(); }); diff --git a/test/fixtures/sanitation/entire-response-body.js b/test/fixtures/sanitation/entire-response-body.js index 5fc334786..dfca44abe 100644 --- a/test/fixtures/sanitation/entire-response-body.js +++ b/test/fixtures/sanitation/entire-response-body.js @@ -1,6 +1,6 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { +hooks.after('Resource > Update Resource', (transaction, done) => { transaction.test.actual.body = ''; transaction.test.expected.body = ''; transaction.test.expected.bodySchema = ''; diff --git a/test/fixtures/sanitation/plain-text-response-body.js b/test/fixtures/sanitation/plain-text-response-body.js index 8fd7f667b..f03025f3b 100644 --- a/test/fixtures/sanitation/plain-text-response-body.js +++ b/test/fixtures/sanitation/plain-text-response-body.js @@ -1,8 +1,9 @@ -var hooks = require('hooks'); -var tokenPattern = /([0-9]|[a-f]){24,}/g; +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { - var body; +const tokenPattern = /([0-9]|[a-f]){24,}/g; + +hooks.after('Resource > Update Resource', (transaction, done) => { + let body; body = transaction.test.actual.body; transaction.test.actual.body = body.replace(tokenPattern, '--- CENSORED ---'); @@ -10,7 +11,7 @@ hooks.after('Resource > Update Resource', function(transaction, done) { body = transaction.test.expected.body; transaction.test.expected.body = body.replace(tokenPattern, '--- CENSORED ---'); - // sanitation of diff in the patch format + // Sanitation of diff in the patch format delete transaction.test.results.body.results.rawData; done(); }); diff --git a/test/fixtures/sanitation/request-body-attribute.js b/test/fixtures/sanitation/request-body-attribute.js index 8b3a3c9d2..fdd3e5e38 100644 --- a/test/fixtures/sanitation/request-body-attribute.js +++ b/test/fixtures/sanitation/request-body-attribute.js @@ -1,7 +1,7 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { - var body = JSON.parse(transaction.test.request.body); +hooks.after('Resource > Update Resource', (transaction, done) => { + const body = JSON.parse(transaction.test.request.body); delete body.token; transaction.test.request.body = JSON.stringify(body); done(); diff --git a/test/fixtures/sanitation/request-headers.js b/test/fixtures/sanitation/request-headers.js index 38cda4f04..94744628b 100644 --- a/test/fixtures/sanitation/request-headers.js +++ b/test/fixtures/sanitation/request-headers.js @@ -1,9 +1,9 @@ -var hooks = require('hooks'); -var caseless = require('caseless'); +const caseless = require('caseless'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { - var headers = transaction.test.request.headers; - var name = caseless(headers).has('Authorization'); +hooks.after('Resource > Update Resource', (transaction, done) => { + const headers = transaction.test.request.headers; + const name = caseless(headers).has('Authorization'); delete headers[name]; transaction.test.request.headers = headers; done(); diff --git a/test/fixtures/sanitation/response-body-attribute.js b/test/fixtures/sanitation/response-body-attribute.js index 3884b4fa5..ddac9d7f6 100644 --- a/test/fixtures/sanitation/response-body-attribute.js +++ b/test/fixtures/sanitation/response-body-attribute.js @@ -1,8 +1,8 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { - // sanitation of the attribute in body - var body; +hooks.after('Resource > Update Resource', (transaction, done) => { + // Sanitation of the attribute in body + let body; body = JSON.parse(transaction.test.actual.body); delete body.token; @@ -12,24 +12,24 @@ hooks.after('Resource > Update Resource', function(transaction, done) { delete body.token; transaction.test.expected.body = JSON.stringify(body); - // sanitation of the attribute in JSON Schema - var bodySchema = JSON.parse(transaction.test.expected.bodySchema); + // Sanitation of the attribute in JSON Schema + const bodySchema = JSON.parse(transaction.test.expected.bodySchema); delete bodySchema.properties.token; transaction.test.expected.bodySchema = JSON.stringify(bodySchema); - // sanitation of the attribute in validation output - var validationOutput = transaction.test.results.body; + // Sanitation of the attribute in validation output + const validationOutput = transaction.test.results.body; - var errors = []; - for (var i = 0; i < validationOutput.results.length; i++) { + const errors = []; + for (let i = 0; i < validationOutput.results.length; i++) { if (validationOutput.results[i].pointer !== '/token') { errors.push(validationOutput.results[i]); } } validationOutput.results = errors; - var rawData = []; - for (var i = 0; i < validationOutput.rawData.length; i++) { + const rawData = []; + for (let i = 0; i < validationOutput.rawData.length; i++) { if (validationOutput.rawData[i].property[0] !== 'token') { rawData.push(validationOutput.rawData[i]); } diff --git a/test/fixtures/sanitation/response-headers.js b/test/fixtures/sanitation/response-headers.js index 4d1f4cbaf..746cfffeb 100644 --- a/test/fixtures/sanitation/response-headers.js +++ b/test/fixtures/sanitation/response-headers.js @@ -1,9 +1,9 @@ -var hooks = require('hooks'); -var caseless = require('caseless'); +const caseless = require('caseless'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { - var headers; - var name; +hooks.after('Resource > Update Resource', (transaction, done) => { + let headers; + let name; headers = transaction.test.actual.headers; name = caseless(headers).has('authorization'); @@ -15,19 +15,19 @@ hooks.after('Resource > Update Resource', function(transaction, done) { delete headers[name]; transaction.test.expected.headers = headers; - // sanitation of the header in validation output - var validationOutput = transaction.test.results.headers; + // Sanitation of the header in validation output + const validationOutput = transaction.test.results.headers; - var errors = []; - for (var i = 0; i < validationOutput.results.length; i++) { + const errors = []; + for (let i = 0; i < validationOutput.results.length; i++) { if (validationOutput.results[i].pointer.toLowerCase() !== '/authorization') { errors.push(validationOutput.results[i]); } } validationOutput.results = errors; - var rawData = []; - for (var i = 0; i < validationOutput.rawData.length; i++) { + const rawData = []; + for (let i = 0; i < validationOutput.rawData.length; i++) { if (validationOutput.rawData[i].property[0].toLowerCase() !== 'authorization') { rawData.push(validationOutput.rawData[i]); } diff --git a/test/fixtures/sanitation/transaction-erroring-hooks.js b/test/fixtures/sanitation/transaction-erroring-hooks.js index d0befdd91..eb58dc28d 100644 --- a/test/fixtures/sanitation/transaction-erroring-hooks.js +++ b/test/fixtures/sanitation/transaction-erroring-hooks.js @@ -1,6 +1,6 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { +hooks.after('Resource > Update Resource', (transaction, done) => { JSON.parse('💥 boom 💥'); done(); }); diff --git a/test/fixtures/sanitation/transaction-marked-failed-after.js b/test/fixtures/sanitation/transaction-marked-failed-after.js index c3e69909f..681d22ded 100644 --- a/test/fixtures/sanitation/transaction-marked-failed-after.js +++ b/test/fixtures/sanitation/transaction-marked-failed-after.js @@ -1,6 +1,6 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { +hooks.after('Resource > Update Resource', (transaction, done) => { transaction.test.request.body = ''; transaction.fail = true; done(); diff --git a/test/fixtures/sanitation/transaction-marked-failed-before.js b/test/fixtures/sanitation/transaction-marked-failed-before.js index fdffb5001..b2fab0d06 100644 --- a/test/fixtures/sanitation/transaction-marked-failed-before.js +++ b/test/fixtures/sanitation/transaction-marked-failed-before.js @@ -1,11 +1,11 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.before('Resource > Update Resource', function(transaction, done) { +hooks.before('Resource > Update Resource', (transaction, done) => { transaction.fail = true; done(); }); -hooks.after('Resource > Update Resource', function(transaction, done) { +hooks.after('Resource > Update Resource', (transaction, done) => { if (transaction.test && transaction.test.request) { transaction.test.request.body = ''; } diff --git a/test/fixtures/sanitation/transaction-marked-skipped.js b/test/fixtures/sanitation/transaction-marked-skipped.js index 4f0897512..bfd169cc6 100644 --- a/test/fixtures/sanitation/transaction-marked-skipped.js +++ b/test/fixtures/sanitation/transaction-marked-skipped.js @@ -1,11 +1,11 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.before('Resource > Update Resource', function(transaction, done) { +hooks.before('Resource > Update Resource', (transaction, done) => { transaction.skip = true; done(); }); -hooks.after('Resource > Update Resource', function(transaction, done) { +hooks.after('Resource > Update Resource', (transaction, done) => { if (transaction.test && transaction.test.request) { transaction.test.request.body = ''; } diff --git a/test/fixtures/sanitation/transaction-passing.js b/test/fixtures/sanitation/transaction-passing.js index 0d6776449..71defdabf 100644 --- a/test/fixtures/sanitation/transaction-passing.js +++ b/test/fixtures/sanitation/transaction-passing.js @@ -1,6 +1,6 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { +hooks.after('Resource > Update Resource', (transaction, done) => { transaction.test.request.body = ''; done(); }); diff --git a/test/fixtures/sanitation/transaction-secured-erroring-hooks.js b/test/fixtures/sanitation/transaction-secured-erroring-hooks.js index 25790a53d..70b202a28 100644 --- a/test/fixtures/sanitation/transaction-secured-erroring-hooks.js +++ b/test/fixtures/sanitation/transaction-secured-erroring-hooks.js @@ -1,10 +1,8 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.after('Resource > Update Resource', function(transaction, done) { +hooks.after('Resource > Update Resource', (transaction, done) => { try { - JSON.parse('💥 boom 💥'); - } catch (error) { transaction.fail = 'Unexpected exception in hooks'; transaction.test = { @@ -12,7 +10,7 @@ hooks.after('Resource > Update Resource', function(transaction, done) { end: transaction.test.end, duration: transaction.test.duration, startedAt: transaction.test.startedAt, - message: transaction.fail, + message: transaction.fail }; } done(); diff --git a/test/fixtures/sanitation/uri-parameters.js b/test/fixtures/sanitation/uri-parameters.js index 39239e484..7fff3632b 100644 --- a/test/fixtures/sanitation/uri-parameters.js +++ b/test/fixtures/sanitation/uri-parameters.js @@ -1,13 +1,14 @@ -var hooks = require('hooks'); -var tokenPattern = /([0-9]|[a-f]){24,}/g; +const hooks = require('hooks'); -hooks.beforeEach(function(transaction, done) { +const tokenPattern = /([0-9]|[a-f]){24,}/g; + +hooks.beforeEach((transaction, done) => { transaction.id = transaction.id.replace(tokenPattern, 'CENSORED'); transaction.origin.resourceName = transaction.origin.resourceName.replace(tokenPattern, 'CENSORED'); done(); }); -hooks.afterEach(function(transaction, done) { +hooks.afterEach((transaction, done) => { transaction.test.request.uri = transaction.test.request.uri.replace(tokenPattern, 'CENSORED'); done(); }); diff --git a/test/fixtures/scripts/dummy-server-crash.coffee b/test/fixtures/scripts/dummy-server-crash.coffee new file mode 100644 index 000000000..e25c37f9a --- /dev/null +++ b/test/fixtures/scripts/dummy-server-crash.coffee @@ -0,0 +1,11 @@ +express = require 'express' +app = express() + +app.get '/machines', (req, res) -> + res.json [{type: 'bulldozer', name: 'willy'}] + +app.get '/machines/:name', (req, res) -> + process.exit 1 + +app.listen process.argv[2], -> + console.log "Dummy server listening on port #{process.argv[2]}!" diff --git a/test/fixtures/scripts/dummy-server-crash.js b/test/fixtures/scripts/dummy-server-crash.js deleted file mode 100644 index 8f5601b2c..000000000 --- a/test/fixtures/scripts/dummy-server-crash.js +++ /dev/null @@ -1,18 +0,0 @@ -/* eslint-disable - no-console, - no-unused-vars, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. - -const express = require('express'); - -const app = express(); - - -app.get('/machines', (req, res) => res.json([{ type: 'bulldozer', name: 'willy' }])); - -app.get('/machines/:name', (req, res) => process.exit(1)); - - -app.listen(process.argv[2], () => console.log(`Dummy server listening on port ${process.argv[2]}!`)); diff --git a/test/fixtures/scripts/dummy-server-ignore-term.coffee b/test/fixtures/scripts/dummy-server-ignore-term.coffee new file mode 100644 index 000000000..08280f0b5 --- /dev/null +++ b/test/fixtures/scripts/dummy-server-ignore-term.coffee @@ -0,0 +1,20 @@ +express = require('express') + +require('./handle-windows-sigint')() + +ignore = -> + console.log('ignoring termination') + +process.on('SIGTERM', ignore) +process.on('SIGINT', ignore) + +app = express() + +app.get '/machines', (req, res) -> + res.json [{type: 'bulldozer', name: 'willy'}] + +app.get '/machines/:name', (req, res) -> + res.json {type: 'bulldozer', name: req.params.name} + +app.listen process.argv[2], -> + console.log "Dummy server listening on port #{process.argv[2]}!" diff --git a/test/fixtures/scripts/dummy-server-ignore-term.js b/test/fixtures/scripts/dummy-server-ignore-term.js deleted file mode 100644 index c97183f2b..000000000 --- a/test/fixtures/scripts/dummy-server-ignore-term.js +++ /dev/null @@ -1,23 +0,0 @@ -/* eslint-disable - no-console, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -const express = require('express'); - -require('./handle-windows-sigint')(); - - -const ignore = () => console.log('ignoring termination'); - -process.on('SIGTERM', ignore); -process.on('SIGINT', ignore); - - -const app = express(); - -app.get('/machines', (req, res) => res.json([{ type: 'bulldozer', name: 'willy' }])); - -app.get('/machines/:name', (req, res) => res.json({ type: 'bulldozer', name: req.params.name })); - -app.listen(process.argv[2], () => console.log(`Dummy server listening on port ${process.argv[2]}!`)); diff --git a/test/fixtures/scripts/dummy-server-kill.coffee b/test/fixtures/scripts/dummy-server-kill.coffee new file mode 100644 index 000000000..477d18e04 --- /dev/null +++ b/test/fixtures/scripts/dummy-server-kill.coffee @@ -0,0 +1,12 @@ +express = require 'express' +app = express() + +app.get '/machines', (req, res) -> + res.json [{type: 'bulldozer', name: 'willy'}] + +app.get '/machines/:name', (req, res) -> + process.kill process.pid, 'SIGKILL' + + +app.listen process.argv[2], -> + console.log "Dummy server listening on port #{process.argv[2]}!" diff --git a/test/fixtures/scripts/dummy-server-kill.js b/test/fixtures/scripts/dummy-server-kill.js deleted file mode 100644 index f966ff180..000000000 --- a/test/fixtures/scripts/dummy-server-kill.js +++ /dev/null @@ -1,18 +0,0 @@ -/* eslint-disable - no-console, - no-unused-vars, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. - -const express = require('express'); - -const app = express(); - - -app.get('/machines', (req, res) => res.json([{ type: 'bulldozer', name: 'willy' }])); - -app.get('/machines/:name', (req, res) => process.kill(process.pid, 'SIGKILL')); - - -app.listen(process.argv[2], () => console.log(`Dummy server listening on port ${process.argv[2]}!`)); diff --git a/test/fixtures/scripts/dummy-server.coffee b/test/fixtures/scripts/dummy-server.coffee new file mode 100644 index 000000000..ee9820282 --- /dev/null +++ b/test/fixtures/scripts/dummy-server.coffee @@ -0,0 +1,11 @@ +express = require 'express' +app = express() + +app.get '/machines', (req, res) -> + res.json [{type: 'bulldozer', name: 'willy'}] + +app.get '/machines/:name', (req, res) -> + res.json {type: 'bulldozer', name: req.params.name} + +app.listen process.argv[2], -> + console.log "Dummy server listening on port #{process.argv[2]}!" \ No newline at end of file diff --git a/test/fixtures/scripts/dummy-server.js b/test/fixtures/scripts/dummy-server.js deleted file mode 100644 index 4a7c6bb73..000000000 --- a/test/fixtures/scripts/dummy-server.js +++ /dev/null @@ -1,17 +0,0 @@ -/* eslint-disable - no-console, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. - -const express = require('express'); - -const app = express(); - - -app.get('/machines', (req, res) => res.json([{ type: 'bulldozer', name: 'willy' }])); - -app.get('/machines/:name', (req, res) => res.json({ type: 'bulldozer', name: req.params.name })); - - -app.listen(process.argv[2], () => console.log(`Dummy server listening on port ${process.argv[2]}!`)); diff --git a/test/fixtures/scripts/endless-ignore-term.coffee b/test/fixtures/scripts/endless-ignore-term.coffee new file mode 100644 index 000000000..a3bd4c1a6 --- /dev/null +++ b/test/fixtures/scripts/endless-ignore-term.coffee @@ -0,0 +1,9 @@ +require('./handle-windows-sigint')() + +ignore = -> + console.log('ignoring termination') + +process.on('SIGTERM', ignore) +process.on('SIGINT', ignore) + +setInterval(( -> ), 1000) diff --git a/test/fixtures/scripts/endless-ignore-term.js b/test/fixtures/scripts/endless-ignore-term.js deleted file mode 100644 index d1e56a18a..000000000 --- a/test/fixtures/scripts/endless-ignore-term.js +++ /dev/null @@ -1,15 +0,0 @@ -/* eslint-disable - no-console, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -require('./handle-windows-sigint')(); - - -const ignore = () => console.log('ignoring termination'); - -process.on('SIGTERM', ignore); -process.on('SIGINT', ignore); - - -setInterval((() => { }), 1000); diff --git a/test/fixtures/scripts/exit-0.coffee b/test/fixtures/scripts/exit-0.coffee new file mode 100644 index 000000000..23d7e9991 --- /dev/null +++ b/test/fixtures/scripts/exit-0.coffee @@ -0,0 +1 @@ +process.exit(0) diff --git a/test/fixtures/scripts/exit-0.js b/test/fixtures/scripts/exit-0.js deleted file mode 100644 index 508e34dff..000000000 --- a/test/fixtures/scripts/exit-0.js +++ /dev/null @@ -1,3 +0,0 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. -process.exit(0); diff --git a/test/fixtures/scripts/exit-3.coffee b/test/fixtures/scripts/exit-3.coffee new file mode 100644 index 000000000..453a2df9e --- /dev/null +++ b/test/fixtures/scripts/exit-3.coffee @@ -0,0 +1 @@ +process.exit(3) diff --git a/test/fixtures/scripts/exit-3.js b/test/fixtures/scripts/exit-3.js deleted file mode 100644 index 92cc0995e..000000000 --- a/test/fixtures/scripts/exit-3.js +++ /dev/null @@ -1,3 +0,0 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. -process.exit(3); diff --git a/test/fixtures/scripts/handle-windows-sigint.coffee b/test/fixtures/scripts/handle-windows-sigint.coffee new file mode 100644 index 000000000..e68be034e --- /dev/null +++ b/test/fixtures/scripts/handle-windows-sigint.coffee @@ -0,0 +1,25 @@ +readline = require('readline') + +ASCII_CTRL_C = 3 + +# To learn about why this is needed and how it works, see +# the 'src/child-process.coffee' file, function 'signalTerm'. +module.exports = -> + # Handling programmatic interruption (Dredd sends '\u0003' + # to stdin) + process.stdin.on('data', (chunk) -> + for char in chunk.toString() + if char.charCodeAt(0) is ASCII_CTRL_C + process.emit('SIGINT') + break + ) + + # Handling manual interruption (user sends '\u0003' to stdin by + # manually pressing Ctrl+C) + rl = readline.createInterface( + input: process.stdin + output: process.stdout + ) + rl.on('SIGINT', -> + process.emit('SIGINT') + ) diff --git a/test/fixtures/scripts/handle-windows-sigint.js b/test/fixtures/scripts/handle-windows-sigint.js deleted file mode 100644 index 477f67a4b..000000000 --- a/test/fixtures/scripts/handle-windows-sigint.js +++ /dev/null @@ -1,36 +0,0 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. -const readline = require('readline'); - - -const ASCII_CTRL_C = 3; - - -// To learn about why this is needed and how it works, see -// the 'src/child-process.coffee' file, function 'signalTerm'. -module.exports = function () { - // Handling programmatic interruption (Dredd sends '\u0003' - // to stdin) - process.stdin.on('data', chunk => - (() => { - const result = []; - for (const char of chunk.toString()) { - if (char.charCodeAt(0) === ASCII_CTRL_C) { - process.emit('SIGINT'); - break; - } else { - result.push(undefined); - } - } - return result; - })() - ); - - // Handling manual interruption (user sends '\u0003' to stdin by - // manually pressing Ctrl+C) - const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout - }); - return rl.on('SIGINT', () => process.emit('SIGINT')); -}; diff --git a/test/fixtures/scripts/kill-self.coffee b/test/fixtures/scripts/kill-self.coffee new file mode 100644 index 000000000..b02f97709 --- /dev/null +++ b/test/fixtures/scripts/kill-self.coffee @@ -0,0 +1,3 @@ +setTimeout( -> + process.kill process.pid, 'SIGKILL' +, 100) diff --git a/test/fixtures/scripts/kill-self.js b/test/fixtures/scripts/kill-self.js deleted file mode 100644 index 4233f8382..000000000 --- a/test/fixtures/scripts/kill-self.js +++ /dev/null @@ -1,4 +0,0 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. -setTimeout(() => process.kill(process.pid, 'SIGKILL') - , 100); diff --git a/test/fixtures/scripts/stderr.coffee b/test/fixtures/scripts/stderr.coffee new file mode 100644 index 000000000..a1bb997fe --- /dev/null +++ b/test/fixtures/scripts/stderr.coffee @@ -0,0 +1,11 @@ +require('./handle-windows-sigint')() + +exit = -> + process.stdout.write('exiting\n') + process.exit(0) + +process.on('SIGTERM', exit) +process.on('SIGINT', exit) + +process.stderr.write('error output text\n') +setInterval(( -> ), 100) diff --git a/test/fixtures/scripts/stderr.js b/test/fixtures/scripts/stderr.js deleted file mode 100644 index ed15c816f..000000000 --- a/test/fixtures/scripts/stderr.js +++ /dev/null @@ -1,16 +0,0 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. -require('./handle-windows-sigint')(); - - -const exit = function () { - process.stdout.write('exiting\n'); - return process.exit(0); -}; - -process.on('SIGTERM', exit); -process.on('SIGINT', exit); - - -process.stderr.write('error output text\n'); -setInterval((() => { }), 100); diff --git a/test/fixtures/scripts/stdout-exit-3.coffee b/test/fixtures/scripts/stdout-exit-3.coffee new file mode 100644 index 000000000..2b1be5abb --- /dev/null +++ b/test/fixtures/scripts/stdout-exit-3.coffee @@ -0,0 +1,11 @@ +require('./handle-windows-sigint')() + +exit = -> + process.stdout.write('exiting\n') + process.exit(3) + +process.on('SIGTERM', exit) +process.on('SIGINT', exit) + +process.stdout.write('standard output text\n') +setInterval(( -> ), 100) diff --git a/test/fixtures/scripts/stdout-exit-3.js b/test/fixtures/scripts/stdout-exit-3.js deleted file mode 100644 index 5ef88d0cf..000000000 --- a/test/fixtures/scripts/stdout-exit-3.js +++ /dev/null @@ -1,16 +0,0 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. -require('./handle-windows-sigint')(); - - -const exit = function () { - process.stdout.write('exiting\n'); - return process.exit(3); -}; - -process.on('SIGTERM', exit); -process.on('SIGINT', exit); - - -process.stdout.write('standard output text\n'); -setInterval((() => { }), 100); diff --git a/test/fixtures/scripts/stdout.coffee b/test/fixtures/scripts/stdout.coffee new file mode 100644 index 000000000..7f9fa16a5 --- /dev/null +++ b/test/fixtures/scripts/stdout.coffee @@ -0,0 +1,11 @@ +require('./handle-windows-sigint')() + +exit = -> + process.stdout.write('exiting\n') + process.exit(0) + +process.on('SIGTERM', exit) +process.on('SIGINT', exit) + +process.stdout.write('standard output text\n') +setInterval(( -> ), 100) diff --git a/test/fixtures/scripts/stdout.js b/test/fixtures/scripts/stdout.js deleted file mode 100644 index a21d95b15..000000000 --- a/test/fixtures/scripts/stdout.js +++ /dev/null @@ -1,16 +0,0 @@ -// TODO: This file was created by bulk-decaffeinate. -// Sanity-check the conversion and remove this comment. -require('./handle-windows-sigint')(); - - -const exit = function () { - process.stdout.write('exiting\n'); - return process.exit(0); -}; - -process.on('SIGTERM', exit); -process.on('SIGINT', exit); - - -process.stdout.write('standard output text\n'); -setInterval((() => { }), 100); diff --git a/test/fixtures/swagger-multiple-responses.js b/test/fixtures/swagger-multiple-responses.js index f05dfe324..dcc3ba774 100644 --- a/test/fixtures/swagger-multiple-responses.js +++ b/test/fixtures/swagger-multiple-responses.js @@ -1,6 +1,6 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.before('/honey > GET > 500 > application/json', function (transaction, done) { +hooks.before('/honey > GET > 500 > application/json', (transaction, done) => { transaction.skip = false; done(); }); diff --git a/test/fixtures/swagger-transaction-names.js b/test/fixtures/swagger-transaction-names.js index 9b6127454..f26acb1af 100644 --- a/test/fixtures/swagger-transaction-names.js +++ b/test/fixtures/swagger-transaction-names.js @@ -1,13 +1,10 @@ - -var hooks = require('hooks'); - +const hooks = require('hooks'); function logTransactionName(transaction, done) { hooks.log(transaction.name); done(); } - hooks.before('/honey > GET > 400 > application/json', logTransactionName); hooks.before('/honey > GET > 500 > application/json', logTransactionName); hooks.before('/honey > GET > 200 > application/json', logTransactionName); diff --git a/test/fixtures/test2_all.js b/test/fixtures/test2_all.js index 0a6930f8a..c349dac9f 100644 --- a/test/fixtures/test2_all.js +++ b/test/fixtures/test2_all.js @@ -1,13 +1,11 @@ -var hooks; +const hooks = require('hooks'); -hooks = require('hooks'); - -hooks.beforeAll(function(done) { +hooks.beforeAll((done) => { console.log('*** beforeAll'); done(); }); -hooks.before('Machines > Machines collection > Get Machines', function(transaction, done) { +hooks.before('Machines > Machines collection > Get Machines', (transaction, done) => { console.log('*** before'); - return done(); + done(); }); diff --git a/test/fixtures/test2_events.js b/test/fixtures/test2_events.js index 4d9423b32..a7ed4c0d1 100644 --- a/test/fixtures/test2_events.js +++ b/test/fixtures/test2_events.js @@ -1,6 +1,6 @@ -var hooks = require('hooks'); +const hooks = require('hooks'); -hooks.beforeAll(function(done) { +hooks.beforeAll((done) => { console.log('hooks.beforeAll'); done(); }); diff --git a/test/fixtures/test2_hooks.js b/test/fixtures/test2_hooks.js index 775750fbe..e34158ad5 100644 --- a/test/fixtures/test2_hooks.js +++ b/test/fixtures/test2_hooks.js @@ -1,10 +1,7 @@ -// Generated by CoffeeScript 1.6.3 -var hooks; +const hooks = require('hooks'); -hooks = require('hooks'); - -hooks.before('Machines > Machines collection > Get Machines', function(transaction, done) { - transaction.request.headers['header'] = '123232323'; +hooks.before('Machines > Machines collection > Get Machines', (transaction, done) => { + transaction.request.headers.header = '123232323'; console.log('before'); - return done(); + done(); }); diff --git a/test/fixtures/test_all.coffee b/test/fixtures/test_all.coffee new file mode 100644 index 000000000..d07ec6835 --- /dev/null +++ b/test/fixtures/test_all.coffee @@ -0,0 +1,8 @@ +{after,afterAll} = require 'hooks' + +after "Machines > Machines collection > Get Machines", (transaction) -> + console.log "*** after" + +afterAll (done) -> + console.log "*** afterAll" + done() diff --git a/test/fixtures/test_all.js b/test/fixtures/test_all.js deleted file mode 100644 index 063722c25..000000000 --- a/test/fixtures/test_all.js +++ /dev/null @@ -1,16 +0,0 @@ -/* eslint-disable - import/no-extraneous-dependencies, - import/no-unresolved, - no-console, - no-unused-vars, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -const { after, afterAll } = require('hooks'); - -after('Machines > Machines collection > Get Machines', transaction => console.log('*** after')); - -afterAll((done) => { - console.log('*** afterAll'); - return done(); -}); diff --git a/test/fixtures/test_events.js b/test/fixtures/test_events.js index 5c0062fe9..905452875 100644 --- a/test/fixtures/test_events.js +++ b/test/fixtures/test_events.js @@ -1,13 +1,6 @@ -/* eslint-disable - import/no-extraneous-dependencies, - import/no-unresolved, - no-console, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. const { afterAll } = require('hooks'); afterAll((done) => { console.log('hooks.afterAll'); - return done(); + done(); }); diff --git a/test/fixtures/test_hooks.coffee b/test/fixtures/test_hooks.coffee new file mode 100644 index 000000000..0be64ceca --- /dev/null +++ b/test/fixtures/test_hooks.coffee @@ -0,0 +1,4 @@ +{after} = require 'hooks' + +after "Machines > Machines collection > Get Machines", (transaction) -> + console.log "after" diff --git a/test/fixtures/test_hooks.js b/test/fixtures/test_hooks.js deleted file mode 100644 index 517877a91..000000000 --- a/test/fixtures/test_hooks.js +++ /dev/null @@ -1,11 +0,0 @@ -/* eslint-disable - import/no-extraneous-dependencies, - import/no-unresolved, - no-console, - no-unused-vars, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -const { after } = require('hooks'); - -after('Machines > Machines collection > Get Machines', transaction => console.log('after')); diff --git a/test/integration/proxy-test.js b/test/integration/proxy-test.js index ed37b74e0..23eb3c05e 100644 --- a/test/integration/proxy-test.js +++ b/test/integration/proxy-test.js @@ -1,56 +1,42 @@ -/* eslint-disable - no-return-assign, - no-shadow, - no-unused-vars, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. const http = require('http'); -const https = require('https'); const url = require('url'); -const clone = require('clone'); const { assert } = require('chai'); const { runDredd, recordLogging, createServer, DEFAULT_SERVER_PORT } = require('./helpers'); -const logger = require('../../src/logger'); const Dredd = require('../../src/dredd'); - const PROXY_PORT = DEFAULT_SERVER_PORT + 1; const PROXY_URL = `http://127.0.0.1:${PROXY_PORT}`; const SERVER_HOST = `127.0.0.1:${DEFAULT_SERVER_PORT}`; const DUMMY_URL = 'http://example.com'; const REGULAR_HTTP_METHODS = ['GET', 'POST', 'PUT', 'DELETE']; - // Normally, tests create Dredd instance and pass it to the 'runDredd' // helper, which captures Dredd's logging while it runs. However, in // this case we need to capture logging also during the instantiation. -const createAndRunDredd = function (configuration, done) { - if (configuration.options == null) { configuration.options = {}; } +function createAndRunDredd(configuration, done) { + if (!configuration.options) { configuration.options = {}; } configuration.options.color = false; configuration.options.silent = true; configuration.options.level = 'silly'; let dredd; - return recordLogging((next) => { + recordLogging((next) => { dredd = new Dredd(configuration); dredd.configuration.http.strictSSL = false; - return next(); - } - , (err, args, dreddInitLogging) => - runDredd(dredd, (err, info) => { + next(); + }, (err, args, dreddInitLogging) => + runDredd(dredd, (error, info) => { info.logging = `${dreddInitLogging}\n${info.logging}`; - return done(err, info); + done(error, info); }) ); -}; - +} // Creates dummy proxy server for given protocol. Records details // about the first received request to the object given as a second // argument. -const createProxyServer = function (protocol, proxyRequestInfo) { +function createProxyServer(protocol, proxyRequestInfo) { let app; if (protocol === 'http') { app = http.createServer(); @@ -59,7 +45,7 @@ const createProxyServer = function (protocol, proxyRequestInfo) { proxyRequestInfo.method = req.method; res.writeHead(200, { 'Content-Type': 'text/plain' }); - return res.end('OK'); + res.end('OK'); }); } else if (protocol === 'https') { // Uses the 'http' module as well, because otherwise we would @@ -76,14 +62,13 @@ const createProxyServer = function (protocol, proxyRequestInfo) { proxyRequestInfo.method = req.method; socket.write('HTTP/1.1 200 Connection Established\r\n\r\n'); - return socket.end(); + socket.end(); }); } else { throw new Error(`Unsupported protocol: ${protocol}`); } return app; -}; - +} // Runs a single scenario. Options: // @@ -96,18 +81,18 @@ const createProxyServer = function (protocol, proxyRequestInfo) { // - proxy - Means Dredd is expected to hit proxy // - server - Means Dredd is expected to hit the dummy server // - expectedUrl (string) - Which URL is Dredd expected to request -const test = function (scenario) { +function test(scenario) { // Setup and prepare dummy proxy let proxy; let proxyRequestInfo = {}; beforeEach((done) => { const app = createProxyServer(scenario.protocol, proxyRequestInfo); - return proxy = app.listen(PROXY_PORT, done); + proxy = app.listen(PROXY_PORT, done); }); afterEach((done) => { proxyRequestInfo = {}; - return proxy.close(done); + proxy.close(done); }); // Setup and prepare dummy server @@ -116,14 +101,14 @@ const test = function (scenario) { beforeEach((done) => { const app = createServer({ protocol: scenario.protocol }); - return server = app.listen(DEFAULT_SERVER_PORT, (err, info) => { + server = app.listen(DEFAULT_SERVER_PORT, (err, info) => { serverRuntimeInfo = info; - return done(err); + done(err); }); }); afterEach((done) => { serverRuntimeInfo = undefined; - return server.close(done); + server.close(done); }); // Setup and run Dredd @@ -133,10 +118,10 @@ const test = function (scenario) { const configuration = { options: {} }; scenario.configureDredd(configuration); - return createAndRunDredd(configuration, (err, info) => { + createAndRunDredd(configuration, (err, info) => { if (err) { return done(err); } dreddLogging = info.logging; - return done(); + done(); }); }); @@ -151,12 +136,14 @@ const test = function (scenario) { if (scenario.protocol === 'http') { it('requests the proxy with regular HTTP method', () => assert.oneOf(proxyRequestInfo.method, REGULAR_HTTP_METHODS)); it('requests the proxy, using the original URL as a path', () => assert.equal(proxyRequestInfo.url, scenario.expectedUrl)); + return; } else if (scenario.protocol === 'https') { it('requests the proxy with CONNECT', () => assert.equal(proxyRequestInfo.method, 'CONNECT')); it('asks the proxy to tunnel SSL connection to the original hostname', () => { const hostname = `${url.parse(scenario.expectedUrl).hostname}:${DEFAULT_SERVER_PORT}`; assert.equal(proxyRequestInfo.url, hostname); }); + return; } throw new Error(`Unsupported protocol: ${scenario.protocol}`); } else if (scenario.expectedDestination === 'server') { @@ -167,7 +154,7 @@ const test = function (scenario) { } else { throw new Error(`Unsupported destination: ${scenario.expectedDestination}`); } -}; +} ['http', 'https'].forEach((protocol) => { @@ -179,7 +166,7 @@ proxy specified by environment variables: \ ${protocol}_proxy=${PROXY_URL}\ `; - beforeEach(() => process.env[`${protocol}_proxy`] = PROXY_URL); + beforeEach(() => { process.env[`${protocol}_proxy`] = PROXY_URL; }); afterEach(() => delete process.env[`${protocol}_proxy`]); describe('Requesting Server Under Test', () => @@ -187,7 +174,7 @@ ${protocol}_proxy=${PROXY_URL}\ protocol, configureDredd(configuration) { configuration.server = serverUrl; - return configuration.options.path = './test/fixtures/single-get.apib'; + configuration.options.path = './test/fixtures/single-get.apib'; }, expectedLog, expectedDestination: 'server', @@ -196,15 +183,15 @@ ${protocol}_proxy=${PROXY_URL}\ ); describe('Using Apiary Reporter', () => { - beforeEach(() => process.env.APIARY_API_URL = serverUrl); + beforeEach(() => { process.env.APIARY_API_URL = serverUrl; }); afterEach(() => delete process.env.APIARY_API_URL); - return test({ + test({ protocol, configureDredd(configuration) { configuration.server = DUMMY_URL; configuration.options.path = './test/fixtures/single-get.apib'; - return configuration.options.reporter = ['apiary']; + configuration.options.reporter = ['apiary']; }, expectedLog, expectedDestination: 'proxy', @@ -217,7 +204,7 @@ ${protocol}_proxy=${PROXY_URL}\ protocol, configureDredd(configuration) { configuration.server = DUMMY_URL; - return configuration.options.path = `${serverUrl}/example.apib`; + configuration.options.path = `${serverUrl}/example.apib`; }, expectedLog, expectedDestination: 'proxy', @@ -237,11 +224,11 @@ http_proxy=${PROXY_URL}, no_proxy=${SERVER_HOST}\ beforeEach(() => { process.env.http_proxy = PROXY_URL; - return process.env.no_proxy = SERVER_HOST; + process.env.no_proxy = SERVER_HOST; }); afterEach(() => { delete process.env.http_proxy; - return delete process.env.no_proxy; + delete process.env.no_proxy; }); describe('Requesting Server Under Test', () => @@ -249,7 +236,7 @@ http_proxy=${PROXY_URL}, no_proxy=${SERVER_HOST}\ protocol: 'http', configureDredd(configuration) { configuration.server = serverUrl; - return configuration.options.path = './test/fixtures/single-get.apib'; + configuration.options.path = './test/fixtures/single-get.apib'; }, expectedLog, expectedDestination: 'server', @@ -258,15 +245,15 @@ http_proxy=${PROXY_URL}, no_proxy=${SERVER_HOST}\ ); describe('Using Apiary Reporter', () => { - beforeEach(() => process.env.APIARY_API_URL = serverUrl); + beforeEach(() => { process.env.APIARY_API_URL = serverUrl; }); afterEach(() => delete process.env.APIARY_API_URL); - return test({ + test({ protocol: 'http', configureDredd(configuration) { configuration.server = DUMMY_URL; configuration.options.path = './test/fixtures/single-get.apib'; - return configuration.options.reporter = ['apiary']; + configuration.options.reporter = ['apiary']; }, expectedLog, expectedDestination: 'server', @@ -279,7 +266,7 @@ http_proxy=${PROXY_URL}, no_proxy=${SERVER_HOST}\ protocol: 'http', configureDredd(configuration) { configuration.server = DUMMY_URL; - return configuration.options.path = `${serverUrl}/example.apib`; + configuration.options.path = `${serverUrl}/example.apib`; }, expectedLog, expectedDestination: 'server', diff --git a/test/unit/hooks-worker-client-test.js b/test/unit/hooks-worker-client-test.js index 60a17d5dd..0b4481639 100644 --- a/test/unit/hooks-worker-client-test.js +++ b/test/unit/hooks-worker-client-test.js @@ -1,128 +1,115 @@ -/* eslint-disable - no-loop-func, - no-return-assign, - no-shadow, - no-undef, - no-unused-vars, - one-var, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. +const clone = require('clone'); +const crossSpawnStub = require('cross-spawn'); +const net = require('net'); +const path = require('path'); const proxyquire = require('proxyquire'); -const { EventEmitter } = require('events'); const sinon = require('sinon'); -const net = require('net'); const { assert } = require('chai'); -const clone = require('clone'); -const childProcess = require('child_process'); +const { EventEmitter } = require('events'); -const crossSpawnStub = require('cross-spawn'); const whichStub = require('../../src/which'); const loggerStub = require('../../src/logger'); const Hooks = require('../../src/hooks'); const commandLineOptions = require('../../src/options'); -const measureExecutionDurationMs = function (fn) { +function measureExecutionDurationMs(fn) { const time = process.hrtime(); fn(); const timeDiff = process.hrtime(time); // timeDiff = [seconds, nanoseconds] return ((timeDiff[0] * 1000) + (timeDiff[1] * 1e-6)); -}; +} const COFFEE_BIN = 'node_modules/.bin/coffee'; const MIN_COMMAND_EXECUTION_DURATION_MS = 2 * measureExecutionDurationMs(() => crossSpawnStub.sync(COFFEE_BIN, ['test/fixtures/scripts/exit-0.coffee'])); const PORT = 61321; -let runner = null; -let logs = null; +let runner; const logLevels = ['error', 'log', 'info', 'warn']; const HooksWorkerClient = proxyquire('../../src/hooks-worker-client', { 'cross-spawn': crossSpawnStub, './which': whichStub, './logger': loggerStub -} -); +}); const TransactionRunner = require('../../src/transaction-runner'); -let hooksWorkerClient = null; +let hooksWorkerClient; -const loadWorkerClient = function (callback) { +function loadWorkerClient(callback) { hooksWorkerClient = new HooksWorkerClient(runner); - return hooksWorkerClient.start(error => callback(error)); -}; + hooksWorkerClient.start(error => callback(error)); +} describe('Hooks worker client', () => { + let logs; + beforeEach(() => { logs = []; - runner = new TransactionRunner({}); runner.hooks = new Hooks({ logs: [], logger: console }); runner.hooks.configuration = { options: {} }; - return Array.from(logLevels).map(level => + Array.from(logLevels).forEach(level => sinon.stub(loggerStub, level).callsFake((msg1, msg2) => { let text = msg1; if (msg2) { text += ` ${msg2}`; } // Uncomment to enable logging for debug // console.log text - return logs.push(text); + logs.push(text); })); }); - afterEach(() => - Array.from(logLevels).map(level => - loggerStub[level].restore()) - ); + afterEach(() => { + Array.from(logLevels).forEach(level => loggerStub[level].restore()); + }); describe('when methods dealing with connection to the handler are stubbed', () => { beforeEach(() => { sinon.stub(HooksWorkerClient.prototype, 'disconnectFromHandler').callsFake(() => { }); - return sinon.stub(HooksWorkerClient.prototype, 'connectToHandler').callsFake(cb => cb()); + sinon.stub(HooksWorkerClient.prototype, 'connectToHandler').callsFake(cb => cb()); }); afterEach(() => { HooksWorkerClient.prototype.disconnectFromHandler.restore(); - return HooksWorkerClient.prototype.connectToHandler.restore(); + HooksWorkerClient.prototype.connectToHandler.restore(); }); it('should pipe spawned process stdout to the Dredd process stdout', (done) => { runner.hooks.configuration.options.language = `${COFFEE_BIN} test/fixtures/scripts/stdout.coffee`; - return loadWorkerClient((workerError) => { + loadWorkerClient((workerError) => { if (workerError) { return done(workerError); } // The handler sometimes doesn't write to stdout or stderr until it // finishes, so we need to manually stop it. However, it could happen // we'll stop it before it actually manages to do what we test here, so // we add some timeout here. - return setTimeout(() => + setTimeout(() => hooksWorkerClient.stop((stopError) => { if (stopError) { return done(stopError); } assert.include(logs, 'Hooks handler stdout: standard output text\n'); - return done(); + done(); }) - , MIN_COMMAND_EXECUTION_DURATION_MS); }); }); it('should pipe spawned process stderr to the Dredd process stderr', (done) => { runner.hooks.configuration.options.language = `${COFFEE_BIN} test/fixtures/scripts/stderr.coffee`; - return loadWorkerClient((workerError) => { + loadWorkerClient((workerError) => { if (workerError) { return done(workerError); } // The handler sometimes doesn't write to stdout or stderr until it // finishes, so we need to manually stop it. However, it could happen // we'll stop it before it actually manages to do what we test here, so // we add some timeout here. - return setTimeout(() => + setTimeout(() => hooksWorkerClient.stop((stopError) => { if (stopError) { return done(stopError); } assert.include(logs, 'Hooks handler stderr: error output text\n'); - return done(); + done(); }) , MIN_COMMAND_EXECUTION_DURATION_MS); @@ -132,59 +119,57 @@ describe('Hooks worker client', () => { it('should not set the error on worker if process gets intentionally killed by Dredd ' + 'because it can be killed after all hooks execution if SIGTERM isn\'t handled', (done) => { runner.hooks.configuration.options.language = `${COFFEE_BIN} test/fixtures/scripts/endless-ignore-term.coffee`; - return loadWorkerClient((workerError) => { + loadWorkerClient((workerError) => { if (workerError) { return done(workerError); } // The handler sometimes doesn't write to stdout or stderr until it // finishes, so we need to manually stop it. However, it could happen // we'll stop it before it actually manages to do what we test here, so // we add some timeout here. - return setTimeout(() => + setTimeout(() => hooksWorkerClient.stop((stopError) => { if (stopError) { return done(stopError); } assert.isNull(runner.hookHandlerError); - return done(); + done(); }) - , MIN_COMMAND_EXECUTION_DURATION_MS); }); }); it('should include the status in the error if spawned process ends with non-zero exit status', (done) => { runner.hooks.configuration.options.language = `${COFFEE_BIN} test/fixtures/scripts/exit-3.coffee`; - return loadWorkerClient((workerError) => { + loadWorkerClient((workerError) => { if (workerError) { return done(workerError); } // The handler sometimes doesn't write to stdout or stderr until it // finishes, so we need to manually stop it. However, it could happen // we'll stop it before it actually manages to do what we test here, so // we add some timeout here. - return setTimeout(() => + setTimeout(() => hooksWorkerClient.stop((stopError) => { if (stopError) { return done(stopError); } assert.isOk(runner.hookHandlerError); assert.include(runner.hookHandlerError.message, '3'); - return done(); + done(); }) - , MIN_COMMAND_EXECUTION_DURATION_MS); }); }); describe('when --language=nodejs option is given', () => { - beforeEach(() => + beforeEach(() => { runner.hooks.configuration = { options: { language: 'nodejs' } - } - ); + }; + }); it('should write a hint that native hooks should be used', done => loadWorkerClient((err) => { assert.isOk(err); assert.include(err.message, 'native Node.js hooks instead'); - return done(); + done(); }) ); }); @@ -205,9 +190,8 @@ describe('Hooks worker client', () => { } }; - sinon.stub(whichStub, 'which').callsFake(command => true); - - return sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); + sinon.stub(whichStub, 'which').callsFake(() => true); + sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); }); afterEach(() => { @@ -216,18 +200,18 @@ describe('Hooks worker client', () => { runner.hooks.configuration = undefined; whichStub.which.restore(); - return HooksWorkerClient.prototype.terminateHandler.restore(); + HooksWorkerClient.prototype.terminateHandler.restore(); }); it('should spawn the server process with command "dredd-hooks-ruby"', done => loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.isTrue(crossSpawnStub.spawn.called); assert.equal(crossSpawnStub.spawn.getCall(0).args[0], 'dredd-hooks-ruby'); - return done(); + done(); }); }) ); @@ -236,10 +220,10 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.equal(crossSpawnStub.spawn.getCall(0).args[1][0], 'somefile.rb'); - return done(); + done(); }); }) ); @@ -247,9 +231,9 @@ describe('Hooks worker client', () => { describe('when --language=ruby option is given and the worker is not installed', () => { beforeEach(() => { - sinon.stub(whichStub, 'which').callsFake(command => false); + sinon.stub(whichStub, 'which').callsFake(() => false); - return runner.hooks.configuration = { + runner.hooks.configuration = { options: { language: 'ruby', hookfiles: 'somefile.rb' @@ -264,7 +248,7 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isOk(err); assert.include(err.message, 'gem install dredd_hooks'); - return done(); + done(); }) ); }); @@ -285,9 +269,8 @@ describe('Hooks worker client', () => { } }; - sinon.stub(whichStub, 'which').callsFake(command => true); - - return sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); + sinon.stub(whichStub, 'which').callsFake(() => true); + sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); }); afterEach(() => { @@ -296,18 +279,18 @@ describe('Hooks worker client', () => { runner.hooks.configuration = undefined; whichStub.which.restore(); - return HooksWorkerClient.prototype.terminateHandler.restore(); + HooksWorkerClient.prototype.terminateHandler.restore(); }); it('should spawn the server process with command "dredd-hooks-python"', done => loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.isTrue(crossSpawnStub.spawn.called); assert.equal(crossSpawnStub.spawn.getCall(0).args[0], 'dredd-hooks-python'); - return done(); + done(); }); }) ); @@ -316,10 +299,10 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.equal(crossSpawnStub.spawn.getCall(0).args[1][0], 'somefile.py'); - return done(); + done(); }); }) ); @@ -327,9 +310,9 @@ describe('Hooks worker client', () => { describe('when --language=python option is given and the worker is not installed', () => { beforeEach(() => { - sinon.stub(whichStub, 'which').callsFake(command => false); + sinon.stub(whichStub, 'which').callsFake(() => false); - return runner.hooks.configuration = { + runner.hooks.configuration = { options: { language: 'python', hookfiles: 'somefile.py' @@ -343,7 +326,7 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isOk(err); assert.include(err.message, 'pip install dredd_hooks'); - return done(); + done(); }) ); }); @@ -364,9 +347,8 @@ describe('Hooks worker client', () => { } }; - sinon.stub(whichStub, 'which').callsFake(command => true); - - return sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); + sinon.stub(whichStub, 'which').callsFake(() => true); + sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); }); afterEach(() => { @@ -375,18 +357,18 @@ describe('Hooks worker client', () => { runner.hooks.configuration = undefined; whichStub.which.restore(); - return HooksWorkerClient.prototype.terminateHandler.restore(); + HooksWorkerClient.prototype.terminateHandler.restore(); }); it('should spawn the server process with command "dredd-hooks-php"', done => loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.isTrue(crossSpawnStub.spawn.called); assert.equal(crossSpawnStub.spawn.getCall(0).args[0], 'dredd-hooks-php'); - return done(); + done(); }); }) ); @@ -395,10 +377,10 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.equal(crossSpawnStub.spawn.getCall(0).args[1][0], 'somefile.py'); - return done(); + done(); }); }) ); @@ -406,9 +388,9 @@ describe('Hooks worker client', () => { describe('when --language=php option is given and the worker is not installed', () => { beforeEach(() => { - sinon.stub(whichStub, 'which').callsFake(command => false); + sinon.stub(whichStub, 'which').callsFake(() => false); - return runner.hooks.configuration = { + runner.hooks.configuration = { options: { language: 'php', hookfiles: 'somefile.py' @@ -422,7 +404,7 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isOk(err); assert.include(err.message, 'composer require ddelnano/dredd-hooks-php --dev'); - return done(); + done(); }) ); }); @@ -436,9 +418,9 @@ describe('Hooks worker client', () => { process.env.GOBIN = '/dummy/gobin/path'; delete process.env.GOPATH; - sinon.stub(whichStub, 'which').callsFake(command => false); + sinon.stub(whichStub, 'which').callsFake(() => false); - return runner.hooks.configuration = { + runner.hooks.configuration = { options: { language: 'go', hookfiles: 'gobinary' @@ -448,14 +430,14 @@ describe('Hooks worker client', () => { afterEach(() => { whichStub.which.restore(); process.env.GOBIN = goBin; - return process.env.GOPATH = goPath; + process.env.GOPATH = goPath; }); it('should write a hint how to install', done => loadWorkerClient((err) => { assert.isOk(err); assert.include(err.message, 'go get github.com/snikch/goodman/cmd/goodman'); - return done(); + done(); }) ); }); @@ -484,7 +466,7 @@ describe('Hooks worker client', () => { } }; - sinon.stub(whichStub, 'which').callsFake(command => true); + sinon.stub(whichStub, 'which').callsFake(() => true); return sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); }); @@ -497,18 +479,18 @@ describe('Hooks worker client', () => { whichStub.which.restore(); HooksWorkerClient.prototype.terminateHandler.restore(); process.env.GOBIN = goBin; - return process.env.GOPATH = goPath; + process.env.GOPATH = goPath; }); it('should spawn the server process with command "$GOBIN/goodman"', done => loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.isTrue(crossSpawnStub.spawn.called); assert.equal(crossSpawnStub.spawn.getCall(0).args[0], path.join(dummyPath, 'goodman')); - return done(); + done(); }); }) ); @@ -517,10 +499,10 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.equal(crossSpawnStub.spawn.getCall(0).args[1][0], 'gobinary'); - return done(); + done(); }); }) ); @@ -528,9 +510,9 @@ describe('Hooks worker client', () => { describe('when --language=rust option is given and the worker is not installed', () => { beforeEach(() => { - sinon.stub(whichStub, 'which').callsFake(command => false); + sinon.stub(whichStub, 'which').callsFake(() => false); - return runner.hooks.configuration = { + runner.hooks.configuration = { options: { language: 'rust', hookfiles: 'rustbinary' @@ -543,7 +525,7 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isOk(err); assert.include(err.message, 'cargo install dredd-hooks'); - return done(); + done(); }) ); }); @@ -564,9 +546,8 @@ describe('Hooks worker client', () => { } }; - sinon.stub(whichStub, 'which').callsFake(command => true); - - return sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); + sinon.stub(whichStub, 'which').callsFake(() => true); + sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); }); afterEach(() => { @@ -575,18 +556,18 @@ describe('Hooks worker client', () => { runner.hooks.configuration = undefined; whichStub.which.restore(); - return HooksWorkerClient.prototype.terminateHandler.restore(); + HooksWorkerClient.prototype.terminateHandler.restore(); }); it('should spawn the server process with command "dredd-hooks-rust"', done => loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.isTrue(crossSpawnStub.spawn.called); assert.equal(crossSpawnStub.spawn.getCall(0).args[0], 'dredd-hooks-rust'); - return done(); + done(); }); }) ); @@ -595,10 +576,10 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.equal(crossSpawnStub.spawn.getCall(0).args[1][0], 'rustbinary'); - return done(); + done(); }); }) ); @@ -620,9 +601,8 @@ describe('Hooks worker client', () => { } }; - sinon.stub(whichStub, 'which').callsFake(command => true); - - return sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); + sinon.stub(whichStub, 'which').callsFake(() => true); + sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); }); afterEach(() => { @@ -631,18 +611,18 @@ describe('Hooks worker client', () => { runner.hooks.configuration = undefined; whichStub.which.restore(); - return HooksWorkerClient.prototype.terminateHandler.restore(); + HooksWorkerClient.prototype.terminateHandler.restore(); }); it('should spawn the server process with command "dredd-hooks-perl"', done => loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.isTrue(crossSpawnStub.spawn.called); assert.equal(crossSpawnStub.spawn.getCall(0).args[0], 'dredd-hooks-perl'); - return done(); + done(); }); }) ); @@ -651,10 +631,10 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.equal(crossSpawnStub.spawn.getCall(0).args[1][0], 'somefile.py'); - return done(); + done(); }); }) ); @@ -662,9 +642,9 @@ describe('Hooks worker client', () => { describe('when --language=perl option is given and the worker is not installed', () => { beforeEach(() => { - sinon.stub(whichStub, 'which').callsFake(command => false); + sinon.stub(whichStub, 'which').callsFake(() => false); - return runner.hooks.configuration = { + runner.hooks.configuration = { options: { language: 'perl', hookfiles: 'somefile.py' @@ -678,7 +658,7 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isOk(err); assert.include(err.message, 'cpanm Dredd::Hooks'); - return done(); + done(); }) ); }); @@ -700,8 +680,7 @@ describe('Hooks worker client', () => { }; sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); - - return sinon.stub(whichStub, 'which').callsFake(() => true); + sinon.stub(whichStub, 'which').callsFake(() => true); }); afterEach(() => { @@ -710,18 +689,18 @@ describe('Hooks worker client', () => { runner.hooks.configuration = undefined; HooksWorkerClient.prototype.terminateHandler.restore(); - return whichStub.which.restore(); + whichStub.which.restore(); }); it('should spawn the server process with command "./my-fancy-command"', done => loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.isTrue(crossSpawnStub.spawn.called); assert.equal(crossSpawnStub.spawn.getCall(0).args[0], './my-fancy-command'); - return done(); + done(); }); }) ); @@ -730,10 +709,10 @@ describe('Hooks worker client', () => { loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); assert.equal(crossSpawnStub.spawn.getCall(0).args[1][0], 'someotherfile'); - return done(); + done(); }); }) ); @@ -750,17 +729,17 @@ describe('Hooks worker client', () => { sinon.stub(HooksWorkerClient.prototype, 'spawnHandler').callsFake(callback => callback()); - sinon.stub(whichStub, 'which').callsFake(command => true); + sinon.stub(whichStub, 'which').callsFake(() => true); sinon.stub(HooksWorkerClient.prototype, 'terminateHandler').callsFake(callback => callback()); - return loadWorkerClient((err) => { + loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { - assert.isUndefined(err); - return done(); + hooksWorkerClient.stop((error) => { + assert.isUndefined(error); + done(); }); }); }); @@ -771,7 +750,7 @@ describe('Hooks worker client', () => { whichStub.which.restore(); HooksWorkerClient.prototype.terminateHandler.restore(); - return HooksWorkerClient.prototype.spawnHandler.restore(); + HooksWorkerClient.prototype.spawnHandler.restore(); }); const eventTypes = [ @@ -782,45 +761,43 @@ describe('Hooks worker client', () => { 'afterAll' ]; - return Array.from(eventTypes).map(eventType => (eventType => + Array.from(eventTypes).forEach((eventType) => { it(`should register hook function for hook type ${eventType}`, () => { const hookFuncs = runner.hooks[`${eventType}Hooks`]; assert.isAbove(hookFuncs.length, 0); - }) - )(eventType)); + }); + }); }); }); describe('when hook handler server is running and not modifying transactions', () => { - let server = null; + let server; let receivedData = ''; - let transaction = null; - let connected = null; - let currentSocket = null; + let transaction; + let connected; + let currentSocket; let sentData = ''; beforeEach(() => { receivedData = ''; - - transaction = - { key: 'value' }; + transaction = { key: 'value' }; server = net.createServer(); server.on('connection', (socket) => { currentSocket = socket; connected = true; - return socket.on('data', (data) => { + socket.on('data', (data) => { receivedData += data.toString(); const receivedObject = JSON.parse(receivedData.replace('\n', '')); const objectToSend = clone(receivedObject); const message = `${JSON.stringify(objectToSend)}\n`; - return currentSocket.write(message); + currentSocket.write(message); }); }); - return server.listen(PORT); + server.listen(PORT); }); afterEach(() => server.close()); @@ -829,13 +806,13 @@ describe('Hooks worker client', () => { it('should connect to the server', (done) => { runner.hooks.configuration.options.language = `${COFFEE_BIN} test/fixtures/scripts/exit-0.coffee`; - return loadWorkerClient((err) => { + loadWorkerClient((err) => { assert.isUndefined(err); - return hooksWorkerClient.stop((err) => { + hooksWorkerClient.stop((error) => { assert.isTrue(connected); - assert.isUndefined(err); - return done(); + assert.isUndefined(error); + done(); }); }); }); @@ -848,16 +825,16 @@ describe('Hooks worker client', () => { 'afterAll' ]; - return Array.from(eventTypes).map(eventType => (eventType => + Array.from(eventTypes).forEach((eventType) => { describe(`when '${eventType}' hook function is triggered`, () => { if (eventType.indexOf('All') > -1) { beforeEach((done) => { receivedData = ''; runner.hooks.configuration.options.language = `${COFFEE_BIN} test/fixtures/scripts/exit-0.coffee`; sentData = clone([transaction]); - return loadWorkerClient((err) => { + loadWorkerClient((err) => { assert.isUndefined(err); - return runner.hooks[`${eventType}Hooks`][0](sentData, () => done()); + runner.hooks[`${eventType}Hooks`][0](sentData, () => done()); }); }); } else { @@ -865,9 +842,9 @@ describe('Hooks worker client', () => { receivedData = ''; runner.hooks.configuration.options.language = `${COFFEE_BIN} test/fixtures/scripts/exit-0.coffee`; sentData = clone(transaction); - return loadWorkerClient((err) => { + loadWorkerClient((err) => { assert.isUndefined(err); - return runner.hooks[`${eventType}Hooks`][0](sentData, () => done()); + runner.hooks[`${eventType}Hooks`][0](sentData, () => done()); }); }); } @@ -877,14 +854,14 @@ describe('Hooks worker client', () => { it('should send JSON to the socket ending with delimiter character', (done) => { assert.include(receivedData, '\n'); assert.include(receivedData, '{'); - return done(); + done(); }); describe('sent object', () => { - let receivedObject = null; + let receivedObject; - beforeEach(() => receivedObject = JSON.parse(receivedData.replace('\n', ''))); + beforeEach(() => { receivedObject = JSON.parse(receivedData.replace('\n', '')); }); const keys = [ 'data', @@ -892,11 +869,9 @@ describe('Hooks worker client', () => { 'uuid' ]; - for (const key of keys) { - (key => - it(`should contain key ${key}`, () => assert.property(receivedObject, key)) - )(key); - } + Array.from(keys).forEach((key) => { + it(`should contain key ${key}`, () => { assert.property(receivedObject, key); }); + }); it(`key event should have value ${eventType}`, () => assert.equal(receivedObject.event, eventType)); @@ -905,14 +880,15 @@ describe('Hooks worker client', () => { assert.isArray(receivedObject.data); assert.propertyVal(receivedObject.data[0], 'key', 'value'); }); + } else { + it('key data should contain the transaction object', () => { + assert.isObject(receivedObject.data); + assert.propertyVal(receivedObject.data, 'key', 'value'); + }); } - it('key data should contain the transaction object', () => { - assert.isObject(receivedObject.data); - assert.propertyVal(receivedObject.data, 'key', 'value'); - }); }); - }) - )(eventType)); + }); + }); }); describe('when hook handler server is running and modifying transactions', () => { @@ -928,8 +904,8 @@ describe('Hooks worker client', () => { 'afterEach', 'afterAll' ].forEach((eventName) => { - let getFirstTransaction, - transactionData; + let getFirstTransaction; + let transactionData; if (eventName.match(/All$/)) { // the hooks which are called '*All' recieve an array of transactions // as a parameter @@ -976,7 +952,7 @@ describe('Hooks worker client', () => { // -- 6 --, sending modified data back to hooks worker client const messageOut = `${JSON.stringify(messageIn)}\n`; - return socket.write(messageOut); + socket.write(messageOut); } }); }); @@ -986,14 +962,14 @@ describe('Hooks worker client', () => { // -- 2 --, runs hooks worker client, starts to send transaction(s), // thus triggers the 'connection' event above - return loadWorkerClient((err) => { + loadWorkerClient((err) => { if (err) { return done(err); } - return runner.hooks[`${eventName}Hooks`][0](transactionData, () => {}); + runner.hooks[`${eventName}Hooks`][0](transactionData, () => {}); }); }); afterEach((done) => { hookHandler.close(); - return hooksWorkerClient.stop(done); + hooksWorkerClient.stop(done); }); it('modifications get applied to the original transaction object', () => { @@ -1039,23 +1015,22 @@ describe('Hooks worker client', () => { } ]; - return Array.from(scenarios).map(scenario => - (scenario => - describe(`Option '${scenario.option}'`, () => { - const defaultValue = commandLineOptions[scenario.option].default; - const changedValue = defaultValue + 42; + Array.from(scenarios).forEach((scenario) => { + describe(`Option '${scenario.option}'`, () => { + const defaultValue = commandLineOptions[scenario.option].default; + const changedValue = defaultValue + 42; - it(`is set to ${defaultValue} by default`, () => { - hooksWorkerClient = new HooksWorkerClient(runner); - assert.equal(hooksWorkerClient[scenario.property], defaultValue); - }); + it(`is set to ${defaultValue} by default`, () => { + hooksWorkerClient = new HooksWorkerClient(runner); + assert.equal(hooksWorkerClient[scenario.property], defaultValue); + }); - it('can be set to a different value', () => { - runner.hooks.configuration.options[scenario.option] = changedValue; - hooksWorkerClient = new HooksWorkerClient(runner); - assert.equal(hooksWorkerClient[scenario.property], changedValue); - }); - }) - )(scenario)); + it('can be set to a different value', () => { + runner.hooks.configuration.options[scenario.option] = changedValue; + hooksWorkerClient = new HooksWorkerClient(runner); + assert.equal(hooksWorkerClient[scenario.property], changedValue); + }); + }); + }); }); });