blob: c2ef027d4d824f982eff5ebf6e8bb4373ecfec2a [file] [log] [blame]
Philipp Schrader175a93c2023-02-19 13:13:40 -08001const child_process = require('child_process');
2const process = require('process');
3
4const cypress = require('cypress');
5
6// Set up the xvfb binary.
Philipp Schrader23768c82023-07-01 14:37:28 -07007// TODO(philipp): Figure out how to point Xvfb at the sandboxed usr/bin
8// directory. Currently impossible as it's hardcoded to use /usr/bin.
9process.env['PATH'] = [
10 `${process.env.RUNFILES_DIR}/xvfb_amd64/wrapped_bin`,
11 `${process.env.RUNFILES_DIR}/xvfb_amd64/usr/bin`,
12 process.env.PATH,
13].join(':');
Philipp Schrader175a93c2023-02-19 13:13:40 -080014
15// Start the web server, database, and fake TBA server.
16// We use file descriptor 3 ('pipe') for the test server to let us know when
17// everything has started up.
18console.log('Starting server.');
19let servers = child_process.spawn(
20 'testing/scouting_test_servers',
21 ['--port=8000', '--notify_fd=3'],
22 {
23 stdio: ['inherit', 'inherit', 'inherit', 'pipe'],
24 }
25);
26
27// Wait for the server to finish starting up.
28const serverStartup = new Promise((resolve, reject) => {
29 let cumulativeData = '';
30 servers.stdio[3].on('data', async (data) => {
31 console.log('Got data: ' + data);
32 cumulativeData += data;
33 if (cumulativeData.includes('READY')) {
34 console.log('Everything is ready!');
35 resolve();
36 }
37 });
38
39 servers.on('error', (err) => {
40 console.log(`Failed to start scouting_test_servers: ${err}`);
41 reject();
42 });
43
44 servers.on('close', (code, signal) => {
45 console.log(`scouting_test_servers closed: ${code} (${signal})`);
46 reject();
47 });
48
49 servers.on('exit', (code, signal) => {
50 console.log(`scouting_test_servers exited: ${code} (${signal})`);
51 reject();
52 });
53});
54
55// Wait for the server to shut down.
56const serverShutdown = new Promise((resolve) => {
57 servers.on('exit', () => {
58 resolve();
59 });
60});
61
62// Wait for the server to be ready, run the tests, then shut down the server.
63(async () => {
Philipp Schrader155e76c2023-02-25 18:42:31 -080064 // Parse command line options.
65 let runOptions = await cypress.cli.parseRunArguments(process.argv.slice(2));
66
Philipp Schrader175a93c2023-02-19 13:13:40 -080067 await serverStartup;
Philipp Schrader155e76c2023-02-25 18:42:31 -080068 const result = await cypress.run(
69 Object.assign(runOptions, {
Philipp Schrader155e76c2023-02-25 18:42:31 -080070 config: {
71 baseUrl: 'http://localhost:8000',
72 screenshotsFolder:
73 process.env.TEST_UNDECLARED_OUTPUTS_DIR + '/screenshots',
74 video: false,
75 videosFolder: process.env.TEST_UNDECLARED_OUTPUTS_DIR + '/videos',
76 },
77 })
78 );
Philipp Schrader175a93c2023-02-19 13:13:40 -080079 await servers.kill();
80 await serverShutdown;
81
82 exitCode = 0;
83 if (result.status == 'failed') {
84 exitCode = 1;
85 console.log('-'.repeat(50));
86 console.log('Test FAILED: ' + result.message);
87 console.log('-'.repeat(50));
88 } else if (result.totalFailed > 0) {
89 // When the "before" hook fails, we don't get a "failed" mesage for some
90 // reason. In that case, we just have to exit with an error.
91 exitCode = 1;
92 }
93 process.exit(exitCode);
94})();