Skip to content

Commit bc98628

Browse files
committed
Merge branch 'master' of github.com:roshan04/browserstack-cypress-cli
2 parents cb11048 + 2430b01 commit bc98628

File tree

9 files changed

+132
-158
lines changed

9 files changed

+132
-158
lines changed

bin/commands/runs.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ module.exports = function run(args, rawArgs) {
204204
}).finally(() =>{
205205
let terminalWidth = (process.stdout.columns) * 0.9;
206206
let lineSeparator = "\n" + "-".repeat(terminalWidth);
207-
console.log(lineSeparator)
208207
logger.info(Constants.userMessages.BUILD_FAILED_ERROR)
209208
process.exitCode = Constants.BUILD_FAILED_EXIT_CODE;
210209
});
@@ -258,7 +257,7 @@ module.exports = function run(args, rawArgs) {
258257
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null, buildReportData, rawArgs);
259258
return;
260259
}).catch(async function (err) {
261-
if (err && err.includes('browserstack.geoLocation')) {
260+
if (err && err.toString().includes('browserstack.geoLocation')) {
262261
err = err.replace(/browserstack.geoLocation/g, 'geolocation');
263262
}
264263
// Build creation failed

bin/helpers/capabilityHelper.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,31 @@ const caps = (bsConfig, zip) => {
7171
obj.local = true;
7272
}
7373

74-
obj.localMode = null;
75-
// Local Mode
76-
if (obj.local === true && bsConfig.connection_settings.local_mode) {
77-
obj.localMode = bsConfig.connection_settings.local_mode;
78-
if (bsConfig.connection_settings.user_defined_local_mode_warning) {
79-
logger.warn(Constants.userMessages.INVALID_LOCAL_MODE_WARNING);
74+
// binary was spawned locally
75+
if(obj.local === true) {
76+
if (!Utils.isUndefined(process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING) && process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING == "true") {
77+
obj.localMode = null;
78+
79+
// Local Mode
80+
if (bsConfig.connection_settings.local_mode) {
81+
obj.localMode = bsConfig.connection_settings.local_mode;
82+
if (bsConfig.connection_settings.user_defined_local_mode_warning) {
83+
logger.warn(Constants.userMessages.INVALID_LOCAL_MODE_WARNING);
84+
}
85+
logger.info(`Local testing set up in ${obj.localMode} mode.`);
86+
}
87+
88+
// Local Identifier
89+
obj.localIdentifier = null;
90+
if (bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier) {
91+
obj.localIdentifier = bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier;
92+
logger.info(`Local testing identifier: ${obj.localIdentifier}`);
93+
}
8094
}
81-
logger.info(`Local testing set up in ${obj.localMode} mode.`);
8295
}
8396

84-
// Local Identifier
85-
obj.localIdentifier = null;
86-
if (obj.local === true && (bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier)) {
87-
obj.localIdentifier = bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier;
88-
logger.info(`Local testing identifier: ${obj.localIdentifier}`);
97+
if (!Utils.isUndefined(process.env.BSTACK_CYPRESS_LOCAL_BINARY_ALREADY_RUNNING) && process.env.BSTACK_CYPRESS_LOCAL_BINARY_ALREADY_RUNNING == "true") {
98+
logger.info(Constants.userMessages.LOCAL_BINARY_ALREADY_RUNNING);
8999
}
90100

91101
logger.info(`Local is set to: ${obj.local} (${obj.local ? Constants.userMessages.LOCAL_TRUE : Constants.userMessages.LOCAL_FALSE})`);
@@ -164,8 +174,6 @@ const validate = (bsConfig, args) => {
164174
// validate local args i.e --local-mode and --local-identifier
165175

166176
if( Utils.searchForOption('--local-identifier') && (Utils.isUndefined(args.localIdentifier) || (!Utils.isUndefined(args.localIdentifier) && !args.localIdentifier.trim()))) reject(Constants.validationMessages.INVALID_CLI_LOCAL_IDENTIFIER);
167-
168-
if( Utils.getLocalFlag(bsConfig.connection_settings) && (Utils.isUndefined(bsConfig["connection_settings"]["local_identifier"]) || ( !Utils.isUndefined(bsConfig["connection_settings"]["local_identifier"]) && !bsConfig["connection_settings"]["local_identifier"].trim()))) reject(Constants.validationMessages.INVALID_LOCAL_IDENTIFIER);
169177

170178
if( Utils.searchForOption('--local-mode') && ( Utils.isUndefined(args.localMode) || (!Utils.isUndefined(args.localMode) && !["always-on","on-demand"].includes(args.localMode)))) reject(Constants.validationMessages.INVALID_LOCAL_MODE);
171179

bin/helpers/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const userMessages = {
5656
LOCAL_START_FAILED: "Local Testing setup failed.",
5757
LOCAL_STOP_FAILED: "Local Binary stop failed.",
5858
INVALID_LOCAL_MODE_WARNING: "Invalid value specified for local_mode. local_mode: (\"always-on\" | \"on-demand\"). For more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference",
59+
LOCAL_BINARY_ALREADY_RUNNING: "We found an existing BrowserStack Local connection running from your account. Using the existing connection for this build. If you wish to use a new Local connection for your build, please specify a value for 'local_identifier' within 'connection_settings' in your browserstack.json config.",
5960
SPEC_LIMIT_WARNING: "You might not see all your results on the dashboard because of high spec count, please consider reducing the number of spec files in this folder.",
6061
DOWNLOAD_BUILD_ARTIFACTS_FAILED: "Downloading build artifacts for the build <build-id> failed for <machine-count> machines.",
6162
ASYNC_DOWNLOADS: "Test artifacts as specified under 'downloads' can be downloaded after the build has completed its run, using 'browserstack-cypress generate-downloads <build-id>'",

bin/helpers/utils.js

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -593,11 +593,6 @@ exports.setLocalIdentifier = (bsConfig, args) => {
593593
!this.isUndefined(bsConfig["connection_settings"]["local_identifier"])
594594
){
595595
bsConfig['connection_settings']['local_mode'] = 'always-on';
596-
} else if (
597-
bsConfig['connection_settings']['local'] &&
598-
this.isUndefined(bsConfig["connection_settings"]["local_identifier"])
599-
){
600-
bsConfig["connection_settings"]["local_identifier"] = this.generateLocalIdentifier(bsConfig['connection_settings']['local_mode']);
601596
}
602597
};
603598

@@ -637,9 +632,18 @@ exports.setLocalMode = (bsConfig, args) => {
637632
exports.setupLocalTesting = (bsConfig, args, rawArgs) => {
638633
return new Promise(async (resolve, reject) => {
639634
if( bsConfig['connection_settings'] && bsConfig['connection_settings']['local'] && String(bsConfig['connection_settings']['local']) === "true" ){
640-
let localIdentifierRunning = await this.checkLocalIdentifierRunning(
641-
bsConfig, bsConfig['connection_settings']['local_identifier']
642-
);
635+
let localBinaryRunning = await this.checkLocalBinaryRunning(bsConfig, bsConfig['connection_settings']['local_identifier']);
636+
let localIdentifierRunning;
637+
if (localBinaryRunning['should_spawn_binary'] == true) {
638+
localIdentifierRunning = false;
639+
if(this.isUndefined(bsConfig["connection_settings"]["local_identifier"])) {
640+
bsConfig["connection_settings"]["local_identifier"] = this.generateLocalIdentifier(bsConfig['connection_settings']['local_mode']);
641+
}
642+
} else {
643+
localIdentifierRunning = true;
644+
process.env.BSTACK_CYPRESS_LOCAL_BINARY_ALREADY_RUNNING = "true"; // shows message when binary is not spawned by the CLI process
645+
process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING = "true";
646+
}
643647
if (!localIdentifierRunning){
644648
bsConfig.connection_settings.usedAutoLocal = true;
645649
var bs_local = this.getLocalBinary();
@@ -648,6 +652,7 @@ exports.setupLocalTesting = (bsConfig, args, rawArgs) => {
648652
logger.info('Setting up Local testing...');
649653
bs_local.start(bs_local_args, function (localStartError) {
650654
if (that.isUndefined(localStartError)) {
655+
process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING = "true";
651656
resolve(bs_local);
652657
} else {
653658
let message = `name: ${localStartError.name}, message: ${localStartError.message}, extra: ${localStartError.extra}`,
@@ -675,22 +680,6 @@ exports.setupLocalTesting = (bsConfig, args, rawArgs) => {
675680

676681
exports.stopLocalBinary = (bsConfig, bs_local, args, rawArgs) => {
677682
return new Promise(async (resolve, reject) => {
678-
if(bsConfig['connection_settings'] && bsConfig['connection_settings']['local']){
679-
let localIdentifierRunning = await this.checkLocalIdentifierRunning(bsConfig,bsConfig["connection_settings"]["local_identifier"]);
680-
if(!localIdentifierRunning){
681-
let message = `Local Binary not running.`,
682-
errorCode = 'local_identifier_error';
683-
this.sendUsageReport(
684-
bsConfig,
685-
args,
686-
message,
687-
Constants.messageTypes.ERROR,
688-
errorCode,
689-
null,
690-
rawArgs
691-
);
692-
}
693-
}
694683
if (!this.isUndefined(bs_local) && bs_local.isRunning() && bsConfig['connection_settings'] && bsConfig['connection_settings']['local_mode'].toLowerCase() != "always-on") {
695684
let that = this;
696685
bs_local.stop(function (localStopError) {
@@ -744,35 +733,26 @@ exports.generateLocalIdentifier = (mode) => {
744733
return Buffer.from(local_identifier).toString("base64");
745734
};
746735

747-
exports.checkLocalIdentifierRunning = (bsConfig, localIdentifier) => {
736+
exports.checkLocalBinaryRunning = (bsConfig, localIdentifier) => {
748737
let options = {
749-
url: `${config.localTestingListUrl}?auth_token=${bsConfig.auth.access_key}&state=running`,
738+
url: `${config.cypress_v1}/local_binary_running_check`,
750739
auth: {
751740
user: bsConfig.auth.username,
752741
password: bsConfig.auth.access_key,
753742
},
754743
headers: {
744+
'Content-Type': 'application/json',
755745
'User-Agent': this.getUserAgent(),
756746
},
747+
body: JSON.stringify({ localIdentifier: localIdentifier}),
757748
};
758-
let that = this;
759749
return new Promise ( function(resolve, reject) {
760-
request.get(options, function (err, resp, body) {
750+
request.post(options, function (err, resp, body) {
761751
if(err){
762752
reject(err);
763753
}
764754
let response = JSON.parse(body);
765-
let localInstances = [];
766-
if(!that.isUndefined(response['instances'])){
767-
localInstances = response['instances'];
768-
}
769-
let localIdentifiers = [];
770-
771-
localInstances.forEach(function(instance){
772-
localIdentifiers.push(instance['localIdentifier']);
773-
});
774-
775-
resolve(localIdentifiers.includes(localIdentifier));
755+
resolve(response);
776756
});
777757
});
778758
};
@@ -1047,7 +1027,6 @@ exports.stopBrowserStackBuild = async (bsConfig, args, buildId, rawArgs) => {
10471027
logger.info(message);
10481028
}
10491029
} catch(err) {
1050-
console.log(err);
10511030
message = Constants.userMessages.BUILD_STOP_FAILED;
10521031
messageType = Constants.messageTypes.ERROR;
10531032
errorCode = 'api_failed_build_stop';

bin/runner.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var argv = yargs
6767
.showHelpOnFail(false, Constants.cliMessages.VERSION.HELP)
6868
.demand(1, Constants.cliMessages.VERSION.DEMAND)
6969
.command('init', Constants.cliMessages.INIT.INFO, function(yargs) {
70-
let rawArgv = yargs.argv;
70+
let rawArgv = process.argv.slice(2);
7171
argv = yargs
7272
.usage("usage: $0 init [filename] [options]")
7373
.options({
@@ -87,7 +87,7 @@ var argv = yargs
8787
}
8888
})
8989
.command('build-info', Constants.cliMessages.BUILD.INFO, function(yargs) {
90-
let rawArgv = yargs.argv;
90+
let rawArgv = process.argv.slice(2);
9191
argv = yargs
9292
.usage('usage: $0 <buildId>')
9393
.demand(1, Constants.cliMessages.BUILD.DEMAND)
@@ -103,7 +103,7 @@ var argv = yargs
103103
}
104104
})
105105
.command('build-stop', Constants.cliMessages.BUILD.STOP, function (yargs) {
106-
let rawArgv = yargs.argv;
106+
let rawArgv = process.argv.slice(2);
107107
argv = yargs
108108
.usage('usage: $0 <buildId>')
109109
.demand(1, Constants.cliMessages.BUILD.DEMAND)
@@ -119,7 +119,7 @@ var argv = yargs
119119
}
120120
})
121121
.command('run', Constants.cliMessages.RUN.INFO, function(yargs) {
122-
let rawArgv = yargs.argv;
122+
let rawArgv = process.argv.slice(2);
123123
argv = yargs
124124
.usage('usage: $0 run <options>')
125125
.options({
@@ -246,7 +246,7 @@ var argv = yargs
246246
}
247247
})
248248
.command('generate-report', Constants.cliMessages.GENERATE_REPORT.INFO, function(yargs) {
249-
let rawArgv = yargs.argv;
249+
let rawArgv = process.argv.slice(2);
250250
argv = yargs
251251
.usage('usage: $0 generate-report <buildId>')
252252
.demand(1, Constants.cliMessages.BUILD.DEMAND)
@@ -262,7 +262,7 @@ var argv = yargs
262262
}
263263
})
264264
.command('generate-downloads', Constants.cliMessages.GENERATE_DOWNLOADS.INFO, function(yargs) {
265-
let rawArgv = yargs.argv;
265+
let rawArgv = process.argv.slice(2);
266266
argv = yargs
267267
.usage('usage: $0 generate-downloads <buildId>')
268268
.demand(1, Constants.cliMessages.BUILD.DEMAND)

bin/templates/configTemplate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = function () {
5656
"project_name": "project-name",
5757
"build_name": "build-name",
5858
"exclude": [],
59-
"parallels": "Here goes the number of parallels you want to run",
59+
"parallels": "Number of parallels you want to run",
6060
"npm_dependencies": {
6161
},
6262
"package_config_options": {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browserstack-cypress-cli",
3-
"version": "1.12.0",
3+
"version": "1.12.1",
44
"description": "BrowserStack Cypress CLI for Cypress integration with BrowserStack's remote devices.",
55
"main": "index.js",
66
"scripts": {

test/unit/bin/helpers/capabilityHelper.js

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -162,37 +162,47 @@ describe("capabilityHelper.js", () => {
162162
});
163163
});
164164

165-
it("handle local_identifier set", () => {
166-
let zip_url = "bs://<random>";
167-
let bsConfig = {
168-
auth: {
169-
username: "random",
170-
access_key: "random",
171-
},
172-
browsers: [
173-
{
174-
browser: "chrome",
175-
os: "Windows 10",
176-
versions: ["78", "77"],
165+
context("handle local_identifier set when local binary spawned", () => {
166+
beforeEach(() => {
167+
process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING = "true";
168+
});
169+
170+
afterEach(() => {
171+
delete process.env.BSTACK_CYPRESS_LOCAL_BINARY_RUNNING;
172+
});
173+
174+
it("handle local_identifier set", () => {
175+
let zip_url = "bs://<random>";
176+
let bsConfig = {
177+
auth: {
178+
username: "random",
179+
access_key: "random",
177180
},
178-
],
179-
connection_settings: {
180-
local: true,
181-
local_identifier: "abc"
182-
},
183-
run_settings: {
184-
}
185-
};
186-
return capabilityHelper
187-
.caps(bsConfig, { zip_url: zip_url })
188-
.then(function (data) {
189-
let parsed_data = JSON.parse(data);
190-
chai.assert.equal(parsed_data.local, true);
191-
chai.assert.equal(parsed_data.localIdentifier, "abc");
192-
})
193-
.catch((error) => {
194-
chai.assert.fail("Promise error");
195-
});
181+
browsers: [
182+
{
183+
browser: "chrome",
184+
os: "Windows 10",
185+
versions: ["78", "77"],
186+
},
187+
],
188+
connection_settings: {
189+
local: true,
190+
local_identifier: "abc"
191+
},
192+
run_settings: {
193+
}
194+
};
195+
return capabilityHelper
196+
.caps(bsConfig, { zip_url: zip_url })
197+
.then(function (data) {
198+
let parsed_data = JSON.parse(data);
199+
chai.assert.equal(parsed_data.local, true);
200+
chai.assert.equal(parsed_data.localIdentifier, "abc");
201+
})
202+
.catch((error) => {
203+
chai.assert.fail("Promise error");
204+
});
205+
});
196206
});
197207

198208
it("handle local_identifier not set", () => {

0 commit comments

Comments
 (0)