Skip to content

Commit 1b315d2

Browse files
committed
[CYP-105] Adding unit tests
1 parent 59fd5f2 commit 1b315d2

File tree

19 files changed

+1065
-86
lines changed

19 files changed

+1065
-86
lines changed

bin/commands/info.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,28 @@ const request = require('request');
44
const config = require("../helpers/config"),
55
logger = require("../helpers/logger").winstonLogger,
66
Constants = require("../helpers/constants"),
7-
util = require("../helpers/util");
7+
utils = require("../helpers/utils");
88

99
module.exports = function info(args) {
10-
return buildInfo(args)
11-
}
12-
13-
function buildInfo(args) {
1410
let bsConfigPath = process.cwd() + args.cf;
1511

16-
util.validateBstackJson(bsConfigPath).then(function (bsConfig) {
17-
util.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
12+
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
13+
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
1814

1915
let buildId = args._[1];
2016

2117
let options = {
2218
url: config.buildUrl + buildId,
23-
method: "GET",
2419
auth: {
2520
user: bsConfig.auth.username,
2621
password: bsConfig.auth.access_key,
2722
},
2823
headers: {
29-
"User-Agent": util.getUserAgent(),
24+
"User-Agent": utils.getUserAgent(),
3025
},
3126
};
3227

33-
request(options, function (err, resp, body) {
28+
request.get(options, function (err, resp, body) {
3429
let message = null;
3530
let messageType = null;
3631
let errorCode = null;
@@ -84,11 +79,11 @@ function buildInfo(args) {
8479
logger.info(message);
8580
}
8681
}
87-
util.sendUsageReport(bsConfig, args, message, messageType, errorCode);
82+
utils.sendUsageReport(bsConfig, args, message, messageType, errorCode);
8883
})
8984
}).catch(function (err) {
9085
logger.error(err);
91-
util.setUsageReportingFlag(null, args.disableUsageReporting);
92-
util.sendUsageReport(null, args, err.message, Constants.messageTypes.ERROR, util.getErrorCodeFromErr(err));
86+
utils.setUsageReportingFlag(null, args.disableUsageReporting);
87+
utils.sendUsageReport(null, args, err.message, Constants.messageTypes.ERROR, utils.getErrorCodeFromErr(err));
9388
})
9489
}

bin/commands/init.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
const fileHelpers = require("../helpers/fileHelpers"),
33
Constants = require("../helpers/constants"),
44
logger = require("../helpers/logger").winstonLogger,
5-
util = require("../helpers/util");
5+
utils = require("../helpers/utils");
66

77
module.exports = function init(args) {
8-
return createBrowserStackConfig(args)
9-
}
10-
11-
function createBrowserStackConfig(args) {
12-
138
if (args.p) {
149
var path_to_bsconf = args.p + "/browserstack.json";
1510
} else {
@@ -24,16 +19,16 @@ function createBrowserStackConfig(args) {
2419
function allDone() {
2520
let message = Constants.userMessages.CONFIG_FILE_CREATED
2621
logger.info(message);
27-
util.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
22+
utils.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
2823
}
2924

3025
return fileHelpers.fileExists(config.path, function(exists){
3126
if (exists) {
3227
let message = Constants.userMessages.CONFIG_FILE_EXISTS;
3328
logger.error(message);
34-
util.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
29+
utils.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
3530
} else {
3631
fileHelpers.write(config, null, allDone);
3732
}
38-
})
33+
});
3934
}

bin/commands/runs.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const archiver = require("../helpers/archiver"),
88
config = require("../helpers/config"),
99
capabilityHelper = require("../helpers/capabilityHelper"),
1010
Constants = require("../helpers/constants"),
11-
util = require("../helpers/util");
11+
utils = require("../helpers/utils");
1212

1313
module.exports = function run(args) {
1414
return runCypress(args);
@@ -27,8 +27,8 @@ function deleteZip() {
2727
function runCypress(args) {
2828
let bsConfigPath = process.cwd() + args.cf;
2929

30-
util.validateBstackJson(bsConfigPath).then(function (bsConfig) {
31-
util.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
30+
utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
31+
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
3232

3333
// Validate browserstack.json values
3434
capabilityHelper.validate(bsConfig).then(function (validated) {
@@ -43,43 +43,43 @@ function runCypress(args) {
4343
// Create build
4444
build.createBuild(bsConfig, zip).then(function (message) {
4545
logger.info(message);
46-
util.sendUsageReport(bsConfig, args, message, Constants.messageTypes.SUCCESS, null);
46+
utils.sendUsageReport(bsConfig, args, message, Constants.messageTypes.SUCCESS, null);
4747
return;
4848
}).catch(function (err) {
4949
// Build creation failed
5050
logger.error(err);
51-
util.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'build_failed');
51+
utils.sendUsageReport(bsConfig, args, err, Constants.messageTypes.ERROR, 'build_failed');
5252
});
5353
}).catch(function (err) {
5454
// Zip Upload failed
5555
logger.error(err)
5656
logger.error(Constants.userMessages.ZIP_UPLOAD_FAILED)
57-
util.sendUsageReport(bsConfig, args, `${err}\n${Constants.userMessages.ZIP_UPLOAD_FAILED}`, Constants.messageTypes.ERROR, 'zip_upload_failed');
57+
utils.sendUsageReport(bsConfig, args, `${err}\n${Constants.userMessages.ZIP_UPLOAD_FAILED}`, Constants.messageTypes.ERROR, 'zip_upload_failed');
5858
}).finally(function () {
5959
deleteZip();
6060
});
6161
}).catch(function (err) {
6262
// Zipping failed
6363
logger.error(err);
6464
logger.error(Constants.userMessages.FAILED_TO_ZIP);
65-
util.sendUsageReport(bsConfig, args, `${err}\n${Constants.userMessages.FAILED_TO_ZIP}`, Constants.messageTypes.ERROR, 'zip_creation_failed');
65+
utils.sendUsageReport(bsConfig, args, `${err}\n${Constants.userMessages.FAILED_TO_ZIP}`, Constants.messageTypes.ERROR, 'zip_creation_failed');
6666
try {
6767
deleteZip();
6868
} catch (err) {
69-
util.sendUsageReport(bsConfig, args, Constants.userMessages.ZIP_DELETE_FAILED, Constants.messageTypes.ERROR, 'zip_deletion_failed');
69+
utils.sendUsageReport(bsConfig, args, Constants.userMessages.ZIP_DELETE_FAILED, Constants.messageTypes.ERROR, 'zip_deletion_failed');
7070
}
7171
});
7272
}).catch(function (err) {
7373
// browerstack.json is not valid
7474
logger.error(err);
7575
logger.error(Constants.validationMessages.NOT_VALID);
7676

77-
let error_code = util.getErrorCodeFromMsg(err);
78-
util.sendUsageReport(bsConfig, args, `${err}\n${Constants.validationMessages.NOT_VALID}`, Constants.messageTypes.ERROR, error_code);
77+
let error_code = utils.getErrorCodeFromMsg(err);
78+
utils.sendUsageReport(bsConfig, args, `${err}\n${Constants.validationMessages.NOT_VALID}`, Constants.messageTypes.ERROR, error_code);
7979
});
8080
}).catch(function (err) {
8181
logger.error(err);
82-
util.setUsageReportingFlag(null, args.disableUsageReporting);
83-
util.sendUsageReport(null, args, err.message, Constants.messageTypes.ERROR, util.getErrorCodeFromErr(err));
82+
utils.setUsageReportingFlag(null, args.disableUsageReporting);
83+
utils.sendUsageReport(null, args, err.message, Constants.messageTypes.ERROR, utils.getErrorCodeFromErr(err));
8484
})
8585
}

bin/commands/stop.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,28 @@ const request = require('request');
44
const config = require("../helpers/config"),
55
logger = require("../helpers/logger").winstonLogger,
66
Constants = require("../helpers/constants"),
7-
util = require("../helpers/util");
7+
utils = require("../helpers/utils");
88

99
module.exports = function stop(args) {
10-
return buildStop(args)
11-
}
12-
13-
function buildStop(args) {
1410
let bsConfigPath = process.cwd() + args.cf;
1511

16-
util.validateBstackJson(bsConfigPath).then(function (bsConfig) {
17-
util.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
12+
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
13+
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
1814

1915
let buildId = args._[1];
2016

2117
let options = {
2218
url: config.buildStopUrl + buildId,
23-
method: "POST",
2419
auth: {
2520
user: bsConfig.auth.username,
2621
password: bsConfig.auth.access_key,
2722
},
2823
headers: {
29-
"User-Agent": util.getUserAgent(),
24+
"User-Agent": utils.getUserAgent(),
3025
},
3126
};
3227

33-
request(options, function (err, resp, body) {
28+
request.post(options, function (err, resp, body) {
3429
let message = null;
3530
let messageType = null;
3631
let errorCode = null;
@@ -80,11 +75,11 @@ function buildStop(args) {
8075
logger.info(message);
8176
}
8277
}
83-
util.sendUsageReport(bsConfig, args, message, messageType, errorCode);
78+
utils.sendUsageReport(bsConfig, args, message, messageType, errorCode);
8479
})
8580
}).catch(function (err) {
8681
logger.error(err);
87-
util.setUsageReportingFlag(null, args.disableUsageReporting);
88-
util.sendUsageReport(null, args, err.message, Constants.messageTypes.ERROR, util.getErrorCodeFromErr(err));
82+
utils.setUsageReportingFlag(null, args.disableUsageReporting);
83+
utils.sendUsageReport(null, args, err.message, Constants.messageTypes.ERROR, utils.getErrorCodeFromErr(err));
8984
})
9085
}

bin/helpers/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const request = require('request');
44
const config = require('./config'),
55
capabilityHelper = require("../helpers/capabilityHelper"),
66
Constants = require('../helpers/constants'),
7-
util =require('../helpers/util');
7+
utils =require('../helpers/utils');
88

99
const createBuild = (bsConfig, zip) => {
1010
return new Promise(function (resolve, reject) {
@@ -17,7 +17,7 @@ const createBuild = (bsConfig, zip) => {
1717
},
1818
headers: {
1919
'Content-Type': 'application/json',
20-
"User-Agent": util.getUserAgent(),
20+
"User-Agent": utils.getUserAgent(),
2121
},
2222
body: data
2323
}

bin/helpers/fileHelpers.js

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,40 @@ const fs = require('fs-extra'),
55

66
const logger = require('./logger').winstonLogger;
77

8-
exports.isEmpty = function(path, cb) {
9-
fs.readdir(path, function(err, files) {
10-
if (err && 'ENOENT' != err.code) throw err;
11-
cb(!files || !files.length);
12-
});
13-
}
14-
15-
exports.isDirectory = function(path, cb) {
16-
fs.stat(path, function(err, stats) {
17-
if (err && 'ENOENT' != err.code) throw err;
18-
if (err) {
19-
return cb(false)
20-
}
21-
cb(stats.isDirectory())
22-
})
23-
}
24-
25-
exports.isFile = function(path, cb) {
26-
fs.stat(path, function(err, stats) {
27-
if (err && 'ENOENT' != err.code) throw err;
28-
if (err) {
29-
return cb(false)
30-
}
31-
cb(stats.isFile())
32-
})
33-
}
34-
35-
exports.mkdir = function(dir, cb) {
36-
mkdirp(dir, '0755', function(err) {
37-
if (err) throw err;
38-
logger.info("Creating directory: ./" + path.relative(process.cwd(), dir));
39-
cb && cb()
40-
})
41-
}
8+
// exports.isEmpty = function(path, cb) {
9+
// fs.readdir(path, function(err, files) {
10+
// if (err && 'ENOENT' != err.code) throw err;
11+
// cb(!files || !files.length);
12+
// });
13+
// }
14+
15+
// exports.isDirectory = function(path, cb) {
16+
// fs.stat(path, function(err, stats) {
17+
// if (err && 'ENOENT' != err.code) throw err;
18+
// if (err) {
19+
// return cb(false)
20+
// }
21+
// cb(stats.isDirectory())
22+
// })
23+
// }
24+
25+
// exports.isFile = function(path, cb) {
26+
// fs.stat(path, function(err, stats) {
27+
// if (err && 'ENOENT' != err.code) throw err;
28+
// if (err) {
29+
// return cb(false)
30+
// }
31+
// cb(stats.isFile())
32+
// })
33+
// }
34+
35+
// exports.mkdir = function(dir, cb) {
36+
// mkdirp(dir, '0755', function(err) {
37+
// if (err) throw err;
38+
// logger.info("Creating directory: ./" + path.relative(process.cwd(), dir));
39+
// cb && cb()
40+
// })
41+
// }
4242

4343
exports.write = function(f, message, cb) {
4444
message = message || 'Creating';

bin/helpers/logger.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if (!fs.existsSync(logDir)) {
1111
const winstonLoggerParams = {
1212
transports: [
1313
new winston.transports.Console({
14+
name: 'console.info',
1415
colorize: true,
1516
timestamp: function () {
1617
return `[${new Date().toLocaleString()}]`;

bin/helpers/util.js renamed to bin/helpers/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ exports.getErrorCodeFromErr = (err) => {
5353
}
5454

5555
exports.sendUsageReport = (bsConfig, args, message, message_type, error_code) => {
56+
console.log("okay")
5657
usageReporting.send({
5758
cli_args: args,
5859
message: message,

bin/helpers/zipUpload.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const config = require("./config"),
44
fs = require("fs"),
55
logger = require("./logger").winstonLogger,
66
Constants = require("./constants"),
7-
util = require("./util");
7+
utils = require("./utils");
88

99
const uploadCypressZip = (bsConfig, filePath) => {
1010
return new Promise(function (resolve, reject) {
@@ -20,7 +20,7 @@ const uploadCypressZip = (bsConfig, filePath) => {
2020
filename: 'tests'
2121
},
2222
headers: {
23-
"User-Agent": util.getUserAgent(),
23+
"User-Agent": utils.getUserAgent(),
2424
}
2525
}
2626

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "BrowserStack Cypress CLI for Cypress integration with BrowserStack's remote devices.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "nyc mocha 'test/**/*.js' --recursive --timeout 60000 --exit"
88
},
99
"author": "BrowserStack",
1010
"license": "MIT",
@@ -27,5 +27,13 @@
2727
"homepage": "https://github.com/browserstack/browserstack-cypress-cli",
2828
"bugs": {
2929
"url": "https://github.com/browserstack/browserstack-cypress-cli/issues"
30+
},
31+
"devDependencies": {
32+
"chai": "^4.2.0",
33+
"chai-as-promised": "^7.1.1",
34+
"mocha": "^7.1.2",
35+
"nyc": "^15.0.1",
36+
"proxyquire": "^2.1.3",
37+
"sinon": "^9.0.2"
3038
}
3139
}

0 commit comments

Comments
 (0)