forked from box/box-annotations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcypress.js
89 lines (73 loc) · 2.69 KB
/
cypress.js
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
/* eslint-disable no-console */
const BoxSDK = require('box-node-sdk');
const childProcess = require('child_process');
const { argv, env } = process;
const { E2E_ACCESS_TOKEN, E2E_CLIENT_ID } = env;
const KILL_SIGNALS = [
'SIGABRT',
'SIGBUS',
'SIGFPE',
'SIGHUP',
'SIGILL',
'SIGINT',
'SIGQUIT',
'SIGSEGV',
'SIGTERM',
'SIGTRAP',
'SIGUSR1',
'SIGUSR2',
];
if (!E2E_ACCESS_TOKEN) {
throw new Error('E2E_ACCESS_TOKEN must be set as an environment variable');
}
if (!E2E_CLIENT_ID) {
throw new Error('E2E_CLIENT_ID must be set as an environment variable');
}
const sdk = new BoxSDK({
clientID: E2E_CLIENT_ID,
clientSecret: 'none',
});
const client = sdk.getBasicClient(E2E_ACCESS_TOKEN);
async function cleanup(folderId) {
console.log('Cleanup test folder and files...');
try {
await client.folders.delete(folderId, { recursive: true });
console.log('Cleanup complete.');
} catch (error) {
console.error(`Cleanup failed. Error: ${error.message}`);
}
}
async function main() {
const testFolderName = `Test ${new Date().toISOString()}`;
// Bootstrap the test folder and copy template files into it
console.log(`Setup test folder: ${testFolderName}...`);
const { id: folderId } = await client.folders.create('118537970832', testFolderName); // Test folder
const { id: documentId } = await client.files.copy('694470903390', folderId); // Document template
const { id: imageId } = await client.files.copy('694468799644', folderId); // Image template
console.log('Setup complete.');
// Attempt to cleanup test folder before script is killed (example: CTL+C)
KILL_SIGNALS.forEach(signal => process.on(signal, () => cleanup(folderId)));
try {
console.log('Cypress run starting...');
const suffix = argv.indexOf('-o') >= 0 ? 'open' : 'run'; // Pass -o to run Cypress in "open" mode
const output = childProcess.execSync(`yarn npm-run-all -p -r start:dev cy:${suffix}`, {
env: {
...env,
CYPRESS_ACCESS_TOKEN: E2E_ACCESS_TOKEN,
CYPRESS_FILE_ID_DOC: documentId,
CYPRESS_FILE_ID_IMAGE: imageId,
},
});
console.log('Cypress run SUCCESS. Output:');
console.log('------------------------------');
console.log(output.toString());
} catch (error) {
console.log('Cypress run FAILURE. Output:');
console.log('------------------------------');
console.log(error.stdout.toString());
process.exitCode = error && error.status ? error.status : 0;
}
await cleanup(folderId);
console.log('Test script complete. Exiting.');
}
main();