From 4c9071041b8d0474022b37f3b9b0f1698f1146ef Mon Sep 17 00:00:00 2001 From: Aditya Samantaray Date: Mon, 23 Jan 2023 12:52:48 +0530 Subject: [PATCH 1/8] fix: poll from v2 endpoint; display duration in the end --- bin/helpers/config.js | 30 ++++++++++++++++++++++++ bin/helpers/sync/specsSummary.js | 2 +- bin/helpers/sync/syncSpecsLogs.js | 31 ++++++++++++++++++++---- bin/helpers/syncRunner.js | 39 +++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 6 deletions(-) diff --git a/bin/helpers/config.js b/bin/helpers/config.js index d1684936..0fba913f 100644 --- a/bin/helpers/config.js +++ b/bin/helpers/config.js @@ -13,7 +13,9 @@ if(config.env !== "production") { } config.cypress_v1 = `${config.rails_host}/automate/cypress/v1`; +config.cypress_v2 = `${config.rails_host}/automate/cypress/v2`; config.buildUrl = `${config.cypress_v1}/builds/`; +config.buildUrlV2 = `${config.cypress_v2}/builds/`; config.buildStopUrl = `${config.cypress_v1}/builds/stop/`; config.checkMd5sum = `${config.cypress_v1}/md5sumcheck/`; config.getInitialDetails = `${config.cypress_v1}/get_initial_details/`; @@ -24,3 +26,31 @@ config.retries = 5; config.networkErrorExitCode = 2; module.exports = config; + + +// Uncomment below or local testing + +// var config = {}; +// config.env = "local"; +// var hosts = { +// prod: { +// uploadUrl: `https://api-cloud.browserstack.com/automate-frameworks/cypress/upload`, +// rails_host: `https://api.browserstack.com` +// }, +// local: { +// uploadUrl: `http://localhost:4000/automate-frameworks/cypress/upload`, +// rails_host: `https://api-local.bsstag.com` +// } +// }; +// config.uploadUrl = hosts[config.env].uploadUrl; +// config.rails_host = hosts[config.env].rails_host; +// config.cypress_v1 = `${config.rails_host}/automate/cypress/v1`; +// config.buildUrl = `${config.cypress_v1}/builds/`; +// config.buildStopUrl = `${config.cypress_v1}/builds/stop/`; +// config.usageReportingUrl = `http://127.0.0.1:8000/send_event_cy_internal`; +// config.fileName = "tests.zip"; +// config.packageFileName = "bstackPackages.tar.gz"; +// config.packageDirName = "tmpBstackPackages"; +// config.retries = 5; +// config.networkErrorExitCode = 2; +// module.exports = config diff --git a/bin/helpers/sync/specsSummary.js b/bin/helpers/sync/specsSummary.js index 80bcc4c1..f29717f3 100644 --- a/bin/helpers/sync/specsSummary.js +++ b/bin/helpers/sync/specsSummary.js @@ -33,7 +33,7 @@ let printSpecsRunSummary = (data, machines, customErrorsToPrint) => { }); logger.info(`Total tests: ${summary.total}, passed: ${summary.passed}, failed: ${summary.failed}, skipped: ${summary.skipped}, passed_with_skipped: ${summary.passed_with_skipped}, pending: ${summary.pending}`); - logger.info(`Done in ${data.duration/1000} seconds using ${machines} machines\n`); + // logger.info(`Done in ${data.duration/1000} seconds using ${machines} machines\n`); if (customErrorsToPrint && customErrorsToPrint.length > 0) { for (const error of customErrorsToPrint) { diff --git a/bin/helpers/sync/syncSpecsLogs.js b/bin/helpers/sync/syncSpecsLogs.js index 437371d4..38028e44 100644 --- a/bin/helpers/sync/syncSpecsLogs.js +++ b/bin/helpers/sync/syncSpecsLogs.js @@ -24,7 +24,7 @@ if (!isNaN(terminalWidth)) lineSeparator = "\n" + "-".repeat(terminalWidth); let getOptions = (auth, build_id) => { return { - url: `${config.buildUrl}${build_id}`, + url: `${config.buildUrlV2}${build_id}`, auth: { user: auth.username, password: auth.access_key @@ -144,10 +144,17 @@ let whileProcess = (whilstCallback) => { whileTries = config.retries; // reset to default after every successful request + console.log(`---> response code: ${response.statusCode} body is: ${JSON.stringify(body)}`) switch (response.statusCode) { case 202: // get data here and print it n = 2 - showSpecsStatus(body); + // try { + // parsed_body = JSON.parse(JSON.stringify(body)); + // showSpecsStatus(parsed_body['data']); + // } catch (error) { + // console.log(`---> Got error: ${JSON.stringify(error)}`) + // } + showSpecsStatus(body, 202); return setTimeout(whilstCallback, timeout * n, null); case 204: // No data available, wait for some time and ask again n = 1 @@ -155,7 +162,13 @@ let whileProcess = (whilstCallback) => { case 200: // Build is completed. whileLoop = false; endTime = Date.now(); - showSpecsStatus(body); + // try { + // parsed_body = JSON.parse(body); + // showSpecsStatus(JSON.stringify(parsed_body['data'])); + // } catch (error) { + // console.log(`---> Got error: ${JSON.stringify(error)}`) + // } + showSpecsStatus(body, 200); return specSummary.exitCode == Constants.BUILD_FAILED_EXIT_CODE ? whilstCallback({ status: 204, message: "No specs ran in the build"} ) : whilstCallback(null, body); default: @@ -169,9 +182,9 @@ let getStackTraceUrl = () => { return specSummary.buildError } -let showSpecsStatus = (data) => { +let showSpecsStatus = (data, statusCode) => { let specData = JSON.parse(data); - specData.forEach(specDetails => { + specData["specDetails"].forEach(specDetails => { if (specDetails.type === Constants.CYPRESS_CUSTOM_ERRORS_TO_PRINT_KEY) { addCustomErrorToPrint(specDetails); } else { @@ -190,6 +203,14 @@ let showSpecsStatus = (data) => { } } }); + if ( statusCode != 200 ) return; + // Below block is for printing build details, return if non 200 status code + if ("buildDetails" in specData) { + const buildDetails = specData["buildDetails"]; + logger.info(`Done in ${buildDetails["duration"]} seconds with ${buildDetails["parallels"]} parallels.\n`); + } else { + logger.debug(`Build details not sent`) + } } let printInitialLog = () => { diff --git a/bin/helpers/syncRunner.js b/bin/helpers/syncRunner.js index d18e3452..b1866e27 100644 --- a/bin/helpers/syncRunner.js +++ b/bin/helpers/syncRunner.js @@ -1,6 +1,10 @@ 'use strict'; +const logger = require("./logger").syncCliLogger; +const request = require('request'); const syncSpecsLogs = require('./sync/syncSpecsLogs'), specDetails = require('./sync/failedSpecsDetails'), + config = require("../helpers/config"), + utils = require('./utils'), specsSummary = require('./sync/specsSummary'); exports.pollBuildStatus = (bsConfig, buildDetails, rawArgs, buildReportData) => { @@ -12,6 +16,41 @@ exports.pollBuildStatus = (bsConfig, buildDetails, rawArgs, buildReportData) => } return specDetails.failedSpecsDetails(data); }).then((data) => { + // logger.info(`--> data: ${JSON.stringify(data)}\n--> buildDetails: ${JSON.stringify(buildDetails)}\n-->${JSON.stringify(rawArgs)}`) + // const buildId = buildDetails.build_id; + // let options = { + // url: config.buildUrl + buildId, + // auth: { + // user: bsConfig.auth.username, + // password: bsConfig.auth.access_key, + // }, + // headers: { + // 'User-Agent': utils.getUserAgent(), + // }, + // }; + // logger.info(`--> options: ${JSON.stringify(options)}`) + // request.get(options, function (err, resp, body) { + // logger.info(`\n\n--> build details body: ${JSON.stringify(body)}`) + // let message = null; + // let messageType = null; + // let errorCode = null; + // if (err) { + // message = Constants.userMessages.BUILD_INFO_FAILED; + // messageType = Constants.messageTypes.ERROR; + // errorCode = 'api_failed_build_info'; + + // logger.info(message); + // logger.error(utils.formatRequest(err, resp, body)); + // } else { + // let build = null; + // try { + // build = JSON.parse(body); + // } catch (error) { + // build = null; + // } + // logger.info(`\n\n--> build details: ${JSON.stringify(build)}`) + // } + // }); return specsSummary.printSpecsRunSummary(data, buildDetails.machines, customErrorsToPrint); }).then((successExitCode) => { resolve(successExitCode); // exit code 0 From 57216d474defcaa92013d0c7db0d1baee9088f95 Mon Sep 17 00:00:00 2001 From: Aditya Samantaray Date: Mon, 23 Jan 2023 23:57:11 +0530 Subject: [PATCH 2/8] fix: duration calculation with dashboard duration --- bin/helpers/sync/specsSummary.js | 2 +- bin/helpers/sync/syncSpecsLogs.js | 27 ++++++++++----------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/bin/helpers/sync/specsSummary.js b/bin/helpers/sync/specsSummary.js index f29717f3..404e655e 100644 --- a/bin/helpers/sync/specsSummary.js +++ b/bin/helpers/sync/specsSummary.js @@ -33,7 +33,7 @@ let printSpecsRunSummary = (data, machines, customErrorsToPrint) => { }); logger.info(`Total tests: ${summary.total}, passed: ${summary.passed}, failed: ${summary.failed}, skipped: ${summary.skipped}, passed_with_skipped: ${summary.passed_with_skipped}, pending: ${summary.pending}`); - // logger.info(`Done in ${data.duration/1000} seconds using ${machines} machines\n`); + logger.debug(`CLI calculated duration: ${data.duration/1000} seconds using ${machines} machines\n`); if (customErrorsToPrint && customErrorsToPrint.length > 0) { for (const error of customErrorsToPrint) { diff --git a/bin/helpers/sync/syncSpecsLogs.js b/bin/helpers/sync/syncSpecsLogs.js index 38028e44..400d2670 100644 --- a/bin/helpers/sync/syncSpecsLogs.js +++ b/bin/helpers/sync/syncSpecsLogs.js @@ -1,4 +1,7 @@ "use strict"; + +const { util } = require("chai"); + const request = require("request"), config = require("../config"), utils = require("../utils"), @@ -148,12 +151,6 @@ let whileProcess = (whilstCallback) => { switch (response.statusCode) { case 202: // get data here and print it n = 2 - // try { - // parsed_body = JSON.parse(JSON.stringify(body)); - // showSpecsStatus(parsed_body['data']); - // } catch (error) { - // console.log(`---> Got error: ${JSON.stringify(error)}`) - // } showSpecsStatus(body, 202); return setTimeout(whilstCallback, timeout * n, null); case 204: // No data available, wait for some time and ask again @@ -162,12 +159,6 @@ let whileProcess = (whilstCallback) => { case 200: // Build is completed. whileLoop = false; endTime = Date.now(); - // try { - // parsed_body = JSON.parse(body); - // showSpecsStatus(JSON.stringify(parsed_body['data'])); - // } catch (error) { - // console.log(`---> Got error: ${JSON.stringify(error)}`) - // } showSpecsStatus(body, 200); return specSummary.exitCode == Constants.BUILD_FAILED_EXIT_CODE ? whilstCallback({ status: 204, message: "No specs ran in the build"} ) : whilstCallback(null, body); @@ -184,7 +175,7 @@ let getStackTraceUrl = () => { let showSpecsStatus = (data, statusCode) => { let specData = JSON.parse(data); - specData["specDetails"].forEach(specDetails => { + specData["specData"].forEach(specDetails => { if (specDetails.type === Constants.CYPRESS_CUSTOM_ERRORS_TO_PRINT_KEY) { addCustomErrorToPrint(specDetails); } else { @@ -205,12 +196,14 @@ let showSpecsStatus = (data, statusCode) => { }); if ( statusCode != 200 ) return; // Below block is for printing build details, return if non 200 status code - if ("buildDetails" in specData) { - const buildDetails = specData["buildDetails"]; - logger.info(`Done in ${buildDetails["duration"]} seconds with ${buildDetails["parallels"]} parallels.\n`); + if ("buildData" in specData) { + const buildDetails = specData.buildData; + const totalDuration = buildDetails.duration?.total_duration + const parallels = buildDetails.parallels + logger.info(`Done in ${totalDuration} seconds with ${parallels} parallels.\n`); } else { logger.debug(`Build details not sent`) - } + } } let printInitialLog = () => { From 3f6cb7ac231649575986ce99a0e4ba8d449b4e8e Mon Sep 17 00:00:00 2001 From: Aditya Samantaray Date: Tue, 24 Jan 2023 11:56:12 +0530 Subject: [PATCH 3/8] refactor: remove unused comments --- bin/helpers/config.js | 28 ------------------------- bin/helpers/sync/syncSpecsLogs.js | 1 - bin/helpers/syncRunner.js | 35 ------------------------------- 3 files changed, 64 deletions(-) diff --git a/bin/helpers/config.js b/bin/helpers/config.js index 0fba913f..967f1617 100644 --- a/bin/helpers/config.js +++ b/bin/helpers/config.js @@ -26,31 +26,3 @@ config.retries = 5; config.networkErrorExitCode = 2; module.exports = config; - - -// Uncomment below or local testing - -// var config = {}; -// config.env = "local"; -// var hosts = { -// prod: { -// uploadUrl: `https://api-cloud.browserstack.com/automate-frameworks/cypress/upload`, -// rails_host: `https://api.browserstack.com` -// }, -// local: { -// uploadUrl: `http://localhost:4000/automate-frameworks/cypress/upload`, -// rails_host: `https://api-local.bsstag.com` -// } -// }; -// config.uploadUrl = hosts[config.env].uploadUrl; -// config.rails_host = hosts[config.env].rails_host; -// config.cypress_v1 = `${config.rails_host}/automate/cypress/v1`; -// config.buildUrl = `${config.cypress_v1}/builds/`; -// config.buildStopUrl = `${config.cypress_v1}/builds/stop/`; -// config.usageReportingUrl = `http://127.0.0.1:8000/send_event_cy_internal`; -// config.fileName = "tests.zip"; -// config.packageFileName = "bstackPackages.tar.gz"; -// config.packageDirName = "tmpBstackPackages"; -// config.retries = 5; -// config.networkErrorExitCode = 2; -// module.exports = config diff --git a/bin/helpers/sync/syncSpecsLogs.js b/bin/helpers/sync/syncSpecsLogs.js index 400d2670..c873eecb 100644 --- a/bin/helpers/sync/syncSpecsLogs.js +++ b/bin/helpers/sync/syncSpecsLogs.js @@ -147,7 +147,6 @@ let whileProcess = (whilstCallback) => { whileTries = config.retries; // reset to default after every successful request - console.log(`---> response code: ${response.statusCode} body is: ${JSON.stringify(body)}`) switch (response.statusCode) { case 202: // get data here and print it n = 2 diff --git a/bin/helpers/syncRunner.js b/bin/helpers/syncRunner.js index b1866e27..9fd3ba8b 100644 --- a/bin/helpers/syncRunner.js +++ b/bin/helpers/syncRunner.js @@ -16,41 +16,6 @@ exports.pollBuildStatus = (bsConfig, buildDetails, rawArgs, buildReportData) => } return specDetails.failedSpecsDetails(data); }).then((data) => { - // logger.info(`--> data: ${JSON.stringify(data)}\n--> buildDetails: ${JSON.stringify(buildDetails)}\n-->${JSON.stringify(rawArgs)}`) - // const buildId = buildDetails.build_id; - // let options = { - // url: config.buildUrl + buildId, - // auth: { - // user: bsConfig.auth.username, - // password: bsConfig.auth.access_key, - // }, - // headers: { - // 'User-Agent': utils.getUserAgent(), - // }, - // }; - // logger.info(`--> options: ${JSON.stringify(options)}`) - // request.get(options, function (err, resp, body) { - // logger.info(`\n\n--> build details body: ${JSON.stringify(body)}`) - // let message = null; - // let messageType = null; - // let errorCode = null; - // if (err) { - // message = Constants.userMessages.BUILD_INFO_FAILED; - // messageType = Constants.messageTypes.ERROR; - // errorCode = 'api_failed_build_info'; - - // logger.info(message); - // logger.error(utils.formatRequest(err, resp, body)); - // } else { - // let build = null; - // try { - // build = JSON.parse(body); - // } catch (error) { - // build = null; - // } - // logger.info(`\n\n--> build details: ${JSON.stringify(build)}`) - // } - // }); return specsSummary.printSpecsRunSummary(data, buildDetails.machines, customErrorsToPrint); }).then((successExitCode) => { resolve(successExitCode); // exit code 0 From 751b75ea54a4a1a3797460a43a19358fce69f954 Mon Sep 17 00:00:00 2001 From: Aditya Samantaray Date: Mon, 30 Jan 2023 11:29:32 +0530 Subject: [PATCH 4/8] chore: push specs for CLI --- bin/helpers/sync/syncSpecsLogs.js | 3 -- test/unit/bin/helpers/sync/specSummary.js | 8 +++- test/unit/bin/helpers/sync/syncSpecsLogs.js | 41 ++++++++++++++++----- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/bin/helpers/sync/syncSpecsLogs.js b/bin/helpers/sync/syncSpecsLogs.js index c873eecb..18c6d61b 100644 --- a/bin/helpers/sync/syncSpecsLogs.js +++ b/bin/helpers/sync/syncSpecsLogs.js @@ -1,7 +1,4 @@ "use strict"; - -const { util } = require("chai"); - const request = require("request"), config = require("../config"), utils = require("../utils"), diff --git a/test/unit/bin/helpers/sync/specSummary.js b/test/unit/bin/helpers/sync/specSummary.js index 084ead97..273dbadd 100644 --- a/test/unit/bin/helpers/sync/specSummary.js +++ b/test/unit/bin/helpers/sync/specSummary.js @@ -45,12 +45,14 @@ describe("printSpecsRunSummary", () => { it('returns passed specs data', () => { var loggerInfoSpy = sinon.spy(logger, 'info'); + var loggerDebugSpy = sinon.spy(logger, 'debug'); specSummary.printSpecsRunSummary(data, machines); sinon.assert.calledWith(loggerInfoSpy, 'Total tests: 4, passed: 1, failed: 2, skipped: 1, passed_with_skipped: 0, pending: 0'); - sinon.assert.calledWith(loggerInfoSpy, `Done in ${time / 1000} seconds using ${machines} machines\n`); + sinon.assert.calledWith(loggerDebugSpy, `CLI calculated duration: ${time / 1000} seconds using ${machines} machines\n`); loggerInfoSpy.restore(); + loggerDebugSpy.restore(); }); }); @@ -74,14 +76,16 @@ describe("printSpecsRunSummary", () => { it('prints the custom error message along with build details', () => { var loggerInfoSpy = sinon.spy(logger, 'info'); + var loggerDebugSpy = sinon.spy(logger, 'debug'); var loggerWarnSpy = sinon.spy(winstonLogger, 'warn'); specSummary.printSpecsRunSummary(data, machines, customErrorsToPrint); sinon.assert.calledWith(loggerInfoSpy, 'Total tests: 4, passed: 1, failed: 2, skipped: 1, passed_with_skipped: 0, pending: 0'); - sinon.assert.calledWith(loggerInfoSpy, `Done in ${time / 1000} seconds using ${machines} machines\n`); + sinon.assert.calledWith(loggerDebugSpy, `CLI calculated duration: ${time / 1000} seconds using ${machines} machines\n`); sinon.assert.calledWith(loggerWarnSpy, `custom error message`); loggerInfoSpy.restore(); + loggerDebugSpy.restore(); loggerWarnSpy.restore(); }); }); diff --git a/test/unit/bin/helpers/sync/syncSpecsLogs.js b/test/unit/bin/helpers/sync/syncSpecsLogs.js index d3e338e6..d0efa7f3 100644 --- a/test/unit/bin/helpers/sync/syncSpecsLogs.js +++ b/test/unit/bin/helpers/sync/syncSpecsLogs.js @@ -80,7 +80,7 @@ describe("syncSpecsLogs", () => { it('should return proper request option for polling', () => { let options = getOptions(auth, build_id); - expect(options.url).to.equal(`${config.buildUrl}${build_id}`); + expect(options.url).to.equal(`${config.buildUrlV2}${build_id}`); expect(options.auth.user).to.equal(auth.username); expect(options.auth.password).to.equal(auth.access_key); expect(options.headers["Content-Type"]).to.equal("application/json"); @@ -216,35 +216,38 @@ describe("syncSpecsLogs", () => { context("showSpecsStatus", () => { const showSpecsStatus = syncSpecsLogs.__get__("showSpecsStatus"); + const buildCreatedStatusCode = 202 + const buildRunningStatusCode = 204 + const buildCompletedStatusCode = 200 it('should not print initial log for running specs when it is the 1st polling response', () => { - let data = JSON.stringify(["created"]) + let data = JSON.stringify({ "specData": ["created"], "buildData": {"duration": "NA", "parallels": "NA"}}) var printInitialLog = sandbox.stub(); syncSpecsLogs.__set__('printInitialLog', printInitialLog); - showSpecsStatus(data); + showSpecsStatus(data, buildCreatedStatusCode); expect(printInitialLog.calledOnce).to.be.false; }); it('should print spec details when spec related data is sent in polling response', () => { let specResult = JSON.stringify({"path": "path"}) - let data = JSON.stringify([specResult]) + let data = JSON.stringify({ "specData": [specResult], "buildData": {"duration": "NA", "parallels": "NA"}}) var printSpecData = sandbox.stub(); syncSpecsLogs.__set__('printSpecData', printSpecData); - showSpecsStatus(data); + showSpecsStatus(data, buildRunningStatusCode); expect(printSpecData.calledOnce).to.be.true; }); it('should print initial and spec details when spec related data is sent in polling response', () => { let specResult = JSON.stringify({"path": "path"}) syncSpecsLogs.__set__('buildStarted', false) - let data = JSON.stringify(["created", specResult]) + let data = JSON.stringify({ "specData": [specResult], "buildData": {"duration": "NA", "parallels": "NA"}}) var printSpecData = sandbox.stub(); syncSpecsLogs.__set__('printSpecData', printSpecData); var printInitialLog = sandbox.stub(); syncSpecsLogs.__set__('printInitialLog', printInitialLog); - showSpecsStatus(data); + showSpecsStatus(data, buildCreatedStatusCode); expect(printSpecData.calledOnce).to.be.true; expect(printInitialLog.calledOnce).to.be.true; }); @@ -253,18 +256,38 @@ describe("syncSpecsLogs", () => { let specResult = JSON.stringify({"path": "path"}) let customError = { id: "custom_error_1", type: "custom_errors_to_print", level: "warn", should_be_unique: true, message: "custom error message" } syncSpecsLogs.__set__('buildStarted', false) - let data = JSON.stringify(["created", specResult, customError]) + let data = JSON.stringify({ "specData": ["created", specResult, customError], "buildData": {"duration": "NA", "parallels": "NA"}}) var printSpecData = sandbox.stub(); syncSpecsLogs.__set__('printSpecData', printSpecData); var printInitialLog = sandbox.stub(); syncSpecsLogs.__set__('printInitialLog', printInitialLog); var addCustomErrorToPrint = sandbox.stub(); syncSpecsLogs.__set__('addCustomErrorToPrint', addCustomErrorToPrint); - showSpecsStatus(data); + showSpecsStatus(data, buildRunningStatusCode); expect(printSpecData.calledOnce).to.be.true; expect(printInitialLog.calledOnce).to.be.true; expect(addCustomErrorToPrint.calledOnce).to.be.true; }); + + it('should add custom error, print initial and spec details when spec related data and duration is sent in polling response', () => { + let specResult = JSON.stringify({"path": "path"}) + let customError = { id: "custom_error_1", type: "custom_errors_to_print", level: "warn", should_be_unique: true, message: "custom error message" } + var loggerInfoSpy = sinon.spy(logger, 'info'); + syncSpecsLogs.__set__('buildStarted', false) + let data = JSON.stringify({ "specData": ["created", specResult, customError], "buildData": {"duration": {"total_duration": "87"}, "parallels": "2"}}) + var printSpecData = sandbox.stub(); + syncSpecsLogs.__set__('printSpecData', printSpecData); + var printInitialLog = sandbox.stub(); + syncSpecsLogs.__set__('printInitialLog', printInitialLog); + var addCustomErrorToPrint = sandbox.stub(); + syncSpecsLogs.__set__('addCustomErrorToPrint', addCustomErrorToPrint); + showSpecsStatus(data, buildCompletedStatusCode); + expect(printSpecData.calledOnce).to.be.true; + expect(printInitialLog.calledOnce).to.be.true; + expect(addCustomErrorToPrint.calledOnce).to.be.true; + sinon.assert.calledWith(loggerInfoSpy, 'Done in 87 seconds with 2 parallels.\n'); + loggerInfoSpy.restore(); + }); }); context("printSpecsStatus", () => { From 68808806e45ebe95a7f6c724c9795eb778c6526c Mon Sep 17 00:00:00 2001 From: Aditya Samantaray Date: Mon, 30 Jan 2023 11:32:08 +0530 Subject: [PATCH 5/8] refactor: remove unused LOC --- bin/helpers/syncRunner.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bin/helpers/syncRunner.js b/bin/helpers/syncRunner.js index 9fd3ba8b..d18e3452 100644 --- a/bin/helpers/syncRunner.js +++ b/bin/helpers/syncRunner.js @@ -1,10 +1,6 @@ 'use strict'; -const logger = require("./logger").syncCliLogger; -const request = require('request'); const syncSpecsLogs = require('./sync/syncSpecsLogs'), specDetails = require('./sync/failedSpecsDetails'), - config = require("../helpers/config"), - utils = require('./utils'), specsSummary = require('./sync/specsSummary'); exports.pollBuildStatus = (bsConfig, buildDetails, rawArgs, buildReportData) => { From dbc2843fefc522c499e227681cc25aa0d0992cd1 Mon Sep 17 00:00:00 2001 From: Aditya Samantaray Date: Mon, 30 Jan 2023 18:13:45 +0530 Subject: [PATCH 6/8] chore: fix alignment issues of the duration output --- bin/helpers/sync/specsSummary.js | 3 ++- bin/helpers/sync/syncSpecsLogs.js | 7 ++++-- test/unit/bin/helpers/sync/specSummary.js | 15 ++++++------ test/unit/bin/helpers/sync/syncSpecsLogs.js | 26 ++++----------------- 4 files changed, 18 insertions(+), 33 deletions(-) diff --git a/bin/helpers/sync/specsSummary.js b/bin/helpers/sync/specsSummary.js index 404e655e..b556aa66 100644 --- a/bin/helpers/sync/specsSummary.js +++ b/bin/helpers/sync/specsSummary.js @@ -33,7 +33,8 @@ let printSpecsRunSummary = (data, machines, customErrorsToPrint) => { }); logger.info(`Total tests: ${summary.total}, passed: ${summary.passed}, failed: ${summary.failed}, skipped: ${summary.skipped}, passed_with_skipped: ${summary.passed_with_skipped}, pending: ${summary.pending}`); - logger.debug(`CLI calculated duration: ${data.duration/1000} seconds using ${machines} machines\n`); + logger.info(`Done in ${data.duration} seconds using ${data.parallels} machines\n`); + winstonlogger.debug(`CLI calculated duration is ${data.cliDuration}\n`); if (customErrorsToPrint && customErrorsToPrint.length > 0) { for (const error of customErrorsToPrint) { diff --git a/bin/helpers/sync/syncSpecsLogs.js b/bin/helpers/sync/syncSpecsLogs.js index 18c6d61b..03eafa97 100644 --- a/bin/helpers/sync/syncSpecsLogs.js +++ b/bin/helpers/sync/syncSpecsLogs.js @@ -14,6 +14,8 @@ let specSummary = { "buildError": null, "specs": [], "duration": null, + "parallels": null, + "cliDuration": null, "customErrorsToPrint": [] } @@ -120,7 +122,7 @@ let printSpecsStatus = (bsConfig, buildDetails, rawArgs, buildReportData) => { } } logger.info(lineSeparator); - specSummary.duration = endTime - startTime + specSummary.cliDuration = endTime - startTime resolve(specSummary); } ); @@ -196,7 +198,8 @@ let showSpecsStatus = (data, statusCode) => { const buildDetails = specData.buildData; const totalDuration = buildDetails.duration?.total_duration const parallels = buildDetails.parallels - logger.info(`Done in ${totalDuration} seconds with ${parallels} parallels.\n`); + specSummary.duration = totalDuration; + specSummary.parallels = parallels; } else { logger.debug(`Build details not sent`) } diff --git a/test/unit/bin/helpers/sync/specSummary.js b/test/unit/bin/helpers/sync/specSummary.js index 273dbadd..7e30bc67 100644 --- a/test/unit/bin/helpers/sync/specSummary.js +++ b/test/unit/bin/helpers/sync/specSummary.js @@ -29,7 +29,7 @@ describe("printSpecsRunSummary", () => { }); context("with data", () => { - let time = 6000, + let time = 6, machines = 2, specs = [ {specName: 'spec2.name.js', status: 'Failed', combination: 'Win 10 / Chrome 78', sessionId: '3d3rdf3r...'}, @@ -40,24 +40,23 @@ describe("printSpecsRunSummary", () => { data = { specs: specs, duration: time, + parallels: machines, exitCode: 0 }; it('returns passed specs data', () => { var loggerInfoSpy = sinon.spy(logger, 'info'); - var loggerDebugSpy = sinon.spy(logger, 'debug'); specSummary.printSpecsRunSummary(data, machines); sinon.assert.calledWith(loggerInfoSpy, 'Total tests: 4, passed: 1, failed: 2, skipped: 1, passed_with_skipped: 0, pending: 0'); - sinon.assert.calledWith(loggerDebugSpy, `CLI calculated duration: ${time / 1000} seconds using ${machines} machines\n`); + sinon.assert.calledWith(loggerInfoSpy, `Done in ${data.duration} seconds using ${data.parallels} machines\n`); loggerInfoSpy.restore(); - loggerDebugSpy.restore(); }); }); context("with custom error data", () => { - let time = 6000, + let time = 6, machines = 2, specs = [ {specName: 'spec2.name.js', status: 'Failed', combination: 'Win 10 / Chrome 78', sessionId: '3d3rdf3r...'}, @@ -68,6 +67,7 @@ describe("printSpecsRunSummary", () => { data = { specs: specs, duration: time, + parallels: machines, exitCode: 0 }, customErrorsToPrint = [ @@ -76,16 +76,15 @@ describe("printSpecsRunSummary", () => { it('prints the custom error message along with build details', () => { var loggerInfoSpy = sinon.spy(logger, 'info'); - var loggerDebugSpy = sinon.spy(logger, 'debug'); var loggerWarnSpy = sinon.spy(winstonLogger, 'warn'); specSummary.printSpecsRunSummary(data, machines, customErrorsToPrint); sinon.assert.calledWith(loggerInfoSpy, 'Total tests: 4, passed: 1, failed: 2, skipped: 1, passed_with_skipped: 0, pending: 0'); - sinon.assert.calledWith(loggerDebugSpy, `CLI calculated duration: ${time / 1000} seconds using ${machines} machines\n`); + sinon.assert.calledWith(loggerInfoSpy, `Done in ${data.duration} seconds using ${data.parallels} machines\n`); + sinon.assert.calledWith(loggerWarnSpy, `custom error message`); loggerInfoSpy.restore(); - loggerDebugSpy.restore(); loggerWarnSpy.restore(); }); }); diff --git a/test/unit/bin/helpers/sync/syncSpecsLogs.js b/test/unit/bin/helpers/sync/syncSpecsLogs.js index d0efa7f3..e19a10c2 100644 --- a/test/unit/bin/helpers/sync/syncSpecsLogs.js +++ b/test/unit/bin/helpers/sync/syncSpecsLogs.js @@ -93,6 +93,8 @@ describe("syncSpecsLogs", () => { "buildError": null, "specs": [], "duration": null, + "parallels": null, + "cliDuration": null, "customErrorsToPrint": [ { id: "custom_error_1", type: "custom_errors_to_print", level: "warn", should_be_unique: true, message: "custom error message" } ] @@ -268,26 +270,6 @@ describe("syncSpecsLogs", () => { expect(printInitialLog.calledOnce).to.be.true; expect(addCustomErrorToPrint.calledOnce).to.be.true; }); - - it('should add custom error, print initial and spec details when spec related data and duration is sent in polling response', () => { - let specResult = JSON.stringify({"path": "path"}) - let customError = { id: "custom_error_1", type: "custom_errors_to_print", level: "warn", should_be_unique: true, message: "custom error message" } - var loggerInfoSpy = sinon.spy(logger, 'info'); - syncSpecsLogs.__set__('buildStarted', false) - let data = JSON.stringify({ "specData": ["created", specResult, customError], "buildData": {"duration": {"total_duration": "87"}, "parallels": "2"}}) - var printSpecData = sandbox.stub(); - syncSpecsLogs.__set__('printSpecData', printSpecData); - var printInitialLog = sandbox.stub(); - syncSpecsLogs.__set__('printInitialLog', printInitialLog); - var addCustomErrorToPrint = sandbox.stub(); - syncSpecsLogs.__set__('addCustomErrorToPrint', addCustomErrorToPrint); - showSpecsStatus(data, buildCompletedStatusCode); - expect(printSpecData.calledOnce).to.be.true; - expect(printInitialLog.calledOnce).to.be.true; - expect(addCustomErrorToPrint.calledOnce).to.be.true; - sinon.assert.calledWith(loggerInfoSpy, 'Done in 87 seconds with 2 parallels.\n'); - loggerInfoSpy.restore(); - }); }); context("printSpecsStatus", () => { @@ -330,7 +312,7 @@ describe("syncSpecsLogs", () => { expect(tableStream.calledOnce).to.be.true; expect(whileProcess.calledOnce).to.be.false; expect(specSummary.specs).deep.to.equal([]) - expect(specSummary.duration).to.eql(endTime - startTime); + expect(specSummary.cliDuration).to.eql(endTime - startTime); }); }); @@ -345,7 +327,7 @@ describe("syncSpecsLogs", () => { expect(getTableConfig.calledOnce).to.be.true; expect(tableStream.calledOnce).to.be.true; expect(whileProcess.callCount).to.eql(3); - expect(specSummary.duration).to.eql(endTime - startTime); + expect(specSummary.cliDuration).to.eql(endTime - startTime); }); }); }); From 46f113ba84a8814babb7932739cb0b3b05b796eb Mon Sep 17 00:00:00 2001 From: Aditya Samantaray Date: Mon, 30 Jan 2023 18:25:44 +0530 Subject: [PATCH 7/8] chore: fix debug lines duration --- bin/helpers/sync/specsSummary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/helpers/sync/specsSummary.js b/bin/helpers/sync/specsSummary.js index b556aa66..6b6ad746 100644 --- a/bin/helpers/sync/specsSummary.js +++ b/bin/helpers/sync/specsSummary.js @@ -34,7 +34,7 @@ let printSpecsRunSummary = (data, machines, customErrorsToPrint) => { logger.info(`Total tests: ${summary.total}, passed: ${summary.passed}, failed: ${summary.failed}, skipped: ${summary.skipped}, passed_with_skipped: ${summary.passed_with_skipped}, pending: ${summary.pending}`); logger.info(`Done in ${data.duration} seconds using ${data.parallels} machines\n`); - winstonlogger.debug(`CLI calculated duration is ${data.cliDuration}\n`); + winstonlogger.debug(`CLI calculated duration is ${data.cliDuration/1000}`); if (customErrorsToPrint && customErrorsToPrint.length > 0) { for (const error of customErrorsToPrint) { From 1754609c6cad388bde9f685a095cf43601af743a Mon Sep 17 00:00:00 2001 From: Aditya Samantaray Date: Tue, 31 Jan 2023 12:29:12 +0530 Subject: [PATCH 8/8] chore: remove optional chaining --- bin/helpers/sync/syncSpecsLogs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/helpers/sync/syncSpecsLogs.js b/bin/helpers/sync/syncSpecsLogs.js index 03eafa97..5f93504f 100644 --- a/bin/helpers/sync/syncSpecsLogs.js +++ b/bin/helpers/sync/syncSpecsLogs.js @@ -196,8 +196,8 @@ let showSpecsStatus = (data, statusCode) => { // Below block is for printing build details, return if non 200 status code if ("buildData" in specData) { const buildDetails = specData.buildData; - const totalDuration = buildDetails.duration?.total_duration - const parallels = buildDetails.parallels + const totalDuration = (utils.isUndefined(buildDetails.duration)) ? "-" : buildDetails.duration.total_duration + const parallels = (utils.isUndefined(buildDetails.parallels)) ? "-" : buildDetails.parallels specSummary.duration = totalDuration; specSummary.parallels = parallels; } else {