-
Notifications
You must be signed in to change notification settings - Fork 7
/
runner_recursive_service.js
107 lines (89 loc) · 2.7 KB
/
runner_recursive_service.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
const dotenv = require("dotenv");
const {
ParallelSpecsClient,
filesToSpecInput,
} = require("@shelex/parallel-specs-client");
const cypress = require("cypress");
dotenv.config();
// get number of executors, command and arguments
const { executors = 1, filter } = process.argv.slice(2).reduce((acc, pair) => {
const [key, value] = pair.split("=");
acc[key] = value;
return acc;
}, {});
// pass credentials and project
const client = new ParallelSpecsClient({
project: process.env.PARALLEL_SPECS_PROJECT_NAME,
token: process.env.PARALLEL_SPECS_API_KEY,
});
// get all specs
const specs = filesToSpecInput(
["**/e2e/**/*.cy.js"],
filter ? filter.split(",").map((f) => f.trim()) : []
);
// create new session
const createdSession = client.addSession(specs);
console.log(`created session ${createdSession.sessionId}`);
let exitCode = 0;
const previousStatusesByMachineId = {};
/* execute cypress run for spec file */
const cypressTask = (spec) =>
new Promise((resolve) =>
cypress
.run({
browser: "chrome",
spec: spec,
config: {
video: false,
},
})
.then((results) => {
process.exitCode += results.totalFailed || 0;
resolve(results.totalFailed || 0);
})
.catch((err) => {
process.stdout.write(err);
process.exitCode += 1;
resolve(1);
})
);
const handleExitCode = (machineId, exitCode) => {
previousStatusesByMachineId[machineId] =
exitCode === 0 ? "passed" : "failed";
};
const getPreviousStatus = (machineId) => {
const status = previousStatusesByMachineId[machineId] || "unknown";
delete previousStatusesByMachineId[machineId];
return status;
};
async function next(machineId) {
const nextSpec = client.next({
machineId,
previousStatus: getPreviousStatus(machineId),
});
if (!nextSpec) {
return Promise.reject(`ALL SPECS PROCESSED for ${machineId}\n`);
}
process.stdout.write(
`PICKING UP NEXT TASK (${nextSpec}) for machine ${machineId}\n`
);
const exitCode = await cypressTask(nextSpec);
handleExitCode(machineId, exitCode);
return Promise.resolve(exitCode);
}
const executor = (machineId) =>
Promise.resolve(
(function recursive() {
next(machineId)
.then(recursive)
.catch((e) => console.log(e));
})()
);
const runners = Array.from({ length: executors }, (_, v) =>
executor(`machine${v + 1}`)
);
Promise.all(runners);
process.on("exit", () => {
process.exit(exitCode);
});