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

Commit 262304e

Browse files
authored
Merge pull request #29 from apiaryio/honzajavorek/remove-ruby
Rewrite from Ruby to Node.js
2 parents eb2c363 + 72dd669 commit 262304e

File tree

11 files changed

+541
-124
lines changed

11 files changed

+541
-124
lines changed

.circleci/config.yml

+4-14
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,23 @@ jobs:
77
steps:
88
- checkout
99
- run: npm install
10-
- run: npm run lint:features
10+
- run: npm run lint
1111

1212
test-with-python-hooks:
1313
docker:
14-
- image: circleci/ruby:2.6-node
14+
- image: circleci/python:3.7-node
1515
working_directory: ~/repo
1616
steps:
1717
- checkout
1818
- run:
1919
name: Install dev dependencies (Node.js)
2020
command: npm install
21-
- run:
22-
name: Install the BDD test suite dependencies (Ruby)
23-
command: bundle install
2421
- run:
2522
name: Install the reference hooks implementation (Python)
26-
command: |
27-
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
28-
sudo python get-pip.py
29-
sudo pip install dredd_hooks
23+
command: sudo pip install dredd_hooks
3024
- run:
3125
name: Test
32-
command: |
33-
node scripts/prepare-test.js
34-
export PATH=$PATH:$(pwd)/node_modules/.bin
35-
cd ./test
36-
bundle exec cucumber
26+
command: npm test
3727

3828
workflows:
3929
version: 2

.travis.example.yml

-7
This file was deleted.

Gemfile

-4
This file was deleted.

Gemfile.lock

-33
This file was deleted.

features/step_definitions/dredd_steps.rb

-42
This file was deleted.

features/support/env.rb

-11
This file was deleted.

features/support/steps.js

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
});

features/tcp_server.feature

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Feature: TCP server and messages
33
Scenario: TCP server
44
When I run `dredd-hooks-{{mylanguage}}` interactively
55
And I wait for output to contain "Starting"
6-
Then It should start listening on localhost port "61321"
6+
Then it should start listening on localhost port 61321
77

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

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

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

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

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

0 commit comments

Comments
 (0)