|
| 1 | +/* eslint-disable no-console */ |
| 2 | +const BoxSDK = require('box-node-sdk'); |
| 3 | +const childProcess = require('child_process'); |
| 4 | + |
| 5 | +const { argv, env } = process; |
| 6 | +const { E2E_ACCESS_TOKEN, E2E_CLIENT_ID } = env; |
| 7 | +const KILL_SIGNALS = [ |
| 8 | + 'SIGABRT', |
| 9 | + 'SIGBUS', |
| 10 | + 'SIGFPE', |
| 11 | + 'SIGHUP', |
| 12 | + 'SIGILL', |
| 13 | + 'SIGINT', |
| 14 | + 'SIGQUIT', |
| 15 | + 'SIGSEGV', |
| 16 | + 'SIGTERM', |
| 17 | + 'SIGTRAP', |
| 18 | + 'SIGUSR1', |
| 19 | + 'SIGUSR2', |
| 20 | +]; |
| 21 | + |
| 22 | +if (!E2E_ACCESS_TOKEN) { |
| 23 | + throw new Error('E2E_ACCESS_TOKEN must be set as an environment variable'); |
| 24 | +} |
| 25 | + |
| 26 | +if (!E2E_CLIENT_ID) { |
| 27 | + throw new Error('E2E_CLIENT_ID must be set as an environment variable'); |
| 28 | +} |
| 29 | + |
| 30 | +const sdk = new BoxSDK({ |
| 31 | + clientID: E2E_CLIENT_ID, |
| 32 | + clientSecret: 'none', |
| 33 | +}); |
| 34 | + |
| 35 | +const client = sdk.getBasicClient(E2E_ACCESS_TOKEN); |
| 36 | + |
| 37 | +async function cleanup(folderId) { |
| 38 | + console.log('Cleanup test folder and files...'); |
| 39 | + |
| 40 | + try { |
| 41 | + await client.folders.delete(folderId, { recursive: true }); |
| 42 | + console.log('Cleanup complete.'); |
| 43 | + } catch (error) { |
| 44 | + console.error(`Cleanup failed. Error: ${error.message}`); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function main() { |
| 49 | + const testFolderName = `Test ${new Date().toISOString()}`; |
| 50 | + |
| 51 | + // Bootstrap the test folder and copy template files into it |
| 52 | + console.log(`Setup test folder: ${testFolderName}...`); |
| 53 | + const { id: folderId } = await client.folders.create('118537970832', testFolderName); // Test folder |
| 54 | + const { id: documentId } = await client.files.copy('694470903390', folderId); // Document template |
| 55 | + const { id: imageId } = await client.files.copy('694468799644', folderId); // Image template |
| 56 | + console.log('Setup complete.'); |
| 57 | + |
| 58 | + // Attempt to cleanup test folder before script is killed (example: CTL+C) |
| 59 | + KILL_SIGNALS.forEach(signal => process.on(signal, () => cleanup(folderId))); |
| 60 | + |
| 61 | + try { |
| 62 | + console.log('Cypress run starting...'); |
| 63 | + |
| 64 | + const suffix = argv.indexOf('-o') >= 0 ? 'open' : 'run'; // Pass -o to run Cypress in "open" mode |
| 65 | + const output = childProcess.execSync(`yarn npm-run-all -p -r start:dev cy:${suffix}`, { |
| 66 | + env: { |
| 67 | + ...env, |
| 68 | + CYPRESS_ACCESS_TOKEN: E2E_ACCESS_TOKEN, |
| 69 | + CYPRESS_FILE_ID_DOC: documentId, |
| 70 | + CYPRESS_FILE_ID_IMAGE: imageId, |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + console.log('Cypress run SUCCESS. Output:'); |
| 75 | + console.log('------------------------------'); |
| 76 | + console.log(output.toString()); |
| 77 | + } catch (error) { |
| 78 | + console.log('Cypress run FAILURE. Output:'); |
| 79 | + console.log('------------------------------'); |
| 80 | + console.log(error.stdout.toString()); |
| 81 | + process.exitCode = error && error.status ? error.status : 0; |
| 82 | + } |
| 83 | + |
| 84 | + await cleanup(folderId); |
| 85 | + |
| 86 | + console.log('Test script complete. Exiting.'); |
| 87 | +} |
| 88 | + |
| 89 | +main(); |
0 commit comments