Skip to content

Commit 536bb6f

Browse files
authored
Merge pull request #179 from browserstack/chore/green-unit-tests
test: fix pre-existing unit-test failures (green the suite)
2 parents 0d29261 + 76ccc28 commit 536bb6f

1 file changed

Lines changed: 76 additions & 49 deletions

File tree

test/local.js

Lines changed: 76 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ var expect = require('expect.js'),
1212

1313
const MAX_TIMEOUT = 600000;
1414

15+
// Assertions that run inside asynchronous callbacks (e.g. the tree-kill
16+
// callback fired from Local.stop, or binary-download callbacks) execute
17+
// outside Mocha's synchronous try/catch. A throw there escapes as an
18+
// uncaught exception and aborts the whole Mocha process, which hides the
19+
// results of every test that has not run yet. `check` runs the assertions
20+
// in a try/catch and routes any failure through `done`, so a failing
21+
// assertion is reported as a normal test failure and the run continues.
22+
function check(done, assertions) {
23+
try {
24+
assertions();
25+
done();
26+
} catch (err) {
27+
done(err);
28+
}
29+
}
30+
1531
describe('Local', function () {
1632
var bsLocal;
1733
beforeEach(function () {
@@ -21,17 +37,19 @@ describe('Local', function () {
2137
it('should have pid when running', function (done) {
2238
this.timeout(600000);
2339
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function(){
24-
expect(bsLocal.tunnel.pid).to.not.equal(0);
25-
done();
40+
check(done, function(){
41+
expect(bsLocal.tunnel.pid).to.not.equal(0);
42+
});
2643
});
2744
});
2845

2946
it('should return is running properly', function (done) {
3047
this.timeout(60000);
3148
expect(bsLocal.isRunning()).to.not.equal(true);
3249
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function(){
33-
expect(bsLocal.isRunning()).to.equal(true);
34-
done();
50+
check(done, function(){
51+
expect(bsLocal.isRunning()).to.equal(true);
52+
});
3553
});
3654
});
3755

@@ -213,10 +231,15 @@ describe('Local', function () {
213231
it('should stop local', function (done) {
214232
this.timeout(MAX_TIMEOUT);
215233
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY}, function(){
216-
expect(bsLocal.isRunning()).to.equal(true);
234+
try {
235+
expect(bsLocal.isRunning()).to.equal(true);
236+
} catch (err) {
237+
return done(err);
238+
}
217239
bsLocal.stop(function(){
218-
expect(bsLocal.isRunning()).to.equal(false);
219-
done();
240+
check(done, function(){
241+
expect(bsLocal.isRunning()).to.equal(false);
242+
});
220243
});
221244
});
222245
});
@@ -280,7 +303,8 @@ describe('LocalBinary', function () {
280303
// ensure that we have a valid binary downloaded
281304

282305
// removeIfInvalid();
283-
(new LocalBinary()).binaryPath({}, 'abc', 9, function(binaryPath) {
306+
// binaryPath signature is (conf, bsHost, key, parentRetries, callback).
307+
(new LocalBinary()).binaryPath({}, null, 'abc', 9, function(binaryPath) {
284308
defaultBinaryPath = binaryPath;
285309
tempfs.mkdir({
286310
recursive: true
@@ -307,13 +331,13 @@ describe('LocalBinary', function () {
307331
var localBinary = new LocalBinary();
308332
var downloadStub = sandBox.stub(localBinary, 'download', function() {
309333
downloadStub.callArgWith(2, [ defaultBinaryPath ]);
310-
expect(downloadStub.args[0][3]).to.be(5);
334+
expect(downloadStub.args[0][3]).to.be(9);
311335
});
312336

313337
fs.writeFile(defaultBinaryPath, 'Random String', function() {
314338
fs.chmod(defaultBinaryPath, '0755', function() {
315339
localBinary.binaryPath({
316-
}, 'abc', 9, function(binaryPath) {
340+
}, null, 'abc', 9, function(binaryPath) {
317341
expect(downloadStub.called).to.be.true;
318342
done();
319343
});
@@ -327,11 +351,11 @@ describe('LocalBinary', function () {
327351
var localBinary = new LocalBinary();
328352
var downloadStub = sandBox.stub(localBinary, 'download', function() {
329353
downloadStub.callArgWith(2, [ defaultBinaryPath ]);
330-
expect(downloadStub.args[0][3]).to.be(5);
354+
expect(downloadStub.args[0][3]).to.be(9);
331355
});
332356

333357
localBinary.binaryPath({
334-
}, 'abc', 9, function(binaryPath) {
358+
}, null, 'abc', 9, function(binaryPath) {
335359
expect(downloadStub.called).to.be.true;
336360
done();
337361
});
@@ -352,55 +376,56 @@ describe('LocalBinary', function () {
352376
});
353377
});
354378

355-
describe('Download Path', function() {
356-
var sandBox;
379+
// The OS/arch -> binary filename mapping used to be asserted via
380+
// getDownloadPath(), but getDownloadPath is now async and prefixes a
381+
// dynamically fetched source URL (see getSourceUrl). The OS-specific part
382+
// of the download path now lives entirely in getBinaryFilename(), so these
383+
// tests exercise that directly. hostOS/is64bits/isArm64/isAlpine are plain
384+
// instance fields, so they are overridden by assignment (no sinon needed,
385+
// which also avoids double-wrapping the same property inside a loop).
386+
describe('Binary filename', function() {
357387
var localBinary;
358388

359389
beforeEach(function() {
360-
sandBox = sinon.sandbox.create();
361390
localBinary = new LocalBinary();
362391
});
363392

364-
it('should return download path of darwin binary', function() {
365-
var osNames = ['darwin', 'mac os'];
366-
osNames.forEach(function(os) {
367-
sandBox.stub(localBinary, 'hostOS', os);
368-
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-darwin-x64');
393+
it('should return darwin binary filename', function() {
394+
['darwin', 'mac os'].forEach(function(os) {
395+
localBinary.hostOS = os;
396+
expect(localBinary.getBinaryFilename()).to.equal('BrowserStackLocal-darwin-x64');
369397
});
370398
});
371399

372-
it('should return download path of exe binary', function() {
373-
var osNames = ['mswin', 'msys', 'mingw', 'cygwin', 'bccwin', 'wince', 'emc', 'win32'];
374-
osNames.forEach(function(os) {
375-
sandBox.stub(localBinary, 'hostOS', os);
376-
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal.exe');
400+
it('should return exe binary filename', function() {
401+
['mswin', 'msys', 'mingw', 'cygwin', 'bccwin', 'wince', 'emc', 'win32'].forEach(function(os) {
402+
localBinary.hostOS = os;
403+
expect(localBinary.getBinaryFilename()).to.equal('BrowserStackLocal.exe');
377404
});
378405
});
379406

380-
it('should return download path of linux 64 arch binary', function() {
381-
sandBox.stub(localBinary, 'hostOS', 'linux');
382-
sandBox.stub(localBinary, 'is64bits', true);
383-
localBinary.isAlpine = sandBox.stub(localBinary, 'isAlpine').returns(false);
384-
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-x64');
407+
it('should return linux 64 arch binary filename', function() {
408+
localBinary.hostOS = 'linux';
409+
localBinary.isArm64 = false;
410+
localBinary.is64bits = true;
411+
localBinary.isAlpine = function() { return false; };
412+
expect(localBinary.getBinaryFilename()).to.equal('BrowserStackLocal-linux-x64');
385413
});
386414

387-
it('should return download path of linux 32 arch binary', function() {
388-
sandBox.stub(localBinary, 'hostOS', 'linux');
389-
sandBox.stub(localBinary, 'is64bits', false);
390-
localBinary.isAlpine = sandBox.stub(localBinary, 'isAlpine').returns(false);
391-
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-ia32');
415+
it('should return linux 32 arch binary filename', function() {
416+
localBinary.hostOS = 'linux';
417+
localBinary.isArm64 = false;
418+
localBinary.is64bits = false;
419+
localBinary.isAlpine = function() { return false; };
420+
expect(localBinary.getBinaryFilename()).to.equal('BrowserStackLocal-linux-ia32');
392421
});
393422

394-
it('should return download path of alpine linux binary', function() {
395-
sandBox.stub(localBinary, 'hostOS', 'linux');
396-
localBinary.isAlpine = sandBox.stub(localBinary, 'isAlpine').returns(true);
397-
sandBox.stub(localBinary, 'is64bits', true);
398-
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-alpine');
399-
});
400-
401-
afterEach(function(done) {
402-
sandBox.restore();
403-
done();
423+
it('should return alpine linux binary filename', function() {
424+
localBinary.hostOS = 'linux';
425+
localBinary.isArm64 = false;
426+
localBinary.is64bits = true;
427+
localBinary.isAlpine = function() { return true; };
428+
expect(localBinary.getBinaryFilename()).to.equal('BrowserStackLocal-alpine');
404429
});
405430
});
406431

@@ -437,8 +462,9 @@ describe('LocalBinary', function () {
437462
this.timeout(MAX_TIMEOUT);
438463
var conf = {};
439464
binary.download(conf, tempDownloadPath, function (result) {
440-
expect(fs.existsSync(result)).to.equal(true);
441-
done();
465+
check(done, function(){
466+
expect(fs.existsSync(result)).to.equal(true);
467+
});
442468
});
443469
});
444470

@@ -450,8 +476,9 @@ describe('LocalBinary', function () {
450476
};
451477
binary.download(conf, tempDownloadPath, function (result) {
452478
// test for file existence
453-
expect(fs.existsSync(result)).to.equal(true);
454-
done();
479+
check(done, function(){
480+
expect(fs.existsSync(result)).to.equal(true);
481+
});
455482
});
456483
});
457484

0 commit comments

Comments
 (0)