Skip to content

Commit 08f5190

Browse files
Merge branch 'master' of github.com:browserstack/browserstack-cypress-cli into ACE_2714_env
2 parents 722da7c + 77c8742 commit 08f5190

File tree

9 files changed

+270
-17
lines changed

9 files changed

+270
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ package-lock.json
88
.env.*
99
log/*.log
1010
results/*
11+
coverage

bin/commands/runs.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ const archiver = require("../helpers/archiver"),
99
utils = require("../helpers/utils"),
1010
fileHelpers = require("../helpers/fileHelpers"),
1111
syncRunner = require("../helpers/syncRunner"),
12-
reportGenerator = require('../helpers/reporterHTML').reportGenerator;
12+
reportGenerator = require('../helpers/reporterHTML').reportGenerator,
13+
{initTimeComponents, markBlockStart, markBlockEnd, getTimeComponents} = require('../helpers/timeComponents');
1314

1415
module.exports = function run(args) {
1516
let bsConfigPath = utils.getConfigPath(args.cf);
1617
//Delete build_results.txt from log folder if already present.
18+
initTimeComponents();
19+
markBlockStart('deleteOldResults');
1720
utils.deleteResults();
21+
markBlockEnd('deleteOldResults');
1822

23+
markBlockStart('validateBstackJson');
1924
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
25+
markBlockEnd('validateBstackJson');
26+
markBlockStart('setConfig');
2027
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
2128

2229
utils.setDefaults(bsConfig, args);
@@ -59,10 +66,13 @@ module.exports = function run(args) {
5966

6067
// set the no-wrap
6168
utils.setNoWrap(bsConfig, args);
69+
markBlockEnd('setConfig');
6270

6371
// Validate browserstack.json values and parallels specified via arguments
72+
markBlockStart('validateConfig');
6473
return capabilityHelper.validate(bsConfig, args).then(function (cypressJson) {
65-
74+
markBlockEnd('validateConfig');
75+
markBlockStart('preArchiveSteps');
6676
//get the number of spec files
6777
let specFiles = utils.getNumberOfSpecFiles(bsConfig, args, cypressJson);
6878

@@ -72,17 +82,29 @@ module.exports = function run(args) {
7282
// warn if specFiles cross our limit
7383
utils.warnSpecLimit(bsConfig, args, specFiles);
7484

85+
markBlockEnd('preArchiveSteps');
7586
// Archive the spec files
87+
markBlockStart('zip');
88+
markBlockStart('zip.archive');
7689
return archiver.archive(bsConfig.run_settings, config.fileName, args.exclude).then(function (data) {
7790

91+
markBlockEnd('zip.archive');
7892
// Uploaded zip file
93+
markBlockStart('zip.zipUpload');
7994
return zipUploader.zipUpload(bsConfig, config.fileName).then(async function (zip) {
95+
96+
markBlockEnd('zip.zipUpload');
97+
markBlockEnd('zip');
8098
// Create build
8199

82100
//setup Local Testing
101+
markBlockStart('localSetup');
83102
let bs_local = await utils.setupLocalTesting(bsConfig, args);
84-
103+
markBlockEnd('localSetup');
104+
markBlockStart('createBuild');
85105
return build.createBuild(bsConfig, zip).then(function (data) {
106+
markBlockEnd('createBuild');
107+
markBlockEnd('total');
86108
let message = `${data.message}! ${Constants.userMessages.BUILD_CREATED} with build id: ${data.build_id}`;
87109
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${data.dashboard_url}`;
88110
utils.exportResults(data.build_id, `${config.dashboardUrl}${data.build_id}`);
@@ -117,7 +139,19 @@ module.exports = function run(args) {
117139
logger.info(message);
118140
logger.info(dashboardLink);
119141
if(!args.sync) logger.info(Constants.userMessages.EXIT_SYNC_CLI_MESSAGE.replace("<build-id>",data.build_id));
120-
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null);
142+
let dataToSend = {
143+
time_components: getTimeComponents(),
144+
build_id: data.build_id,
145+
};
146+
if (bsConfig && bsConfig.connection_settings) {
147+
if (bsConfig.connection_settings.local_mode) {
148+
dataToSend.local_mode = bsConfig.connection_settings.local_mode;
149+
}
150+
if (bsConfig.connection_settings.usedAutoLocal) {
151+
dataToSend.used_auto_local = bsConfig.connection_settings.usedAutoLocal;
152+
}
153+
}
154+
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null, dataToSend);
121155
return;
122156
}).catch(async function (err) {
123157
// Build creation failed

bin/helpers/sync/syncSpecsLogs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ let printSpecsStatus = (bsConfig, buildDetails) => {
103103
whileProcess(callback)
104104
},
105105
function(err, result) { // when loop ends
106+
if (err) {
107+
utils.sendUsageReport(bsConfig, {}, `buildId: ${buildDetails.build_id}`, 'error', 'sync_cli_error', err);
108+
}
106109
logger.info(lineSeparator);
107110
specSummary.duration = endTime - startTime
108111
resolve(specSummary)

bin/helpers/timeComponents.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict'
2+
3+
const { isUndefined } = require('./utils');
4+
5+
let sessionTimes = {
6+
referenceTimes: {
7+
absoluteStartTime: Date.now()
8+
}, // Absolute times which needs to be used later to calculate logTimes
9+
logTimes: {}, // Time Difference in ms which we need to push to EDS
10+
};
11+
12+
const initTimeComponents = () => {
13+
sessionTimes = {referenceTimes: {absoluteStartTime: Date.now()}, logTimes: {}};
14+
};
15+
16+
const markBlockStart = (blockName) => {
17+
sessionTimes.referenceTimes[blockName] = Date.now();
18+
};
19+
20+
const markBlockEnd = (blockName) => {
21+
const startTime = sessionTimes.referenceTimes[blockName] || sessionTimes.referenceTimes.absoluteStartTime;
22+
markBlockDiff(blockName, startTime, Date.now());
23+
};
24+
25+
const markBlockDiff = (blockName, startTime, stopTime) => {
26+
sessionTimes.logTimes[blockName] = stopTime - startTime;
27+
}
28+
29+
const getTimeComponents = () => {
30+
const data = convertDotToNestedObject(sessionTimes.logTimes);
31+
32+
return data;
33+
};
34+
35+
const convertDotToNestedObject = (dotNotationObject) => {
36+
let nestedObject = {};
37+
38+
Object.keys(dotNotationObject).forEach((key) => {
39+
let dotKeys = key.split('.');
40+
let currentKey = nestedObject;
41+
for(let i = 0; i < dotKeys.length - 1; i++) {
42+
if (isUndefined(currentKey[dotKeys[i]])) {
43+
currentKey[dotKeys[i]] = {};
44+
} else if (Number.isInteger(currentKey[dotKeys[i]])) {
45+
currentKey[dotKeys[i]] = {total: currentKey[dotKeys[i]]};
46+
}
47+
currentKey = currentKey[dotKeys[i]];
48+
}
49+
if (isUndefined(currentKey[dotKeys[dotKeys.length - 1]]) || Number.isInteger(currentKey[dotKeys[dotKeys.length - 1]])) {
50+
currentKey[dotKeys[dotKeys.length - 1]] = dotNotationObject[key];
51+
} else {
52+
currentKey[dotKeys[dotKeys.length - 1]].total = dotNotationObject[key];
53+
}
54+
});
55+
56+
return nestedObject;
57+
};
58+
59+
module.exports = {
60+
initTimeComponents,
61+
markBlockStart,
62+
markBlockEnd,
63+
markBlockDiff,
64+
getTimeComponents,
65+
};

bin/helpers/usageReporting.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const cp = require("child_process"),
66
path = require('path');
77

88
const config = require('./config'),
9-
fileLogger = require('./logger').fileLogger;
9+
fileLogger = require('./logger').fileLogger,
10+
utils = require('./utils');
1011

1112
function get_version(package_name) {
1213
try {
@@ -33,7 +34,7 @@ function local_cypress_version(bsConfig) {
3334
// 1. check version of Cypress installed in local project
3435
// 2. check version of Cypress installed globally if not present in project
3536

36-
if (bsConfig && bsConfig.run_settings.cypressProjectDir) {
37+
if (bsConfig && bsConfig.run_settings && bsConfig.run_settings.cypressProjectDir) {
3738
let version = get_version(path.join(bsConfig.run_settings.cypressProjectDir, 'node_modules', '.bin', 'cypress'));
3839
if (!version) {
3940
version = get_version('cypress');
@@ -80,7 +81,7 @@ function cli_version_and_path(bsConfig) {
8081
// 1. check version of Cypress installed in local project
8182
// 2. check version of Cypress installed globally if not present in project
8283

83-
if (bsConfig && bsConfig.run_settings.cypressProjectDir) {
84+
if (bsConfig && bsConfig.run_settings && bsConfig.run_settings.cypressProjectDir) {
8485
let _path = path.join(bsConfig.run_settings.cypressProjectDir, 'node_modules', 'browserstack-cypress');
8586
let version = get_version(_path);
8687
if (!version) {
@@ -174,7 +175,7 @@ function send(args) {
174175

175176
let bsConfig = args.bstack_config;
176177
let cli_details = cli_version_and_path(bsConfig);
177-
let data = {}
178+
let data = utils.isUndefined(args.data) ? {} : args.data;
178179

179180
if (bsConfig && bsConfig.run_settings) {
180181
data.cypress_version = bsConfig.run_settings.cypress_version

bin/helpers/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,16 @@ exports.sendUsageReport = (
107107
args,
108108
message,
109109
message_type,
110-
error_code
110+
error_code,
111+
data
111112
) => {
112113
usageReporting.send({
113114
cli_args: args,
114115
message: message,
115116
message_type: message_type,
116117
error_code: error_code,
117118
bstack_config: bsConfig,
119+
data,
118120
});
119121
};
120122

@@ -496,6 +498,7 @@ exports.setupLocalTesting = (bsConfig, args) => {
496498
bsConfig, bsConfig['connection_settings']['local_identifier']
497499
);
498500
if (!localIdentifierRunning){
501+
bsConfig.connection_settings.usedAutoLocal = true;
499502
var bs_local = this.getLocalBinary();
500503
var bs_local_args = this.setLocalArgs(bsConfig, args);
501504
let that = this;

test/unit/bin/commands/runs.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const chai = require("chai"),
55
const Constants = require("../../../../bin/helpers/constants"),
66
logger = require("../../../../bin/helpers/logger").winstonLogger,
77
testObjects = require("../../support/fixtures/testObjects");
8+
const { initTimeComponents, markBlockStart, markBlockEnd } = require("../../../../bin/helpers/timeComponents");
89
const { setHeaded, setupLocalTesting, stopLocalBinary, setUserSpecs, setLocalConfigFile } = require("../../../../bin/helpers/utils");
910

1011
const proxyquire = require("proxyquire").noCallThru();
@@ -618,6 +619,10 @@ describe("runs", () => {
618619
setNoWrapStub = sandbox.stub();
619620
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
620621
setLocalConfigFileStub = sandbox.stub();
622+
getTimeComponentsStub = sandbox.stub().returns({});
623+
initTimeComponentsStub = sandbox.stub();
624+
markBlockStartStub = sandbox.stub();
625+
markBlockEndStub = sandbox.stub();
621626
});
622627

623628
afterEach(() => {
@@ -630,6 +635,7 @@ describe("runs", () => {
630635
let errorCode = null;
631636
let message = `Success! ${Constants.userMessages.BUILD_CREATED} with build id: random_build_id`;
632637
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${dashboardUrl}`;
638+
let data = {time_components: {}, build_id: 'random_build_id'}
633639

634640
const runs = proxyquire('../../../../bin/commands/runs', {
635641
'../helpers/utils': {
@@ -656,7 +662,7 @@ describe("runs", () => {
656662
setDefaults: setDefaultsStub,
657663
isUndefined: isUndefinedStub,
658664
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
659-
setLocalConfigFile: setLocalConfigFileStub
665+
setLocalConfigFile: setLocalConfigFileStub,
660666
},
661667
'../helpers/capabilityHelper': {
662668
validate: capabilityValidatorStub,
@@ -676,6 +682,12 @@ describe("runs", () => {
676682
'../helpers/config': {
677683
dashboardUrl: dashboardUrl,
678684
},
685+
'../helpers/timeComponents': {
686+
initTimeComponents: initTimeComponentsStub,
687+
getTimeComponents: getTimeComponentsStub,
688+
markBlockStart: markBlockStartStub,
689+
markBlockEnd: markBlockEndStub,
690+
}
679691
});
680692

681693
validateBstackJsonStub.returns(Promise.resolve(bsConfig));
@@ -713,13 +725,16 @@ describe("runs", () => {
713725
sinon.assert.calledOnce(exportResultsStub);
714726
sinon.assert.calledOnce(deleteResultsStub);
715727
sinon.assert.calledOnce(setDefaultsStub);
716-
sinon.assert.calledOnceWithExactly(
717-
sendUsageReportStub,
718-
bsConfig,
719-
args,
720-
`${message}\n${dashboardLink}`,
721-
messageType,
722-
errorCode
728+
sinon.assert.match(
729+
sendUsageReportStub.getCall(0).args,
730+
[
731+
bsConfig,
732+
args,
733+
`${message}\n${dashboardLink}`,
734+
messageType,
735+
errorCode,
736+
data
737+
]
723738
);
724739
});
725740
});

test/unit/bin/helpers/sync/syncSpecsLogs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ var syncSpecsLogs = rewire("../../../../../bin/helpers/sync/syncSpecsLogs.js");
1414
var logger = require("../../../../../bin/helpers/logger").syncCliLogger;
1515
var Constants = require("../../../../../bin/helpers/constants.js");
1616
var config = require("../../../../../bin/helpers/config.js");
17+
var utils = require("../../../../../bin/helpers/utils");
1718

1819
describe("syncSpecsLogs", () => {
1920
var sandbox;
2021

2122
beforeEach(() => {
2223
sandbox = sinon.createSandbox();
24+
sinon.stub(utils, 'sendUsageReport');
2325
});
2426

2527
afterEach(() => {
2628
sandbox.restore();
29+
utils.sendUsageReport.restore();
2730
});
2831

2932
context("getCombinationName", () => {

0 commit comments

Comments
 (0)