Philipp Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 1 | const child_process = require('child_process'); |
| 2 | const process = require('process'); |
| 3 | |
| 4 | const cypress = require('cypress'); |
| 5 | |
| 6 | // Set up the xvfb binary. |
Philipp Schrader | 23768c8 | 2023-07-01 14:37:28 -0700 | [diff] [blame] | 7 | // 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. |
| 9 | process.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 Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 14 | |
| 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. |
| 18 | console.log('Starting server.'); |
| 19 | let 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. |
| 28 | const 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. |
| 56 | const 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 Schrader | 155e76c | 2023-02-25 18:42:31 -0800 | [diff] [blame] | 64 | // Parse command line options. |
| 65 | let runOptions = await cypress.cli.parseRunArguments(process.argv.slice(2)); |
| 66 | |
Philipp Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 67 | await serverStartup; |
Philipp Schrader | 155e76c | 2023-02-25 18:42:31 -0800 | [diff] [blame] | 68 | const result = await cypress.run( |
| 69 | Object.assign(runOptions, { |
Philipp Schrader | 155e76c | 2023-02-25 18:42:31 -0800 | [diff] [blame] | 70 | 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 Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 79 | 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 | })(); |