Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const path = require('path');
const fs = require('fs-extra');
const { settings } = require('./settings');

const { getTestSuitePaths, distributeTestsByWeight, getMaxPathLenghtFrom } = require('./test-suites');
const {
getTestSuitePaths,
distributeTestsByWeight,
getMaxPathLenghtFrom
} = require('./test-suites');
const {
formatTime,
generateWeightsFile,
Expand All @@ -15,25 +19,29 @@ const {
const { executeThread } = require('./thread');
const { resultsPath } = require('./shared-config');

function cleanResultsPath() {
if(!fs.existsSync(resultsPath)) {
fs.mkdirSync(resultsPath, { recursive: true })
function cleanResultsPath() {
if (!fs.existsSync(resultsPath)) {
fs.mkdirSync(resultsPath, { recursive: true });
} else {
fs.readdir(resultsPath, (err, files) => {
if (err) console.log(err);
for (const file of files) {
fs.unlink(path.join(resultsPath, file), err => { if (err) console.log(err); });
}
});
}
if (err) console.log(err);
for (const file of files) {
fs.unlink(path.join(resultsPath, file), (err) => {
if (err) console.log(err);
});
}
});
}
}

async function start() {
cleanResultsPath();
const testSuitePaths = await getTestSuitePaths();
const threads = distributeTestsByWeight(testSuitePaths);
const start = new Date();
await Promise.all(threads.map(executeThread));
const failedResults = (
await Promise.allSettled(threads.map(executeThread))
).filter(({ status }) => status === 'rejected');
const end = new Date();
const timeTaken = end.getTime() - start.getTime();

Expand Down Expand Up @@ -117,6 +125,13 @@ async function start() {
process.exit(1);
}

if (failedResults.length > 0) {
process.stderr.write(
`\x1b[31m${failedResults.length} thread(s) exited with errors\n`
);
process.exit(1);
}

const timeSaved = totalDuration - timeTaken;
console.log(
`Total run time: ${totalDuration / 1000}s, executed in: ${
Expand Down
54 changes: 47 additions & 7 deletions lib/thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function getPackageManager() {
const pckManager = isYarn
? 'yarn'
: process.platform === 'win32'
? 'npm.cmd'
: 'npm';
? 'npm.cmd'
: 'npm';

return pckManager;
}
Expand Down Expand Up @@ -50,7 +50,7 @@ function createReporterConfigFile(path) {
}

function createCommandArguments(thread) {
const specFiles = `${thread.list.map(path => globEscape(path)).join(',')}`;
const specFiles = `${thread.list.map((path) => globEscape(path)).join(',')}`;
const childOptions = [
'run',
`${settings.script}`,
Expand All @@ -77,38 +77,78 @@ function createCommandArguments(thread) {
async function executeThread(thread, index) {
const packageManager = getPackageManager();
const commandArguments = createCommandArguments(thread);
const threadNumber = index + 1;
const logPrefix = `[${threadNumber}/${settings.threadCount}]`;

// staggered start (when executed in container with xvfb ends up having a race condition causing intermittent failures)
await sleep((index +1) * 2000);
await sleep(threadNumber * 2000);

const timeMap = new Map();

const promise = new Promise((resolve, reject) => {
const processOptions = {
cwd: process.cwd(),
stdio: 'inherit',
stdio: 'pipe',
env: {
...process.env,
CYPRESS_THREAD: (index + 1).toString()
CYPRESS_THREAD: threadNumber.toString()
}
};
const child = spawn(packageManager, commandArguments, processOptions);

let childStdout = '';
let childStderr = '';

child.stdout.on('data', (chunk) => {
childStdout += chunk;
const lines = childStdout.split('\n');
while (lines.length > 1) {
console.log(logPrefix, lines.shift());
}
childStdout = lines.shift();
});

child.stdout.on('end', () => {
console.log(logPrefix, childStdout);
});

child.stderr.on('data', (chunk) => {
childStderr += chunk;
const lines = childStderr.split('\n');
while (lines.length > 1) {
console.error(logPrefix, lines.shift());
}
childStderr = lines.shift();
});

child.stderr.on('end', () => {
console.error(logPrefix, childStderr);
});

child.on('exit', (exitCode) => {
if (settings.isVerbose) {
console.log(
`Thread ${index} likely finished with failure count: ${exitCode}`
logPrefix,
`Thread likely finished with failure count: ${exitCode}`
);
}
// should preferably exit earlier, but this is simple and better than nothing
if (settings.shouldBail) {
if (exitCode > 0) {
console.error(
logPrefix,
'BAIL set and thread exited with errors, exit early with error'
);
process.exit(exitCode);
}
}

if (exitCode !== 0) {
console.error(logPrefix, `Thread failed with exit code: ${exitCode}`);

reject(exitCode);
}

resolve(timeMap);
});
});
Expand Down