55
66import assert = require( 'assert' ) ;
77import { readdirSync , readFileSync , existsSync , writeFileSync } from 'fs' ;
8- import { join } from 'path' ;
8+ import { join , resolve } from 'path' ;
99import { FileAccess } from 'vs/base/common/network' ;
1010import { SmartLinesDiffComputer } from 'vs/editor/common/diff/smartLinesDiffComputer' ;
1111import { StandardLinesDiffComputer } from 'vs/editor/common/diff/standardLinesDiffComputer' ;
1212
1313suite ( 'diff fixtures' , ( ) => {
14- const fixturesDir = FileAccess . asFileUri ( 'vs/editor/test/node/diffing/fixtures' ) . fsPath ;
15- const folders = readdirSync ( fixturesDir ) ;
14+ const fixturesOutDir = FileAccess . asFileUri ( 'vs/editor/test/node/diffing/fixtures' ) . fsPath ;
15+ // We want the dir in src, so we can directly update the source files if they disagree and create invalid files to capture the previous state.
16+ // This makes it very easy to update the fixtures.
17+ const fixturesSrcDir = resolve ( fixturesOutDir ) . replaceAll ( '\\' , '/' ) . replace ( '/out/vs/editor/' , '/src/vs/editor/' ) ;
18+ const folders = readdirSync ( fixturesSrcDir ) ;
1619
1720 for ( const folder of folders ) {
1821 for ( const diffingAlgoName of [ 'smart' , 'experimental' ] ) {
19-
2022 test ( `${ folder } -${ diffingAlgoName } ` , ( ) => {
21- const folderPath = join ( fixturesDir , folder ) ;
23+ const folderPath = join ( fixturesSrcDir , folder ) ;
2224 const files = readdirSync ( folderPath ) ;
2325
2426 const firstFileName = files . find ( f => f . startsWith ( '1.' ) ) ! ;
@@ -31,7 +33,7 @@ suite('diff fixtures', () => {
3133
3234 const diff = diffingAlgo . computeDiff ( firstContentLines , secondContentLines , { ignoreTrimWhitespace : false , maxComputationTime : Number . MAX_SAFE_INTEGER } ) ;
3335
34- const diffingResult : DiffingResult = {
36+ const actualDiffingResult : DiffingResult = {
3537 originalFileName : `./${ firstFileName } ` ,
3638 modifiedFileName : `./${ secondFileName } ` ,
3739 diffs : diff . changes . map < IDetailedDiff > ( c => ( {
@@ -44,29 +46,34 @@ suite('diff fixtures', () => {
4446 } ) )
4547 } ;
4648
47- const actualFilePath = join ( folderPath , `${ diffingAlgoName } .actual.diff.json` ) ;
4849 const expectedFilePath = join ( folderPath , `${ diffingAlgoName } .expected.diff.json` ) ;
50+ const invalidFilePath = join ( folderPath , `${ diffingAlgoName } .invalid.diff.json` ) ;
51+
52+ const expectedFileContentFromActual = JSON . stringify ( actualDiffingResult , null , '\t' ) ;
4953
50- const expectedFileContent = JSON . stringify ( diffingResult , null , '\t' ) ;
54+ const invalidExists = existsSync ( invalidFilePath ) ;
5155
52- if ( ! existsSync ( actualFilePath ) ) {
53- writeFileSync ( actualFilePath , expectedFileContent ) ;
54- writeFileSync ( expectedFilePath , expectedFileContent ) ;
55- throw new Error ( 'No actual file! Actual and expected files were written.' ) ;
56+ if ( ! existsSync ( expectedFilePath ) ) {
57+ writeFileSync ( expectedFilePath , expectedFileContentFromActual ) ;
58+ writeFileSync ( invalidFilePath , '' ) ;
59+ throw new Error ( 'No expected file! Expected and invalid files were written. Delete the invalid file to make the test pass .' ) ;
5660 } else {
57- const actualFileContent = readFileSync ( actualFilePath , 'utf8' ) ;
58- const actualFileDiffResult : DiffingResult = JSON . parse ( actualFileContent ) ;
61+ const expectedFileContent = readFileSync ( invalidExists ? invalidFilePath : expectedFilePath , 'utf8' ) ;
62+ const expectedFileDiffResult : DiffingResult = JSON . parse ( expectedFileContent ) ;
5963
6064 try {
61- assert . deepStrictEqual ( actualFileDiffResult , diffingResult ) ;
65+ assert . deepStrictEqual ( actualDiffingResult , expectedFileDiffResult ) ;
6266 } catch ( e ) {
63- writeFileSync ( expectedFilePath , expectedFileContent ) ;
67+ if ( ! invalidExists ) {
68+ writeFileSync ( invalidFilePath , expectedFileContent ) ;
69+ }
70+ writeFileSync ( expectedFilePath , expectedFileContentFromActual ) ;
6471 throw e ;
6572 }
6673 }
6774
68- if ( existsSync ( expectedFilePath ) ) {
69- throw new Error ( 'Expected file exists! Please delete it .' ) ;
75+ if ( invalidExists ) {
76+ throw new Error ( 'Invalid file exists and agrees with expected file! Delete the invalid file to make the test pass .' ) ;
7077 }
7178 } ) ;
7279 }
0 commit comments