]> git.lizzy.rs Git - rust.git/blob - src/tools/rustdoc-gui/tester.js
Rollup merge of #93443 - spastorino:add-stable-hash-impl-doc, r=cjgillot
[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             "--no-screenshot",
142             "--variable", "DOC_PATH", opts["doc_folder"],
143         ];
144         if (opts["debug"]) {
145             debug = true;
146             args.push("--debug");
147         }
148         if (opts["show_text"]) {
149             args.push("--show-text");
150         }
151         if (opts["no_headless"]) {
152             args.push("--no-headless");
153             headless = false;
154         }
155         options.parseArguments(args);
156     } catch (error) {
157         console.error(`invalid argument: ${error}`);
158         process.exit(1);
159     }
160
161     let failed = false;
162     let files;
163     if (opts["files"].length === 0) {
164         files = fs.readdirSync(opts["tests_folder"]);
165     } else {
166         files = opts["files"];
167     }
168     files = files.filter(file => path.extname(file) == ".goml");
169     if (files.length === 0) {
170         console.error("rustdoc-gui: No test selected");
171         process.exit(2);
172     }
173     files.sort();
174
175     if (!headless) {
176         opts["jobs"] = 1;
177         console.log("`--no-headless` option is active, disabling concurrency for running tests.");
178     }
179
180     console.log(`Running ${files.length} rustdoc-gui (${opts["jobs"]} concurrently) ...`);
181
182     if (opts["jobs"] < 1) {
183         process.setMaxListeners(files.length + 1);
184     } else if (headless) {
185         process.setMaxListeners(opts["jobs"] + 1);
186     }
187
188     const tests_queue = [];
189     let results = {
190         successful: [],
191         failed: [],
192         errored: [],
193     };
194     const status_bar = char_printer(files.length);
195     for (let i = 0; i < files.length; ++i) {
196         const file_name = files[i];
197         const testPath = path.join(opts["tests_folder"], file_name);
198         const callback = runTest(testPath, options)
199             .then(out => {
200                 const [output, nb_failures] = out;
201                 results[nb_failures === 0 ? "successful" : "failed"].push({
202                     file_name: testPath,
203                     output: output,
204                 });
205                 if (nb_failures > 0) {
206                     status_bar.erroneous();
207                     failed = true;
208                 } else {
209                     status_bar.successful();
210                 }
211             })
212             .catch(err => {
213                 results.errored.push({
214                     file_name: testPath + file_name,
215                     output: err,
216                 });
217                 status_bar.erroneous();
218                 failed = true;
219             })
220             .finally(() => {
221                 // We now remove the promise from the tests_queue.
222                 tests_queue.splice(tests_queue.indexOf(callback), 1);
223             });
224         tests_queue.push(callback);
225         if (opts["jobs"] > 0 && tests_queue.length >= opts["jobs"]) {
226             await Promise.race(tests_queue);
227         }
228     }
229     if (tests_queue.length > 0) {
230         await Promise.all(tests_queue);
231     }
232     status_bar.finish();
233
234     if (debug) {
235         results.successful.sort(by_filename);
236         results.successful.forEach(r => {
237             console.log(r.output);
238         });
239     }
240
241     if (results.failed.length > 0) {
242         console.log("");
243         results.failed.sort(by_filename);
244         results.failed.forEach(r => {
245             console.log(r.file_name, r.output);
246         });
247     }
248     if (results.errored.length > 0) {
249         console.log(os.EOL);
250         // print run errors on the bottom so developers see them better
251         results.errored.sort(by_filename);
252         results.errored.forEach(r => {
253             console.error(r.file_name, r.output);
254         });
255     }
256
257     if (failed) {
258         process.exit(1);
259     }
260 }
261
262 main(process.argv);