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",
155 args.push("--debug");
157 if (opts["show_text"]) {
158 args.push("--show-text");
160 if (opts["no_sandbox"]) {
161 args.push("--no-sandbox");
163 if (opts["no_headless"]) {
164 args.push("--no-headless");
167 if (opts["executable_path"] !== null) {
168 args.push("--executable-path");
169 args.push(opts["executable_path"]);
171 options.parseArguments(args);
173 console.error(`invalid argument: ${error}`);
179 if (opts["files"].length === 0) {
180 files = fs.readdirSync(opts["tests_folder"]);
182 files = opts["files"];
184 files = files.filter(file => path.extname(file) == ".goml");
185 if (files.length === 0) {
186 console.error("rustdoc-gui: No test selected");
193 console.log("`--no-headless` option is active, disabling concurrency for running tests.");
196 console.log(`Running ${files.length} rustdoc-gui (${opts["jobs"]} concurrently) ...`);
198 if (opts["jobs"] < 1) {
199 process.setMaxListeners(files.length + 1);
200 } else if (headless) {
201 process.setMaxListeners(opts["jobs"] + 1);
204 // We catch this "event" to display a nicer message in case of unexpected exit (because of a
205 // missing `--no-sandbox`).
206 const exitHandling = (code) => {
207 if (!opts["no_sandbox"]) {
210 "`browser-ui-test` crashed unexpectedly. Please try again with adding `--test-args \
211 --no-sandbox` at the end. For example: `x.py test src/test/rustdoc-gui --test-args --no-sandbox`");
215 process.on('exit', exitHandling);
217 const tests_queue = [];
223 const status_bar = char_printer(files.length);
224 for (let i = 0; i < files.length; ++i) {
225 const file_name = files[i];
226 const testPath = path.join(opts["tests_folder"], file_name);
227 const callback = runTest(testPath, options)
229 const [output, nb_failures] = out;
230 results[nb_failures === 0 ? "successful" : "failed"].push({
234 if (nb_failures > 0) {
235 status_bar.erroneous();
238 status_bar.successful();
242 results.errored.push({
243 file_name: testPath + file_name,
246 status_bar.erroneous();
250 // We now remove the promise from the tests_queue.
251 tests_queue.splice(tests_queue.indexOf(callback), 1);
253 tests_queue.push(callback);
254 if (opts["jobs"] > 0 && tests_queue.length >= opts["jobs"]) {
255 await Promise.race(tests_queue);
258 if (tests_queue.length > 0) {
259 await Promise.all(tests_queue);
263 // We don't need this listener anymore.
264 process.removeListener("exit", exitHandling);
267 results.successful.sort(by_filename);
268 results.successful.forEach(r => {
269 console.log(r.output);
273 if (results.failed.length > 0) {
275 results.failed.sort(by_filename);
276 results.failed.forEach(r => {
277 console.log(r.file_name, r.output);
280 if (results.errored.length > 0) {
282 // print run errors on the bottom so developers see them better
283 results.errored.sort(by_filename);
284 results.errored.forEach(r => {
285 console.error(r.file_name, r.output);