@@ -44,7 +44,13 @@ function parseFile(filename, recover, env) {
4444 let args = [ "-print" , "ml" ] ;
4545 if ( recover ) args . push ( "-recover" ) ;
4646 args . push ( filename ) ;
47- return env ? cp . spawnSync ( parser , args , { env } ) : cp . spawnSync ( parser , args ) ;
47+ let result = env ? cp . spawnSync ( parser , args , { env } ) : cp . spawnSync ( parser , args ) ;
48+
49+ return {
50+ result : result . stdout . toString ( ) ,
51+ status : result . status ,
52+ errorOutput : result . stderr
53+ }
4854}
4955
5056function parseOcamlFileToNapkin ( filename ) {
@@ -154,7 +160,11 @@ function printFile(filename) {
154160
155161 case "reason" :
156162 parserSrc = "re" ;
157- return parseReasonFileToNapkin ( filename , 80 ) ;
163+ return {
164+ result : parseReasonFileToNapkin ( filename , 80 ) ,
165+ status : 0 ,
166+ errorOutput : ""
167+ } ;
158168 break ;
159169
160170 case "rescript" :
@@ -173,7 +183,13 @@ function printFile(filename) {
173183
174184 args . push ( filename ) ;
175185
176- return cp . spawnSync ( parser , args ) . stdout . toString ( "utf8" ) ;
186+ let result = cp . spawnSync ( parser , args ) ;
187+
188+ return {
189+ result : result . stdout . toString ( "utf8" ) ,
190+ status : result . status ,
191+ errorOutput : result . stderr
192+ }
177193}
178194
179195/* Parser error output format:
@@ -196,16 +212,27 @@ global.runPrinter = (dirname) => {
196212 }
197213
198214 test ( base , ( ) => {
199- let napkin = printFile ( filename ) ;
200- expect ( napkin ) . toMatchSnapshot ( ) ;
215+ let { result, errorOutput, status} = printFile ( filename ) ;
216+ if ( status > 0 ) {
217+ let msg = `Test from file: ${ filename } failed with error output:
218+
219+ ------------ BEGIN ------------
220+ ${ errorOutput }
221+ ------------- END -------------
222+
223+ Make sure the test input is syntactically valid.` ;
224+ fail ( msg ) ;
225+ } else {
226+ expect ( result ) . toMatchSnapshot ( ) ;
227+ }
201228
202229 if ( process . env . ROUNDTRIP_TEST ) {
203230 let intf = isInterface ( filename ) ;
204231 let sexpAst = parseFileToSexp ( filename ) ;
205- let napkin2 = parseNapkinStdinToNapkin ( napkin , intf , 80 ) ;
206- let napkinSexpAst = parseNapkinStdinToSexp ( napkin , intf ) ;
207- expect ( sexpAst ) . toEqual ( napkinSexpAst ) ;
208- expect ( napkin ) . toEqual ( napkin2 ) ;
232+ let result2 = parseNapkinStdinToNapkin ( result , intf , 80 ) ;
233+ let resultSexpAst = parseNapkinStdinToSexp ( result , intf ) ;
234+ expect ( sexpAst ) . toEqual ( resultSexpAst ) ;
235+ expect ( result ) . toEqual ( result2 ) ;
209236 }
210237 } ) ;
211238 } ) ;
@@ -219,20 +246,31 @@ global.runParser = (dirname, recover = false, showError = false, env) => {
219246 }
220247
221248 test ( base , ( ) => {
222- let res = parseFile ( filename , recover , env ) ;
223- let parsetree = res . stdout . toString ( ) ;
224- let output = "" ;
225- if ( showError ) {
226- output += `=====Parsetree==========================================\n` ;
227- output += `${ parsetree } \n` ;
228- output += `=====Errors=============================================\n` ;
229- output += `${ makeReproducibleFilename ( res . stderr . toString ( ) ) } \n` ;
230- output += `========================================================` ;
249+ let { result, errorOutput, status} = parseFile ( filename , recover , env ) ;
250+ if ( status > 0 ) {
251+ let msg = `Test from file: ${ filename } failed with error output:
252+
253+ ------------ BEGIN ------------
254+ ${ errorOutput }
255+ ------------- END -------------
256+
257+ Make sure the test input is syntactically valid or run your test suite with 'recover' set to true.` ;
258+ fail ( msg ) ;
231259 } else {
232- output = parsetree ;
260+ let parsetree = result ;
261+ let output = "" ;
262+ if ( showError ) {
263+ output += `=====Parsetree==========================================\n` ;
264+ output += `${ parsetree } \n` ;
265+ output += `=====Errors=============================================\n` ;
266+ output += `${ makeReproducibleFilename ( errorOutput . toString ( ) ) } \n` ;
267+ output += `========================================================` ;
268+ } else {
269+ output = parsetree ;
270+ }
271+
272+ expect ( output ) . toMatchSnapshot ( ) ;
233273 }
234-
235- expect ( output ) . toMatchSnapshot ( ) ;
236274 } ) ;
237275 } ) ;
238276} ;
0 commit comments