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(" --help : show this message then quit");
20 console.log(" --tests-folder [PATH] : location of the .GOML tests folder");
21 console.log(" --jobs [NUMBER] : number of threads to run tests on");
22 console.log(" --executable-path [PATH] : path of the browser's executable to be used");
25 function isNumeric(s) {
26 return /^\d+$/.test(s);
29 function parseOptions(args) {
38 "executable_path": null,
41 var correspondances = {
42 "--doc-folder": "doc_folder",
43 "--tests-folder": "tests_folder",
45 "--show-text": "show_text",
46 "--no-headless": "no_headless",
47 "--executable-path": "executable_path",
48 "--no-sandbox": "no_sandbox",
51 for (var i = 0; i < args.length; ++i) {
52 if (args[i] === "--doc-folder"
53 || args[i] === "--tests-folder"
54 || args[i] === "--file"
55 || args[i] === "--jobs"
56 || args[i] === "--executable-path") {
58 if (i >= args.length) {
59 console.log("Missing argument after `" + args[i - 1] + "` option.");
62 if (args[i - 1] === "--jobs") {
63 if (!isNumeric(args[i])) {
65 "`--jobs` option expects a positive number, found `" + args[i] + "`");
68 opts["jobs"] = parseInt(args[i]);
69 } else if (args[i - 1] !== "--file") {
70 opts[correspondances[args[i - 1]]] = args[i];
72 opts["files"].push(args[i]);
74 } else if (args[i] === "--help") {
77 } else if (args[i] === "--no-sandbox") {
78 console.log("`--no-sandbox` is being used. Be very careful!");
79 opts[correspondances[args[i]]] = true;
80 } else if (correspondances[args[i]]) {
81 opts[correspondances[args[i]]] = true;
83 console.log("Unknown option `" + args[i] + "`.");
84 console.log("Use `--help` to see the list of options");
88 if (opts["tests_folder"].length < 1) {
89 console.log("Missing `--tests-folder` option.");
90 } else if (opts["doc_folder"].length < 1) {
91 console.log("Missing `--doc-folder` option.");
98 /// Print single char status information without \n
99 function char_printer(n_tests) {
100 const max_per_line = 10;
103 successful: function() {
105 if (current % max_per_line === 0) {
106 process.stdout.write(`. (${current}/${n_tests})${os.EOL}`);
108 process.stdout.write(".");
111 erroneous: function() {
113 if (current % max_per_line === 0) {
114 process.stderr.write(`F (${current}/${n_tests})${os.EOL}`);
116 process.stderr.write("F");
120 if (current % max_per_line === 0) {
121 // Don't output if we are already at a matching line end
124 const spaces = " ".repeat(max_per_line - (current % max_per_line));
125 process.stdout.write(`${spaces} (${current}/${n_tests})${os.EOL}${os.EOL}`);
131 /// Sort array by .file_name property
132 function by_filename(a, b) {
133 return a.file_name - b.file_name;
136 async function main(argv) {
137 let opts = parseOptions(argv.slice(2));
142 // Print successful tests too
144 // Run tests in sequentially
146 const options = new Options();
148 // This is more convenient that setting fields one by one.
150 "--variable", "DOC_PATH", opts["doc_folder"], "--enable-fail-on-js-error",
154 args.push("--debug");
156 if (opts["show_text"]) {
157 args.push("--show-text");
159 if (opts["no_sandbox"]) {
160 args.push("--no-sandbox");
162 if (opts["no_headless"]) {
163 args.push("--no-headless");
166 if (opts["executable_path"] !== null) {
167 args.push("--executable-path");
168 args.push(opts["executable_path"]);
170 options.parseArguments(args);
172 console.error(`invalid argument: ${error}`);
178 if (opts["files"].length === 0) {
179 files = fs.readdirSync(opts["tests_folder"]);
181 files = opts["files"];
183 files = files.filter(file => path.extname(file) == ".goml");
184 if (files.length === 0) {
185 console.error("rustdoc-gui: No test selected");
192 console.log("`--no-headless` option is active, disabling concurrency for running tests.");
195 console.log(`Running ${files.length} rustdoc-gui (${opts["jobs"]} concurrently) ...`);
197 if (opts["jobs"] < 1) {
198 process.setMaxListeners(files.length + 1);
199 } else if (headless) {
200 process.setMaxListeners(opts["jobs"] + 1);
203 const tests_queue = [];
209 const status_bar = char_printer(files.length);
210 for (let i = 0; i < files.length; ++i) {
211 const file_name = files[i];
212 const testPath = path.join(opts["tests_folder"], file_name);
213 const callback = runTest(testPath, options)
215 const [output, nb_failures] = out;
216 results[nb_failures === 0 ? "successful" : "failed"].push({
220 if (nb_failures > 0) {
221 status_bar.erroneous();
224 status_bar.successful();
228 results.errored.push({
229 file_name: testPath + file_name,
232 status_bar.erroneous();
236 // We now remove the promise from the tests_queue.
237 tests_queue.splice(tests_queue.indexOf(callback), 1);
239 tests_queue.push(callback);
240 if (opts["jobs"] > 0 && tests_queue.length >= opts["jobs"]) {
241 await Promise.race(tests_queue);
244 if (tests_queue.length > 0) {
245 await Promise.all(tests_queue);
250 results.successful.sort(by_filename);
251 results.successful.forEach(r => {
252 console.log(r.output);
256 if (results.failed.length > 0) {
258 results.failed.sort(by_filename);
259 results.failed.forEach(r => {
260 console.log(r.file_name, r.output);
263 if (results.errored.length > 0) {
265 // print run errors on the bottom so developers see them better
266 results.errored.sort(by_filename);
267 results.errored.forEach(r => {
268 console.error(r.file_name, r.output);