Skip to content

Commit 6c19278

Browse files
Merge pull request #182 from browserstack/instrument_size
Instrument zip sizes
2 parents 98aec7f + 83e67cf commit 6c19278

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

bin/commands/runs.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
'use strict';
2+
const path = require('path');
3+
24
const archiver = require("../helpers/archiver"),
35
zipUploader = require("../helpers/zipUpload"),
46
build = require("../helpers/build"),
@@ -114,6 +116,9 @@ module.exports = function run(args) {
114116
return archiver.archive(bsConfig.run_settings, config.fileName, args.exclude, md5data).then(function (data) {
115117
markBlockEnd('zip.archive');
116118

119+
let test_zip_size = utils.fetchZipSize(path.join(process.cwd(), config.fileName));
120+
let npm_zip_size = utils.fetchZipSize(path.join(process.cwd(), config.packageFileName));
121+
117122
// Uploaded zip file
118123
markBlockStart('zip.zipUpload');
119124
return zipUploader.zipUpload(bsConfig, md5data, packageData).then(async function (zip) {
@@ -186,6 +191,8 @@ module.exports = function run(args) {
186191
package_error: utils.checkError(packageData),
187192
checkmd5_error: utils.checkError(md5data),
188193
build_id: data.build_id,
194+
test_zip_size: test_zip_size,
195+
npm_zip_size: npm_zip_size,
189196
};
190197
if (bsConfig && bsConfig.connection_settings) {
191198
if (bsConfig.connection_settings.local_mode) {

bin/helpers/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,3 +1012,13 @@ async function processExitHandler(exitData){
10121012
await this.stopLocalBinary(exitData.bsConfig, exitData.bsLocalInstance, exitData.args);
10131013
process.exit(0);
10141014
}
1015+
1016+
exports.fetchZipSize = (fileName) => {
1017+
try {
1018+
let stats = fs.statSync(fileName)
1019+
return stats.size; // in bytes
1020+
}
1021+
catch(err) {
1022+
return 0;
1023+
}
1024+
}

test/unit/bin/commands/runs.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ describe("runs", () => {
391391
setConfigStub = sandbox.stub();
392392
setBrowsersStub = sandbox.stub();
393393
setCLIModeStub = sandbox.stub();
394+
fetchZipSizeStub = sandbox.stub();
394395
});
395396

396397
afterEach(() => {
@@ -431,7 +432,8 @@ describe("runs", () => {
431432
setLocalConfigFile: setLocalConfigFileStub,
432433
setBrowsers: setBrowsersStub,
433434
setConfig: setConfigStub,
434-
setCLIMode: setCLIModeStub
435+
setCLIMode: setCLIModeStub,
436+
fetchZipSize: fetchZipSizeStub,
435437
},
436438
'../helpers/capabilityHelper': {
437439
validate: capabilityValidatorStub,
@@ -461,6 +463,7 @@ describe("runs", () => {
461463
packageInstallerStub.returns(Promise.resolve({ packageArchieveCreated: false }));
462464
archiverStub.returns(Promise.resolve("Zipping completed"));
463465
zipUploadStub.returns(Promise.reject("random-error"));
466+
fetchZipSizeStub.returns(123);
464467

465468
return runs(args)
466469
.then(function (_bsConfig) {
@@ -469,6 +472,7 @@ describe("runs", () => {
469472
.catch((error) => {
470473
sinon.assert.calledOnce(getConfigPathStub);
471474
sinon.assert.calledOnce(getConfigPathStub);
475+
sinon.assert.calledTwice(fetchZipSizeStub);
472476
sinon.assert.calledOnce(setLocalModeStub);
473477
sinon.assert.calledOnce(setLocalConfigFileStub);
474478
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
@@ -546,6 +550,7 @@ describe("runs", () => {
546550
setConfigStub = sandbox.stub();
547551
setBrowsersStub = sandbox.stub();
548552
setCLIModeStub = sandbox.stub();
553+
fetchZipSizeStub = sandbox.stub();
549554
});
550555

551556
afterEach(() => {
@@ -587,7 +592,8 @@ describe("runs", () => {
587592
setLocalConfigFile: setLocalConfigFileStub,
588593
setBrowsers: setBrowsersStub,
589594
setConfig: setConfigStub,
590-
setCLIMode: setCLIModeStub
595+
setCLIMode: setCLIModeStub,
596+
fetchZipSize: fetchZipSizeStub,
591597
},
592598
'../helpers/capabilityHelper': {
593599
validate: capabilityValidatorStub,
@@ -624,6 +630,7 @@ describe("runs", () => {
624630
zipUploadStub.returns(Promise.resolve("zip uploaded"));
625631
stopLocalBinaryStub.returns(Promise.resolve("nothing"));
626632
createBuildStub.returns(Promise.reject("random-error"));
633+
fetchZipSizeStub.returns(123);
627634

628635
return runs(args)
629636
.then(function (_bsConfig) {
@@ -632,6 +639,7 @@ describe("runs", () => {
632639
.catch((error) => {
633640
sinon.assert.calledOnce(getConfigPathStub);
634641
sinon.assert.calledOnce(getConfigPathStub);
642+
sinon.assert.calledTwice(fetchZipSizeStub);
635643
sinon.assert.calledOnce(setLocalConfigFileStub);
636644
sinon.assert.calledOnce(setLocalModeStub);
637645
sinon.assert.calledOnce(setupLocalTestingStub);
@@ -694,6 +702,9 @@ describe("runs", () => {
694702
return "end";
695703
});
696704
dashboardUrl = "dashboard-url";
705+
packageDirName = "package-dir";
706+
packageFileName = "package-file";
707+
fileName = "file-name";
697708
capabilityValidatorStub = sandbox.stub();
698709
archiverStub = sandbox.stub();
699710
zipUploadStub = sandbox.stub();
@@ -724,6 +735,7 @@ describe("runs", () => {
724735
nonEmptyArrayStub = sandbox.stub();
725736
setCLIModeStub = sandbox.stub();
726737
setProcessHooksStub = sandbox.stub();
738+
fetchZipSizeStub = sandbox.stub();
727739
});
728740

729741
afterEach(() => {
@@ -736,7 +748,7 @@ describe("runs", () => {
736748
let errorCode = null;
737749
let message = `Success! ${Constants.userMessages.BUILD_CREATED} with build id: random_build_id`;
738750
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${dashboardUrl}`;
739-
let data = {time_components: {}, unique_id: 'random_hash', package_error: 'test', checkmd5_error: 'test', build_id: 'random_build_id'}
751+
let data = {time_components: {}, unique_id: 'random_hash', package_error: 'test', checkmd5_error: 'test', build_id: 'random_build_id', test_zip_size: 123, npm_zip_size: 123}
740752

741753
const runs = proxyquire('../../../../bin/commands/runs', {
742754
'../helpers/utils': {
@@ -773,7 +785,8 @@ describe("runs", () => {
773785
nonEmptyArray: nonEmptyArrayStub,
774786
checkError: checkErrorStub,
775787
setCLIMode: setCLIModeStub,
776-
setProcessHooks: setProcessHooksStub
788+
setProcessHooks: setProcessHooksStub,
789+
fetchZipSize: fetchZipSizeStub,
777790
},
778791
'../helpers/capabilityHelper': {
779792
validate: capabilityValidatorStub,
@@ -793,6 +806,9 @@ describe("runs", () => {
793806
},
794807
'../helpers/config': {
795808
dashboardUrl: dashboardUrl,
809+
packageDirName: packageDirName,
810+
packageFileName: packageFileName,
811+
fileName: fileName,
796812
},
797813
'../helpers/checkUploaded': {
798814
checkUploadedMd5: checkUploadedStub,
@@ -821,6 +837,7 @@ describe("runs", () => {
821837
stopLocalBinaryStub.returns(Promise.resolve("nothing"));
822838
nonEmptyArrayStub.returns(false);
823839
checkErrorStub.returns('test');
840+
fetchZipSizeStub.returns(123);
824841
createBuildStub.returns(Promise.resolve({ message: 'Success', build_id: 'random_build_id', dashboard_url: dashboardUrl }));
825842

826843
return runs(args)
@@ -836,6 +853,7 @@ describe("runs", () => {
836853
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
837854
sinon.assert.calledOnce(setParallelsStub);
838855
sinon.assert.calledOnce(warnSpecLimitStub);
856+
sinon.assert.calledTwice(fetchZipSizeStub);
839857
sinon.assert.calledOnce(setLocalStub);
840858
sinon.assert.calledOnce(setLocalModeStub);
841859
sinon.assert.calledOnce(setupLocalTestingStub);

test/unit/bin/helpers/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2663,4 +2663,16 @@ describe('utils', () => {
26632663
process.exit.restore();
26642664
});
26652665
});
2666+
2667+
describe('fetchZipSize', () => {
2668+
it('should return size in bytes if file is present', () => {
2669+
sinon.stub(fs, 'statSync').returns({size: 123});
2670+
expect(utils.fetchZipSize('unknown.zip')).to.be.eql(123);
2671+
fs.statSync.restore();
2672+
});
2673+
2674+
it('handle file not present', () => {
2675+
expect(utils.fetchZipSize('unknown.tar.gz')).to.be.eql(0);
2676+
});
2677+
});
26662678
});

0 commit comments

Comments
 (0)