]> git.lizzy.rs Git - rust.git/blob - src/tools/rustdoc-js/tester.js
Rollup merge of #69732 - GuillaumeGomez:cleanup-e0382-e0384, r=Dylan-DPC
[rust.git] / src / tools / rustdoc-js / tester.js
1 const fs = require('fs');
2 const { spawnSync } = require('child_process');
3
4 const TEST_FOLDER = 'src/test/rustdoc-js/';
5
6 function getNextStep(content, pos, stop) {
7     while (pos < content.length && content[pos] !== stop &&
8            (content[pos] === ' ' || content[pos] === '\t' || content[pos] === '\n')) {
9         pos += 1;
10     }
11     if (pos >= content.length) {
12         return null;
13     }
14     if (content[pos] !== stop) {
15         return pos * -1;
16     }
17     return pos;
18 }
19
20 // Stupid function extractor based on indent. Doesn't support block
21 // comments. If someone puts a ' or an " in a block comment this
22 // will blow up. Template strings are not tested and might also be
23 // broken.
24 function extractFunction(content, functionName) {
25     var indent = 0;
26     var splitter = "function " + functionName + "(";
27
28     while (true) {
29         var start = content.indexOf(splitter);
30         if (start === -1) {
31             break;
32         }
33         var pos = start;
34         while (pos < content.length && content[pos] !== ')') {
35             pos += 1;
36         }
37         if (pos >= content.length) {
38             break;
39         }
40         pos = getNextStep(content, pos + 1, '{');
41         if (pos === null) {
42             break;
43         } else if (pos < 0) {
44             content = content.slice(-pos);
45             continue;
46         }
47         while (pos < content.length) {
48             // Eat single-line comments
49             if (content[pos] === '/' && pos > 0 && content[pos-1] === '/') {
50                 do {
51                     pos += 1;
52                 } while (pos < content.length && content[pos] !== '\n');
53
54             // Eat quoted strings
55             } else if (content[pos] === '"' || content[pos] === "'" || content[pos] === "`") {
56                 var stop = content[pos];
57                 var is_escaped = false;
58                 do {
59                     if (content[pos] === '\\') {
60                         pos += 2;
61                     } else {
62                         pos += 1;
63                     }
64                 } while (pos < content.length &&
65                          (content[pos] !== stop || content[pos - 1] === '\\'));
66
67             // Otherwise, check for indent
68             } else if (content[pos] === '{') {
69                 indent += 1;
70             } else if (content[pos] === '}') {
71                 indent -= 1;
72                 if (indent === 0) {
73                     return content.slice(start, pos + 1);
74                 }
75             }
76             pos += 1;
77         }
78         content = content.slice(start + 1);
79     }
80     return null;
81 }
82
83 // Stupid function extractor for array.
84 function extractArrayVariable(content, arrayName) {
85     var splitter = "var " + arrayName;
86     while (true) {
87         var start = content.indexOf(splitter);
88         if (start === -1) {
89             break;
90         }
91         var pos = getNextStep(content, start, '=');
92         if (pos === null) {
93             break;
94         } else if (pos < 0) {
95             content = content.slice(-pos);
96             continue;
97         }
98         pos = getNextStep(content, pos, '[');
99         if (pos === null) {
100             break;
101         } else if (pos < 0) {
102             content = content.slice(-pos);
103             continue;
104         }
105         while (pos < content.length) {
106             if (content[pos] === '"' || content[pos] === "'") {
107                 var stop = content[pos];
108                 do {
109                     if (content[pos] === '\\') {
110                         pos += 2;
111                     } else {
112                         pos += 1;
113                     }
114                 } while (pos < content.length &&
115                          (content[pos] !== stop || content[pos - 1] === '\\'));
116             } else if (content[pos] === ']' &&
117                        pos + 1 < content.length &&
118                        content[pos + 1] === ';') {
119                 return content.slice(start, pos + 2);
120             }
121             pos += 1;
122         }
123         content = content.slice(start + 1);
124     }
125     return null;
126 }
127
128 // Stupid function extractor for variable.
129 function extractVariable(content, varName) {
130     var splitter = "var " + varName;
131     while (true) {
132         var start = content.indexOf(splitter);
133         if (start === -1) {
134             break;
135         }
136         var pos = getNextStep(content, start, '=');
137         if (pos === null) {
138             break;
139         } else if (pos < 0) {
140             content = content.slice(-pos);
141             continue;
142         }
143         while (pos < content.length) {
144             if (content[pos] === '"' || content[pos] === "'") {
145                 var stop = content[pos];
146                 do {
147                     if (content[pos] === '\\') {
148                         pos += 2;
149                     } else {
150                         pos += 1;
151                     }
152                 } while (pos < content.length &&
153                          (content[pos] !== stop || content[pos - 1] === '\\'));
154             } else if (content[pos] === ';') {
155                 return content.slice(start, pos + 1);
156             }
157             pos += 1;
158         }
159         content = content.slice(start + 1);
160     }
161     return null;
162 }
163
164 function loadContent(content) {
165     var Module = module.constructor;
166     var m = new Module();
167     m._compile(content, "tmp.js");
168     m.exports.ignore_order = content.indexOf("\n// ignore-order\n") !== -1 ||
169         content.startsWith("// ignore-order\n");
170     m.exports.exact_check = content.indexOf("\n// exact-check\n") !== -1 ||
171         content.startsWith("// exact-check\n");
172     m.exports.should_fail = content.indexOf("\n// should-fail\n") !== -1 ||
173         content.startsWith("// should-fail\n");
174     return m.exports;
175 }
176
177 function readFile(filePath) {
178     return fs.readFileSync(filePath, 'utf8');
179 }
180
181 function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
182     var content = '';
183     for (var i = 0; i < thingsToLoad.length; ++i) {
184         var tmp = funcToCall(fileContent, thingsToLoad[i]);
185         if (tmp === null) {
186             console.error('unable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
187             process.exit(1);
188         }
189         content += tmp;
190         content += 'exports.' + thingsToLoad[i] + ' = ' + thingsToLoad[i] + ';';
191     }
192     return content;
193 }
194
195 function lookForEntry(entry, data) {
196     for (var i = 0; i < data.length; ++i) {
197         var allGood = true;
198         for (var key in entry) {
199             if (!entry.hasOwnProperty(key)) {
200                 continue;
201             }
202             var value = data[i][key];
203             // To make our life easier, if there is a "parent" type, we add it to the path.
204             if (key === 'path' && data[i]['parent'] !== undefined) {
205                 if (value.length > 0) {
206                     value += '::' + data[i]['parent']['name'];
207                 } else {
208                     value = data[i]['parent']['name'];
209                 }
210             }
211             if (value !== entry[key]) {
212                 allGood = false;
213                 break;
214             }
215         }
216         if (allGood === true) {
217             return i;
218         }
219     }
220     return null;
221 }
222
223 function load_files(out_folder, crate) {
224     var mainJs = readFile(out_folder + "/main.js");
225     var ALIASES = readFile(out_folder + "/aliases.js");
226     var searchIndex = readFile(out_folder + "/search-index.js").split("\n");
227     if (searchIndex[searchIndex.length - 1].length === 0) {
228         searchIndex.pop();
229     }
230     searchIndex.pop();
231     searchIndex = loadContent(searchIndex.join("\n") + '\nexports.searchIndex = searchIndex;');
232     finalJS = "";
233
234     var arraysToLoad = ["itemTypes"];
235     var variablesToLoad = ["MAX_LEV_DISTANCE", "MAX_RESULTS",
236                            "GENERICS_DATA", "NAME", "INPUTS_DATA", "OUTPUT_DATA",
237                            "TY_PRIMITIVE", "TY_KEYWORD",
238                            "levenshtein_row2"];
239     // execQuery first parameter is built in getQuery (which takes in the search input).
240     // execQuery last parameter is built in buildIndex.
241     // buildIndex requires the hashmap from search-index.
242     var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
243                            "getQuery", "buildIndex", "execQuery", "execSearch"];
244
245     finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
246     finalJS += 'var rootPath = "../";\n';
247     finalJS += ALIASES;
248     finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
249     finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
250     finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
251
252     var loaded = loadContent(finalJS);
253     return [loaded, loaded.buildIndex(searchIndex.searchIndex)];
254 }
255
256 function main(argv) {
257     if (argv.length < 4) {
258         console.error("USAGE: node tester.js OUT_FOLDER [TESTS]");
259         return 1;
260     }
261     if (argv[2].substr(-1) !== "/") {
262         argv[2] += "/";
263     }
264     const out_folder = argv[2];
265
266     var errors = 0;
267
268     for (var j = 3; j < argv.length; ++j) {
269         const test_name = argv[j];
270
271         process.stdout.write('Checking "' + test_name + '" ... ');
272         if (!fs.existsSync(TEST_FOLDER + test_name + ".js")) {
273             errors += 1;
274             console.error("FAILED");
275             console.error("==> Missing '" + test_name + ".js' file...");
276             continue;
277         }
278
279         const test_out_folder = out_folder + test_name;
280
281         var [loaded, index] = load_files(test_out_folder, test_name);
282         var loadedFile = loadContent(readFile(TEST_FOLDER + test_name + ".js") +
283                                'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
284         const expected = loadedFile.EXPECTED;
285         const query = loadedFile.QUERY;
286         const filter_crate = loadedFile.FILTER_CRATE;
287         const ignore_order = loadedFile.ignore_order;
288         const exact_check = loadedFile.exact_check;
289         const should_fail = loadedFile.should_fail;
290         var results = loaded.execSearch(loaded.getQuery(query), index);
291         var error_text = [];
292         for (var key in expected) {
293             if (!expected.hasOwnProperty(key)) {
294                 continue;
295             }
296             if (!results.hasOwnProperty(key)) {
297                 error_text.push('==> Unknown key "' + key + '"');
298                 break;
299             }
300             var entry = expected[key];
301             var prev_pos = -1;
302             for (var i = 0; i < entry.length; ++i) {
303                 var entry_pos = lookForEntry(entry[i], results[key]);
304                 if (entry_pos === null) {
305                     error_text.push("==> Result not found in '" + key + "': '" +
306                                     JSON.stringify(entry[i]) + "'");
307                 } else if (exact_check === true && prev_pos + 1 !== entry_pos) {
308                     error_text.push("==> Exact check failed at position " + (prev_pos + 1) + ": " +
309                                     "expected '" + JSON.stringify(entry[i]) + "' but found '" +
310                                     JSON.stringify(results[key][i]) + "'");
311                 } else if (ignore_order === false && entry_pos < prev_pos) {
312                     error_text.push("==> '" + JSON.stringify(entry[i]) + "' was supposed to be " +
313                                     " before '" + JSON.stringify(results[key][entry_pos]) + "'");
314                 } else {
315                     prev_pos = entry_pos;
316                 }
317             }
318         }
319         if (error_text.length === 0 && should_fail === true) {
320             errors += 1;
321             console.error("FAILED");
322             console.error("==> Test was supposed to fail but all items were found...");
323         } else if (error_text.length !== 0 && should_fail === false) {
324             errors += 1;
325             console.error("FAILED");
326             console.error(error_text.join("\n"));
327         } else {
328             console.log("OK");
329         }
330     }
331     return errors;
332 }
333
334 process.exit(main(process.argv));