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 #55 from apiaryio/honzajavorek/running-hooks-handler
Browse files Browse the repository at this point in the history
Allow running hooks handler using command and/or relative path
  • Loading branch information
honzajavorek authored May 29, 2019
2 parents d1a7770 + 3267ce7 commit 3d980a1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
3 changes: 1 addition & 2 deletions features/execution_order.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Feature: Execution order

Background:
Given I have "{{my-executable-path}}" command installed
And I have "dredd" command installed
Given I have Dredd installed
And a file named "server.js" with:
"""
require('http')
Expand Down
3 changes: 1 addition & 2 deletions features/failing_transaction.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Feature: Failing a transaction

Background:
Given I have "{{my-executable-path}}" command installed
And I have "dredd" command installed
Given I have Dredd installed
And a file named "server.js" with:
"""
require('http')
Expand Down
3 changes: 1 addition & 2 deletions features/hook_handlers.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Feature: Hook handlers

Background:
Given I have "{{my-executable-path}}" command installed
And I have "dredd" command installed
Given I have Dredd installed
And a file named "server.js" with:
"""
require('http')
Expand Down
3 changes: 1 addition & 2 deletions features/multiple_hookfiles.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Feature: Multiple hook files with a glob

Background:
Given I have "{{my-executable-path}}" command installed
And I have "dredd" command installed
Given I have Dredd installed
And a file named "server.js" with:
"""
require('http')
Expand Down
17 changes: 5 additions & 12 deletions features/support/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ const {
} = require('cucumber');


const DREDD_BIN = path.join(process.cwd(), 'node_modules', '.bin', 'dredd');


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

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


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

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


When(/^I run `([^`]+)`$/, function step(match) {
const command = match.replace(/^dredd(?= )/, DREDD_BIN);
this.proc = childProcess.spawnSync(command, [], {
When(/^I run `dredd ([^`]+)`$/, function step(args) {
this.proc = childProcess.spawnSync(`${this.dreddBin} ${args}`, [], {
shell: true,
cwd: this.dir,
env: this.env,
Expand Down
25 changes: 22 additions & 3 deletions scripts/smoke-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ const os = require('os');
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const which = require('which');

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


function replacePlaceholders(content) {
function replacePlaceholders(content, handlerCommand = 'dredd-hooks-python') {
return content
.replace(/\{\{my-executable-path\}\}/g, 'dredd-hooks-python')
.replace(/\{\{my-executable-path\}\}/g, handlerCommand)
.replace(/import hooks/g, 'import dredd_hooks as hooks')
.replace(/\.\{\{my-extension\}\}/g, '.py');
}
Expand All @@ -34,6 +35,12 @@ function uncommentPythonCodeBlocks(content) {
.join('\n');
}

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


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

// find out what is the relative path to the Python hooks executable so
// we can use it in the test suite (with Python hooks this is not necessary,
// plain 'dredd-hooks-python' would work fine, but we want to test here that
// relative paths and complex commands work with the test suite)
//
// (instead of 'dredd-hooks-python', the handler command is going to be
// something like '../…/bin/python ../…/bin/dredd-hooks-python')
const executablePath = which.sync('dredd-hooks-python');
const pythonPath = parseShebang(fs.readFileSync(executablePath));
const relativeBase = path.join(testDir, 'package.json');
const handlerCommand = `${path.relative(relativeBase, pythonPath)} ${path.relative(relativeBase, executablePath)}`;

// make custom changes to the '*.feature' files so they're able to test
// the Python hooks (reference implementation)
glob.sync(path.join(testDir, '**/*.feature')).forEach((featurePath) => {
const content = fs.readFileSync(featurePath, { encoding: 'utf-8' });
const modifiedContent = uncommentPythonCodeBlocks(replacePlaceholders(content));
const modifiedContent = uncommentPythonCodeBlocks(replacePlaceholders(content, handlerCommand));
fs.writeFileSync(featurePath, modifiedContent, { encoding: 'utf-8' });
});

Expand Down

0 comments on commit 3d980a1

Please sign in to comment.