]> git.lizzy.rs Git - rust.git/blob - src/tools/rustdoc-js-not-std/tester.js
61490b2f48d03f47dac1d79a7833850d93a63070
[rust.git] / src / tools / rustdoc-js-not-std / tester.js
1 const fs = require('fs');
2 const { spawnSync } = require('child_process');
3
4 const TEST_FOLDER = 'src/test/rustdoc-js-not-std/';
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 remove_docs(out_dir) {
224     spawnSync('rm', ['-rf', out_dir]);
225 }
226
227 function build_docs(out_dir, rustdoc_path, file_to_document) {
228     remove_docs(out_dir);
229     var c = spawnSync(rustdoc_path, [file_to_document, '-o', out_dir]);
230     var s = '';
231     if (c.error || c.stderr.length > 0) {
232         if (c.stderr.length > 0) {
233             s += '==> STDERR: ' + c.stderr + '\n';
234         }
235         s += '==> ERROR: ' + c.error;
236     }
237     return s;
238 }
239
240 function load_files(out_folder, crate) {
241     var mainJs = readFile(out_folder + "/main.js");
242     var ALIASES = readFile(out_folder + "/aliases.js");
243     var searchIndex = readFile(out_folder + "/search-index.js").split("\n");
244     if (searchIndex[searchIndex.length - 1].length === 0) {
245         searchIndex.pop();
246     }
247     searchIndex.pop();
248     searchIndex = loadContent(searchIndex.join("\n") + '\nexports.searchIndex = searchIndex;');
249     finalJS = "";
250
251     var arraysToLoad = ["itemTypes"];
252     var variablesToLoad = ["MAX_LEV_DISTANCE", "MAX_RESULTS",
253                            "GENERICS_DATA", "NAME", "INPUTS_DATA", "OUTPUT_DATA",
254                            "TY_PRIMITIVE", "TY_KEYWORD",
255                            "levenshtein_row2"];
256     // execQuery first parameter is built in getQuery (which takes in the search input).
257     // execQuery last parameter is built in buildIndex.
258     // buildIndex requires the hashmap from search-index.
259     var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
260                            "getQuery", "buildIndex", "execQuery", "execSearch"];
261
262     finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
263     finalJS += 'var rootPath = "../";\n';
264     finalJS += ALIASES;
265     finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
266     finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
267     finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
268
269     var loaded = loadContent(finalJS);
270     return [loaded, loaded.buildIndex(searchIndex.searchIndex)];
271 }
272
273 function main(argv) {
274     if (argv.length !== 4) {
275         console.error("USAGE: node tester.js [TOOLCHAIN] [STAGE]");
276         return 1;
277     }
278     const toolchain = argv[2];
279     const stage = argv[3];
280     const rustdoc_path = './build/' + toolchain + '/stage' + stage + '/bin/rustdoc';
281
282     var errors = 0;
283
284     fs.readdirSync(TEST_FOLDER).forEach(function(file) {
285         if (!file.endsWith('.js')) {
286             return;
287         }
288         var test_name = file.substring(0, file.length - 3);
289         process.stdout.write('Checking "' + test_name + '" ... ');
290         var rust_file = TEST_FOLDER + test_name + '.rs';
291
292         if (!fs.existsSync(rust_file)) {
293             console.error("FAILED");
294             console.error("==> Missing '" + test_name + ".rs' file...");
295             errors += 1;
296             return;
297         }
298
299         var out_folder = "build/" + toolchain + "/stage" + stage + "/tests/rustdoc-js-not-std/" +
300                          test_name;
301
302         var ret = build_docs(out_folder, rustdoc_path, rust_file);
303         if (ret.length > 0) {
304             console.error("FAILED");
305             console.error(ret);
306             errors += 1;
307             return;
308         }
309
310         var [loaded, index] = load_files(out_folder, test_name);
311         var loadedFile = loadContent(readFile(TEST_FOLDER + file) +
312                                'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;');
313         const expected = loadedFile.EXPECTED;
314         const query = loadedFile.QUERY;
315         const filter_crate = loadedFile.FILTER_CRATE;
316         const ignore_order = loadedFile.ignore_order;
317         const exact_check = loadedFile.exact_check;
318         const should_fail = loadedFile.should_fail;
319         var results = loaded.execSearch(loaded.getQuery(query), index);
320         var error_text = [];
321         for (var key in expected) {
322             if (!expected.hasOwnProperty(key)) {
323                 continue;
324             }
325             if (!results.hasOwnProperty(key)) {
326                 error_text.push('==> Unknown key "' + key + '"');
327                 break;
328             }
329             var entry = expected[key];
330             var prev_pos = -1;
331             for (var i = 0; i < entry.length; ++i) {
332                 var entry_pos = lookForEntry(entry[i], results[key]);
333                 if (entry_pos === null) {
334                     error_text.push("==> Result not found in '" + key + "': '" +
335                                     JSON.stringify(entry[i]) + "'");
336                 } else if (exact_check === true && prev_pos + 1 !== entry_pos) {
337                     error_text.push("==> Exact check failed at position " + (prev_pos + 1) + ": " +
338                                     "expected '" + JSON.stringify(entry[i]) + "' but found '" +
339                                     JSON.stringify(results[key][i]) + "'");
340                 } else if (ignore_order === false && entry_pos < prev_pos) {
341                     error_text.push("==> '" + JSON.stringify(entry[i]) + "' was supposed to be " +
342                                     " before '" + JSON.stringify(results[key][entry_pos]) + "'");
343                 } else {
344                     prev_pos = entry_pos;
345                 }
346             }
347         }
348         if (error_text.length === 0 && should_fail === true) {
349             errors += 1;
350             console.error("FAILED");
351             console.error("==> Test was supposed to fail but all items were found...");
352         } else if (error_text.length !== 0 && should_fail === false) {
353             errors += 1;
354             console.error("FAILED");
355             console.error(error_text.join("\n"));
356         } else {
357             // In this case, we remove the docs, no need to keep them around.
358             remove_docs(out_folder);
359             console.log("OK");
360         }
361     });
362     return errors;
363 }
364
365 process.exit(main(process.argv));