diff --git a/package.json b/package.json index 2314b66..95c16fc 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,9 @@ "license": "MIT", "dependencies": { "https-proxy-agent": "^1.0.0", - "is-running": "^2.0.0" + "is-running": "^2.0.0", + "sinon": "^1.17.6", + "temp-fs": "^0.9.9" }, "devDependencies": { "eslint": "1.10.3", diff --git a/test/local.js b/test/local.js index 5fd442d..02a5e6a 100644 --- a/test/local.js +++ b/test/local.js @@ -1,12 +1,17 @@ var expect = require('expect.js'), + sinon = require('sinon'), mocks = require('mocks'), path = require('path'), fs = require('fs'), rimraf = require('rimraf'), Proxy = require('proxy'), + tempfs = require('temp-fs'), browserstack = require('../index'), LocalBinary = require('../lib/LocalBinary'); + +const MAX_TIMEOUT = 600000; + describe('Local', function () { var bsLocal; beforeEach(function () { @@ -157,53 +162,138 @@ describe('Local', function () { }); describe('LocalBinary', function () { - var proxy; - var proxyPort; - var binary; - var tempDownloadPath; - - before(function (done) { - // setup HTTP proxy server - proxy = new Proxy(); - proxy.listen(function () { - proxyPort = proxy.address().port; - done(); + describe('Retries', function() { + var unlinkTmp, + defaultBinaryPath, + validBinaryPath, + sandBox; + + before(function(done) { + this.timeout(MAX_TIMEOUT); + // ensure that we have a valid binary downloaded + + // removeIfInvalid(); + (new LocalBinary()).binaryPath({}, function(binaryPath) { + defaultBinaryPath = binaryPath; + tempfs.mkdir({ + recursive: true + }, function(err, dir) { + if(err) { throw err; } + + validBinaryPath = path.join(dir.path, path.basename(binaryPath)); + fs.rename(defaultBinaryPath, validBinaryPath, function(err) { + if(err) { throw err; } + + unlinkTmp = dir.unlink; + done(); + }); + }); + }); }); - }); - after(function (done) { - proxy.once('close', function () { done(); }); - proxy.close(); - }); + beforeEach(function() { + sandBox = sinon.sandbox.create(); + }); - beforeEach(function () { - binary = new LocalBinary(); - tempDownloadPath = path.join(process.cwd(), 'download'); - }); + it('Tries to download binary if its corrupted', function(done) { + fs.unlink(defaultBinaryPath, function() { + var localBinary = new LocalBinary(); + var downloadStub = sandBox.stub(localBinary, 'download', function() { + downloadStub.callArgWith(2, [ defaultBinaryPath ]); + expect(downloadStub.args[0][3]).to.be(5); + }); - afterEach(function () { - rimraf.sync(tempDownloadPath); - }); + fs.writeFile(defaultBinaryPath, 'Random String', function() { + fs.chmod(defaultBinaryPath, '0755', function() { + localBinary.binaryPath({ + }, function(binaryPath) { + expect(downloadStub.called).to.be.true; + done(); + }); + }); + }); + }); + }); - it('should download binaries without proxy', function (done) { - this.timeout(600000); - var conf = {}; - binary.download(conf, tempDownloadPath, function (result) { - expect(fs.existsSync(result)).to.equal(true); + it('Tries to download binary if its not present', function(done) { + fs.unlink(defaultBinaryPath, function() { + var localBinary = new LocalBinary(); + var downloadStub = sandBox.stub(localBinary, 'download', function() { + downloadStub.callArgWith(2, [ defaultBinaryPath ]); + expect(downloadStub.args[0][3]).to.be(5); + }); + + localBinary.binaryPath({ + }, function(binaryPath) { + expect(downloadStub.called).to.be.true; + done(); + }); + }); + }); + + afterEach(function(done) { + sandBox.restore(); done(); }); + + after(function(done) { + fs.rename(validBinaryPath, defaultBinaryPath, function(err) { + if(err) { throw err; } + + unlinkTmp(done); + }); + }); }); - it('should download binaries with proxy', function (done) { - this.timeout(600000); - var conf = { - proxyHost: '127.0.0.1', - proxyPort: proxyPort - }; - binary.download(conf, tempDownloadPath, function (result) { - // test for file existence - expect(fs.existsSync(result)).to.equal(true); - done(); + describe('Download', function() { + var proxy; + var proxyPort; + var binary; + var tempDownloadPath; + + before(function (done) { + // setup HTTP proxy server + proxy = new Proxy(); + proxy.listen(function () { + proxyPort = proxy.address().port; + done(); + }); + }); + + after(function (done) { + proxy.once('close', function () { done(); }); + proxy.close(); + }); + + beforeEach(function () { + binary = new LocalBinary(); + tempDownloadPath = path.join(process.cwd(), 'download'); + }); + + afterEach(function () { + rimraf.sync(tempDownloadPath); + }); + + it('should download binaries without proxy', function (done) { + this.timeout(MAX_TIMEOUT); + var conf = {}; + binary.download(conf, tempDownloadPath, function (result) { + expect(fs.existsSync(result)).to.equal(true); + done(); + }); + }); + + it('should download binaries with proxy', function (done) { + this.timeout(MAX_TIMEOUT); + var conf = { + proxyHost: '127.0.0.1', + proxyPort: proxyPort + }; + binary.download(conf, tempDownloadPath, function (result) { + // test for file existence + expect(fs.existsSync(result)).to.equal(true); + done(); + }); }); }); });