|
| 1 | +const os = require('os'); |
| 2 | +const path = require('path'); |
| 3 | +const util = require('util'); |
| 4 | +const childProcess = require('child_process'); |
| 5 | +const { expect } = require('chai'); |
| 6 | +const fs = require('fs-extra'); |
| 7 | +const net = require('net'); |
| 8 | +const which = require('which'); |
| 9 | +const kill = require('tree-kill'); |
| 10 | +const { Given, When, Then, Before, After } = require('cucumber'); |
| 11 | + |
| 12 | + |
| 13 | +Before(function () { |
| 14 | + this.dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dredd-hooks-template-')); |
| 15 | + this.env = { ...process.env }; |
| 16 | + this.commands = []; |
| 17 | + this.dataSent = ''; |
| 18 | +}); |
| 19 | + |
| 20 | +After(async function () { |
| 21 | + fs.remove(this.dir); |
| 22 | + await util.promisify(kill)(this.proc.pid); |
| 23 | +}); |
| 24 | + |
| 25 | + |
| 26 | +Given(/^I have "([^"]+)" command installed$/, function (command) { |
| 27 | + which.sync(command); // throws if the command is not found |
| 28 | +}); |
| 29 | + |
| 30 | +Given(/^a file named "([^"]+)" with:$/, function (filename, content) { |
| 31 | + fs.writeFileSync(path.join(this.dir, filename), content); |
| 32 | +}); |
| 33 | + |
| 34 | +Given(/^I set the environment variables to:$/, function (env) { |
| 35 | + this.env = { ...this.env, ...env.rowsHash() }; |
| 36 | +}); |
| 37 | + |
| 38 | + |
| 39 | +When(/^I run `([^`]+)`$/, function (command) { |
| 40 | + this.proc = childProcess.spawnSync(command, [], { |
| 41 | + shell: true, |
| 42 | + cwd: this.dir, |
| 43 | + env: this.env, |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +When(/^I run `([^`]+)` interactively$/, function (command) { |
| 48 | + this.proc = childProcess.spawn(command, [], { |
| 49 | + shell: true, |
| 50 | + cwd: this.dir, |
| 51 | + env: this.env, |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +When('I wait for output to contain {string}', function (output, callback) { |
| 56 | + const proc = this.proc; |
| 57 | + |
| 58 | + function read(data) { |
| 59 | + if (data.toString().includes(output)) { |
| 60 | + proc.stdout.removeListener('data', read); |
| 61 | + proc.stderr.removeListener('data', read); |
| 62 | + callback(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + proc.stdout.on('data', read); |
| 67 | + proc.stderr.on('data', read); |
| 68 | +}); |
| 69 | + |
| 70 | +When('I connect to the server', async function () { |
| 71 | + this.socket = new net.Socket(); |
| 72 | + const connect = util.promisify(this.socket.connect.bind(this.socket)); |
| 73 | + await connect(61321, '127.0.0.1'); |
| 74 | +}); |
| 75 | + |
| 76 | +When('I send a JSON message to the socket:', function (message) { |
| 77 | + this.socket.write(message); |
| 78 | + this.dataSent += message; |
| 79 | +}); |
| 80 | + |
| 81 | +When('I send a newline character as a message delimiter to the socket', function () { |
| 82 | + this.socket.write('\n'); |
| 83 | +}); |
| 84 | + |
| 85 | + |
| 86 | +Then('the exit status should be {int}', function (status) { |
| 87 | + expect(this.proc.status).to.equal(status); |
| 88 | +}); |
| 89 | + |
| 90 | +Then('the output should contain:', function (output) { |
| 91 | + expect(this.proc.stdout.toString() + this.proc.stderr.toString()).to.contain(output); |
| 92 | +}); |
| 93 | + |
| 94 | +Then('it should start listening on localhost port {int}', async function (port) { |
| 95 | + this.socket = new net.Socket(); |
| 96 | + const connect = util.promisify(this.socket.connect.bind(this.socket)); |
| 97 | + await connect(port, '127.0.0.1'); // throws if there's an issue |
| 98 | + this.socket.end(); |
| 99 | +}); |
| 100 | + |
| 101 | +Then('I should receive the same response', function (callback) { |
| 102 | + this.socket.on('data', (data) => { |
| 103 | + const dataReceived = JSON.parse(data.toString()); |
| 104 | + const dataSent = JSON.parse(this.dataSent); |
| 105 | + expect(dataReceived).to.deep.equal(dataSent); |
| 106 | + callback(); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +Then('I should be able to gracefully disconnect', function () { |
| 111 | + this.socket.end(); |
| 112 | +}); |
0 commit comments