Skip to content

Commit 70a6daa

Browse files
committed
Added specs
1 parent ca7920b commit 70a6daa

File tree

2 files changed

+131
-39
lines changed

2 files changed

+131
-39
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"license": "MIT",
1919
"dependencies": {
2020
"https-proxy-agent": "^1.0.0",
21-
"is-running": "^2.0.0"
21+
"is-running": "^2.0.0",
22+
"sinon": "^1.17.6",
23+
"temp-fs": "^0.9.9"
2224
},
2325
"devDependencies": {
2426
"eslint": "1.10.3",

test/local.js

Lines changed: 128 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
var expect = require('expect.js'),
2+
sinon = require('sinon'),
23
mocks = require('mocks'),
34
path = require('path'),
45
fs = require('fs'),
56
rimraf = require('rimraf'),
67
Proxy = require('proxy'),
8+
tempfs = require('temp-fs'),
79
browserstack = require('../index'),
810
LocalBinary = require('../lib/LocalBinary');
911

12+
13+
const MAX_TIMEOUT = 600000;
14+
1015
describe('Local', function () {
1116
var bsLocal;
1217
beforeEach(function () {
@@ -157,53 +162,138 @@ describe('Local', function () {
157162
});
158163

159164
describe('LocalBinary', function () {
160-
var proxy;
161-
var proxyPort;
162-
var binary;
163-
var tempDownloadPath;
164-
165-
before(function (done) {
166-
// setup HTTP proxy server
167-
proxy = new Proxy();
168-
proxy.listen(function () {
169-
proxyPort = proxy.address().port;
170-
done();
165+
describe('Retries', function() {
166+
var unlinkTmp,
167+
defaultBinaryPath,
168+
validBinaryPath,
169+
sandBox;
170+
171+
before(function(done) {
172+
this.timeout(MAX_TIMEOUT);
173+
// ensure that we have a valid binary downloaded
174+
175+
// removeIfInvalid();
176+
(new LocalBinary()).binaryPath({}, function(binaryPath) {
177+
defaultBinaryPath = binaryPath;
178+
tempfs.mkdir({
179+
recursive: true
180+
}, function(err, dir) {
181+
if(err) { throw err; }
182+
183+
validBinaryPath = path.join(dir.path, path.basename(binaryPath));
184+
fs.rename(defaultBinaryPath, validBinaryPath, function(err) {
185+
if(err) { throw err; }
186+
187+
unlinkTmp = dir.unlink;
188+
done();
189+
});
190+
});
191+
});
171192
});
172-
});
173193

174-
after(function (done) {
175-
proxy.once('close', function () { done(); });
176-
proxy.close();
177-
});
194+
beforeEach(function() {
195+
sandBox = sinon.sandbox.create();
196+
});
178197

179-
beforeEach(function () {
180-
binary = new LocalBinary();
181-
tempDownloadPath = path.join(process.cwd(), 'download');
182-
});
198+
it('Tries to download binary if its corrupted', function(done) {
199+
fs.unlink(defaultBinaryPath, function() {
200+
var localBinary = new LocalBinary();
201+
var downloadStub = sandBox.stub(localBinary, 'download', function() {
202+
downloadStub.callArgWith(2, [ defaultBinaryPath ]);
203+
expect(downloadStub.args[0][3]).to.be(5);
204+
});
183205

184-
afterEach(function () {
185-
rimraf.sync(tempDownloadPath);
186-
});
206+
fs.writeFile(defaultBinaryPath, 'Random String', function() {
207+
fs.chmod(defaultBinaryPath, '0755', function() {
208+
localBinary.binaryPath({
209+
}, function(binaryPath) {
210+
expect(downloadStub.called).to.be.true;
211+
done();
212+
});
213+
});
214+
});
215+
});
216+
});
187217

188-
it('should download binaries without proxy', function (done) {
189-
this.timeout(600000);
190-
var conf = {};
191-
binary.download(conf, tempDownloadPath, function (result) {
192-
expect(fs.existsSync(result)).to.equal(true);
218+
it('Tries to download binary if its not present', function(done) {
219+
fs.unlink(defaultBinaryPath, function() {
220+
var localBinary = new LocalBinary();
221+
var downloadStub = sandBox.stub(localBinary, 'download', function() {
222+
downloadStub.callArgWith(2, [ defaultBinaryPath ]);
223+
expect(downloadStub.args[0][3]).to.be(5);
224+
});
225+
226+
localBinary.binaryPath({
227+
}, function(binaryPath) {
228+
expect(downloadStub.called).to.be.true;
229+
done();
230+
});
231+
});
232+
});
233+
234+
afterEach(function(done) {
235+
sandBox.restore();
193236
done();
194237
});
238+
239+
after(function(done) {
240+
fs.rename(validBinaryPath, defaultBinaryPath, function(err) {
241+
if(err) { throw err; }
242+
243+
unlinkTmp(done);
244+
});
245+
});
195246
});
196247

197-
it('should download binaries with proxy', function (done) {
198-
this.timeout(600000);
199-
var conf = {
200-
proxyHost: '127.0.0.1',
201-
proxyPort: proxyPort
202-
};
203-
binary.download(conf, tempDownloadPath, function (result) {
204-
// test for file existence
205-
expect(fs.existsSync(result)).to.equal(true);
206-
done();
248+
describe('Download', function() {
249+
var proxy;
250+
var proxyPort;
251+
var binary;
252+
var tempDownloadPath;
253+
254+
before(function (done) {
255+
// setup HTTP proxy server
256+
proxy = new Proxy();
257+
proxy.listen(function () {
258+
proxyPort = proxy.address().port;
259+
done();
260+
});
261+
});
262+
263+
after(function (done) {
264+
proxy.once('close', function () { done(); });
265+
proxy.close();
266+
});
267+
268+
beforeEach(function () {
269+
binary = new LocalBinary();
270+
tempDownloadPath = path.join(process.cwd(), 'download');
271+
});
272+
273+
afterEach(function () {
274+
rimraf.sync(tempDownloadPath);
275+
});
276+
277+
it('should download binaries without proxy', function (done) {
278+
this.timeout(MAX_TIMEOUT);
279+
var conf = {};
280+
binary.download(conf, tempDownloadPath, function (result) {
281+
expect(fs.existsSync(result)).to.equal(true);
282+
done();
283+
});
284+
});
285+
286+
it('should download binaries with proxy', function (done) {
287+
this.timeout(MAX_TIMEOUT);
288+
var conf = {
289+
proxyHost: '127.0.0.1',
290+
proxyPort: proxyPort
291+
};
292+
binary.download(conf, tempDownloadPath, function (result) {
293+
// test for file existence
294+
expect(fs.existsSync(result)).to.equal(true);
295+
done();
296+
});
207297
});
208298
});
209299
});

0 commit comments

Comments
 (0)