@@ -20,7 +20,8 @@ import { unzip } from "../src/core/zip.ts";
2020import { dirAndStem , safeRemoveSync , which } from "../src/core/path.ts" ;
2121import { isWindows } from "../src/deno_ral/platform.ts" ;
2222import { execProcess , ExecProcessOptions } from "../src/core/process.ts" ;
23- import { canonicalizeSnapshot , checkSnapshot } from "./verify-snapshot.ts" ;
23+ import { checkSnapshot , generateSnapshotDiff , generateInlineDiff , WordDiffPart } from "./verify-snapshot.ts" ;
24+ import * as colors from "fmt/colors" ;
2425
2526export const withDocxContent = async < T > (
2627 file : string ,
@@ -440,18 +441,87 @@ export const ensureHtmlElementCount = (
440441 } ;
441442} ;
442443
444+ const printColoredDiff = ( diff : string ) => {
445+ for ( const line of diff . split ( "\n" ) ) {
446+ if ( line . startsWith ( "+" ) && ! line . startsWith ( "+++" ) ) {
447+ console . log ( colors . green ( line ) ) ;
448+ } else if ( line . startsWith ( "-" ) && ! line . startsWith ( "---" ) ) {
449+ console . log ( colors . red ( line ) ) ;
450+ } else if ( line . startsWith ( "@@" ) ) {
451+ console . log ( colors . dim ( line ) ) ;
452+ } else {
453+ console . log ( line ) ;
454+ }
455+ }
456+ } ;
457+
458+ const escapeWhitespace = ( s : string ) : string => {
459+ return s . replace ( / \n / g, "⏎\\n" ) . replace ( / \t / g, "→\\t" ) . replace ( / / g, "·" ) ;
460+ } ;
461+
462+ const printCompactInlineDiff = ( parts : WordDiffPart [ ] ) => {
463+ const chunks : string [ ] = [ ] ;
464+ let currentChunk = "" ;
465+ let hasChanges = false ;
466+
467+ for ( let i = 0 ; i < parts . length ; i ++ ) {
468+ const part = parts [ i ] ;
469+ if ( part . added || part . removed ) {
470+ hasChanges = true ;
471+ const displayValue = / ^ \s + $ / . test ( part . value ) ? escapeWhitespace ( part . value ) : part . value ;
472+ if ( part . added ) {
473+ currentChunk += colors . bgGreen ( colors . black ( displayValue ) ) ;
474+ } else {
475+ currentChunk += colors . bgRed ( colors . white ( displayValue ) ) ;
476+ }
477+ } else {
478+ if ( hasChanges ) {
479+ const contextBefore = part . value . slice ( 0 , 40 ) ;
480+ chunks . push ( currentChunk + colors . dim ( contextBefore + ( part . value . length > 40 ? "..." : "" ) ) ) ;
481+ currentChunk = "" ;
482+ hasChanges = false ;
483+ }
484+ const nextHasChange = parts . slice ( i + 1 ) . some ( p => p . added || p . removed ) ;
485+ if ( nextHasChange ) {
486+ const contextAfter = part . value . slice ( - 40 ) ;
487+ currentChunk = colors . dim ( ( part . value . length > 40 ? "..." : "" ) + contextAfter ) ;
488+ }
489+ }
490+ }
491+ if ( currentChunk ) {
492+ chunks . push ( currentChunk ) ;
493+ }
494+
495+ for ( const chunk of chunks ) {
496+ console . log ( chunk ) ;
497+ console . log ( "" ) ;
498+ }
499+ } ;
500+
443501export const ensureSnapshotMatches = (
444502 file : string ,
445503) : Verify => {
446504 return {
447505 name : "Inspecting Snapshot" ,
448506 verify : async ( _output : ExecuteOutput [ ] ) => {
449507 const good = await checkSnapshot ( file ) ;
508+ const diffFile = file + ".diff" ;
450509 if ( ! good ) {
451- console . log ( "output:" ) ;
452- console . log ( await canonicalizeSnapshot ( file ) ) ;
453- console . log ( "snapshot:" ) ;
454- console . log ( await canonicalizeSnapshot ( file + ".snapshot" ) ) ;
510+ const diff = await generateSnapshotDiff ( file ) ;
511+ const inlineParts = await generateInlineDiff ( file ) ;
512+
513+ await Deno . writeTextFile ( diffFile , diff ) ;
514+ console . log ( `\nDiff saved to: ${ diffFile } ` ) ;
515+
516+ console . log ( "\n--- Unified Diff ---" ) ;
517+ printColoredDiff ( diff ) ;
518+ console . log ( "--- End Unified Diff ---\n" ) ;
519+
520+ console . log ( "--- Word-level Changes (with context) ---" ) ;
521+ printCompactInlineDiff ( inlineParts ) ;
522+ console . log ( "--- End Word-level Changes ---\n" ) ;
523+ } else {
524+ safeRemoveSync ( diffFile ) ;
455525 }
456526 assert (
457527 good ,
0 commit comments