Skip to content

Commit 5fb2f48

Browse files
Merge pull request #48 from browserstack/CYP_372_ENV_VAR
Cyp 372 env var
2 parents 22c892e + e2a7ddd commit 5fb2f48

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

bin/commands/runs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const archiver = require("../helpers/archiver"),
1111

1212
module.exports = function run(args) {
1313
let bsConfigPath = utils.getConfigPath(args.cf);
14+
//Delete build_results.txt from log folder if already present.
15+
utils.deleteResults();
1416

1517
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
1618
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
@@ -41,6 +43,7 @@ module.exports = function run(args) {
4143
return build.createBuild(bsConfig, zip).then(function (data) {
4244
let message = `${data.message}! ${Constants.userMessages.BUILD_CREATED} with build id: ${data.build_id}`;
4345
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${config.dashboardUrl}${data.build_id}`;
46+
utils.exportResults(data.build_id, `${config.dashboardUrl}${data.build_id}`);
4447
if ((utils.isUndefined(bsConfig.run_settings.parallels) && utils.isUndefined(args.parallels)) || (!utils.isUndefined(bsConfig.run_settings.parallels) && bsConfig.run_settings.parallels == Constants.constants.DEFAULT_PARALLEL_MESSAGE)) {
4548
logger.warn(Constants.userMessages.NO_PARALLELS);
4649
}

bin/helpers/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const os = require("os");
33
const path = require("path");
4+
const fs = require("fs");
45

56
const usageReporting = require('./usageReporting'),
67
logger = require('./logger').winstonLogger,
@@ -136,6 +137,21 @@ exports.configCreated = (args) => {
136137
this.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
137138
}
138139

140+
exports.exportResults = (buildId, buildUrl) => {
141+
let data = "BUILD_ID=" + buildId + "\nBUILD_URL="+buildUrl;
142+
fs.writeFileSync("log/build_results.txt", data , function(err){
143+
if(err) {
144+
logger.warn(`Couldn't write BUILD_ID with value: ${buildId} to browserstack/build_results.txt`);
145+
logger.warn(`Couldn't write BUILD_URL with value: ${buildUrl} to browserstack/build_results.txt`);
146+
}
147+
});
148+
}
149+
150+
exports.deleteResults = () => {
151+
fs.unlink("log/build_results.txt", function (err){
152+
});
153+
}
154+
139155
exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => {
140156
// Getting absolute path
141157
cypressDir = path.resolve(cypressDir);

test/unit/bin/commands/runs.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe("runs", () => {
2727
return "end";
2828
});
2929
getErrorCodeFromErrStub = sandbox.stub().returns("random-error-code");
30+
deleteResultsStub = sandbox.stub();
3031
});
3132

3233
afterEach(() => {
@@ -45,7 +46,8 @@ describe("runs", () => {
4546
getErrorCodeFromErr: getErrorCodeFromErrStub,
4647
sendUsageReport: sendUsageReportStub,
4748
setUsageReportingFlag: setUsageReportingFlagStub,
48-
getConfigPath: getConfigPathStub
49+
getConfigPath: getConfigPathStub,
50+
deleteResults: deleteResultsStub
4951
},
5052
});
5153

@@ -61,6 +63,7 @@ describe("runs", () => {
6163
sinon.assert.calledOnce(validateBstackJsonStub);
6264
sinon.assert.calledOnce(setUsageReportingFlagStub);
6365
sinon.assert.calledOnce(getErrorCodeFromErrStub);
66+
sinon.assert.calledOnce(deleteResultsStub);
6467
sinon.assert.calledOnceWithExactly(
6568
sendUsageReportStub,
6669
null,
@@ -89,6 +92,7 @@ describe("runs", () => {
8992
});
9093
getErrorCodeFromMsgStub = sandbox.stub().returns("random-error-code");
9194
capabilityValidatorStub = sandbox.stub();
95+
deleteResultsStub = sandbox.stub();
9296
});
9397

9498
afterEach(() => {
@@ -110,7 +114,8 @@ describe("runs", () => {
110114
setUsername: setUsernameStub,
111115
setAccessKey: setAccessKeyStub,
112116
setBuildName: setBuildNameStub,
113-
getConfigPath: getConfigPathStub
117+
getConfigPath: getConfigPathStub,
118+
deleteResults: deleteResultsStub
114119
},
115120
"../helpers/capabilityHelper": {
116121
validate: capabilityValidatorStub,
@@ -131,6 +136,7 @@ describe("runs", () => {
131136
sinon.assert.calledOnce(capabilityValidatorStub);
132137
sinon.assert.calledOnce(setUsageReportingFlagStub);
133138
sinon.assert.calledOnce(getErrorCodeFromMsgStub);
139+
sinon.assert.calledOnce(deleteResultsStub);
134140
sinon.assert.calledOnceWithExactly(
135141
sendUsageReportStub,
136142
bsConfig,
@@ -161,6 +167,7 @@ describe("runs", () => {
161167
capabilityValidatorStub = sandbox.stub();
162168
archiverStub = sandbox.stub();
163169
deleteZipStub = sandbox.stub();
170+
deleteResultsStub = sandbox.stub();
164171
});
165172

166173
afterEach(() => {
@@ -182,7 +189,8 @@ describe("runs", () => {
182189
setAccessKey: setAccessKeyStub,
183190
setBuildName: setBuildNameStub,
184191
setUsageReportingFlag: setUsageReportingFlagStub,
185-
getConfigPath: getConfigPathStub
192+
getConfigPath: getConfigPathStub,
193+
deleteResults: deleteResultsStub
186194
},
187195
"../helpers/capabilityHelper": {
188196
validate: capabilityValidatorStub,
@@ -212,6 +220,7 @@ describe("runs", () => {
212220
sinon.assert.calledOnce(archiverStub);
213221
sinon.assert.calledOnce(setUsageReportingFlagStub);
214222
sinon.assert.calledOnce(deleteZipStub);
223+
sinon.assert.calledOnce(deleteResultsStub);
215224
sinon.assert.calledOnceWithExactly(
216225
sendUsageReportStub,
217226
bsConfig,
@@ -243,6 +252,7 @@ describe("runs", () => {
243252
archiverStub = sandbox.stub();
244253
zipUploadStub = sandbox.stub();
245254
deleteZipStub = sandbox.stub();
255+
deleteResultsStub = sandbox.stub();
246256
});
247257

248258
afterEach(() => {
@@ -264,7 +274,8 @@ describe("runs", () => {
264274
setAccessKey: setAccessKeyStub,
265275
setBuildName: setBuildNameStub,
266276
setUsageReportingFlag: setUsageReportingFlagStub,
267-
getConfigPath: getConfigPathStub
277+
getConfigPath: getConfigPathStub,
278+
deleteResults: deleteResultsStub
268279
},
269280
"../helpers/capabilityHelper": {
270281
validate: capabilityValidatorStub,
@@ -298,7 +309,7 @@ describe("runs", () => {
298309
sinon.assert.calledOnce(archiverStub);
299310
sinon.assert.calledOnce(setUsageReportingFlagStub);
300311
sinon.assert.calledOnce(zipUploadStub);
301-
312+
sinon.assert.calledOnce(deleteResultsStub);
302313
sinon.assert.calledOnceWithExactly(
303314
sendUsageReportStub,
304315
bsConfig,
@@ -334,6 +345,7 @@ describe("runs", () => {
334345
zipUploadStub = sandbox.stub();
335346
createBuildStub = sandbox.stub();
336347
deleteZipStub = sandbox.stub();
348+
deleteResultsStub = sandbox.stub();
337349
});
338350

339351
afterEach(() => {
@@ -355,7 +367,8 @@ describe("runs", () => {
355367
setAccessKey: setAccessKeyStub,
356368
setBuildName: setBuildNameStub,
357369
setUsageReportingFlag: setUsageReportingFlagStub,
358-
getConfigPath: getConfigPathStub
370+
getConfigPath: getConfigPathStub,
371+
deleteResults: deleteResultsStub
359372
},
360373
"../helpers/capabilityHelper": {
361374
validate: capabilityValidatorStub,
@@ -398,6 +411,7 @@ describe("runs", () => {
398411
sinon.assert.calledOnce(createBuildStub);
399412

400413
sinon.assert.calledOnce(sendUsageReportStub);
414+
sinon.assert.calledOnce(deleteResultsStub);
401415

402416
sinon.assert.calledOnceWithExactly(
403417
sendUsageReportStub,
@@ -436,6 +450,8 @@ describe("runs", () => {
436450
zipUploadStub = sandbox.stub();
437451
createBuildStub = sandbox.stub();
438452
deleteZipStub = sandbox.stub();
453+
exportResultsStub = sandbox.stub();
454+
deleteResultsStub = sandbox.stub();
439455
isUndefinedStub = sandbox.stub();
440456
});
441457

@@ -460,6 +476,8 @@ describe("runs", () => {
460476
setUsageReportingFlag: setUsageReportingFlagStub,
461477
setParallels: setParallelsStub,
462478
getConfigPath: getConfigPathStub,
479+
exportResults: exportResultsStub,
480+
deleteResults: deleteResultsStub,
463481
isUndefined: isUndefinedStub
464482
},
465483
"../helpers/capabilityHelper": {
@@ -504,7 +522,8 @@ describe("runs", () => {
504522
sinon.assert.calledOnce(setUsageReportingFlagStub);
505523
sinon.assert.calledOnce(zipUploadStub);
506524
sinon.assert.calledOnce(createBuildStub);
507-
525+
sinon.assert.calledOnce(exportResultsStub);
526+
sinon.assert.calledOnce(deleteResultsStub);
508527
sinon.assert.calledOnceWithExactly(
509528
sendUsageReportStub,
510529
bsConfig,

test/unit/bin/helpers/utils.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const path = require('path');
44
const chai = require("chai"),
55
expect = chai.expect,
66
sinon = require('sinon'),
7-
chaiAsPromised = require("chai-as-promised");
7+
chaiAsPromised = require("chai-as-promised"),
8+
fs = require('fs');
89

910
const utils = require('../../../../bin/helpers/utils'),
1011
constant = require('../../../../bin/helpers/constants'),
@@ -242,6 +243,35 @@ describe("utils", () => {
242243
});
243244
});
244245

246+
describe("exportResults", () => {
247+
248+
it("should export results to log/build_results.txt", () => {
249+
sinon.stub(fs, 'writeFileSync').returns(true);
250+
utils.exportResults("build_id", "build_url");
251+
fs.writeFileSync.restore();
252+
});
253+
254+
it("should log warning if write to log/build_results.txt fails", () => {
255+
let writeFileSyncStub = sinon.stub(fs, 'writeFileSync');
256+
let loggerWarnStub = sinon.stub(logger, "warn");
257+
writeFileSyncStub.yields(new Error("Write Failed"));
258+
utils.exportResults("build_id", "build_url");
259+
sinon.assert.calledOnce(writeFileSyncStub);
260+
sinon.assert.calledTwice(loggerWarnStub);
261+
fs.writeFileSync.restore();
262+
});
263+
264+
});
265+
266+
describe("deleteResults", () => {
267+
268+
it("should delete log/build_results.txt", () => {
269+
sinon.stub(fs, 'unlink').returns(true);
270+
utils.deleteResults();
271+
fs.unlink.restore();
272+
});
273+
});
274+
245275
describe("isCypressProjDirValid", () => {
246276
it("should return true when cypressDir and cypressProjDir is same", () =>{
247277
expect(utils.isCypressProjDirValid("/absolute/path","/absolute/path")).to.be.true;

0 commit comments

Comments
 (0)