|
1 | 1 | var expect = require('expect.js'),
|
| 2 | + sinon = require('sinon'), |
2 | 3 | mocks = require('mocks'),
|
3 | 4 | path = require('path'),
|
4 | 5 | fs = require('fs'),
|
5 | 6 | rimraf = require('rimraf'),
|
6 | 7 | Proxy = require('proxy'),
|
| 8 | + tempfs = require('temp-fs'), |
7 | 9 | browserstack = require('../index'),
|
8 | 10 | LocalBinary = require('../lib/LocalBinary');
|
9 | 11 |
|
| 12 | + |
| 13 | +const MAX_TIMEOUT = 600000; |
| 14 | + |
10 | 15 | describe('Local', function () {
|
11 | 16 | var bsLocal;
|
12 | 17 | beforeEach(function () {
|
@@ -157,53 +162,138 @@ describe('Local', function () {
|
157 | 162 | });
|
158 | 163 |
|
159 | 164 | 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 | + }); |
171 | 192 | });
|
172 |
| - }); |
173 | 193 |
|
174 |
| - after(function (done) { |
175 |
| - proxy.once('close', function () { done(); }); |
176 |
| - proxy.close(); |
177 |
| - }); |
| 194 | + beforeEach(function() { |
| 195 | + sandBox = sinon.sandbox.create(); |
| 196 | + }); |
178 | 197 |
|
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 | + }); |
183 | 205 |
|
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 | + }); |
187 | 217 |
|
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(); |
193 | 236 | done();
|
194 | 237 | });
|
| 238 | + |
| 239 | + after(function(done) { |
| 240 | + fs.rename(validBinaryPath, defaultBinaryPath, function(err) { |
| 241 | + if(err) { throw err; } |
| 242 | + |
| 243 | + unlinkTmp(done); |
| 244 | + }); |
| 245 | + }); |
195 | 246 | });
|
196 | 247 |
|
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 | + }); |
207 | 297 | });
|
208 | 298 | });
|
209 | 299 | });
|
0 commit comments