Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #29 from apiaryio/honzajavorek/remove-ruby
Browse files Browse the repository at this point in the history
Rewrite from Ruby to Node.js
  • Loading branch information
honzajavorek authored Apr 26, 2019
2 parents eb2c363 + 72dd669 commit 262304e
Show file tree
Hide file tree
Showing 11 changed files with 541 additions and 124 deletions.
18 changes: 4 additions & 14 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,23 @@ jobs:
steps:
- checkout
- run: npm install
- run: npm run lint:features
- run: npm run lint

test-with-python-hooks:
docker:
- image: circleci/ruby:2.6-node
- image: circleci/python:3.7-node
working_directory: ~/repo
steps:
- checkout
- run:
name: Install dev dependencies (Node.js)
command: npm install
- run:
name: Install the BDD test suite dependencies (Ruby)
command: bundle install
- run:
name: Install the reference hooks implementation (Python)
command: |
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python get-pip.py
sudo pip install dredd_hooks
command: sudo pip install dredd_hooks
- run:
name: Test
command: |
node scripts/prepare-test.js
export PATH=$PATH:$(pwd)/node_modules/.bin
cd ./test
bundle exec cucumber
command: npm test

workflows:
version: 2
Expand Down
7 changes: 0 additions & 7 deletions .travis.example.yml

This file was deleted.

4 changes: 0 additions & 4 deletions Gemfile

This file was deleted.

33 changes: 0 additions & 33 deletions Gemfile.lock

This file was deleted.

42 changes: 0 additions & 42 deletions features/step_definitions/dredd_steps.rb

This file was deleted.

11 changes: 0 additions & 11 deletions features/support/env.rb

This file was deleted.

112 changes: 112 additions & 0 deletions features/support/steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const os = require('os');
const path = require('path');
const util = require('util');
const childProcess = require('child_process');
const { expect } = require('chai');
const fs = require('fs-extra');
const net = require('net');
const which = require('which');
const kill = require('tree-kill');
const { Given, When, Then, Before, After } = require('cucumber');


Before(function () {
this.dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dredd-hooks-template-'));
this.env = { ...process.env };
this.commands = [];
this.dataSent = '';
});

After(async function () {
fs.remove(this.dir);
await util.promisify(kill)(this.proc.pid);
});


Given(/^I have "([^"]+)" command installed$/, function (command) {
which.sync(command); // throws if the command is not found
});

Given(/^a file named "([^"]+)" with:$/, function (filename, content) {
fs.writeFileSync(path.join(this.dir, filename), content);
});

Given(/^I set the environment variables to:$/, function (env) {
this.env = { ...this.env, ...env.rowsHash() };
});


When(/^I run `([^`]+)`$/, function (command) {
this.proc = childProcess.spawnSync(command, [], {
shell: true,
cwd: this.dir,
env: this.env,
});
});

When(/^I run `([^`]+)` interactively$/, function (command) {
this.proc = childProcess.spawn(command, [], {
shell: true,
cwd: this.dir,
env: this.env,
});
});

When('I wait for output to contain {string}', function (output, callback) {
const proc = this.proc;

function read(data) {
if (data.toString().includes(output)) {
proc.stdout.removeListener('data', read);
proc.stderr.removeListener('data', read);
callback();
}
}

proc.stdout.on('data', read);
proc.stderr.on('data', read);
});

When('I connect to the server', async function () {
this.socket = new net.Socket();
const connect = util.promisify(this.socket.connect.bind(this.socket));
await connect(61321, '127.0.0.1');
});

When('I send a JSON message to the socket:', function (message) {
this.socket.write(message);
this.dataSent += message;
});

When('I send a newline character as a message delimiter to the socket', function () {
this.socket.write('\n');
});


Then('the exit status should be {int}', function (status) {
expect(this.proc.status).to.equal(status);
});

Then('the output should contain:', function (output) {
expect(this.proc.stdout.toString() + this.proc.stderr.toString()).to.contain(output);
});

Then('it should start listening on localhost port {int}', async function (port) {
this.socket = new net.Socket();
const connect = util.promisify(this.socket.connect.bind(this.socket));
await connect(port, '127.0.0.1'); // throws if there's an issue
this.socket.end();
});

Then('I should receive the same response', function (callback) {
this.socket.on('data', (data) => {
const dataReceived = JSON.parse(data.toString());
const dataSent = JSON.parse(this.dataSent);
expect(dataReceived).to.deep.equal(dataSent);
callback();
});
});

Then('I should be able to gracefully disconnect', function () {
this.socket.end();
});
12 changes: 6 additions & 6 deletions features/tcp_server.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Feature: TCP server and messages
Scenario: TCP server
When I run `dredd-hooks-{{mylanguage}}` interactively
And I wait for output to contain "Starting"
Then It should start listening on localhost port "61321"
Then it should start listening on localhost port 61321

Scenario: Message exchange for event beforeEach
Given I run `dredd-hooks-{{mylanguage}}` interactively
Expand All @@ -14,7 +14,7 @@ Scenario: Message exchange for event beforeEach
{"event": "beforeEach", "uuid": "1234-abcd", "data": {"key":"value"}}
"""
And I send a newline character as a message delimiter to the socket
Then I should receive same response
Then I should receive the same response
And I should be able to gracefully disconnect

Scenario: Message exchange for event beforeEachValidation
Expand All @@ -26,7 +26,7 @@ Scenario: Message exchange for event beforeEachValidation
{"event": "beforeEachValidation", "uuid": "2234-abcd", "data": {"key":"value"}}
"""
And I send a newline character as a message delimiter to the socket
Then I should receive same response
Then I should receive the same response
And I should be able to gracefully disconnect

Scenario: Message exchange for event afterEach
Expand All @@ -38,7 +38,7 @@ Scenario: Message exchange for event afterEach
{"event": "afterEach", "uuid": "3234-abcd", "data": {"key":"value"}}
"""
And I send a newline character as a message delimiter to the socket
Then I should receive same response
Then I should receive the same response
And I should be able to gracefully disconnect

Scenario: Message exchange for event beforeAll
Expand All @@ -50,7 +50,7 @@ Scenario: Message exchange for event beforeAll
{"event": "beforeAll", "uuid": "4234-abcd", "data": {"key":"value"}}
"""
And I send a newline character as a message delimiter to the socket
Then I should receive same response
Then I should receive the same response
And I should be able to gracefully disconnect

Scenario: Message exchange for event afterAll
Expand All @@ -62,5 +62,5 @@ Scenario: Message exchange for event afterAll
{"event": "afterAll", "uuid": "5234-abcd", "data": {"key":"value"}}
"""
And I send a newline character as a message delimiter to the socket
Then I should receive same response
Then I should receive the same response
And I should be able to gracefully disconnect
Loading

0 comments on commit 262304e

Please sign in to comment.