@@ -2,14 +2,15 @@ const os = require('os');
22const fs = require ( 'fs-extra' ) ;
33const path = require ( 'path' ) ;
44const glob = require ( 'glob' ) ;
5+ const which = require ( 'which' ) ;
56
67const packageData = require ( '../package' ) ;
78const run = require ( '../cli/run' ) ;
89
910
10- function replacePlaceholders ( content ) {
11+ function replacePlaceholders ( content , handlerCommand = 'dredd-hooks-python' ) {
1112 return content
12- . replace ( / \{ \{ m y - e x e c u t a b l e - p a t h \} \} / g, 'dredd-hooks-python' )
13+ . replace ( / \{ \{ m y - e x e c u t a b l e - p a t h \} \} / g, handlerCommand )
1314 . replace ( / i m p o r t h o o k s / g, 'import dredd_hooks as hooks' )
1415 . replace ( / \. \{ \{ m y - e x t e n s i o n \} \} / 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
3946const 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
5158run ( '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)
5574glob . 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