1 const fs = require('fs');
2 const path = require('path');
4 function getNextStep(content, pos, stop) {
5 while (pos < content.length && content[pos] !== stop &&
6 (content[pos] === ' ' || content[pos] === '\t' || content[pos] === '\n')) {
9 if (pos >= content.length) {
12 if (content[pos] !== stop) {
18 // Stupid function extractor based on indent. Doesn't support block
19 // comments. If someone puts a ' or an " in a block comment this
20 // will blow up. Template strings are not tested and might also be
22 function extractFunction(content, functionName) {
24 var splitter = "function " + functionName + "(";
29 start = content.indexOf(splitter);
34 while (pos < content.length && content[pos] !== ')') {
37 if (pos >= content.length) {
40 pos = getNextStep(content, pos + 1, '{');
44 content = content.slice(-pos);
47 while (pos < content.length) {
48 // Eat single-line comments
49 if (content[pos] === '/' && pos > 0 && content[pos - 1] === '/') {
52 } while (pos < content.length && content[pos] !== '\n');
54 // Eat multiline comment.
55 } else if (content[pos] === '*' && pos > 0 && content[pos - 1] === '/') {
58 } while (pos < content.length && content[pos] !== '/' && content[pos - 1] !== '*');
61 } else if (content[pos] === '"' || content[pos] === "'" || content[pos] === "`") {
64 if (content[pos] === '\\') {
68 } while (pos < content.length && content[pos] !== stop);
70 // Otherwise, check for block level.
71 } else if (content[pos] === '{') {
73 } else if (content[pos] === '}') {
76 return content.slice(start, pos + 1);
81 content = content.slice(start + 1);
86 // Stupid function extractor for array.
87 function extractArrayVariable(content, arrayName) {
88 var splitter = "var " + arrayName;
90 var start = content.indexOf(splitter);
94 var pos = getNextStep(content, start, '=');
98 content = content.slice(-pos);
101 pos = getNextStep(content, pos, '[');
104 } else if (pos < 0) {
105 content = content.slice(-pos);
108 while (pos < content.length) {
109 if (content[pos] === '"' || content[pos] === "'") {
110 var stop = content[pos];
112 if (content[pos] === '\\') {
117 } while (pos < content.length &&
118 (content[pos] !== stop || content[pos - 1] === '\\'));
119 } else if (content[pos] === ']' &&
120 pos + 1 < content.length &&
121 content[pos + 1] === ';') {
122 return content.slice(start, pos + 2);
126 content = content.slice(start + 1);
131 // Stupid function extractor for variable.
132 function extractVariable(content, varName) {
133 var splitter = "var " + varName;
135 var start = content.indexOf(splitter);
139 var pos = getNextStep(content, start, '=');
142 } else if (pos < 0) {
143 content = content.slice(-pos);
146 while (pos < content.length) {
147 if (content[pos] === '"' || content[pos] === "'") {
148 var stop = content[pos];
150 if (content[pos] === '\\') {
155 } while (pos < content.length &&
156 (content[pos] !== stop || content[pos - 1] === '\\'));
157 } else if (content[pos] === ';' || content[pos] === ',') {
158 return content.slice(start, pos + 1);
162 content = content.slice(start + 1);
167 function loadContent(content) {
168 var Module = module.constructor;
169 var m = new Module();
170 m._compile(content, "tmp.js");
171 m.exports.ignore_order = content.indexOf("\n// ignore-order\n") !== -1 ||
172 content.startsWith("// ignore-order\n");
173 m.exports.exact_check = content.indexOf("\n// exact-check\n") !== -1 ||
174 content.startsWith("// exact-check\n");
175 m.exports.should_fail = content.indexOf("\n// should-fail\n") !== -1 ||
176 content.startsWith("// should-fail\n");
180 function readFile(filePath) {
181 return fs.readFileSync(filePath, 'utf8');
184 function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
186 for (var i = 0; i < thingsToLoad.length; ++i) {
187 var tmp = funcToCall(fileContent, thingsToLoad[i]);
189 console.log('unable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
193 content += 'exports.' + thingsToLoad[i] + ' = ' + thingsToLoad[i] + ';';
198 function contentToDiffLine(key, value) {
199 return `"${key}": "${value}",`;
202 // This function is only called when no matching result was found and therefore will only display
203 // the diff between the two items.
204 function betterLookingDiff(entry, data) {
207 for (let key in entry) {
208 if (!entry.hasOwnProperty(key)) {
211 if (!data || !data.hasOwnProperty(key)) {
212 output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
215 let value = data[key];
216 if (value !== entry[key]) {
217 output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
218 output += '+' + spaces + contentToDiffLine(key, value) + '\n';
220 output += spaces + contentToDiffLine(key, value) + '\n';
223 return output + ' }';
226 function lookForEntry(entry, data) {
227 for (var i = 0; i < data.length; ++i) {
229 for (var key in entry) {
230 if (!entry.hasOwnProperty(key)) {
233 var value = data[i][key];
234 // To make our life easier, if there is a "parent" type, we add it to the path.
235 if (key === 'path' && data[i]['parent'] !== undefined) {
236 if (value.length > 0) {
237 value += '::' + data[i]['parent']['name'];
239 value = data[i]['parent']['name'];
242 if (value !== entry[key]) {
247 if (allGood === true) {
254 function loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate) {
255 if (searchIndex[searchIndex.length - 1].length === 0) {
259 var fullSearchIndex = searchIndex.join("\n") + '\nexports.rawSearchIndex = searchIndex;';
260 searchIndex = loadContent(fullSearchIndex);
263 var arraysToLoad = ["itemTypes"];
264 var variablesToLoad = ["MAX_LEV_DISTANCE", "MAX_RESULTS", "NO_TYPE_FILTER",
265 "GENERICS_DATA", "NAME", "INPUTS_DATA", "OUTPUT_DATA",
266 "TY_PRIMITIVE", "TY_KEYWORD",
268 // execQuery first parameter is built in getQuery (which takes in the search input).
269 // execQuery last parameter is built in buildIndex.
270 // buildIndex requires the hashmap from search-index.
271 var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
272 "handleAliases", "getQuery", "buildIndex", "execQuery", "execSearch",
273 "removeEmptyStringsFromArray"];
275 const functions = ["hasOwnPropertyRustdoc", "onEach"];
277 finalJS += 'window = { "currentCrate": "' + crate + '", rootPath: "../" };\n';
278 finalJS += loadThings(functions, 'function', extractFunction, storageJs);
279 finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, searchJs);
280 finalJS += loadThings(variablesToLoad, 'variable', extractVariable, searchJs);
281 finalJS += loadThings(functionsToLoad, 'function', extractFunction, searchJs);
283 var loaded = loadContent(finalJS);
284 var index = loaded.buildIndex(searchIndex.rawSearchIndex);
286 return [loaded, index];
289 function runSearch(query, expected, index, loaded, loadedFile, queryName) {
290 const filter_crate = loadedFile.FILTER_CRATE;
291 const ignore_order = loadedFile.ignore_order;
292 const exact_check = loadedFile.exact_check;
294 var results = loaded.execSearch(loaded.getQuery(query), index, filter_crate);
297 for (var key in expected) {
298 if (!expected.hasOwnProperty(key)) {
301 if (!results.hasOwnProperty(key)) {
302 error_text.push('==> Unknown key "' + key + '"');
305 var entry = expected[key];
307 if (exact_check == true && entry.length !== results[key].length) {
308 error_text.push(queryName + "==> Expected exactly " + entry.length +
309 " results but found " + results[key].length + " in '" + key + "'");
313 for (var i = 0; i < entry.length; ++i) {
314 var entry_pos = lookForEntry(entry[i], results[key]);
315 if (entry_pos === null) {
316 error_text.push(queryName + "==> Result not found in '" + key + "': '" +
317 JSON.stringify(entry[i]) + "'");
318 // By default, we just compare the two first items.
319 let item_to_diff = 0;
320 if ((ignore_order === false || exact_check === true) && i < results[key].length) {
323 error_text.push("Diff of first error:\n" +
324 betterLookingDiff(entry[i], results[key][item_to_diff]));
325 } else if (exact_check === true && prev_pos + 1 !== entry_pos) {
326 error_text.push(queryName + "==> Exact check failed at position " + (prev_pos + 1) +
327 ": expected '" + JSON.stringify(entry[i]) + "' but found '" +
328 JSON.stringify(results[key][i]) + "'");
329 } else if (ignore_order === false && entry_pos < prev_pos) {
330 error_text.push(queryName + "==> '" + JSON.stringify(entry[i]) + "' was supposed " +
331 "to be before '" + JSON.stringify(results[key][entry_pos]) + "'");
333 prev_pos = entry_pos;
340 function checkResult(error_text, loadedFile, displaySuccess) {
341 if (error_text.length === 0 && loadedFile.should_fail === true) {
342 console.log("FAILED");
343 console.log("==> Test was supposed to fail but all items were found...");
344 } else if (error_text.length !== 0 && loadedFile.should_fail === false) {
345 console.log("FAILED");
346 console.log(error_text.join("\n"));
348 if (displaySuccess) {
356 function runChecks(testFile, loaded, index) {
357 var testFileContent = readFile(testFile) + 'exports.QUERY = QUERY;exports.EXPECTED = EXPECTED;';
358 if (testFileContent.indexOf("FILTER_CRATE") !== -1) {
359 testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;";
361 var loadedFile = loadContent(testFileContent);
363 const expected = loadedFile.EXPECTED;
364 const query = loadedFile.QUERY;
366 if (Array.isArray(query)) {
367 if (!Array.isArray(expected)) {
368 console.log("FAILED");
369 console.log("==> If QUERY variable is an array, EXPECTED should be an array too");
371 } else if (query.length !== expected.length) {
372 console.log("FAILED");
373 console.log("==> QUERY variable should have the same length as EXPECTED");
376 for (var i = 0; i < query.length; ++i) {
377 var error_text = runSearch(query[i], expected[i], index, loaded, loadedFile,
378 "[ query `" + query[i] + "`]");
379 if (checkResult(error_text, loadedFile, false) !== 0) {
386 var error_text = runSearch(query, expected, index, loaded, loadedFile, "");
387 return checkResult(error_text, loadedFile, true);
390 function load_files(doc_folder, resource_suffix, crate) {
391 var searchJs = readFile(path.join(doc_folder, "search" + resource_suffix + ".js"));
392 var storageJs = readFile(path.join(doc_folder, "storage" + resource_suffix + ".js"));
393 var searchIndex = readFile(
394 path.join(doc_folder, "search-index" + resource_suffix + ".js")).split("\n");
396 return loadSearchJsAndIndex(searchJs, searchIndex, storageJs, crate);
399 function showHelp() {
400 console.log("rustdoc-js options:");
401 console.log(" --doc-folder [PATH] : location of the generated doc folder");
402 console.log(" --help : show this message then quit");
403 console.log(" --crate-name [STRING] : crate name to be used");
404 console.log(" --test-file [PATH] : location of the JS test file");
405 console.log(" --test-folder [PATH] : location of the JS tests folder");
406 console.log(" --resource-suffix [STRING] : suffix to refer to the correct files");
409 function parseOptions(args) {
412 "resource_suffix": "",
417 var correspondances = {
418 "--resource-suffix": "resource_suffix",
419 "--doc-folder": "doc_folder",
420 "--test-folder": "test_folder",
421 "--test-file": "test_file",
422 "--crate-name": "crate_name",
425 for (var i = 0; i < args.length; ++i) {
426 if (args[i] === "--resource-suffix"
427 || args[i] === "--doc-folder"
428 || args[i] === "--test-folder"
429 || args[i] === "--test-file"
430 || args[i] === "--crate-name") {
432 if (i >= args.length) {
433 console.log("Missing argument after `" + args[i - 1] + "` option.");
436 opts[correspondances[args[i - 1]]] = args[i];
437 } else if (args[i] === "--help") {
441 console.log("Unknown option `" + args[i] + "`.");
442 console.log("Use `--help` to see the list of options");
446 if (opts["doc_folder"].length < 1) {
447 console.log("Missing `--doc-folder` option.");
448 } else if (opts["crate_name"].length < 1) {
449 console.log("Missing `--crate-name` option.");
450 } else if (opts["test_folder"].length < 1 && opts["test_file"].length < 1) {
451 console.log("At least one of `--test-folder` or `--test-file` option is required.");
458 function checkFile(test_file, opts, loaded, index) {
459 const test_name = path.basename(test_file, ".js");
461 process.stdout.write('Checking "' + test_name + '" ... ');
462 return runChecks(test_file, loaded, index);
465 function main(argv) {
466 var opts = parseOptions(argv.slice(2));
471 var [loaded, index] = load_files(
473 opts["resource_suffix"],
477 if (opts["test_file"].length !== 0) {
478 errors += checkFile(opts["test_file"], opts, loaded, index);
480 if (opts["test_folder"].length !== 0) {
481 fs.readdirSync(opts["test_folder"]).forEach(function(file) {
482 if (!file.endsWith(".js")) {
485 errors += checkFile(path.join(opts["test_folder"], file), opts, loaded, index);
488 return errors > 0 ? 1 : 0;
491 process.exit(main(process.argv));