Skip to content

Commit

Permalink
release(2.0.1): enhance the log messages [DS-1275] (#15)
Browse files Browse the repository at this point in the history
release(2.0.1): enhance the log messages [DS-1275] (#15)
  • Loading branch information
0xIslamTaha authored Jan 21, 2022
1 parent 7014bdf commit d5b9255
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 21 deletions.
23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@0xislamtaha/orchestrator",
"version": "2.0.0",
"description": "orchestrate cypress parallel execution across multiple Docker containers",
"version": "2.0.1",
"description": "Execute cypress specs in parallel across multiple docker containers",
"main": "src/orchestrator.js",
"scripts": {
"test": "echo \"No test specified\""
Expand All @@ -13,7 +13,6 @@
"publishConfig": {
"access": "public"
},
"author": "0xIslamTaha",
"license": "MIT",
"dependencies": {
"arg": "^4.1.3",
Expand All @@ -26,5 +25,19 @@
"files": [
"bin/",
"src/"
]
}
],
"keywords": [
"cypress",
"parallel execution",
"chrome",
"firefox",
"parallel",
"cypress parallel"
],
"homepage": "https://github.com/0xIslamTaha/orchestrator",
"author": {
"name": "Islam Taha",
"email": "islamtaha2012@gmail.com",
"url": "https://twitter.com/0xIslamTaha"
}
}
7 changes: 2 additions & 5 deletions src/analyseReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
writeJsonFile,
} from "./helper.js";
import path from "path";
import * as lg from "./logger";


const browsers = ["chrome", "firefox"];
Expand Down Expand Up @@ -43,8 +44,6 @@ function updateSpecData(suites, specName) {
}

export function analyseReport(mochaReportPath, executiontimeReportJsonPath) {
console.log("analyse the json report .... ");

if (checkFileIsExisting(mochaReportPath)) {
const executionTimeReportDir = path.dirname(executiontimeReportJsonPath);
const executionTimeReporJson = executiontimeReportJsonPath.split("/").pop();
Expand All @@ -61,9 +60,7 @@ export function analyseReport(mochaReportPath, executiontimeReportJsonPath) {
writeJsonFile(specs, executionTimeReportDir, executionTimeReporJson);

for (let browser of browsers) {
console.log(
`------------------------- ${browser} -------------------------`
);
lg.subStep(`${browser}`);
let data = orderBasedOnBrowserDuration(specs, browser).map((spec) => {
return {
specName: spec.specName,
Expand Down
30 changes: 30 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function banner() {
let banner = `
██████ ██████ ██████ ██ ██ ███████ ███████ ████████ ██████ █████ ████████ ██████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ██ ███████ █████ ███████ ██ ██████ ███████ ██ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ██████ ██ ██ ███████ ███████ ██ ██ ██ ██ ██ ██ ██████ ██ ██
v 2.0.1 @0xIslamTaha
`;

console.log(banner);
}


function step(msg, newLine=false) {
let message;
if (newLine) {
message = `\n[*] ${msg}`;
} else {
message = `[*] ${msg}`;
}
console.log(message);
}

function subStep(subStep){
let message = `[-] ${subStep}`;
console.log(message);
}

export { banner, step, subStep };
36 changes: 25 additions & 11 deletions src/orchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
orderBasedOnBrowserDuration,
} from "./helper.js";

import * as lg from "./logger";

const executionTimeReportDir = "executionTimeReprot";
const executionTimeReportDirPath = path.resolve(process.cwd(), executionTimeReportDir)
const executiontimeReportJson = "specsExecutionTime.json"
Expand Down Expand Up @@ -75,20 +77,26 @@ function parseArgumentsIntoConfig(rawArgs) {
}

function overWriteConfig(args) {
lg.step('Overwrite the config file with the arguments if there is any');
const configFile = args["--config"] || path.resolve(__dirname, "config.json");
const defaultConfig = JSON.parse(fs.readFileSync(configFile, "utf-8"));
const config = { ...defaultConfig, ...args };
return config;
}

function setEnvVars(config) {
lg.step("Export the environment variables");
Object.keys(config.environment).forEach((key) => {
sh.env[key] = config.environment[key];
let value = config.environment[key];
sh.env[key] = value;
lg.subStep(`${key}=${value}`);
});
}

function execPreCommands(config) {
lg.step("Execute the pre commands", true);
config.preCommands.forEach((command) => {
lg.subStep(`~$ ${command}`);
sh.exec(command);
});
}
Expand Down Expand Up @@ -202,6 +210,7 @@ function _constructCypressCommands(config) {
}

function upConrainters(config) {
lg.step("Start the cypress containers", true);
let promises = [];
let commands = [];
let [container_name, command] = ["", ""];
Expand All @@ -214,33 +223,37 @@ function upConrainters(config) {

command = `timeout --preserve-status ${config.timeout} docker-compose -f ${config.dockerComposePath} run --name ${container_name} ${config.cypressContainerName} bash -c '${cmd}'`;
commands.push(command);
console.log(command);
lg.subStep(`~$ ${command}`);
promises.push(execa(command));
});

return promises;
}

function downContainers(config) {
lg.step("Stop the cypress containers", true);
let dockerComposeDown = `docker-compose -f ${config.dockerComposePath} down`;
sh.exec(dockerComposeDown);
}

function generateReport(config) {
console.log("generate the report ....");
lg.step("Generate the reports", true);
return merge({ files: [config.mochawesomeJSONPath] })
.then((report) =>
.then((report) =>{
lg.subStep(`HTML report: ${config.reportPath}/mochawsome.html`);
marge.create(report, {
reportDir: config.reportPath,
charts: true,
saveJson: true,
})
)

})
.then(() => {
if (config.analyseReport) {
if (!fs.existsSync(executionTimeReportDirPath)){
sh.mkdir(executionTimeReportDirPath);
}
lg.subStep(`Execution time report: ${executionTimeReportDir}/${executiontimeReportJson}`);
_analyseReport(config);
}
});
Expand All @@ -267,16 +280,14 @@ function _analyseReport(config) {
function afterPromises(config, timer) {
downContainers(config);
generateReport(config).then(() => {
console.log(
`\b------------------------- Exeuction Time -------------------------`
);
console.log("\bAll down in: ");
console.timeEnd(timer);
});
}

export async function orchestrator(rawArgs) {
let orchestratorTime = "orchestratorTime";
lg.banner();

let orchestratorTime = "\n[*] Total execution time";
let config = overWriteConfig(parseArgumentsIntoConfig(rawArgs));

console.time(orchestratorTime);
Expand All @@ -288,7 +299,10 @@ export async function orchestrator(rawArgs) {
afterPromises(config, orchestratorTime);
const failedPromises = promises.filter(promise => promise.status === 'rejected');
if(failedPromises.length){
setTimeout(() => sh.exit(1), 5000);
setTimeout(() => {
lg.step('Exit code: 1');
sh.exit(1);
}, 5000);
}
});
}

0 comments on commit d5b9255

Please sign in to comment.