This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Rewrite from Ruby to Node.js #29
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
afd57c4
chore: add cucumber as a dev dependency
honzajavorek f5f77a4
chore: remove Ruby from CI
honzajavorek 863eb96
refactor: rewrite some basic steps to Node.js
honzajavorek 847944a
refactor: rewrite to Node.js
honzajavorek 72dd669
chore: fix Node.js version required
honzajavorek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In our ES6 style guide, this and all the closures below are not permitted as they miss function names. |
||
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not saying you have to change this, but you can avoid using sudo with:
Then to use any binary it provides:
I believe Python would automatically pick up the library from
~/.local
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's neat. I think it doesn't matter in CI, but it's good to know this is so easy now. (Or was it the same in the past as well? 🤔 )