From 24792934105f62b1abf760fc900ca7c25c82edfd Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 2 Nov 2018 15:04:17 -0400 Subject: [PATCH] Parser: Pass-through PHP errors in tests (#11429) 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. --- .../shared-tests.js | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/block-serialization-spec-parser/shared-tests.js b/packages/block-serialization-spec-parser/shared-tests.js index e9b90337061ee..ca9fde49dc350 100644 --- a/packages/block-serialization-spec-parser/shared-tests.js +++ b/packages/block-serialization-spec-parser/shared-tests.js @@ -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 ) ) : () => {} + } ) : () => {} );