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

Commit 3d980a1

Browse files
authored
Merge pull request #55 from apiaryio/honzajavorek/running-hooks-handler
Allow running hooks handler using command and/or relative path
2 parents d1a7770 + 3267ce7 commit 3d980a1

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

features/execution_order.feature

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
Feature: Execution order
22

33
Background:
4-
Given I have "{{my-executable-path}}" command installed
5-
And I have "dredd" command installed
4+
Given I have Dredd installed
65
And a file named "server.js" with:
76
"""
87
require('http')

features/failing_transaction.feature

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
Feature: Failing a transaction
22

33
Background:
4-
Given I have "{{my-executable-path}}" command installed
5-
And I have "dredd" command installed
4+
Given I have Dredd installed
65
And a file named "server.js" with:
76
"""
87
require('http')

features/hook_handlers.feature

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
Feature: Hook handlers
22

33
Background:
4-
Given I have "{{my-executable-path}}" command installed
5-
And I have "dredd" command installed
4+
Given I have Dredd installed
65
And a file named "server.js" with:
76
"""
87
require('http')

features/multiple_hookfiles.feature

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
Feature: Multiple hook files with a glob
22

33
Background:
4-
Given I have "{{my-executable-path}}" command installed
5-
And I have "dredd" command installed
4+
Given I have Dredd installed
65
And a file named "server.js" with:
76
"""
87
require('http')

features/support/steps.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ const {
1616
} = require('cucumber');
1717

1818

19-
const DREDD_BIN = path.join(process.cwd(), 'node_modules', '.bin', 'dredd');
20-
21-
2219
Before(function hook() {
2320
this.dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dredd-hooks-template-'));
21+
this.dreddBin = path.join(process.cwd(), 'node_modules', '.bin', 'dredd');
2422
this.env = { ...process.env };
25-
this.commands = [];
2623
this.dataSent = '';
2724
});
2825

@@ -32,11 +29,8 @@ After(async function hook() {
3229
});
3330

3431

35-
Given(/^I have "([^"]+)" command installed$/, (match) => {
36-
const command = match === 'dredd'
37-
? DREDD_BIN
38-
: match;
39-
which.sync(command); // throws if the command is not found
32+
Given('I have Dredd installed', function step() {
33+
which.sync(this.dreddBin); // throws if not found
4034
});
4135

4236
Given(/^a file named "([^"]+)" with:$/, function step(filename, content) {
@@ -48,9 +42,8 @@ Given(/^I set the environment variables to:$/, function step(env) {
4842
});
4943

5044

51-
When(/^I run `([^`]+)`$/, function step(match) {
52-
const command = match.replace(/^dredd(?= )/, DREDD_BIN);
53-
this.proc = childProcess.spawnSync(command, [], {
45+
When(/^I run `dredd ([^`]+)`$/, function step(args) {
46+
this.proc = childProcess.spawnSync(`${this.dreddBin} ${args}`, [], {
5447
shell: true,
5548
cwd: this.dir,
5649
env: this.env,

scripts/smoke-test.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ const os = require('os');
22
const fs = require('fs-extra');
33
const path = require('path');
44
const glob = require('glob');
5+
const which = require('which');
56

67
const packageData = require('../package');
78
const run = require('../cli/run');
89

910

10-
function replacePlaceholders(content) {
11+
function replacePlaceholders(content, handlerCommand = 'dredd-hooks-python') {
1112
return content
12-
.replace(/\{\{my-executable-path\}\}/g, 'dredd-hooks-python')
13+
.replace(/\{\{my-executable-path\}\}/g, handlerCommand)
1314
.replace(/import hooks/g, 'import dredd_hooks as hooks')
1415
.replace(/\.\{\{my-extension\}\}/g, '.py');
1516
}
@@ -34,6 +35,12 @@ function uncommentPythonCodeBlocks(content) {
3435
.join('\n');
3536
}
3637

38+
// https://en.wikipedia.org/wiki/Shebang_(Unix)
39+
function parseShebang(contents) {
40+
const shebang = contents.toString().split(/[\r\n]+/)[0];
41+
return shebang.replace(/^#!/, '');
42+
}
43+
3744

3845
// create a temporary directory and init an npm package there
3946
const testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dredd-hooks-template-test-'));
@@ -50,11 +57,23 @@ run('npm', ['install', tgzPath, '--save-dev'], { cwd: testDir });
5057
// initialize the test suite template
5158
run('npx', ['dredd-hooks-template', 'init'], { cwd: testDir });
5259

60+
// find out what is the relative path to the Python hooks executable so
61+
// we can use it in the test suite (with Python hooks this is not necessary,
62+
// plain 'dredd-hooks-python' would work fine, but we want to test here that
63+
// relative paths and complex commands work with the test suite)
64+
//
65+
// (instead of 'dredd-hooks-python', the handler command is going to be
66+
// something like '../…/bin/python ../…/bin/dredd-hooks-python')
67+
const executablePath = which.sync('dredd-hooks-python');
68+
const pythonPath = parseShebang(fs.readFileSync(executablePath));
69+
const relativeBase = path.join(testDir, 'package.json');
70+
const handlerCommand = `${path.relative(relativeBase, pythonPath)} ${path.relative(relativeBase, executablePath)}`;
71+
5372
// make custom changes to the '*.feature' files so they're able to test
5473
// the Python hooks (reference implementation)
5574
glob.sync(path.join(testDir, '**/*.feature')).forEach((featurePath) => {
5675
const content = fs.readFileSync(featurePath, { encoding: 'utf-8' });
57-
const modifiedContent = uncommentPythonCodeBlocks(replacePlaceholders(content));
76+
const modifiedContent = uncommentPythonCodeBlocks(replacePlaceholders(content, handlerCommand));
5877
fs.writeFileSync(featurePath, modifiedContent, { encoding: 'utf-8' });
5978
});
6079

0 commit comments

Comments
 (0)