1 const fs = require('fs');
2 const { spawnSync } = require('child_process');
4 const TEST_FOLDER = 'src/test/rustdoc-js-not-std/';
6 function getNextStep(content, pos, stop) {
7 while (pos < content.length && content[pos] !== stop &&
8 (content[pos] === ' ' || content[pos] === '\t' || content[pos] === '\n')) {
11 if (pos >= content.length) {
14 if (content[pos] !== stop) {
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
24 function extractFunction(content, functionName) {
26 var splitter = "function " + functionName + "(";
29 var 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');
55 } else if (content[pos] === '"' || content[pos] === "'" || content[pos] === "`") {
56 var stop = content[pos];
57 var is_escaped = false;
59 if (content[pos] === '\\') {
64 } while (pos < content.length &&
65 (content[pos] !== stop || content[pos - 1] === '\\'));
67 // Otherwise, check for indent
68 } else if (content[pos] === '{') {
70 } else if (content[pos] === '}') {
73 return content.slice(start, pos + 1);
78 content = content.slice(start + 1);
83 // Stupid function extractor for array.
84 function extractArrayVariable(content, arrayName) {
85 var splitter = "var " + arrayName;
87 var start = content.indexOf(splitter);
91 var pos = getNextStep(content, start, '=');
95 content = content.slice(-pos);
98 pos = getNextStep(content, pos, '[');
101 } else if (pos < 0) {
102 content = content.slice(-pos);
105 while (pos < content.length) {
106 if (content[pos] === '"' || content[pos] === "'") {
107 var stop = content[pos];
109 if (content[pos] === '\\') {
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);
123 content = content.slice(start + 1);
128 // Stupid function extractor for variable.
129 function extractVariable(content, varName) {
130 var splitter = "var " + varName;
132 var start = content.indexOf(splitter);
136 var pos = getNextStep(content, start, '=');
139 } else if (pos < 0) {
140 content = content.slice(-pos);
143 while (pos < content.length) {
144 if (content[pos] === '"' || content[pos] === "'") {
145 var stop = content[pos];
147 if (content[pos] === '\\') {
152 } while (pos < content.length &&
153 (content[pos] !== stop || content[pos - 1] === '\\'));
154 } else if (content[pos] === ';') {
155 return content.slice(start, pos + 1);
159 content = content.slice(start + 1);
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");
177 function readFile(filePath) {
178 return fs.readFileSync(filePath, 'utf8');
181 function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
183 for (var i = 0; i < thingsToLoad.length; ++i) {
184 var tmp = funcToCall(fileContent, thingsToLoad[i]);
186 console.error('unable to find ' + kindOfLoad + ' "' + thingsToLoad[i] + '"');
190 content += 'exports.' + thingsToLoad[i] + ' = ' + thingsToLoad[i] + ';';
195 function lookForEntry(entry, data) {
196 for (var i = 0; i < data.length; ++i) {
198 for (var key in entry) {
199 if (!entry.hasOwnProperty(key)) {
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'];
208 value = data[i]['parent']['name'];
211 if (value !== entry[key]) {
216 if (allGood === true) {
223 function remove_docs(out_dir) {
224 spawnSync('rm', ['-rf', out_dir]);
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]);
231 if (c.error || c.stderr.length > 0) {
232 if (c.stderr.length > 0) {
233 s += '==> STDERR: ' + c.stderr + '\n';
235 s += '==> ERROR: ' + c.error;
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) {
248 searchIndex = loadContent(searchIndex.join("\n") + '\nexports.searchIndex = searchIndex;');
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",
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"];
262 finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
263 finalJS += 'var rootPath = "../";\n';
265 finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
266 finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
267 finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
269 var loaded = loadContent(finalJS);
270 return [loaded, loaded.buildIndex(searchIndex.searchIndex)];
273 function main(argv) {
274 if (argv.length !== 4) {
275 console.error("USAGE: node tester.js [TOOLCHAIN] [STAGE]");
278 const toolchain = argv[2];
279 const stage = argv[3];
280 const rustdoc_path = './build/' + toolchain + '/stage' + stage + '/bin/rustdoc';
284 fs.readdirSync(TEST_FOLDER).forEach(function(file) {
285 if (!file.endsWith('.js')) {
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';
292 if (!fs.existsSync(rust_file)) {
293 console.error("FAILED");
294 console.error("==> Missing '" + test_name + ".rs' file...");
299 var out_folder = "build/" + toolchain + "/stage" + stage + "/tests/rustdoc-js-not-std/" +
302 var ret = build_docs(out_folder, rustdoc_path, rust_file);
303 if (ret.length > 0) {
304 console.error("FAILED");
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);
321 for (var key in expected) {
322 if (!expected.hasOwnProperty(key)) {
325 if (!results.hasOwnProperty(key)) {
326 error_text.push('==> Unknown key "' + key + '"');
329 var entry = expected[key];
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]) + "'");
344 prev_pos = entry_pos;
348 if (error_text.length === 0 && should_fail === true) {
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) {
354 console.error("FAILED");
355 console.error(error_text.join("\n"));
357 // In this case, we remove the docs, no need to keep them around.
358 remove_docs(out_folder);
365 process.exit(main(process.argv));