@@ -2,62 +2,199 @@ import * as path from 'path';
2
2
3
3
import { TSDocConfigFile } from '../TSDocConfigFile' ;
4
4
5
- function testLoadingFolder ( assetPath : string ) : unknown {
6
- const configFile : TSDocConfigFile = TSDocConfigFile . loadForFolder ( path . join ( __dirname , assetPath ) ) ;
5
+ interface ISnapshot {
6
+ _0_filePath : string ;
7
+ _1_fileNotFound : boolean ;
8
+ _2_hasErrors : boolean ;
9
+ _3_errorSummary : string ;
10
+ _4_log : string [ ] ;
11
+ _5_extends : ISnapshot [ ] ;
12
+ }
13
+
14
+ // To make the unit tests deterministic, we need to replace all OS-dependent absolute paths
15
+ // with OS-independent paths that are relative to the unit test folder.
16
+ function makeStablePath ( testPath : string ) : string {
17
+ if ( testPath . length === 0 ) {
18
+ return '' ;
19
+ }
20
+ return '.../' + path . relative ( __dirname , testPath ) . split ( '\\' ) . join ( '/' ) ;
21
+ }
22
+
23
+ // Build a map from absolute path --> stable path, for each TSDocConfigFile.filePath value
24
+ function buildStablePathMap ( stablePathMap : Map < string , string > , configFile : TSDocConfigFile ) : void {
25
+ if ( ! stablePathMap . has ( configFile . filePath ) ) {
26
+ stablePathMap . set ( configFile . filePath , makeStablePath ( configFile . filePath ) ) ;
27
+ }
28
+ for ( const extendsFile of configFile . extendsFiles ) {
29
+ buildStablePathMap ( stablePathMap , extendsFile ) ;
30
+ }
31
+ }
7
32
33
+ // Search and replace all absolute paths with the corresponding stable path.
34
+ // For example, "Found C:\A\B\C.txt in C:\A\D\E.txt" becomes "Found .../B/C.txt in .../D/E.txt".
35
+ function convertToStablePaths ( text : string , stablePathMap : Map < string , string > ) : string {
36
+ for ( const pair of Array . from ( stablePathMap . entries ( ) ) ) {
37
+ text = text . split ( pair [ 0 ] ) . join ( pair [ 1 ] ) ;
38
+ }
39
+ return text ;
40
+ }
41
+
42
+ function createSnapshot ( configFile : TSDocConfigFile , pathFixupMap : Map < string , string > ) : ISnapshot {
8
43
return {
9
- fileNotFound : configFile . fileNotFound ,
10
- hasErrors : configFile . hasErrors ,
11
- log : configFile . log . messages . map ( ( x ) => `[${ x . messageId } ] ${ x . text } ` ) ,
44
+ _0_filePath : convertToStablePaths ( configFile . filePath , pathFixupMap ) ,
45
+ _1_fileNotFound : configFile . fileNotFound ,
46
+ _2_hasErrors : configFile . hasErrors ,
47
+ _4_log : configFile . log . messages . map ( ( x ) => `[${ x . messageId } ] ${ convertToStablePaths ( x . text , pathFixupMap ) } ` ) ,
48
+ _5_extends : configFile . extendsFiles . map ( ( x ) => createSnapshot ( x , pathFixupMap ) ) ,
49
+ _3_errorSummary : convertToStablePaths ( configFile . getErrorSummary ( ) , pathFixupMap ) ,
12
50
} ;
13
51
}
14
52
53
+ function testLoadingFolder ( assetPath : string ) : ISnapshot {
54
+ const configFile : TSDocConfigFile = TSDocConfigFile . loadForFolder ( path . join ( __dirname , assetPath ) ) ;
55
+
56
+ const pathFixupMap : Map < string , string > = new Map ( ) ;
57
+ buildStablePathMap ( pathFixupMap , configFile ) ;
58
+
59
+ return createSnapshot ( configFile , pathFixupMap ) ;
60
+ }
61
+
15
62
test ( 'Load e1' , ( ) => {
16
63
expect ( testLoadingFolder ( 'assets/e1' ) ) . toMatchInlineSnapshot ( `
17
64
Object {
18
- "fileNotFound": false,
19
- "hasErrors": true,
20
- "log": Array [
65
+ "_0_filePath": ".../assets/e1/tsdoc.json",
66
+ "_1_fileNotFound": false,
67
+ "_2_hasErrors": true,
68
+ "_3_errorSummary": "Error encountered for .../assets/e1/tsdoc.json:
69
+ Error loading config file: data should NOT have additional properties
70
+ ",
71
+ "_4_log": Array [
21
72
"[tsdoc-config-schema-error] Error loading config file: data should NOT have additional properties",
22
73
],
74
+ "_5_extends": Array [],
23
75
}
24
76
` ) ;
25
77
} ) ;
26
78
27
79
test ( 'Load e2' , ( ) => {
28
80
expect ( testLoadingFolder ( 'assets/e2' ) ) . toMatchInlineSnapshot ( `
29
81
Object {
30
- "fileNotFound": true,
31
- "hasErrors": false,
32
- "log": Array [
82
+ "_0_filePath": ".../assets/e2/tsdoc.json",
83
+ "_1_fileNotFound": false,
84
+ "_2_hasErrors": true,
85
+ "_3_errorSummary": "Error encountered for .../assets/e2/tsdoc.json:
86
+ Error parsing JSON input: Unexpected token '\\\\n' at 3:12
87
+ \\"invalid
88
+ ^
89
+ ",
90
+ "_4_log": Array [
33
91
"[tsdoc-config-invalid-json] Error parsing JSON input: Unexpected token '\\\\n' at 3:12
34
92
\\"invalid
35
93
^",
36
94
],
95
+ "_5_extends": Array [],
37
96
}
38
97
` ) ;
39
98
} ) ;
40
99
41
100
test ( 'Load e3' , ( ) => {
42
101
expect ( testLoadingFolder ( 'assets/e3' ) ) . toMatchInlineSnapshot ( `
43
102
Object {
44
- "fileNotFound": false,
45
- "hasErrors": true,
46
- "log": Array [
103
+ "_0_filePath": ".../assets/e3/tsdoc.json",
104
+ "_1_fileNotFound": false,
105
+ "_2_hasErrors": true,
106
+ "_3_errorSummary": "Error encountered for .../assets/e3/tsdoc.json:
107
+ Unsupported JSON \\"$schema\\" value; expecting \\"https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json\\"
108
+ ",
109
+ "_4_log": Array [
47
110
"[tsdoc-config-unsupported-schema] Unsupported JSON \\"$schema\\" value; expecting \\"https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json\\"",
48
111
],
112
+ "_5_extends": Array [],
49
113
}
50
114
` ) ;
51
115
} ) ;
52
116
53
117
test ( 'Load e4' , ( ) => {
54
118
expect ( testLoadingFolder ( 'assets/e4' ) ) . toMatchInlineSnapshot ( `
55
119
Object {
56
- "fileNotFound": false,
57
- "hasErrors": true,
58
- "log": Array [
120
+ "_0_filePath": ".../assets/e4/tsdoc.json",
121
+ "_1_fileNotFound": false,
122
+ "_2_hasErrors": true,
123
+ "_3_errorSummary": "Error encountered for .../assets/e4/tsdoc.json:
124
+ The \\"tagDefinitions\\" field specifies more than one tag with the name \\"@dupe\\"
125
+ ",
126
+ "_4_log": Array [
59
127
"[tsdoc-config-duplicate-tag-name] The \\"tagDefinitions\\" field specifies more than one tag with the name \\"@dupe\\"",
60
128
],
129
+ "_5_extends": Array [],
130
+ }
131
+ ` ) ;
132
+ } ) ;
133
+
134
+ test ( 'Load e5' , ( ) => {
135
+ expect ( testLoadingFolder ( 'assets/e5' ) ) . toMatchInlineSnapshot ( `
136
+ Object {
137
+ "_0_filePath": ".../assets/e5/tsdoc.json",
138
+ "_1_fileNotFound": false,
139
+ "_2_hasErrors": true,
140
+ "_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
141
+ Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
142
+
143
+ Error encountered for .../assets/e5/tsdoc-c.json:
144
+ Error loading config file: data should NOT have additional properties
145
+ ",
146
+ "_4_log": Array [],
147
+ "_5_extends": Array [
148
+ Object {
149
+ "_0_filePath": ".../assets/e5/tsdoc-a.json",
150
+ "_1_fileNotFound": false,
151
+ "_2_hasErrors": true,
152
+ "_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
153
+ Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
154
+
155
+ Error encountered for .../assets/e5/tsdoc-c.json:
156
+ Error loading config file: data should NOT have additional properties
157
+ ",
158
+ "_4_log": Array [],
159
+ "_5_extends": Array [
160
+ Object {
161
+ "_0_filePath": ".../assets/e5/tsdoc-b.json",
162
+ "_1_fileNotFound": false,
163
+ "_2_hasErrors": true,
164
+ "_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
165
+ Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
166
+ ",
167
+ "_4_log": Array [],
168
+ "_5_extends": Array [
169
+ Object {
170
+ "_0_filePath": ".../assets/e5/tsdoc-a.json",
171
+ "_1_fileNotFound": false,
172
+ "_2_hasErrors": true,
173
+ "_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
174
+ Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
175
+ ",
176
+ "_4_log": Array [
177
+ "[tsdoc-config-cyclic-extends] Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"",
178
+ ],
179
+ "_5_extends": Array [],
180
+ },
181
+ ],
182
+ },
183
+ Object {
184
+ "_0_filePath": ".../assets/e5/tsdoc-c.json",
185
+ "_1_fileNotFound": false,
186
+ "_2_hasErrors": true,
187
+ "_3_errorSummary": "Error encountered for .../assets/e5/tsdoc-c.json:
188
+ Error loading config file: data should NOT have additional properties
189
+ ",
190
+ "_4_log": Array [
191
+ "[tsdoc-config-schema-error] Error loading config file: data should NOT have additional properties",
192
+ ],
193
+ "_5_extends": Array [],
194
+ },
195
+ ],
196
+ },
197
+ ],
61
198
}
62
199
` ) ;
63
200
} ) ;
0 commit comments