Skip to content

Commit bc2657c

Browse files
committed
Corrected Tests
1 parent def45db commit bc2657c

File tree

5 files changed

+218
-135
lines changed

5 files changed

+218
-135
lines changed

lib/ZipBinary.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
var https = require('https'),
22
unzip = require('unzip'),
33
fs = require('fs'),
4-
helper = require('./helper'),
5-
log = helper.log;
4+
Helper = require('./helper');
5+
6+
function ZipBinary() {
7+
var helper = new Helper.helper();
8+
var log = helper.log;
9+
10+
var platform = helper.getPlatform();
11+
var arch = helper.getArch();
12+
if (platform === 'darwin') {
13+
arch = 'x64';
14+
} else if (platform !== 'linux' && platform !== 'darwin') {
15+
platform = 'win32';
16+
arch = null;
17+
}
618

7-
function ZipBinary(platform, arch) {
8-
this.command = helper.getBinaryPath();
919
this.args = [];
1020

1121
this.update = function (callback) {

lib/browserStackTunnel.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
var fs = require('fs'),
22
childProcess = require('child_process'),
33
ZipBinary = require('./ZipBinary'),
4-
helper = require('./helper'),
5-
log = helper.log;
4+
Helper = require('./helper');
65

76
function BrowserStackTunnel(options) {
7+
var helper = new Helper.helper();
8+
var log = helper.log;
9+
810
if (options.path) {
911
helper.setBasePath(options.path);
1012
}
@@ -23,25 +25,7 @@ function BrowserStackTunnel(options) {
2325
}
2426
};
2527

26-
var binary;
27-
switch (helper.getPlatform()) {
28-
case 'linux':
29-
switch (helper.getArch()) {
30-
case 'x64':
31-
binary = new ZipBinary('linux', 'x64');
32-
break;
33-
case 'ia32':
34-
binary = new ZipBinary('linux', 'ia32');
35-
break;
36-
}
37-
break;
38-
case 'darwin':
39-
binary = new ZipBinary('darwin', 'x64');
40-
break;
41-
default:
42-
binary = new ZipBinary('win32', null);
43-
break;
44-
}
28+
var binary = new ZipBinary();
4529

4630
this.stdoutData = '';
4731
this.tunnel = null;
@@ -159,7 +143,7 @@ function BrowserStackTunnel(options) {
159143
this._startTunnel = function () {
160144
this.cleanUp();
161145
log.info('Local started with args: ' + JSON.stringify(params));
162-
this.tunnel = childProcess.spawn(binary.command, binary.args.concat([options.key]).concat(params));
146+
this.tunnel = childProcess.spawn(helper.getBinaryPath(), binary.args.concat([options.key]).concat(params));
163147
this.tunnel.stdout.on('data', this.updateState.bind(this));
164148
this.tunnel.stderr.on('data', this.updateState.bind(this));
165149
this.tunnel.on('error', this.killTunnel.bind(this));

lib/helper.js

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,54 @@ var fs = require('fs'),
33
log = require('npmlog'),
44
os = require('os');
55

6-
const osHomedir = require('os-homedir');
7-
var basePath = osHomedir(),
8-
appendLogs = true;
6+
function helper() {
7+
const osHomedir = require('os-homedir');
8+
var basePath = osHomedir(),
9+
appendLogs = true;
910

10-
exports.getPlatform = function() {
11-
return os.platform();
12-
};
13-
exports.gerArch = function() {
14-
return os.arch();
15-
};
11+
this.getPlatform = function() {
12+
return os.platform();
13+
};
14+
this.getArch = function() {
15+
return os.arch();
16+
};
1617

17-
const localBinaryName = 'BrowserStackLocal' + ( exports.getPlatform() === 'win32' ? '.exe' : '' );
18-
const zipName = 'BrowserStackLocal.zip';
19-
const logFileName = 'local.log';
18+
const localBinaryName = 'BrowserStackLocal' + ( this.getPlatform() === 'win32' ? '.exe' : '' );
19+
const zipName = 'BrowserStackLocal.zip';
20+
const logFileName = 'local.log';
2021

21-
exports.getBinaryPath = function() {
22-
return path.resolve(path.join(basePath, localBinaryName));
23-
};
24-
exports.getZipPath = function() {
25-
return path.resolve(path.join(basePath, zipName));
26-
};
27-
exports.setBasePath = function(path) {
28-
basePath = path;
29-
};
30-
exports.getBasePath = function() {
31-
return basePath;
32-
};
22+
this.getBinaryPath = function() {
23+
return path.resolve(path.join(basePath, localBinaryName));
24+
};
25+
this.getZipPath = function() {
26+
return path.resolve(path.join(basePath, zipName));
27+
};
28+
this.setBasePath = function(path) {
29+
basePath = path;
30+
};
31+
this.getBasePath = function() {
32+
return basePath;
33+
};
3334

34-
if(process.env.NODE_ENV === 'testing') {
35-
log.level = 'silent';
36-
appendLogs = false;
35+
if(process.env.NODE_ENV === 'testing') {
36+
log.level = 'silent';
37+
appendLogs = false;
38+
}
39+
this.log = log;
40+
41+
var getLogFilePath = function() {
42+
return path.resolve(path.join(basePath, logFileName));
43+
};
44+
this.logBinaryOutput = function(log) {
45+
if(appendLogs) {
46+
fs.appendFile(getLogFilePath(), log, function (err) {
47+
if(err) {
48+
exports.log.warn('Error Ssaving log file to ' + getLogFilePath());
49+
appendLogs = false;
50+
}
51+
});
52+
}
53+
};
3754
}
38-
exports.log = log;
3955

40-
var getLogFilePath = function() {
41-
return path.resolve(path.join(basePath, logFileName));
42-
};
43-
exports.logBinaryOutput = function(log) {
44-
if(appendLogs) {
45-
fs.appendFile(getLogFilePath(), log, function (err) {
46-
if(err) {
47-
exports.log.warn('Error Ssaving log file to ' + getLogFilePath());
48-
appendLogs = false;
49-
}
50-
});
51-
}
52-
};
56+
exports.helper = helper;

test/ZipBinary.js

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@ var path = require('path');
44
var mocks = require('mocks'),
55
httpMock = require('./lib/mocks').httpMock,
66
fsMock = require('./lib/mocks').fsMock,
7-
unzipMock = require('./lib/mocks').unzipMock;
7+
unzipMock = require('./lib/mocks').unzipMock,
8+
sinon = require('sinon');
89

9-
var zb = mocks.loadFile('./lib/ZipBinary.js', {
10-
https: httpMock,
11-
fs: fsMock,
12-
unzip: unzipMock
13-
});
14-
var ZipBinary = zb.ZipBinary;
15-
16-
var PLATFORM = 'platform';
17-
var ARCH = 'arch';
10+
var PLATFORM = 'linux';
11+
var ARCH = 'x64';
1812
var EXT = 'exe';
1913
var DEFAULT_BINARY_DIR = path.resolve(path.join(__dirname, '../bin', PLATFORM, ARCH));
2014
var DEFAULT_BINARY_DIR_NO_ARCH = path.resolve(path.join(__dirname, '../bin', PLATFORM));
@@ -25,44 +19,69 @@ var OTHER_BINARY_DIR = '/bin';
2519
var OTHER_BINARY_FILE = path.join(OTHER_BINARY_DIR, 'BrowserStackLocal');
2620
var OTHER_BINARY_FILE_WITH_EXT = path.join(OTHER_BINARY_DIR, 'BrowserStackLocal.' + EXT);
2721
var ZIP_URL = 'https://www.browserstack.com/browserstack-local/BrowserStackLocal-' + PLATFORM + '-' + ARCH + '.zip';
28-
var ZIP_URL_NO_ARCH = 'https://www.browserstack.com/browserstack-local/BrowserStackLocal-' + PLATFORM + '.zip';
22+
var ZIP_URL_NO_ARCH = 'https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip';
2923

3024
describe('ZipBinary', function () {
31-
var zipBinary;
25+
var zipBinary, ZipBinary, platformMock, archMock, binaryPathMock, zipPathMock,
26+
logBinaryOutputMock, warnLogMock, infoLogMock, helper, basePathMock;
3227

3328
beforeEach(function () {
3429
fsMock.fileNameModded = undefined;
3530
fsMock.mode = undefined;
3631
unzipMock.dirName = undefined;
3732
httpMock.url = undefined;
38-
});
39-
40-
describe('with default binary path', function () {
41-
beforeEach(function () {
42-
zipBinary = new ZipBinary(PLATFORM, ARCH);
43-
});
44-
45-
it('should have the correct path', function () {
46-
expect(zipBinary.path).to.equal(DEFAULT_BINARY_FILE);
47-
});
48-
49-
describe('with extension', function () {
50-
it('should have the correct path', function () {
51-
zipBinary = new ZipBinary(PLATFORM, ARCH, null, EXT);
52-
expect(zipBinary.path).to.equal(DEFAULT_BINARY_FILE_WITH_EXT);
53-
});
54-
});
5533

56-
it('should have the correct command', function () {
57-
expect(zipBinary.command).to.equal(DEFAULT_BINARY_FILE);
34+
platformMock = sinon.stub();
35+
archMock = sinon.stub();
36+
binaryPathMock = sinon.stub();
37+
zipPathMock = sinon.stub();
38+
logBinaryOutputMock = sinon.stub();
39+
warnLogMock = sinon.stub();
40+
infoLogMock = sinon.stub();
41+
basePathMock = sinon.stub();
42+
43+
helper = {
44+
helper: function() {
45+
this._basePath = 'default';
46+
47+
this.getPlatform = platformMock;
48+
this.getArch = archMock;
49+
this.getBinaryPath = binaryPathMock;
50+
this.getZipPath = zipPathMock;
51+
this.logBinaryOutput = logBinaryOutputMock;
52+
this.setBasePath = function(path) {
53+
console.log("CALLED");
54+
this._basePath = path
55+
};
56+
this.getBasePath = basePathMock;
57+
this.log = {
58+
warn: warnLogMock,
59+
info: infoLogMock
60+
};
61+
}
62+
};
63+
var zb = mocks.loadFile('./lib/ZipBinary.js', {
64+
https: httpMock,
65+
fs: fsMock,
66+
unzip: unzipMock,
67+
'./helper': helper
5868
});
69+
ZipBinary = zb.ZipBinary;
70+
});
5971

72+
describe('with default binary path', function () {
6073
it('should have the correct args', function () {
74+
zipBinary = new ZipBinary();
6175
expect(zipBinary.args).to.eql([]);
6276
});
6377

6478
describe('#update', function () {
6579
it('should download the zip file', function (done) {
80+
platformMock.returns('linux');
81+
archMock.returns('x64');
82+
basePathMock.returns(DEFAULT_BINARY_DIR);
83+
binaryPathMock.returns(DEFAULT_BINARY_FILE);
84+
zipBinary = new ZipBinary();
6685
zipBinary.update(function () {
6786
expect(fsMock.fileNameModded).to.equal(DEFAULT_BINARY_FILE);
6887
expect(fsMock.mode).to.equal('0755');
@@ -74,7 +93,11 @@ describe('ZipBinary', function () {
7493

7594
describe('with no arch', function () {
7695
it('should download the zip file', function (done) {
77-
zipBinary = new ZipBinary(PLATFORM);
96+
platformMock.returns('win32');
97+
archMock.returns('');
98+
basePathMock.returns(DEFAULT_BINARY_DIR_NO_ARCH);
99+
binaryPathMock.returns(DEFAULT_BINARY_FILE_NO_ARCH);
100+
zipBinary = new ZipBinary();
78101
zipBinary.update(function () {
79102
expect(fsMock.fileNameModded).to.equal(DEFAULT_BINARY_FILE_NO_ARCH);
80103
expect(fsMock.mode).to.equal('0755');
@@ -88,31 +111,18 @@ describe('ZipBinary', function () {
88111
});
89112

90113
describe('with given binary path', function () {
91-
beforeEach(function () {
92-
zipBinary = new ZipBinary(PLATFORM, ARCH, OTHER_BINARY_DIR);
93-
});
94-
95-
it('should have the correct path', function () {
96-
expect(zipBinary.path).to.equal(OTHER_BINARY_FILE);
97-
});
98-
99-
describe('with extension', function () {
100-
it('should have the correct path', function () {
101-
zipBinary = new ZipBinary(PLATFORM, ARCH, OTHER_BINARY_DIR, EXT);
102-
expect(zipBinary.path).to.equal(OTHER_BINARY_FILE_WITH_EXT);
103-
});
104-
});
105-
106-
it('should have the correct command', function () {
107-
expect(zipBinary.command).to.equal(OTHER_BINARY_FILE);
108-
});
109-
110114
it('should have the correct args', function () {
115+
zipBinary = new ZipBinary();
111116
expect(zipBinary.args).to.eql([]);
112117
});
113118

114119
describe('#update', function () {
115120
it('should download the zip file', function (done) {
121+
platformMock.returns('linux');
122+
archMock.returns('x64');
123+
basePathMock.returns(OTHER_BINARY_DIR);
124+
binaryPathMock.returns(OTHER_BINARY_FILE);
125+
zipBinary = new ZipBinary();
116126
zipBinary.update(function () {
117127
expect(fsMock.fileNameModded).to.equal(OTHER_BINARY_FILE);
118128
expect(fsMock.mode).to.equal('0755');
@@ -124,7 +134,11 @@ describe('ZipBinary', function () {
124134

125135
describe('with no arch', function () {
126136
it('should download the zip file', function (done) {
127-
zipBinary = new ZipBinary(PLATFORM, null, OTHER_BINARY_DIR);
137+
platformMock.returns('win32');
138+
archMock.returns('');
139+
basePathMock.returns(OTHER_BINARY_DIR);
140+
binaryPathMock.returns(OTHER_BINARY_FILE);
141+
zipBinary = new ZipBinary();
128142
zipBinary.update(function () {
129143
expect(fsMock.fileNameModded).to.equal(OTHER_BINARY_FILE);
130144
expect(fsMock.mode).to.equal('0755');

0 commit comments

Comments
 (0)