1 const fs = require('fs');
2 const path = require('path');
4 function loadContent(content) {
5 var Module = module.constructor;
7 m._compile(content, "tmp.js");
8 m.exports.ignore_order = content.indexOf("\n// ignore-order\n") !== -1 ||
9 content.startsWith("// ignore-order\n");
10 m.exports.exact_check = content.indexOf("\n// exact-check\n") !== -1 ||
11 content.startsWith("// exact-check\n");
12 m.exports.should_fail = content.indexOf("\n// should-fail\n") !== -1 ||
13 content.startsWith("// should-fail\n");
17 function readFile(filePath) {
18 return fs.readFileSync(filePath, 'utf8');
21 function contentToDiffLine(key, value) {
22 return `"${key}": "${value}",`;
25 // This function is only called when no matching result was found and therefore will only display
26 // the diff between the two items.
27 function betterLookingDiff(entry, data) {
30 for (let key in entry) {
31 if (!entry.hasOwnProperty(key)) {
34 if (!data || !data.hasOwnProperty(key)) {
35 output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
38 let value = data[key];
39 if (value !== entry[key]) {
40 output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
41 output += '+' + spaces + contentToDiffLine(key, value) + '\n';
43 output += spaces + contentToDiffLine(key, value) + '\n';
49 function lookForEntry(entry, data) {
50 for (var i = 0; i < data.length; ++i) {
52 for (var key in entry) {
53 if (!entry.hasOwnProperty(key)) {
56 var value = data[i][key];
57 // To make our life easier, if there is a "parent" type, we add it to the path.
58 if (key === 'path' && data[i]['parent'] !== undefined) {
59 if (value.length > 0) {
60 value += '::' + data[i]['parent']['name'];
62 value = data[i]['parent']['name'];
65 if (value !== entry[key]) {
70 if (allGood === true) {
77 // This function checks if `expected` has all the required fields needed for the checks.
78 function checkNeededFields(fullPath, expected, error_text, queryName, position) {
80 if (fullPath.length === 0) {
89 } else if (fullPath.endsWith("elems") || fullPath.endsWith("generics")) {
100 for (var i = 0; i < fieldsToCheck.length; ++i) {
101 const field = fieldsToCheck[i];
102 if (!expected.hasOwnProperty(field)) {
103 let text = `${queryName}==> Mandatory key \`${field}\` is not present`;
104 if (fullPath.length > 0) {
105 text += ` in field \`${fullPath}\``;
106 if (position != null) {
107 text += ` (position ${position})`;
110 error_text.push(text);
115 function valueCheck(fullPath, expected, result, error_text, queryName) {
116 if (Array.isArray(expected)) {
117 for (var i = 0; i < expected.length; ++i) {
118 checkNeededFields(fullPath, expected[i], error_text, queryName, i);
119 if (i >= result.length) {
120 error_text.push(`${queryName}==> EXPECTED has extra value in array from field ` +
121 `\`${fullPath}\` (position ${i}): \`${JSON.stringify(expected[i])}\``);
123 valueCheck(fullPath + '[' + i + ']', expected[i], result[i], error_text, queryName);
126 for (; i < result.length; ++i) {
127 error_text.push(`${queryName}==> RESULT has extra value in array from field ` +
128 `\`${fullPath}\` (position ${i}): \`${JSON.stringify(result[i])}\` ` +
129 'compared to EXPECTED');
131 } else if (expected !== null && typeof expected !== "undefined" &&
132 expected.constructor == Object) {
133 for (const key in expected) {
134 if (!expected.hasOwnProperty(key)) {
137 if (!result.hasOwnProperty(key)) {
138 error_text.push('==> Unknown key "' + key + '"');
141 const obj_path = fullPath + (fullPath.length > 0 ? '.' : '') + key;
142 valueCheck(obj_path, expected[key], result[key], error_text, queryName);
145 expectedValue = JSON.stringify(expected);
146 resultValue = JSON.stringify(result);
147 if (expectedValue != resultValue) {
148 error_text.push(`${queryName}==> Different values for field \`${fullPath}\`:\n` +
149 `EXPECTED: \`${expectedValue}\`\nRESULT: \`${resultValue}\``);
154 function runParser(query, expected, parseQuery, queryName) {
156 checkNeededFields("", expected, error_text, queryName, null);
157 if (error_text.length === 0) {
158 valueCheck('', expected, parseQuery(query), error_text, queryName);
163 function runSearch(query, expected, doSearch, loadedFile, queryName) {
164 const ignore_order = loadedFile.ignore_order;
165 const exact_check = loadedFile.exact_check;
167 var results = doSearch(query, loadedFile.FILTER_CRATE);
170 for (var key in expected) {
171 if (!expected.hasOwnProperty(key)) {
174 if (!results.hasOwnProperty(key)) {
175 error_text.push('==> Unknown key "' + key + '"');
178 var entry = expected[key];
180 if (exact_check == true && entry.length !== results[key].length) {
181 error_text.push(queryName + "==> Expected exactly " + entry.length +
182 " results but found " + results[key].length + " in '" + key + "'");
186 for (var i = 0; i < entry.length; ++i) {
187 var entry_pos = lookForEntry(entry[i], results[key]);
188 if (entry_pos === null) {
189 error_text.push(queryName + "==> Result not found in '" + key + "': '" +
190 JSON.stringify(entry[i]) + "'");
191 // By default, we just compare the two first items.
192 let item_to_diff = 0;
193 if ((ignore_order === false || exact_check === true) && i < results[key].length) {
196 error_text.push("Diff of first error:\n" +
197 betterLookingDiff(entry[i], results[key][item_to_diff]));
198 } else if (exact_check === true && prev_pos + 1 !== entry_pos) {
199 error_text.push(queryName + "==> Exact check failed at position " + (prev_pos + 1) +
200 ": expected '" + JSON.stringify(entry[i]) + "' but found '" +
201 JSON.stringify(results[key][i]) + "'");
202 } else if (ignore_order === false && entry_pos < prev_pos) {
203 error_text.push(queryName + "==> '" + JSON.stringify(entry[i]) + "' was supposed " +
204 "to be before '" + JSON.stringify(results[key][entry_pos]) + "'");
206 prev_pos = entry_pos;
213 function checkResult(error_text, loadedFile, displaySuccess) {
214 if (error_text.length === 0 && loadedFile.should_fail === true) {
215 console.log("FAILED");
216 console.log("==> Test was supposed to fail but all items were found...");
217 } else if (error_text.length !== 0 && loadedFile.should_fail === false) {
218 console.log("FAILED");
219 console.log(error_text.join("\n"));
221 if (displaySuccess) {
229 function runCheck(loadedFile, key, callback) {
230 const expected = loadedFile[key];
231 const query = loadedFile.QUERY;
233 if (Array.isArray(query)) {
234 if (!Array.isArray(expected)) {
235 console.log("FAILED");
236 console.log(`==> If QUERY variable is an array, ${key} should be an array too`);
238 } else if (query.length !== expected.length) {
239 console.log("FAILED");
240 console.log(`==> QUERY variable should have the same length as ${key}`);
243 for (var i = 0; i < query.length; ++i) {
244 var error_text = callback(query[i], expected[i], "[ query `" + query[i] + "`]");
245 if (checkResult(error_text, loadedFile, false) !== 0) {
251 var error_text = callback(query, expected, "");
252 if (checkResult(error_text, loadedFile, true) !== 0) {
259 function runChecks(testFile, doSearch, parseQuery) {
260 var checkExpected = false;
261 var checkParsed = false;
262 var testFileContent = readFile(testFile) + 'exports.QUERY = QUERY;';
264 if (testFileContent.indexOf("FILTER_CRATE") !== -1) {
265 testFileContent += "exports.FILTER_CRATE = FILTER_CRATE;";
267 testFileContent += "exports.FILTER_CRATE = null;";
270 if (testFileContent.indexOf("\nconst EXPECTED") !== -1) {
271 testFileContent += 'exports.EXPECTED = EXPECTED;';
272 checkExpected = true;
274 if (testFileContent.indexOf("\nconst PARSED") !== -1) {
275 testFileContent += 'exports.PARSED = PARSED;';
278 if (!checkParsed && !checkExpected) {
279 console.log("FAILED");
280 console.log("==> At least `PARSED` or `EXPECTED` is needed!");
284 const loadedFile = loadContent(testFileContent);
288 res += runCheck(loadedFile, "EXPECTED", (query, expected, text) => {
289 return runSearch(query, expected, doSearch, loadedFile, text);
293 res += runCheck(loadedFile, "PARSED", (query, expected, text) => {
294 return runParser(query, expected, parseQuery, text);
301 * Load searchNNN.js and search-indexNNN.js.
303 * @param {string} doc_folder - Path to a folder generated by running rustdoc
304 * @param {string} resource_suffix - Version number between filename and .js, e.g. "1.59.0"
305 * @returns {Object} - Object containing two keys: `doSearch`, which runs a search
306 * with the loaded index and returns a table of results; and `parseQuery`, which is the
307 * `parseQuery` function exported from the search module.
309 function loadSearchJS(doc_folder, resource_suffix) {
310 const searchJs = path.join(doc_folder, "search" + resource_suffix + ".js");
311 const searchIndexJs = path.join(doc_folder, "search-index" + resource_suffix + ".js");
312 const searchIndex = require(searchIndexJs);
313 const searchModule = require(searchJs);
314 const searchWords = searchModule.initSearch(searchIndex.searchIndex);
317 doSearch: function (queryStr, filterCrate, currentCrate) {
318 return searchModule.execQuery(searchModule.parseQuery(queryStr), searchWords,
319 filterCrate, currentCrate);
321 parseQuery: searchModule.parseQuery,
325 function showHelp() {
326 console.log("rustdoc-js options:");
327 console.log(" --doc-folder [PATH] : location of the generated doc folder");
328 console.log(" --help : show this message then quit");
329 console.log(" --crate-name [STRING] : crate name to be used");
330 console.log(" --test-file [PATHs] : location of the JS test files (can be called " +
332 console.log(" --test-folder [PATH] : location of the JS tests folder");
333 console.log(" --resource-suffix [STRING] : suffix to refer to the correct files");
336 function parseOptions(args) {
339 "resource_suffix": "",
344 var correspondences = {
345 "--resource-suffix": "resource_suffix",
346 "--doc-folder": "doc_folder",
347 "--test-folder": "test_folder",
348 "--test-file": "test_file",
349 "--crate-name": "crate_name",
352 for (var i = 0; i < args.length; ++i) {
353 if (correspondences.hasOwnProperty(args[i])) {
355 if (i >= args.length) {
356 console.log("Missing argument after `" + args[i - 1] + "` option.");
359 if (args[i - 1] !== "--test-file") {
360 opts[correspondences[args[i - 1]]] = args[i];
362 opts[correspondences[args[i - 1]]].push(args[i]);
364 } else if (args[i] === "--help") {
368 console.log("Unknown option `" + args[i] + "`.");
369 console.log("Use `--help` to see the list of options");
373 if (opts["doc_folder"].length < 1) {
374 console.log("Missing `--doc-folder` option.");
375 } else if (opts["crate_name"].length < 1) {
376 console.log("Missing `--crate-name` option.");
377 } else if (opts["test_folder"].length < 1 && opts["test_file"].length < 1) {
378 console.log("At least one of `--test-folder` or `--test-file` option is required.");
385 function main(argv) {
386 var opts = parseOptions(argv.slice(2));
391 let parseAndSearch = loadSearchJS(
393 opts["resource_suffix"]);
396 let doSearch = function (queryStr, filterCrate) {
397 return parseAndSearch.doSearch(queryStr, filterCrate, opts["crate_name"]);
400 if (opts["test_file"].length !== 0) {
401 opts["test_file"].forEach(function (file) {
402 process.stdout.write(`Testing ${file} ... `);
403 errors += runChecks(file, doSearch, parseAndSearch.parseQuery);
405 } else if (opts["test_folder"].length !== 0) {
406 fs.readdirSync(opts["test_folder"]).forEach(function (file) {
407 if (!file.endsWith(".js")) {
410 process.stdout.write(`Testing ${file} ... `);
411 errors += runChecks(path.join(opts["test_folder"], file), doSearch,
412 parseAndSearch.parseQuery);
415 return errors > 0 ? 1 : 0;
418 process.exit(main(process.argv));