Skip to content

Binary download #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added specs
  • Loading branch information
vedharish committed Oct 24, 2016
commit 70a6daacf14e3e744a3b5d7a3fa47595474dd7f7
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
166 changes: 128 additions & 38 deletions test/local.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down Expand Up @@ -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();
});
});
});
});