Skip to content

Commit c2e201a

Browse files
committed
Fix an issue where the 'Load e5' test produced different paths on Windows vs Linux
1 parent 345f24e commit c2e201a

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

tsdoc-config/src/__tests__/ErrorHandling.test.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ interface ISnapshot {
1212
s5_extends: ISnapshot[];
1313
}
1414

15+
function replaceAll(text: string, search: string, replace: string): string {
16+
return text.split(search).join(replace);
17+
}
18+
1519
// To make the unit tests deterministic, we need to replace all OS-dependent absolute paths
1620
// with OS-independent paths that are relative to the unit test folder.
1721
function makeStablePath(testPath: string): string {
1822
if (testPath.length === 0) {
1923
return '';
2024
}
21-
return '.../' + path.relative(__dirname, testPath).split('\\').join('/');
25+
console.log('IN: ' + testPath);
26+
console.log('OUT: ' + replaceAll(path.relative(__dirname, testPath), '\\', '/'));
27+
return '.../' + replaceAll(path.relative(__dirname, testPath), '\\', '/');
2228
}
2329

2430
// Build a map from absolute path --> stable path, for each TSDocConfigFile.filePath value
@@ -37,9 +43,14 @@ function buildStablePathMap(stablePathMap: Map<string, string>, configFile: TSDo
3743
// Search and replace all absolute paths with the corresponding stable path.
3844
// For example, "Found C:\A\B\C.txt in C:\A\D\E.txt" becomes "Found .../B/C.txt in .../D/E.txt".
3945
function convertToStablePaths(text: string, stablePathMap: Map<string, string>): string {
40-
for (const pair of Array.from(stablePathMap.entries())) {
41-
text = text.split(pair[0]).join(pair[1]);
46+
// Sort the [key,value] pairs by key length from longest to shortest.
47+
// This ensures that a shorter path does not replace a subpath of a longer path.
48+
const pairs: [string, string][] = Array.from(stablePathMap.entries()).sort((x, y) => y[0].length - x[0].length);
49+
for (const pair of pairs) {
50+
text = replaceAll(text, pair[0], pair[1]);
4251
}
52+
// Also convert any loose backslashes to slashes
53+
//text = replaceAll(text, '\\', '/');
4354
return text;
4455
}
4556

@@ -141,54 +152,54 @@ test('Load e5', () => {
141152
"s0_filePath": ".../assets/e5/tsdoc.json",
142153
"s1_fileNotFound": false,
143154
"s2_hasErrors": true,
144-
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-a.json:
145-
Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"
155+
"s3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
156+
Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
146157
147-
Error encountered for .../assets/e5\\\\tsdoc-c.json:
158+
Error encountered for .../assets/e5/tsdoc-c.json:
148159
Error loading config file: data should NOT have additional properties
149160
",
150161
"s4_log": Array [],
151162
"s5_extends": Array [
152163
Object {
153-
"s0_filePath": ".../assets/e5\\\\tsdoc-a.json",
164+
"s0_filePath": ".../assets/e5/tsdoc-a.json",
154165
"s1_fileNotFound": false,
155166
"s2_hasErrors": true,
156-
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-a.json:
157-
Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"
167+
"s3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
168+
Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
158169
159-
Error encountered for .../assets/e5\\\\tsdoc-c.json:
170+
Error encountered for .../assets/e5/tsdoc-c.json:
160171
Error loading config file: data should NOT have additional properties
161172
",
162173
"s4_log": Array [],
163174
"s5_extends": Array [
164175
Object {
165-
"s0_filePath": ".../assets/e5\\\\tsdoc-b.json",
176+
"s0_filePath": ".../assets/e5/tsdoc-b.json",
166177
"s1_fileNotFound": false,
167178
"s2_hasErrors": true,
168-
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-a.json:
169-
Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"
179+
"s3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
180+
Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
170181
",
171182
"s4_log": Array [],
172183
"s5_extends": Array [
173184
Object {
174-
"s0_filePath": ".../assets/e5\\\\tsdoc-a.json",
185+
"s0_filePath": ".../assets/e5/tsdoc-a.json",
175186
"s1_fileNotFound": false,
176187
"s2_hasErrors": true,
177-
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-a.json:
178-
Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"
188+
"s3_errorSummary": "Error encountered for .../assets/e5/tsdoc-a.json:
189+
Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"
179190
",
180191
"s4_log": Array [
181-
"[tsdoc-config-cyclic-extends] Circular reference encountered for \\"extends\\" field of \\".../assets/e5\\\\tsdoc-b.json\\"",
192+
"[tsdoc-config-cyclic-extends] Circular reference encountered for \\"extends\\" field of \\".../assets/e5/tsdoc-b.json\\"",
182193
],
183194
"s5_extends": Array [],
184195
},
185196
],
186197
},
187198
Object {
188-
"s0_filePath": ".../assets/e5\\\\tsdoc-c.json",
199+
"s0_filePath": ".../assets/e5/tsdoc-c.json",
189200
"s1_fileNotFound": false,
190201
"s2_hasErrors": true,
191-
"s3_errorSummary": "Error encountered for .../assets/e5\\\\tsdoc-c.json:
202+
"s3_errorSummary": "Error encountered for .../assets/e5/tsdoc-c.json:
192203
Error loading config file: data should NOT have additional properties
193204
",
194205
"s4_log": Array [

0 commit comments

Comments
 (0)