]> git.lizzy.rs Git - rust.git/blob - src/tools/rustdoc-gui/tester.js
Rollup merge of #88136 - spastorino:fix-test-directory, r=oli-obk
[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 const fs = require("fs");
7 const path = require("path");
8 const os = require('os');
9 const {Options, runTest} = require('browser-ui-test');
10
11 function showHelp() {
12     console.log("rustdoc-js options:");
13     console.log("  --doc-folder [PATH]        : location of the generated doc folder");
14     console.log("  --file [PATH]              : file to run (can be repeated)");
15     console.log("  --debug                    : show extra information about script run");
16     console.log("  --show-text                : render font in pages");
17     console.log("  --no-headless              : disable headless mode");
18     console.log("  --help                     : show this message then quit");
19     console.log("  --tests-folder [PATH]      : location of the .GOML tests folder");
20     console.log("  --jobs [NUMBER]            : number of threads to run tests on");
21 }
22
23 function isNumeric(s) {
24     return /^\d+$/.test(s);
25 }
26
27 function parseOptions(args) {
28     var opts = {
29         "doc_folder": "",
30         "tests_folder": "",
31         "files": [],
32         "debug": false,
33         "show_text": false,
34         "no_headless": false,
35         "jobs": -1,
36     };
37     var correspondances = {
38         "--doc-folder": "doc_folder",
39         "--tests-folder": "tests_folder",
40         "--debug": "debug",
41         "--show-text": "show_text",
42         "--no-headless": "no_headless",
43     };
44
45     for (var i = 0; i < args.length; ++i) {
46         if (args[i] === "--doc-folder"
47             || args[i] === "--tests-folder"
48             || args[i] === "--file"
49             || args[i] === "--jobs") {
50             i += 1;
51             if (i >= args.length) {
52                 console.log("Missing argument after `" + args[i - 1] + "` option.");
53                 return null;
54             }
55             if (args[i - 1] === "--jobs") {
56                 if (!isNumeric(args[i])) {
57                     console.log(
58                         "`--jobs` option expects a positive number, found `" + args[i] + "`");
59                     return null;
60                 }
61                 opts["jobs"] = parseInt(args[i]);
62             } else if (args[i - 1] !== "--file") {
63                 opts[correspondances[args[i - 1]]] = args[i];
64             } else {
65                 opts["files"].push(args[i]);
66             }
67         } else if (args[i] === "--help") {
68             showHelp();
69             process.exit(0);
70         } else if (correspondances[args[i]]) {
71             opts[correspondances[args[i]]] = true;
72         } else {
73             console.log("Unknown option `" + args[i] + "`.");
74             console.log("Use `--help` to see the list of options");
75             return null;
76         }
77     }
78     if (opts["tests_folder"].length < 1) {
79         console.log("Missing `--tests-folder` option.");
80     } else if (opts["doc_folder"].length < 1) {
81         console.log("Missing `--doc-folder` option.");
82     } else {
83         return opts;
84     }
85     return null;
86 }
87
88 /// Print single char status information without \n
89 function char_printer(n_tests) {
90     const max_per_line = 10;
91     let current = 0;
92     return {
93         successful: function() {
94             current += 1;
95             if (current % max_per_line === 0) {
96                 process.stdout.write(`. (${current}/${n_tests})${os.EOL}`);
97             } else {
98                 process.stdout.write(".");
99             }
100         },
101         erroneous: function() {
102             current += 1;
103             if (current % max_per_line === 0) {
104                 process.stderr.write(`F (${current}/${n_tests})${os.EOL}`);
105             } else {
106                 process.stderr.write("F");
107             }
108         },
109         finish: function() {
110             if (current % max_per_line === 0) {
111                 // Don't output if we are already at a matching line end
112                 console.log("");
113             } else {
114                 const spaces = " ".repeat(max_per_line - (current % max_per_line));
115                 process.stdout.write(`${spaces} (${current}/${n_tests})${os.EOL}${os.EOL}`);
116             }
117         },
118     };
119 }
120
121 /// Sort array by .file_name property
122 function by_filename(a, b) {
123     return a.file_name - b.file_name;
124 }
125
126 async function main(argv) {
127     let opts = parseOptions(argv.slice(2));
128     if (opts === null) {
129         process.exit(1);
130     }
131
132     // Print successful tests too
133     let debug = false;
134     // Run tests in sequentially
135     let no_headless = false;
136     const options = new Options();
137     try {
138         // This is more convenient that setting fields one by one.
139         let args = [
140             "--no-screenshot",
141             "--variable", "DOC_PATH", opts["doc_folder"],
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             no_headless = true;
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     console.log(`Running ${files.length} rustdoc-gui tests...`);
175     if (opts["jobs"] < 1) {
176         process.setMaxListeners(files.length + 1);
177     } else {
178         process.setMaxListeners(opts["jobs"]);
179     }
180     let tests = [];
181     let results = {
182         successful: [],
183         failed: [],
184         errored: [],
185     };
186     const status_bar = char_printer(files.length);
187     for (let i = 0; i < files.length; ++i) {
188         const file_name = files[i];
189         const testPath = path.join(opts["tests_folder"], file_name);
190         tests.push(
191             runTest(testPath, options)
192             .then(out => {
193                 const [output, nb_failures] = out;
194                 results[nb_failures === 0 ? "successful" : "failed"].push({
195                     file_name: file_name,
196                     output: output,
197                 });
198                 if (nb_failures > 0) {
199                     status_bar.erroneous()
200                     failed = true;
201                 } else {
202                     status_bar.successful()
203                 }
204             })
205             .catch(err => {
206                 results.errored.push({
207                     file_name: file_name,
208                     output: err,
209                 });
210                 status_bar.erroneous();
211                 failed = true;
212             })
213         );
214         if (no_headless) {
215             await tests[i];
216         }
217     }
218     if (!no_headless) {
219         await Promise.all(tests);
220     }
221     status_bar.finish();
222
223     if (debug) {
224         results.successful.sort(by_filename);
225         results.successful.forEach(r => {
226             console.log(r.output);
227         });
228     }
229
230     if (results.failed.length > 0) {
231         console.log("");
232         results.failed.sort(by_filename);
233         results.failed.forEach(r => {
234             console.log(r.output);
235         });
236     }
237     if (results.errored.length > 0) {
238         console.log(os.EOL);
239         // print run errors on the bottom so developers see them better
240         results.errored.sort(by_filename);
241         results.errored.forEach(r => {
242             console.error(r.output);
243         });
244     }
245
246     if (failed) {
247         process.exit(1);
248     }
249 }
250
251 main(process.argv);