Skip to content

Commit

Permalink
Parser: Pass-through PHP errors in tests (#11429)
Browse files Browse the repository at this point in the history
In #11320 we started running all parsers against the same set of unit
tests. Unfortunately when the PHP-based parsers failed inside the PHP
process or returned non-JSON data then the tests would fail without
providing any information about why.

My personal workaround was to manually run the PHP test runner from
the command line to see the output. This was inefficient.

In this patch we're trapping the response code from the PHP test runner
and throwing an `Error` with the output from `php` so that our `jest`
tests can see and report them. This will make it easier debug failing
tests.
  • Loading branch information
dmsnell committed Nov 2, 2018
1 parent 122e569 commit 2479293
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions packages/block-serialization-spec-parser/shared-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,25 @@ const makeTest = hasPHP ? ( ...args ) => describe( ...args ) : ( ...args ) => de

export const phpTester = ( name, filename ) => makeTest(
name,
'test' === process.env.NODE_ENV ? jsTester( ( doc ) => JSON.parse( require( 'child_process' ).spawnSync(
'php',
[ '-f', filename ],
{
input: doc,
encoding: 'utf8',
timeout: 30 * 1000, // abort after 30 seconds, that's too long anyway
'test' === process.env.NODE_ENV ? jsTester( ( doc ) => {
const process = require( 'child_process' ).spawnSync(
'php',
[ '-f', filename ],
{
input: doc,
encoding: 'utf8',
timeout: 30 * 1000, // abort after 30 seconds, that's too long anyway
}
);

if ( process.status !== 0 ) {
throw new Error( process.stderr || process.stdout );
}

try {
return JSON.parse( process.stdout );
} catch ( e ) {
throw new Error( 'failed to parse JSON:\n' + process.stdout );
}
).stdout ) ) : () => {}
} ) : () => {}
);

0 comments on commit 2479293

Please sign in to comment.