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

Commit

Permalink
refactor: rewrite to Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek committed Apr 25, 2019
1 parent 863eb96 commit 4a27f13
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 121 deletions.
8 changes: 2 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
steps:
- checkout
- run: npm install
- run: npm run lint:features
- run: npm run lint

test-with-python-hooks:
docker:
Expand All @@ -23,11 +23,7 @@ jobs:
command: sudo pip install dredd_hooks
- run:
name: Test
command: |
node scripts/prepare-test.js
export PATH=$PATH:$(pwd)/node_modules/.bin
cd ./test
cucumber-js features
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.

76 changes: 70 additions & 6 deletions features/support/steps.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
const { expect } = require('chai');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const which = require('which');
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(function () {
After(async function () {
fs.remove(this.dir);
await util.promisify(kill)(this.proc.pid);
});


Expand All @@ -38,11 +44,69 @@ When(/^I run `([^`]+)`$/, function (command) {
});
});

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);
});

Then('the exit status should be {int}', function (number) {
expect(this.proc.status).to.equal(parseInt(number, 10));
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
18 changes: 14 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
"fs-extra": "7.0.1",
"gherkin-lint": "3.0.3",
"glob": "7.1.3",
"tree-kill": "1.2.1",
"which": "1.3.1"
},
"scripts": {
"lint:features": "gherkin-lint features/"
"test": "node scripts/test.js",
"lint": "gherkin-lint features/"
},
"repository": {
"type": "git",
Expand Down
11 changes: 10 additions & 1 deletion scripts/prepare-test.js → scripts/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const { spawnSync } = require('child_process');


const PROJECT_DIR = path.join(__dirname, '..')
const PROJECT_DIR = path.join(__dirname, '..');
const TEST_DIR = path.join(PROJECT_DIR, 'test');


Expand Down Expand Up @@ -45,3 +46,11 @@ glob.sync(path.join(TEST_DIR, '**/*.feature')).forEach((featurePath) => {
const modifiedContent = uncommentPythonCodeBlocks(replacePlaceholders(content));
fs.writeFileSync(featurePath, modifiedContent, { encoding: 'utf-8' });
})

const binDir = path.join(PROJECT_DIR, 'node_modules', '.bin');
const featuresDir = path.join(TEST_DIR, 'features');

const PATH = process.env.PATH.split(path.delimiter).concat([binDir]).join(path.delimiter);
const env = { ...process.env, PATH };

spawnSync('cucumber-js', [featuresDir], { cwd: TEST_DIR, env, stdio: 'inherit' });

0 comments on commit 4a27f13

Please sign in to comment.