Skip to content

Commit 426055c

Browse files
Improve rustdoc js testers code
1 parent d6b75e0 commit 426055c

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

src/bootstrap/test.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,9 @@ impl Step for RustdocJSStd {
628628
let mut command = Command::new(nodejs);
629629
command
630630
.arg(builder.src.join("src/tools/rustdoc-js/tester.js"))
631-
.arg("--std")
632-
.arg("--version")
631+
.arg("--crate-name")
632+
.arg("std")
633+
.arg("--resource-suffix")
633634
.arg(crate::channel::CFG_RELEASE_NUM)
634635
.arg("--doc-folder")
635636
.arg(builder.doc_out(self.target))

src/tools/compiletest/src/runtest.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2807,11 +2807,15 @@ impl<'test> TestCx<'test> {
28072807
self.document(&out_dir);
28082808

28092809
let root = self.config.find_rust_src_root().unwrap();
2810+
let file_stem =
2811+
self.testpaths.file.file_stem().and_then(|f| f.to_str()).expect("no file stem");
28102812
let res = self.cmd2procres(
28112813
Command::new(&nodejs)
28122814
.arg(root.join("src/tools/rustdoc-js/tester.js"))
28132815
.arg("--doc-folder")
2814-
.arg(out_dir.parent().expect("no parent"))
2816+
.arg(out_dir)
2817+
.arg("--crate-name")
2818+
.arg(file_stem.replace("-", "_"))
28152819
.arg("--test-file")
28162820
.arg(self.testpaths.file.with_extension("js")),
28172821
);

src/tools/rustdoc-js/tester.js

+31-42
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
224224
}
225225
searchIndex.pop();
226226
searchIndex = loadContent(searchIndex.join("\n") + '\nexports.searchIndex = searchIndex;');
227-
finalJS = "";
227+
var finalJS = "";
228228

229229
var arraysToLoad = ["itemTypes"];
230230
var variablesToLoad = ["MAX_LEV_DISTANCE", "MAX_RESULTS", "NO_TYPE_FILTER",
@@ -306,52 +306,53 @@ function runChecks(testFile, loaded, index) {
306306
return errors;
307307
}
308308

309-
function load_files(doc_folder, version, crate) {
310-
var mainJs = readFile(doc_folder + "/main" + version + ".js");
311-
var aliases = readFile(doc_folder + "/aliases" + version + ".js");
312-
var searchIndex = readFile(doc_folder + "/search-index" + version + ".js").split("\n");
309+
function load_files(doc_folder, resource_suffix, crate) {
310+
var mainJs = readFile(path.join(doc_folder, "main" + resource_suffix + ".js"));
311+
var aliases = readFile(path.join(doc_folder, "aliases" + resource_suffix + ".js"));
312+
var searchIndex = readFile(
313+
path.join(doc_folder, "search-index" + resource_suffix + ".js")).split("\n");
313314

314315
return loadMainJsAndIndex(mainJs, aliases, searchIndex, crate);
315316
}
316317

317318
function showHelp() {
318319
console.log("rustdoc-js options:");
319-
console.log(" --doc-folder [PATH] : location of the generated doc folder");
320-
console.log(" --help : show this message then quit");
321-
console.log(" --std : to run std tests");
322-
console.log(" --test-file [PATH]: location of the JS test file");
323-
console.log(" --test-folder [PATH]: location of the JS tests folder");
324-
console.log(" --version [STRING] : version used when generating docs (used to get js files)");
320+
console.log(" --doc-folder [PATH] : location of the generated doc folder");
321+
console.log(" --help : show this message then quit");
322+
console.log(" --crate-name [STRING] : crate name to be used");
323+
console.log(" --test-file [PATH] : location of the JS test file");
324+
console.log(" --test-folder [PATH] : location of the JS tests folder");
325+
console.log(" --resource-suffix [STRING] : suffix to refer to the correct files");
325326
}
326327

327328
function parseOptions(args) {
328329
var opts = {
329-
"is_std": false,
330-
"version": "",
330+
"crate_name": "",
331+
"resource_suffix": "",
331332
"doc_folder": "",
332333
"test_folder": "",
333334
"test_file": "",
334335
};
335336
var correspondances = {
336-
"--version": "version",
337+
"--resource-suffix": "resource_suffix",
337338
"--doc-folder": "doc_folder",
338339
"--test-folder": "test_folder",
339340
"--test-file": "test_file",
341+
"--crate-name": "crate_name",
340342
};
341343

342344
for (var i = 0; i < args.length; ++i) {
343-
if (args[i] === "--version"
345+
if (args[i] === "--resource-suffix"
344346
|| args[i] === "--doc-folder"
345347
|| args[i] === "--test-folder"
346-
|| args[i] === "--test-file") {
348+
|| args[i] === "--test-file"
349+
|| args[i] === "--crate-name") {
347350
i += 1;
348351
if (i >= args.length) {
349352
console.error("Missing argument after `" + args[i - 1] + "` option.");
350353
return null;
351354
}
352355
opts[correspondances[args[i - 1]]] = args[i];
353-
} else if (args[i] === "--std") {
354-
opts["is_std"] = true;
355356
} else if (args[i] === "--help") {
356357
showHelp();
357358
process.exit(0);
@@ -363,28 +364,20 @@ function parseOptions(args) {
363364
}
364365
if (opts["doc_folder"].length < 1) {
365366
console.error("Missing `--doc-folder` option.");
366-
return null;
367+
} else if (opts["crate_name"].length < 1) {
368+
console.error("Missing `--crate-name` option.");
367369
} else if (opts["test_folder"].length < 1 && opts["test_file"].length < 1) {
368370
console.error("At least one of `--test-folder` or `--test-file` option is required.");
369-
return null;
370-
} else if (opts["is_std"] === true && opts["test_file"].length !== 0) {
371-
console.error("`--std` and `--test-file` options can't be used at the same time.")
371+
} else {
372+
return opts;
372373
}
373-
return opts;
374+
return null;
374375
}
375376

376-
function checkFile(test_file, opts, std_loaded, std_index) {
377+
function checkFile(test_file, opts, loaded, index) {
377378
const test_name = path.basename(test_file, ".js");
378379

379380
process.stdout.write('Checking "' + test_name + '" ... ');
380-
381-
var loaded = std_loaded;
382-
var index = std_index;
383-
if (opts["is_std"] !== true) {
384-
var tmp = load_files(path.join(opts["doc_folder"], test_name), opts["version"], test_name);
385-
loaded = tmp[0];
386-
index = tmp[1];
387-
}
388381
return runChecks(test_file, loaded, index);
389382
}
390383

@@ -394,25 +387,21 @@ function main(argv) {
394387
return 1;
395388
}
396389

397-
var std_loaded = null;
398-
var std_index = null;
399-
if (opts["is_std"] === true) {
400-
var tmp = load_files(opts["doc_folder"], opts["version"], "std");
401-
std_loaded = tmp[0];
402-
std_index = tmp[1];
403-
}
404-
390+
var [loaded, index] = load_files(
391+
opts["doc_folder"],
392+
opts["resource_suffix"],
393+
opts["crate_name"]);
405394
var errors = 0;
406395

407396
if (opts["test_file"].length !== 0) {
408-
errors += checkFile(opts["test_file"], opts, null, null);
397+
errors += checkFile(opts["test_file"], opts, loaded, index);
409398
}
410399
if (opts["test_folder"].length !== 0) {
411400
fs.readdirSync(opts["test_folder"]).forEach(function(file) {
412401
if (!file.endsWith(".js")) {
413402
return;
414403
}
415-
errors += checkFile(path.join(opts["test_folder"], file), opts, std_loaded, std_index);
404+
errors += checkFile(path.join(opts["test_folder"], file), opts, loaded, index);
416405
});
417406
}
418407
return errors > 0 ? 1 : 0;

0 commit comments

Comments
 (0)