diff --git a/bin/commands/init.js b/bin/commands/init.js index 044144c2..240e9193 100644 --- a/bin/commands/init.js +++ b/bin/commands/init.js @@ -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)); } }); } diff --git a/bin/helpers/fileHelpers.js b/bin/helpers/fileHelpers.js index 8c437a9b..2bb9b048 100644 --- a/bin/helpers/fileHelpers.js +++ b/bin/helpers/fileHelpers.js @@ -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() }); } diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index 663ba677..8b6fc253 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -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); +} diff --git a/test/unit/bin/commands/init.js b/test/unit/bin/commands/init.js index fe568fa1..1d0fd8d9 100644 --- a/test/unit/bin/commands/init.js +++ b/test/unit/bin/commands/init.js @@ -17,6 +17,7 @@ describe("init", () => { before(() => { sandbox = sinon.createSandbox(); + configCreatedStub = sandbox.stub() sendUsageReportStub = sandbox.stub().callsFake(function () { return "end"; }); @@ -55,6 +56,7 @@ describe("init", () => { const init = proxyquire("../../../../bin/commands/init", { "../helpers/utils": { sendUsageReport: sendUsageReportStub, + configCreated: configCreatedStub }, "../helpers/fileHelpers": { fileExists: fileExistsStub, diff --git a/test/unit/bin/helpers/utils.js b/test/unit/bin/helpers/utils.js index b25b7337..47559a66 100644 --- a/test/unit/bin/helpers/utils.js +++ b/test/unit/bin/helpers/utils.js @@ -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; @@ -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); @@ -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); + }); }); });