Skip to content

Commit

Permalink
improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SagarGaniga committed Jul 10, 2020
1 parent f8477dc commit b582a14
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
8 changes: 1 addition & 7 deletions bin/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@ module.exports = function init(args) {
path: path_to_bsconf
};

function allDone() {
let message = Constants.userMessages.CONFIG_FILE_CREATED
logger.info(message);
utils.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
}

return fileHelpers.fileExists(config.path, function(exists){
if (exists) {
let message = Constants.userMessages.CONFIG_FILE_EXISTS;
logger.error(message);
utils.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
} else {
fileHelpers.write(config, null, allDone);
fileHelpers.write(config, null, utils.configCreated(args));
}
});
}
2 changes: 1 addition & 1 deletion bin/helpers/fileHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const logger = require("./logger").winstonLogger,
exports.write = function(f, message, cb) {
message = message || 'Creating';
fs.writeFile(f.path, f.file, function() {
logger.info(message + " file: ./" + path.relative(process.cwd(), f.path));
logger.info(message + " file: " + path.relative(process.cwd(), f.path));
cb && cb()
});
}
Expand Down
6 changes: 6 additions & 0 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,9 @@ exports.isAbsolute = (configPath) => {
exports.getConfigPath = (configPath) => {
return this.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath);
}

exports.configCreated = (args) => {
let message = Constants.userMessages.CONFIG_FILE_CREATED
logger.info(message);
this.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
}
2 changes: 2 additions & 0 deletions test/unit/bin/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe("init", () => {

before(() => {
sandbox = sinon.createSandbox();
configCreatedStub = sandbox.stub()
sendUsageReportStub = sandbox.stub().callsFake(function () {
return "end";
});
Expand Down Expand Up @@ -55,6 +56,7 @@ describe("init", () => {
const init = proxyquire("../../../../bin/commands/init", {
"../helpers/utils": {
sendUsageReport: sendUsageReportStub,
configCreated: configCreatedStub
},
"../helpers/fileHelpers": {
fileExists: fileExistsStub,
Expand Down
18 changes: 16 additions & 2 deletions test/unit/bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const path = require('path');

const chai = require("chai"),
expect = chai.expect,
sinon = require('sinon'),
chaiAsPromised = require("chai-as-promised");

const utils = require('../../../../bin/helpers/utils'),
constant = require('../../../../bin/helpers/constants'),
logger = require('../../../../bin/helpers/logger').winstonLogger;
logger = require('../../../../bin/helpers/logger').winstonLogger,
testObjects = require("../../support/fixtures/testObjects");

chai.use(chaiAsPromised);
logger.transports["console.info"].silent = true;
Expand Down Expand Up @@ -35,7 +37,7 @@ describe("utils", () => {
expect(utils.isParallelValid("1.2.2.2")).to.be.equal(false);
expect(utils.isParallelValid("1.456789")).to.be.equal(false);
});

it("should return false for a string which is not a number", () => {
expect(utils.isParallelValid("cypress")).to.be.equal(false);
expect(utils.isParallelValid("browserstack")).to.be.equal(false);
Expand Down Expand Up @@ -220,6 +222,18 @@ describe("utils", () => {
let configPath = "../Relative/Path"
expect(utils.getConfigPath(configPath)).to.be.eq(path.join(process.cwd(), configPath));
});
});

describe("configCreated", () => {
let args = testObjects.initSampleArgs;

it("should call sendUsageReport", () => {
sandbox = sinon.createSandbox();
sendUsageReportStub = sandbox.stub(utils, "sendUsageReport").callsFake(function () {
return "end";
});
utils.configCreated(args);
sinon.assert.calledOnce(sendUsageReportStub);
});
});
});

0 comments on commit b582a14

Please sign in to comment.