5
5
6
6
import assert = require( 'assert' ) ;
7
7
import { readdirSync , readFileSync , existsSync , writeFileSync } from 'fs' ;
8
- import { join } from 'path' ;
8
+ import { join , resolve } from 'path' ;
9
9
import { FileAccess } from 'vs/base/common/network' ;
10
10
import { SmartLinesDiffComputer } from 'vs/editor/common/diff/smartLinesDiffComputer' ;
11
11
import { StandardLinesDiffComputer } from 'vs/editor/common/diff/standardLinesDiffComputer' ;
12
12
13
13
suite ( '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 ) ;
16
19
17
20
for ( const folder of folders ) {
18
21
for ( const diffingAlgoName of [ 'smart' , 'experimental' ] ) {
19
-
20
22
test ( `${ folder } -${ diffingAlgoName } ` , ( ) => {
21
- const folderPath = join ( fixturesDir , folder ) ;
23
+ const folderPath = join ( fixturesSrcDir , folder ) ;
22
24
const files = readdirSync ( folderPath ) ;
23
25
24
26
const firstFileName = files . find ( f => f . startsWith ( '1.' ) ) ! ;
@@ -31,7 +33,7 @@ suite('diff fixtures', () => {
31
33
32
34
const diff = diffingAlgo . computeDiff ( firstContentLines , secondContentLines , { ignoreTrimWhitespace : false , maxComputationTime : Number . MAX_SAFE_INTEGER } ) ;
33
35
34
- const diffingResult : DiffingResult = {
36
+ const actualDiffingResult : DiffingResult = {
35
37
originalFileName : `./${ firstFileName } ` ,
36
38
modifiedFileName : `./${ secondFileName } ` ,
37
39
diffs : diff . changes . map < IDetailedDiff > ( c => ( {
@@ -44,29 +46,34 @@ suite('diff fixtures', () => {
44
46
} ) )
45
47
} ;
46
48
47
- const actualFilePath = join ( folderPath , `${ diffingAlgoName } .actual.diff.json` ) ;
48
49
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' ) ;
49
53
50
- const expectedFileContent = JSON . stringify ( diffingResult , null , '\t' ) ;
54
+ const invalidExists = existsSync ( invalidFilePath ) ;
51
55
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 .' ) ;
56
60
} 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 ) ;
59
63
60
64
try {
61
- assert . deepStrictEqual ( actualFileDiffResult , diffingResult ) ;
65
+ assert . deepStrictEqual ( actualDiffingResult , expectedFileDiffResult ) ;
62
66
} catch ( e ) {
63
- writeFileSync ( expectedFilePath , expectedFileContent ) ;
67
+ if ( ! invalidExists ) {
68
+ writeFileSync ( invalidFilePath , expectedFileContent ) ;
69
+ }
70
+ writeFileSync ( expectedFilePath , expectedFileContentFromActual ) ;
64
71
throw e ;
65
72
}
66
73
}
67
74
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 .' ) ;
70
77
}
71
78
} ) ;
72
79
}
0 commit comments