-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathindex.ts
More file actions
175 lines (150 loc) · 6.32 KB
/
index.ts
File metadata and controls
175 lines (150 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import path from "node:path";
import { Command } from "@gemini-testing/commander";
import getPort from "get-port";
import defaults from "../config/defaults";
import { configOverriding } from "./info";
import { Testplane } from "../testplane";
import pkg from "../../package.json";
import * as logger from "../utils/logger";
import { shouldIgnoreUnhandledRejection } from "../utils/errors";
import { utilInspectSafe } from "../utils/secret-replacer";
import { withCommonCliOptions, collectCliValues, handleRequires } from "../utils/cli";
import { CliCommands } from "./constants";
export type TestplaneRunOpts = { cliName?: string };
let testplane: Testplane;
process.on("uncaughtException", err => {
logger.error(utilInspectSafe(err));
process.exit(1);
});
process.on("unhandledRejection", (reason, p) => {
if (shouldIgnoreUnhandledRejection(reason as Error)) {
logger.warn(`Unhandled Rejection "${reason}" in testplane:master:${process.pid} was ignored`);
return;
}
const error = [
`Unhandled Rejection in testplane:master:${process.pid}:`,
`Promise: ${utilInspectSafe(p)}`,
`Reason: ${utilInspectSafe(reason)}`,
].join("\n");
if (testplane) {
testplane.halt(new Error(error));
} else {
logger.error(error);
process.exit(1);
}
});
export const run = async (opts: TestplaneRunOpts = {}): Promise<void> => {
const program = new Command(opts.cliName || "testplane");
program.version(pkg.version).allowUnknownOption().option("-c, --config <path>", "path to configuration file");
const programToParseRequires = new Command(opts.cliName || "testplane")
.version(pkg.version)
.allowUnknownOption()
.option("-r, --require <module>", "require module", collectCliValues);
const requireModules = preparseOption(programToParseRequires, "require") as string[];
if (requireModules) {
await handleRequires(requireModules);
}
const configPath = preparseOption(program, "config") as string;
testplane = Testplane.create(configPath);
withCommonCliOptions({ cmd: program, actionName: "run" })
.on("--help", () => console.log(configOverriding(opts)))
.description("Run tests")
.option("--reporter <name>", "test reporters", collectCliValues)
.option(
"--update-refs",
'update screenshot references or gather them if they do not exist ("assertView" command)',
)
.option("--inspect [inspect]", "nodejs inspector on [=[host:]port]")
.option("--inspect-brk [inspect-brk]", "nodejs inspector with break at the start")
.option(
"--repl [type]",
"run one test, call `browser.switchToRepl` in test code to open repl interface",
Boolean,
false,
)
.option("--repl-before-test [type]", "open repl interface before test run", Boolean, false)
.option("--repl-on-fail [type]", "open repl interface on test fail only", Boolean, false)
.option(
"--repl-port <number>",
"run net server on port to exchange messages with repl (used free random port by default)",
Number,
0,
)
.option("--devtools", "switches the browser to the devtools mode with using CDP protocol")
.option("--local", "use local browsers, managed by testplane (same as 'gridUrl': 'local')")
.option("--keep-browser", "do not close browser session after test completion")
.option("--keep-browser-on-fail", "do not close browser session when test fails")
.arguments("[paths...]")
.action(async (paths: string[]) => {
try {
const {
reporter: reporters,
browser: browsers,
set: sets,
grep,
tag,
updateRefs,
inspect,
inspectBrk,
replBeforeTest,
replOnFail,
devtools,
local,
keepBrowser,
keepBrowserOnFail,
} = program;
const isTestsSuccess = await testplane.run(paths, {
reporters: reporters || defaults.reporters,
browsers,
sets,
grep,
tag,
updateRefs,
requireModules,
inspectMode: (inspect || inspectBrk) && { inspect, inspectBrk },
replMode: {
enabled: isReplModeEnabled(program),
beforeTest: replBeforeTest,
onFail: replOnFail,
port: await getReplPort(program),
},
devtools: devtools || false,
local: local || false,
keepBrowserMode: {
enabled: keepBrowser || keepBrowserOnFail || false,
onFail: keepBrowserOnFail || false,
},
});
process.exit(isTestsSuccess ? 0 : 1);
} catch (err) {
logger.error((err as Error).stack || err);
process.exit(1);
}
});
for (const commandName of Object.values(CliCommands)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { registerCmd } = require(path.resolve(__dirname, "./commands", commandName));
registerCmd(program, testplane);
}
testplane.extendCli(program);
program.parse(process.argv);
};
function preparseOption(program: Command, option: string): unknown {
// do not display any help, do not exit
const configFileParser = Object.create(program);
configFileParser.options = [].concat(program.options);
configFileParser.option("-h, --help");
configFileParser.parse(process.argv);
return configFileParser[option];
}
function isReplModeEnabled(program: Command): boolean {
const { repl, replBeforeTest, replOnFail } = program;
return repl || replBeforeTest || replOnFail;
}
async function getReplPort(program: Command): Promise<number> {
let { replPort } = program;
if (isReplModeEnabled(program) && !replPort) {
replPort = await getPort();
}
return replPort;
}