blob: 6c63adb6e018de2cef8e1922d79157d74b5c66ce [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.
7process.env[
8 'PATH'
9] = `${process.env.RUNFILES_DIR}/xvfb_amd64/wrapped_bin:${process.env.PATH}`;
10
11// Start the web server, database, and fake TBA server.
12// We use file descriptor 3 ('pipe') for the test server to let us know when
13// everything has started up.
14console.log('Starting server.');
15let servers = child_process.spawn(
16 'testing/scouting_test_servers',
17 ['--port=8000', '--notify_fd=3'],
18 {
19 stdio: ['inherit', 'inherit', 'inherit', 'pipe'],
20 }
21);
22
23// Wait for the server to finish starting up.
24const serverStartup = new Promise((resolve, reject) => {
25 let cumulativeData = '';
26 servers.stdio[3].on('data', async (data) => {
27 console.log('Got data: ' + data);
28 cumulativeData += data;
29 if (cumulativeData.includes('READY')) {
30 console.log('Everything is ready!');
31 resolve();
32 }
33 });
34
35 servers.on('error', (err) => {
36 console.log(`Failed to start scouting_test_servers: ${err}`);
37 reject();
38 });
39
40 servers.on('close', (code, signal) => {
41 console.log(`scouting_test_servers closed: ${code} (${signal})`);
42 reject();
43 });
44
45 servers.on('exit', (code, signal) => {
46 console.log(`scouting_test_servers exited: ${code} (${signal})`);
47 reject();
48 });
49});
50
51// Wait for the server to shut down.
52const serverShutdown = new Promise((resolve) => {
53 servers.on('exit', () => {
54 resolve();
55 });
56});
57
58// Wait for the server to be ready, run the tests, then shut down the server.
59(async () => {
60 await serverStartup;
61 const result = await cypress.run({
62 headless: true,
63 config: {
64 baseUrl: 'http://localhost:8000',
65 screenshotsFolder:
66 process.env.TEST_UNDECLARED_OUTPUTS_DIR + '/screenshots',
67 video: false,
68 videosFolder: process.env.TEST_UNDECLARED_OUTPUTS_DIR + '/videos',
69 },
70 });
71 await servers.kill();
72 await serverShutdown;
73
74 exitCode = 0;
75 if (result.status == 'failed') {
76 exitCode = 1;
77 console.log('-'.repeat(50));
78 console.log('Test FAILED: ' + result.message);
79 console.log('-'.repeat(50));
80 } else if (result.totalFailed > 0) {
81 // When the "before" hook fails, we don't get a "failed" mesage for some
82 // reason. In that case, we just have to exit with an error.
83 exitCode = 1;
84 }
85 process.exit(exitCode);
86})();