]> git.lizzy.rs Git - rust.git/blob - src/tools/rustdoc-gui/tester.js
8532410a1bf3a5c21436cf08231ed1bddacfc8ef
[rust.git] / src / tools / rustdoc-gui / tester.js
1 // This package needs to be install:
2 //
3 // ```
4 // npm install browser-ui-test
5 // ```
6
7 const fs = require("fs");
8 const path = require("path");
9 const os = require('os');
10 const {Options, runTest} = require('browser-ui-test');
11
12 function showHelp() {
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 }
23
24 function isNumeric(s) {
25     return /^\d+$/.test(s);
26 }
27
28 function parseOptions(args) {
29     var opts = {
30         "doc_folder": "",
31         "tests_folder": "",
32         "files": [],
33         "debug": false,
34         "show_text": false,
35         "no_headless": false,
36         "jobs": -1,
37     };
38     var correspondances = {
39         "--doc-folder": "doc_folder",
40         "--tests-folder": "tests_folder",
41         "--debug": "debug",
42         "--show-text": "show_text",
43         "--no-headless": "no_headless",
44     };
45
46     for (var i = 0; i < args.length; ++i) {
47         if (args[i] === "--doc-folder"
48             || args[i] === "--tests-folder"
49             || args[i] === "--file"
50             || args[i] === "--jobs") {
51             i += 1;
52             if (i >= args.length) {
53                 console.log("Missing argument after `" + args[i - 1] + "` option.");
54                 return null;
55             }
56             if (args[i - 1] === "--jobs") {
57                 if (!isNumeric(args[i])) {
58                     console.log(
59                         "`--jobs` option expects a positive number, found `" + args[i] + "`");
60                     return null;
61                 }
62                 opts["jobs"] = parseInt(args[i]);
63             } else if (args[i - 1] !== "--file") {
64                 opts[correspondances[args[i - 1]]] = args[i];
65             } else {
66                 opts["files"].push(args[i]);
67             }
68         } else if (args[i] === "--help") {
69             showHelp();
70             process.exit(0);
71         } else if (correspondances[args[i]]) {
72             opts[correspondances[args[i]]] = true;
73         } else {
74             console.log("Unknown option `" + args[i] + "`.");
75             console.log("Use `--help` to see the list of options");
76             return null;
77         }
78     }
79     if (opts["tests_folder"].length < 1) {
80         console.log("Missing `--tests-folder` option.");
81     } else if (opts["doc_folder"].length < 1) {
82         console.log("Missing `--doc-folder` option.");
83     } else {
84         return opts;
85     }
86     return null;
87 }
88
89 /// Print single char status information without \n
90 function char_printer(n_tests) {
91     const max_per_line = 10;
92     let current = 0;
93     return {
94         successful: function() {
95             current += 1;
96             if (current % max_per_line === 0) {
97                 process.stdout.write(`. (${current}/${n_tests})${os.EOL}`);
98             } else {
99                 process.stdout.write(".");
100             }
101         },
102         erroneous: function() {
103             current += 1;
104             if (current % max_per_line === 0) {
105                 process.stderr.write(`F (${current}/${n_tests})${os.EOL}`);
106             } else {
107                 process.stderr.write("F");
108             }
109         },
110         finish: function() {
111             if (current % max_per_line === 0) {
112                 // Don't output if we are already at a matching line end
113                 console.log("");
114             } else {
115                 const spaces = " ".repeat(max_per_line - (current % max_per_line));
116                 process.stdout.write(`${spaces} (${current}/${n_tests})${os.EOL}${os.EOL}`);
117             }
118         },
119     };
120 }
121
122 /// Sort array by .file_name property
123 function by_filename(a, b) {
124     return a.file_name - b.file_name;
125 }
126
127 async function main(argv) {
128     let opts = parseOptions(argv.slice(2));
129     if (opts === null) {
130         process.exit(1);
131     }
132
133     // Print successful tests too
134     let debug = false;
135     // Run tests in sequentially
136     let headless = true;
137     const options = new Options();
138     try {
139         // This is more convenient that setting fields one by one.
140         let args = [
141             "--variable", "DOC_PATH", opts["doc_folder"], "--enable-fail-on-js-error",
142         ];
143         if (opts["debug"]) {
144             debug = true;
145             args.push("--debug");
146         }
147         if (opts["show_text"]) {
148             args.push("--show-text");
149         }
150         if (opts["no_headless"]) {
151             args.push("--no-headless");
152             headless = false;
153         }
154         options.parseArguments(args);
155     } catch (error) {
156         console.error(`invalid argument: ${error}`);
157         process.exit(1);
158     }
159
160     let failed = false;
161     let files;
162     if (opts["files"].length === 0) {
163         files = fs.readdirSync(opts["tests_folder"]);
164     } else {
165         files = opts["files"];
166     }
167     files = files.filter(file => path.extname(file) == ".goml");
168     if (files.length === 0) {
169         console.error("rustdoc-gui: No test selected");
170         process.exit(2);
171     }
172     files.sort();
173
174     if (!headless) {
175         opts["jobs"] = 1;
176         console.log("`--no-headless` option is active, disabling concurrency for running tests.");
177     }
178
179     console.log(`Running ${files.length} rustdoc-gui (${opts["jobs"]} concurrently) ...`);
180
181     if (opts["jobs"] < 1) {
182         process.setMaxListeners(files.length + 1);
183     } else if (headless) {
184         process.setMaxListeners(opts["jobs"] + 1);
185     }
186
187     const tests_queue = [];
188     let results = {
189         successful: [],
190         failed: [],
191         errored: [],
192     };
193     const status_bar = char_printer(files.length);
194     for (let i = 0; i < files.length; ++i) {
195         const file_name = files[i];
196         const testPath = path.join(opts["tests_folder"], file_name);
197         const callback = runTest(testPath, options)
198             .then(out => {
199                 const [output, nb_failures] = out;
200                 results[nb_failures === 0 ? "successful" : "failed"].push({
201                     file_name: testPath,
202                     output: output,
203                 });
204                 if (nb_failures > 0) {
205                     status_bar.erroneous();
206                     failed = true;
207                 } else {
208                     status_bar.successful();
209                 }
210             })
211             .catch(err => {
212                 results.errored.push({
213                     file_name: testPath + file_name,
214                     output: err,
215                 });
216                 status_bar.erroneous();
217                 failed = true;
218             })
219             .finally(() => {
220                 // We now remove the promise from the tests_queue.
221                 tests_queue.splice(tests_queue.indexOf(callback), 1);
222             });
223         tests_queue.push(callback);
224         if (opts["jobs"] > 0 && tests_queue.length >= opts["jobs"]) {
225             await Promise.race(tests_queue);
226         }
227     }
228     if (tests_queue.length > 0) {
229         await Promise.all(tests_queue);
230     }
231     status_bar.finish();
232
233     if (debug) {
234         results.successful.sort(by_filename);
235         results.successful.forEach(r => {
236             console.log(r.output);
237         });
238     }
239
240     if (results.failed.length > 0) {
241         console.log("");
242         results.failed.sort(by_filename);
243         results.failed.forEach(r => {
244             console.log(r.file_name, r.output);
245         });
246     }
247     if (results.errored.length > 0) {
248         console.log(os.EOL);
249         // print run errors on the bottom so developers see them better
250         results.errored.sort(by_filename);
251         results.errored.forEach(r => {
252             console.error(r.file_name, r.output);
253         });
254     }
255
256     if (failed) {
257         process.exit(1);
258     }
259 }
260
261 main(process.argv);