@@ -301,19 +301,38 @@ export function formatSnapshot(
301301 return out . join ( '' ) ;
302302}
303303
304- export function writeSnapshot ( outputPath : string , obtained : string ) : void {
304+ export function writeSnapshot ( outputPath : string , obtained : string , tempDir ?: string ) : void {
305+ let finalPath : string ;
306+ let targetDir : string ;
307+
308+ if ( tempDir ) {
309+ // Extract the relative path from the test output directory to preserve structure
310+ const outputParts = outputPath . split ( path . sep ) ;
311+ const testOutputIndex = outputParts . findIndex ( part => part === 'output' ) + 1 ;
312+ if ( testOutputIndex > 0 && testOutputIndex < outputParts . length ) {
313+ const relativeStructure = outputParts . slice ( testOutputIndex + 1 ) . join ( path . sep ) ;
314+ finalPath = path . join ( tempDir , relativeStructure ) ;
315+ } else {
316+ finalPath = path . join ( tempDir , path . basename ( outputPath ) ) ;
317+ }
318+ targetDir = path . dirname ( finalPath ) ;
319+ } else {
320+ finalPath = outputPath ;
321+ targetDir = path . dirname ( outputPath ) ;
322+ }
323+
305324 // eslint-disable-next-line no-sync
306- fs . mkdirSync ( path . dirname ( outputPath ) , {
325+ fs . mkdirSync ( targetDir , {
307326 recursive : true ,
308327 } ) ;
309328 // eslint-disable-next-line no-sync
310- fs . writeFileSync ( outputPath , obtained , { flag : 'w' } ) ;
329+ fs . writeFileSync ( finalPath , obtained , { flag : 'w' } ) ;
311330}
312331
313- export function diffSnapshot ( outputPath : string , obtained : string ) : void {
332+ export function diffSnapshot ( outputPath : string , obtained : string ) : 'equal' | 'different' {
314333 let existing = fs . readFileSync ( outputPath , { encoding : 'utf8' } ) ;
315334 if ( obtained === existing ) {
316- return ;
335+ return 'equal' ;
317336 }
318337
319338 console . error (
@@ -326,7 +345,22 @@ export function diffSnapshot(outputPath: string, obtained: string): void {
326345 '(what the current code produces). Run the command "npm run update-snapshots" to accept the new behavior.'
327346 )
328347 ) ;
329- exit ( 1 ) ;
348+ return 'different' ;
349+ }
350+
351+
352+
353+ export function checkSnapshotMatch ( outputPath : string , obtained : string ) : boolean {
354+ try {
355+ if ( ! fs . existsSync ( outputPath ) ) {
356+ return false ;
357+ }
358+
359+ const existing = fs . readFileSync ( outputPath , { encoding : 'utf8' } ) ;
360+ return obtained === existing ;
361+ } catch ( error ) {
362+ return false ;
363+ }
330364}
331365
332366function occurrencesByLine ( a : scip . Occurrence , b : scip . Occurrence ) : number {
0 commit comments