1 // This package needs to be install:
4 // npm install browser-ui-test
7 const fs = require("fs");
8 const path = require("path");
9 const os = require('os');
10 const {Options, runTest} = require('browser-ui-test');
13 console.log("rustdoc-js options:");
14 console.log(" --doc-folder [PATH] : location of the generated doc folder");
15 console.log(" --file [PATH] : file to run (can be repeated)");
16 console.log(" --debug : show extra information about script run");
17 console.log(" --show-text : render font in pages");
18 console.log(" --no-headless : disable headless mode");
19 console.log(" --no-sandbox : disable sandbox mode");
20 console.log(" --help : show this message then quit");
21 console.log(" --tests-folder [PATH] : location of the .GOML tests folder");
22 console.log(" --jobs [NUMBER] : number of threads to run tests on");
23 console.log(" --executable-path [PATH] : path of the browser's executable to be used");
26 function isNumeric(s) {
27 return /^\d+$/.test(s);
30 function parseOptions(args) {
39 "executable_path": null,
42 var correspondances = {
43 "--doc-folder": "doc_folder",
44 "--tests-folder": "tests_folder",
46 "--show-text": "show_text",
47 "--no-headless": "no_headless",
48 "--executable-path": "executable_path",
49 "--no-sandbox": "no_sandbox",
52 for (var i = 0; i < args.length; ++i) {
53 if (args[i] === "--doc-folder"
54 || args[i] === "--tests-folder"
55 || args[i] === "--file"
56 || args[i] === "--jobs"
57 || args[i] === "--executable-path") {
59 if (i >= args.length) {
60 console.log("Missing argument after `" + args[i - 1] + "` option.");
63 if (args[i - 1] === "--jobs") {
64 if (!isNumeric(args[i])) {
66 "`--jobs` option expects a positive number, found `" + args[i] + "`");
69 opts["jobs"] = parseInt(args[i]);
70 } else if (args[i - 1] !== "--file") {
71 opts[correspondances[args[i - 1]]] = args[i];
73 opts["files"].push(args[i]);
75 } else if (args[i] === "--help") {
78 } else if (args[i] === "--no-sandbox") {
79 console.log("`--no-sandbox` is being used. Be very careful!");
80 opts[correspondances[args[i]]] = true;
81 } else if (correspondances[args[i]]) {
82 opts[correspondances[args[i]]] = true;
84 console.log("Unknown option `" + args[i] + "`.");
85 console.log("Use `--help` to see the list of options");
89 if (opts["tests_folder"].length < 1) {
90 console.log("Missing `--tests-folder` option.");
91 } else if (opts["doc_folder"].length < 1) {
92 console.log("Missing `--doc-folder` option.");
99 /// Print single char status information without \n
100 function char_printer(n_tests) {
101 const max_per_line = 10;
104 successful: function() {
106 if (current % max_per_line === 0) {
107 process.stdout.write(`. (${current}/${n_tests})${os.EOL}`);
109 process.stdout.write(".");
112 erroneous: function() {
114 if (current % max_per_line === 0) {
115 process.stderr.write(`F (${current}/${n_tests})${os.EOL}`);
117 process.stderr.write("F");
121 if (current % max_per_line === 0) {
122 // Don't output if we are already at a matching line end
125 const spaces = " ".repeat(max_per_line - (current % max_per_line));
126 process.stdout.write(`${spaces} (${current}/${n_tests})${os.EOL}${os.EOL}`);
132 /// Sort array by .file_name property
133 function by_filename(a, b) {
134 return a.file_name - b.file_name;
137 async function main(argv) {
138 let opts = parseOptions(argv.slice(2));
143 // Print successful tests too
145 // Run tests in sequentially
147 const options = new Options();
149 // This is more convenient that setting fields one by one.
151 "--variable", "DOC_PATH", opts["doc_folder"], "--enable-fail-on-js-error",
152 "--allow-file-access-from-files",
156 args.push("--debug");
158 if (opts["show_text"]) {
159 args.push("--show-text");
161 if (opts["no_sandbox"]) {
162 args.push("--no-sandbox");
164 if (opts["no_headless"]) {
165 args.push("--no-headless");
168 if (opts["executable_path"] !== null) {
169 args.push("--executable-path");
170 args.push(opts["executable_path"]);
172 options.parseArguments(args);
174 console.error(`invalid argument: ${error}`);
180 if (opts["files"].length === 0) {
181 files = fs.readdirSync(opts["tests_folder"]);
183 files = opts["files"];
185 files = files.filter(file => path.extname(file) == ".goml");
186 if (files.length === 0) {
187 console.error("rustdoc-gui: No test selected");
194 console.log("`--no-headless` option is active, disabling concurrency for running tests.");
197 console.log(`Running ${files.length} rustdoc-gui (${opts["jobs"]} concurrently) ...`);
199 if (opts["jobs"] < 1) {
200 process.setMaxListeners(files.length + 1);
201 } else if (headless) {
202 process.setMaxListeners(opts["jobs"] + 1);
205 // We catch this "event" to display a nicer message in case of unexpected exit (because of a
206 // missing `--no-sandbox`).
207 const exitHandling = (code) => {
208 if (!opts["no_sandbox"]) {
211 "`browser-ui-test` crashed unexpectedly. Please try again with adding `--test-args \
212 --no-sandbox` at the end. For example: `x.py test src/test/rustdoc-gui --test-args --no-sandbox`");
216 process.on('exit', exitHandling);
218 const tests_queue = [];
224 const status_bar = char_printer(files.length);
225 for (let i = 0; i < files.length; ++i) {
226 const file_name = files[i];
227 const testPath = path.join(opts["tests_folder"], file_name);
228 const callback = runTest(testPath, options)
230 const [output, nb_failures] = out;
231 results[nb_failures === 0 ? "successful" : "failed"].push({
235 if (nb_failures > 0) {
236 status_bar.erroneous();
239 status_bar.successful();
243 results.errored.push({
244 file_name: testPath + file_name,
247 status_bar.erroneous();
251 // We now remove the promise from the tests_queue.
252 tests_queue.splice(tests_queue.indexOf(callback), 1);
254 tests_queue.push(callback);
255 if (opts["jobs"] > 0 && tests_queue.length >= opts["jobs"]) {
256 await Promise.race(tests_queue);
259 if (tests_queue.length > 0) {
260 await Promise.all(tests_queue);
264 // We don't need this listener anymore.
265 process.removeListener("exit", exitHandling);
268 results.successful.sort(by_filename);
269 results.successful.forEach(r => {
270 console.log(r.output);
274 if (results.failed.length > 0) {
276 results.failed.sort(by_filename);
277 results.failed.forEach(r => {
278 console.log(r.file_name, r.output);
281 if (results.errored.length > 0) {
283 // print run errors on the bottom so developers see them better
284 results.errored.sort(by_filename);
285 results.errored.forEach(r => {
286 console.error(r.file_name, r.output);