@@ -951,12 +951,31 @@ class MediaConversionOperation {
951951
952952 try {
953953 await new Promise < void > ( ( resolve , reject ) => {
954+ const recordStdOut : string [ ] = [ ]
955+ const recordStdErr : string [ ] = [ ]
954956 this . spawnedProcess = spawnProcess (
955957 executable ,
956958 args ,
957959 ( ) => {
958960 // On Done
959- resolve ( )
961+ try {
962+ if ( this . conversion . requiredStdOut ?. length ) {
963+ this . requireStringsInProcessOutput ( recordStdOut , this . conversion . requiredStdOut )
964+ }
965+ if ( this . conversion . forbiddenStdOut ?. length ) {
966+ this . requireNoStringsInProcessOutput ( recordStdOut , this . conversion . forbiddenStdOut )
967+ }
968+ if ( this . conversion . requiredStdErr ?. length ) {
969+ this . requireStringsInProcessOutput ( recordStdErr , this . conversion . requiredStdErr )
970+ }
971+ if ( this . conversion . forbiddenStdErr ?. length ) {
972+ this . requireNoStringsInProcessOutput ( recordStdErr , this . conversion . forbiddenStdErr )
973+ }
974+
975+ resolve ( )
976+ } catch ( err ) {
977+ reject ( err )
978+ }
960979 } ,
961980 ( err ) => {
962981 // On Error
@@ -968,6 +987,21 @@ class MediaConversionOperation {
968987 }
969988 // this.logger.silly
970989 )
990+
991+ // Only collect stdout if needed:
992+ if ( this . conversion . requiredStdOut ?. length || this . conversion . forbiddenStdOut ?. length ) {
993+ this . spawnedProcess . execProcess . stdout . on ( 'data' , ( data : Buffer ) => {
994+ const str = data . toString ( )
995+ recordStdOut . push ( str )
996+ } )
997+ }
998+ // Only collect stderr if needed:
999+ if ( this . conversion . requiredStdErr ?. length || this . conversion . forbiddenStdErr ?. length ) {
1000+ this . spawnedProcess . execProcess . stderr . on ( 'data' , ( data : Buffer ) => {
1001+ const str = data . toString ( )
1002+ recordStdErr . push ( str )
1003+ } )
1004+ }
9711005 } )
9721006 } finally {
9731007 this . spawnedProcess = undefined
@@ -1056,6 +1090,38 @@ class MediaConversionOperation {
10561090 }
10571091 )
10581092 }
1093+
1094+ private limitToLastFewLines ( lines : string [ ] , maxLines : number ) : string {
1095+ return lines . slice ( - maxLines ) . join ( '\n' )
1096+ }
1097+ private requireStringsInProcessOutput ( recordStdOut : string [ ] , requiredStrings : string [ ] | undefined ) {
1098+ if ( ! requiredStrings ?. length ) return
1099+
1100+ for ( const requiredStr of requiredStrings ) {
1101+ if ( ! recordStdOut . some ( ( line ) => line . includes ( requiredStr ) ) ) {
1102+ throw new Error (
1103+ `Required string "${ requiredStr } " not found in stdout. Recorded stdout:\n${ this . limitToLastFewLines (
1104+ recordStdOut ,
1105+ 10
1106+ ) } `
1107+ )
1108+ }
1109+ }
1110+ }
1111+ private requireNoStringsInProcessOutput ( recordStdOut : string [ ] , forbiddenStrings : string [ ] | undefined ) {
1112+ if ( ! forbiddenStrings ?. length ) return
1113+
1114+ for ( const forbiddenStr of forbiddenStrings ) {
1115+ if ( recordStdOut . some ( ( line ) => line . includes ( forbiddenStr ) ) ) {
1116+ throw new Error (
1117+ `Forbidden string "${ forbiddenStr } " found in stdout. Recorded stdout:\n${ this . limitToLastFewLines (
1118+ recordStdOut ,
1119+ 10
1120+ ) } `
1121+ )
1122+ }
1123+ }
1124+ }
10591125}
10601126/**
10611127 * This is a pointer to a file, along with the means to access it.
0 commit comments