forked from tnicola/cypress-parallel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
53 lines (45 loc) · 1.37 KB
/
utility.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
const fs = require('fs');
const path = require('path');
const { settings } = require('./settings');
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const formatTime = function (timeMs) {
const seconds = Math.ceil(timeMs / 1000);
const sec = seconds % 60;
const min = Math.floor(seconds / 60);
let res = '';
if (min) res += `${min}m `;
res += `${sec}s`;
return res;
};
function generateWeightsFile(specWeights, totalDuration, totalWeight) {
Object.keys(specWeights).forEach((spec) => {
specWeights[spec].weight = Math.floor(
(specWeights[spec].time / totalDuration) * totalWeight
);
});
const weightsJson = JSON.stringify(specWeights);
try {
const weightPath = path.join(process.cwd(), settings.weightsJSON);
fs.writeFileSync(`${weightPath}`, weightsJson, 'utf8');
console.log('Weights file generated under path: ' + weightPath)
} catch(e) {
console.error(e)
}
}
function collectResults(resultsPath) {
const resultFiles = fs.readdirSync(resultsPath);
const results = new Map();
resultFiles.forEach((fileName) => {
const filePath = path.join(resultsPath, fileName);
const content = fs.readFileSync(filePath);
const result = JSON.parse(content);
results.set(result.file, result);
});
return results;
}
module.exports = {
collectResults,
sleep,
formatTime,
generateWeightsFile
};