]> git.lizzy.rs Git - rust.git/blob - src/tools/rustdoc-gui/tester.js
Rollup merge of #87504 - ehuss:update-mdbook, r=Mark-Simulacrum
[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 {Options, runTest} = require('browser-ui-test');
9
10 function showHelp() {
11     console.log("rustdoc-js options:");
12     console.log("  --doc-folder [PATH]        : location of the generated doc folder");
13     console.log("  --file [PATH]              : file to run (can be repeated)");
14     console.log("  --debug                    : show extra information about script run");
15     console.log("  --show-text                : render font in pages");
16     console.log("  --no-headless              : disable headless mode");
17     console.log("  --help                     : show this message then quit");
18     console.log("  --tests-folder [PATH]      : location of the .GOML tests folder");
19 }
20
21 function parseOptions(args) {
22     var opts = {
23         "doc_folder": "",
24         "tests_folder": "",
25         "files": [],
26         "debug": false,
27         "show_text": false,
28         "no_headless": false,
29     };
30     var correspondances = {
31         "--doc-folder": "doc_folder",
32         "--tests-folder": "tests_folder",
33         "--debug": "debug",
34         "--show-text": "show_text",
35         "--no-headless": "no_headless",
36     };
37
38     for (var i = 0; i < args.length; ++i) {
39         if (args[i] === "--doc-folder"
40             || args[i] === "--tests-folder"
41             || args[i] === "--file") {
42             i += 1;
43             if (i >= args.length) {
44                 console.log("Missing argument after `" + args[i - 1] + "` option.");
45                 return null;
46             }
47             if (args[i - 1] !== "--file") {
48                 opts[correspondances[args[i - 1]]] = args[i];
49             } else {
50                 opts["files"].push(args[i]);
51             }
52         } else if (args[i] === "--help") {
53             showHelp();
54             process.exit(0);
55         } else if (correspondances[args[i]]) {
56             opts[correspondances[args[i]]] = true;
57         } else {
58             console.log("Unknown option `" + args[i] + "`.");
59             console.log("Use `--help` to see the list of options");
60             return null;
61         }
62     }
63     if (opts["tests_folder"].length < 1) {
64         console.log("Missing `--tests-folder` option.");
65     } else if (opts["doc_folder"].length < 1) {
66         console.log("Missing `--doc-folder` option.");
67     } else {
68         return opts;
69     }
70     return null;
71 }
72
73 async function main(argv) {
74     let opts = parseOptions(argv.slice(2));
75     if (opts === null) {
76         process.exit(1);
77     }
78
79     const options = new Options();
80     try {
81         // This is more convenient that setting fields one by one.
82         let args = [
83             "--no-screenshot",
84             "--variable", "DOC_PATH", opts["doc_folder"],
85         ];
86         if (opts["debug"]) {
87             args.push("--debug");
88         }
89         if (opts["show_text"]) {
90             args.push("--show-text");
91         }
92         if (opts["no_headless"]) {
93             args.push("--no-headless");
94         }
95         options.parseArguments(args);
96     } catch (error) {
97         console.error(`invalid argument: ${error}`);
98         process.exit(1);
99     }
100
101     let failed = false;
102     let files;
103     if (opts["files"].length === 0) {
104         files = fs.readdirSync(opts["tests_folder"]).filter(file => path.extname(file) == ".goml");
105     } else {
106         files = opts["files"].filter(file => path.extname(file) == ".goml");
107     }
108
109     files.sort();
110     for (var i = 0; i < files.length; ++i) {
111         const testPath = path.join(opts["tests_folder"], files[i]);
112         await runTest(testPath, options).then(out => {
113             const [output, nb_failures] = out;
114             console.log(output);
115             if (nb_failures > 0) {
116                 failed = true;
117             }
118         }).catch(err => {
119             console.error(err);
120             failed = true;
121         });
122     }
123     if (failed) {
124         process.exit(1);
125     }
126 }
127
128 main(process.argv);