Skip to content

Handle absolute path and verify init path #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/commands/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = require("../helpers/config"),
utils = require("../helpers/utils");

module.exports = function info(args) {
let bsConfigPath = process.cwd() + args.cf;
let bsConfigPath = utils.getConfigPath(args.cf);

return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
// accept the username from command line if provided
Expand Down
32 changes: 18 additions & 14 deletions bin/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
const fileHelpers = require("../helpers/fileHelpers"),
Constants = require("../helpers/constants"),
logger = require("../helpers/logger").winstonLogger,
utils = require("../helpers/utils");
utils = require("../helpers/utils"),
util = require("util"),
path = require('path');

module.exports = function init(args) {
if (args.p) {
var path_to_bsconf = args.p + "/browserstack.json";
var path_to_bsconf = path.join(args.p + "/browserstack.json");
} else {
var path_to_bsconf = "./browserstack.json";
}
Expand All @@ -16,19 +18,21 @@ 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');
return fileHelpers.dirExists(config.path, function(dirExists){
if (dirExists) {
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, args, utils.configCreated);
}
});
} else {
fileHelpers.write(config, null, allDone);
let message = util.format(Constants.userMessages.DIR_NOT_FOUND, path.dirname(config.path));
logger.error(message);
utils.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'path_to_init_not_found');
}
});
}
2 changes: 1 addition & 1 deletion bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const archiver = require("../helpers/archiver"),
fileHelpers = require("../helpers/fileHelpers");

module.exports = function run(args) {
let bsConfigPath = process.cwd() + args.cf;
let bsConfigPath = utils.getConfigPath(args.cf);

return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
Expand Down
2 changes: 1 addition & 1 deletion bin/commands/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = require("../helpers/config"),
utils = require("../helpers/utils");

module.exports = function stop(args) {
let bsConfigPath = process.cwd() + args.cf;
let bsConfigPath = utils.getConfigPath(args.cf);

return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
// accept the username from command line if provided
Expand Down
1 change: 1 addition & 0 deletions bin/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const userMessages = {
ZIP_UPLOAD_FAILED: "Zip Upload failed.",
CONFIG_FILE_CREATED: "BrowserStack Config File created, you can now run browserstack-cypress --config-file run",
CONFIG_FILE_EXISTS: "File already exists, delete the browserstack.json file manually. skipping...",
DIR_NOT_FOUND: "Given path does not exist. Failed to create browserstack.json in %s",
ZIP_DELETE_FAILED: "Could not delete local file.",
ZIP_DELETED: "Zip file deleted successfully.",
API_DEPRECATED: "This version of API is deprecated, please use latest version of API.",
Expand Down
14 changes: 11 additions & 3 deletions bin/helpers/fileHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const logger = require("./logger").winstonLogger,
Constants = require("../helpers/constants"),
config = require("../helpers/config");

exports.write = function(f, message, cb) {
exports.write = function(f, message, args, cb) {
message = message || 'Creating';
fs.writeFile(f.path, f.file, function() {
logger.info(message + " file: ./" + path.relative(process.cwd(), f.path));
cb && cb()
logger.info(message + " file: " + f.path);
cb && cb(args)
});
}

Expand All @@ -35,3 +35,11 @@ exports.deleteZip = () => {
}
});
}

exports.dirExists = function (filePath, cb) {
let exists = false;
if (fs.existsSync(path.dirname(filePath), cb)) {
exists = true;
}
cb && cb(exists);
}
15 changes: 15 additions & 0 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const os = require("os");
const path = require("path");

const usageReporting = require('./usageReporting'),
logger = require('./logger').winstonLogger,
Expand Down Expand Up @@ -105,3 +106,17 @@ exports.isParallelValid = (value) => {
exports.getUserAgent = () => {
return `BStack-Cypress-CLI/1.2.0 (${os.arch()}/${os.platform()}/${os.release()})`;
}

exports.isAbsolute = (configPath) => {
return path.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);
}
6 changes: 3 additions & 3 deletions bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var argv = yargs
'cf': {
alias: 'config-file',
describe: Constants.cliMessages.BUILD.DESC,
default: '/browserstack.json',
default: 'browserstack.json',
type: 'string',
nargs: 1,
demand: true,
Expand Down Expand Up @@ -91,7 +91,7 @@ var argv = yargs
'cf': {
alias: 'config-file',
describe: Constants.cliMessages.BUILD.DESC,
default: '/browserstack.json',
default: 'browserstack.json',
type: 'string',
nargs: 1,
demand: true,
Expand Down Expand Up @@ -130,7 +130,7 @@ var argv = yargs
'cf': {
alias: 'config-file',
describe: Constants.cliMessages.RUN.DESC,
default: '/browserstack.json',
default: 'browserstack.json',
type: 'string',
nargs: 1,
demand: true,
Expand Down
17 changes: 17 additions & 0 deletions test/unit/bin/commands/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe("buildInfo", () => {
setUsernameStub = sandbox.stub();
setAccessKeyStub = sandbox.stub();
validateBstackJsonStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
getUserAgentStub = sandbox.stub().returns("random user-agent");
sendUsageReportStub = sandbox.stub().callsFake(function () {
Expand Down Expand Up @@ -54,6 +55,7 @@ describe("buildInfo", () => {
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
},
request: { get: requestStub },
});
Expand All @@ -63,6 +65,7 @@ describe("buildInfo", () => {
return info(args)
.then(function (_bsConfig) {
sinon.assert.calledOnce(requestStub);
sinon.assert.calledOnce(getConfigPathStub);
sinon.assert.calledOnce(getUserAgentStub);
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
}).catch((error) => {
Expand All @@ -88,6 +91,7 @@ describe("buildInfo", () => {
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
},
request: { get: requestStub },
});
Expand All @@ -98,6 +102,7 @@ describe("buildInfo", () => {
.then(function (_bsConfig) {
sinon.assert.calledOnce(requestStub);
sinon.assert.calledOnce(getUserAgentStub);
sinon.assert.calledOnce(getConfigPathStub);
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
})
.catch((error) => {
Expand All @@ -112,6 +117,7 @@ describe("buildInfo", () => {
setUsernameStub = sandbox.stub();
setAccessKeyStub = sandbox.stub();
validateBstackJsonStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
getUserAgentStub = sandbox.stub().returns("random user-agent");
sendUsageReportStub = sandbox.stub().callsFake(function () {
Expand Down Expand Up @@ -143,6 +149,7 @@ describe("buildInfo", () => {
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
},
request: { get: requestStub },
});
Expand All @@ -153,6 +160,7 @@ describe("buildInfo", () => {
.then(function (_bsConfig) {
sinon.assert.calledOnce(requestStub);
sinon.assert.calledOnce(getUserAgentStub);
sinon.assert.calledOnce(getConfigPathStub);
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
})
.catch((error) => {
Expand Down Expand Up @@ -183,6 +191,7 @@ describe("buildInfo", () => {
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
},
request: { get: requestStub },
});
Expand All @@ -193,6 +202,7 @@ describe("buildInfo", () => {
.then(function (_bsConfig) {
sinon.assert.calledOnce(requestStub);
sinon.assert.calledOnce(getUserAgentStub);
sinon.assert.calledOnce(getConfigPathStub);
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
})
.catch((error) => {
Expand All @@ -218,6 +228,7 @@ describe("buildInfo", () => {
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
},
request: { get: requestStub },
});
Expand All @@ -228,6 +239,7 @@ describe("buildInfo", () => {
.then(function (_bsConfig) {
sinon.assert.calledOnce(requestStub);
sinon.assert.calledOnce(getUserAgentStub);
sinon.assert.calledOnce(getConfigPathStub);
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
})
.catch((error) => {
Expand All @@ -244,6 +256,7 @@ describe("buildInfo", () => {
setUsernameStub = sandbox.stub();
setAccessKeyStub = sandbox.stub();
validateBstackJsonStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
getUserAgentStub = sandbox.stub().returns("random user-agent");
sendUsageReportStub = sandbox.stub().callsFake(function () {
Expand Down Expand Up @@ -273,6 +286,7 @@ describe("buildInfo", () => {
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getUserAgent: getUserAgentStub,
getConfigPath: getConfigPathStub
},
request: { get: requestStub },
});
Expand All @@ -283,6 +297,7 @@ describe("buildInfo", () => {
.then(function (_bsConfig) {
sinon.assert.calledOnce(requestStub);
sinon.assert.calledOnce(getUserAgentStub);
sinon.assert.calledOnce(getConfigPathStub);
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
}).catch((error) => {
chai.assert.isNotOk(error,'Promise error');
Expand All @@ -297,6 +312,7 @@ describe("buildInfo", () => {
sandbox = sinon.createSandbox();
setUsernameStub = sandbox.stub();
setAccessKeyStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
validateBstackJsonStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
sendUsageReportStub = sandbox.stub().callsFake(function () {
Expand All @@ -319,6 +335,7 @@ describe("buildInfo", () => {
getErrorCodeFromErr: getErrorCodeFromErrStub,
sendUsageReport: sendUsageReportStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getConfigPath: getConfigPathStub
},
});

Expand Down
41 changes: 38 additions & 3 deletions test/unit/bin/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const chai = require("chai"),
sinon = require("sinon"),
chaiAsPromised = require("chai-as-promised");
chaiAsPromised = require("chai-as-promised"),
util = require("util");

const Constants = require("../../../../bin/helpers/constants"),
logger = require("../../../../bin/helpers/logger").winstonLogger,
Expand All @@ -15,20 +16,48 @@ describe("init", () => {
let args = testObjects.initSampleArgs;
var sandbox;

before(() => {
beforeEach(() => {
sandbox = sinon.createSandbox();
configCreatedStub = sandbox.stub()
sendUsageReportStub = sandbox.stub().callsFake(function () {
return "end";
});
});

after(() => {
afterEach(() => {
sandbox.restore();
sinon.restore();
});

describe("init", () => {
it("fail if given path is not present", () => {
dirExistsStub = sandbox.stub().yields(false);
writeStub = sandbox.stub();
formatStub = sandbox.stub().callsFake(function (args) {
return args;
});

const init = proxyquire("../../../../bin/commands/init", {
"../helpers/utils": {
sendUsageReport: sendUsageReportStub,
},
"../helpers/fileHelpers": {
dirExists: dirExistsStub,
write: writeStub,
},
"util": {
format: formatStub
}
});

init(args);
sinon.assert.calledOnce(dirExistsStub);
sinon.assert.notCalled(writeStub);
sinon.assert.calledOnceWithExactly(sendUsageReportStub, null, args, Constants.userMessages.DIR_NOT_FOUND, Constants.messageTypes.ERROR, 'path_to_init_not_found');
});

it("fail if browserstack.json is already present", () => {
dirExistsStub = sandbox.stub().yields(true);
fileExistsStub = sandbox.stub().yields(true);
writeStub = sandbox.stub();

Expand All @@ -37,32 +66,38 @@ describe("init", () => {
sendUsageReport: sendUsageReportStub,
},
"../helpers/fileHelpers": {
dirExists: dirExistsStub,
fileExists: fileExistsStub,
write: writeStub,
},
});

init(args);
sinon.assert.calledOnce(dirExistsStub);
sinon.assert.calledOnce(fileExistsStub);
sinon.assert.notCalled(writeStub);
sinon.assert.calledOnceWithExactly(sendUsageReportStub, null, args, Constants.userMessages.CONFIG_FILE_EXISTS, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
});

it("create browserstack.json if not already present", () => {
dirExistsStub = sandbox.stub().yields(true);
fileExistsStub = sandbox.stub().yields(false);
writeStub = sandbox.stub();

const init = proxyquire("../../../../bin/commands/init", {
"../helpers/utils": {
sendUsageReport: sendUsageReportStub,
configCreated: configCreatedStub
},
"../helpers/fileHelpers": {
dirExists: dirExistsStub,
fileExists: fileExistsStub,
write: writeStub,
},
});

init(args);
sinon.assert.calledOnce(dirExistsStub);
sinon.assert.calledOnce(fileExistsStub);
sinon.assert.calledOnce(writeStub);
});
Expand Down
Loading