@@ -56,6 +56,34 @@ function runSnapshotCli(
5656 } ) ;
5757}
5858
59+ function readProcessOutput ( output : string | Buffer | null | undefined ) : string {
60+ return typeof output === 'string' ? output : ( output ?. toString ( 'utf8' ) ?? '' ) ;
61+ }
62+
63+ export function assertCliSnapshotProcessResult (
64+ result : Pick < ReturnType < typeof spawnSync > , 'error' | 'signal' | 'status' | 'stderr' > ,
65+ label : string ,
66+ ) : void {
67+ if ( result . error ) {
68+ throw new Error ( `CLI process failed for ${ label } : ${ result . error . message } ` ) ;
69+ }
70+
71+ if ( result . signal ) {
72+ throw new Error ( `CLI process for ${ label } was terminated by signal ${ result . signal } .` ) ;
73+ }
74+
75+ if ( result . status === null ) {
76+ throw new Error (
77+ `CLI process exit status was null for ${ label } ; the process may have timed out or been killed by a signal.` ,
78+ ) ;
79+ }
80+
81+ const stderr = readProcessOutput ( result . stderr ) . trim ( ) ;
82+ if ( stderr . length > 0 ) {
83+ throw new Error ( `CLI process emitted unexpected stderr for ${ label } :\n${ stderr } ` ) ;
84+ }
85+ }
86+
5987function parseStructuredEnvelope (
6088 stdout : string ,
6189 label : string ,
@@ -107,9 +135,10 @@ export async function createSnapshotHarness(
107135 throw new Error ( `Tool '${ cliToolName } ' in workflow '${ workflow } ' is not CLI-available` ) ;
108136 }
109137
138+ const label = `${ workflow } /${ cliToolName } ` ;
110139 const result = runSnapshotCli ( workflow , cliToolName , args , 'text' , options ) ;
111- const stdout =
112- typeof result . stdout === 'string' ? result . stdout : ( result . stdout ?. toString ( 'utf8' ) ?? '' ) ;
140+ assertCliSnapshotProcessResult ( result , label ) ;
141+ const stdout = readProcessOutput ( result . stdout ) ;
113142
114143 return {
115144 text : normalizeSnapshotOutput ( stdout ) ,
@@ -141,19 +170,16 @@ export async function createCliJsonSnapshotHarness(
141170 throw new Error ( `Tool '${ cliToolName } ' in workflow '${ workflow } ' is not CLI-available` ) ;
142171 }
143172
173+ const label = `${ workflow } /${ cliToolName } ` ;
144174 const result = runSnapshotCli ( workflow , cliToolName , args , 'json' , options ) ;
145- const stdout =
146- typeof result . stdout === 'string' ? result . stdout : ( result . stdout ?. toString ( 'utf8' ) ?? '' ) ;
147- const envelope = parseStructuredEnvelope ( stdout , ` ${ workflow } / ${ cliToolName } ` ) ;
175+ assertCliSnapshotProcessResult ( result , label ) ;
176+ const stdout = readProcessOutput ( result . stdout ) ;
177+ const envelope = parseStructuredEnvelope ( stdout , label ) ;
148178
149179 return {
150180 text : formatStructuredEnvelopeFixture ( envelope ) ,
151181 rawText : stdout ,
152- isError : resolveCliJsonSnapshotErrorState (
153- result . status ,
154- envelope ,
155- `${ workflow } /${ cliToolName } ` ,
156- ) ,
182+ isError : resolveCliJsonSnapshotErrorState ( result . status , envelope , label ) ,
157183 structuredEnvelope : envelope ,
158184 } ;
159185 }
0 commit comments