Skip to content
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

Cypress config file support #54

Merged
merged 20 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1ed4c1f
cypress config file options
suryart Sep 1, 2020
ef3f536
Making ./cypress.json as default
suryart Sep 1, 2020
ecd66ec
Adding cypress_config_filename and cypress_config_file after processi…
suryart Sep 1, 2020
0b1c6cb
Fix the description message
suryart Sep 1, 2020
8c21fd0
Merge branch 'master' into cypress_config_file_support
suryart Sep 2, 2020
c4faf12
Adding cypressConfigFilePath and userProvidedCypressConfigFile options
suryart Sep 2, 2020
ec076fd
Merge branch 'master' into cypress_config_file_support
suryart Sep 4, 2020
59080f2
Merge branch 'master' into cypress_config_file_support
suryart Sep 7, 2020
3b263bd
Fixing runs.js tests for the cypress config file changes
suryart Sep 7, 2020
3d58db3
fixing capabilityHelper tests
suryart Sep 7, 2020
3069019
updating template with cypress_config_file
suryart Sep 7, 2020
fa44af7
Adding tests for utils methods and fixing issue with method
suryart Sep 7, 2020
85dc12c
Adding test for cypress config filename set to false
suryart Sep 7, 2020
c1aed86
Adding todo when removing the cypress_proj_dir option
suryart Sep 8, 2020
4dcd7a9
Merge branch 'master' into cypress_config_file_support
suryart Sep 9, 2020
d8d560d
Merge branch 'master' into cypress_config_file_support
suryart Sep 14, 2020
71838b6
Merge branch 'master' into cypress_config_file_support
suryart Sep 21, 2020
fa97586
Adding cypressProjectDir for overriding old cypress_proj_dir key
suryart Sep 21, 2020
abc4f2f
Merge branch 'master' into cypress_config_file_support
suryart Sep 23, 2020
3886bd5
reverting the cypress_proj_dir change until we deprecate it
suryart Sep 23, 2020
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
3 changes: 3 additions & 0 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module.exports = function run(args) {
// accept the build name from command line if provided
utils.setBuildName(bsConfig, args);

// set cypress config filename
utils.setCypressConfigFilename(bsConfig, args);

//accept the local from env variable if provided
utils.setLocal(bsConfig);

Expand Down
14 changes: 11 additions & 3 deletions bin/helpers/archiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
const fs = require("fs");

const archiver = require("archiver"),
logger = require("./logger").winstonLogger;
logger = require("./logger").winstonLogger,
path = require('path');

const archiveSpecs = (runSettings, filePath) => {
return new Promise(function (resolve, reject) {
var output = fs.createWriteStream(filePath);

var cypressFolderPath = runSettings.cypress_proj_dir;
var cypressFolderPath = path.dirname(runSettings.cypressConfigFilePath);

var archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
Expand Down Expand Up @@ -38,7 +39,7 @@ const archiveSpecs = (runSettings, filePath) => {

let allowedFileTypes = [ 'js', 'json', 'txt', 'ts', 'feature', 'features', 'pdf', 'jpg', 'jpeg', 'png', 'zip' ];
allowedFileTypes.forEach(fileType => {
archive.glob(`**/*.${fileType}`, { cwd: cypressFolderPath, matchBase: true, ignore: ['**/node_modules/**', './node_modules/**', 'package-lock.json', 'package.json', 'browserstack-package.json', 'tests.zip'] });
archive.glob(`**/*.${fileType}`, { cwd: cypressFolderPath, matchBase: true, ignore: ['**/node_modules/**', './node_modules/**', 'package-lock.json', 'package.json', 'browserstack-package.json', 'tests.zip', 'cypress.json'] });
});

let packageJSON = {};
Expand All @@ -56,6 +57,13 @@ const archiveSpecs = (runSettings, filePath) => {
archive.append(packageJSONString, { name: 'browserstack-package.json' });
}

// do not add cypress.json if arg provided is false
if (runSettings.cypress_config_file && runSettings.cypress_config_filename !== 'false') {
let cypressJSON = JSON.parse(fs.readFileSync(runSettings.cypressConfigFilePath));
let cypressJSONString = JSON.stringify(cypressJSON, null, 4);
archive.append(cypressJSONString, { name: 'cypress.json' });
}

archive.finalize();
});
}
Expand Down
32 changes: 21 additions & 11 deletions bin/helpers/capabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ const caps = (bsConfig, zip) => {
obj.callbackURL = bsConfig.run_settings.callback_url;
obj.projectNotifyURL = bsConfig.run_settings.project_notify_URL;
obj.parallels = bsConfig.run_settings.parallels;

if (bsConfig.run_settings.cypress_config_filename) {
obj.cypress_config_filename = bsConfig.run_settings.cypress_config_filename;
}
}

if(obj.parallels === Constants.constants.DEFAULT_PARALLEL_MESSAGE) obj.parallels = undefined

if (obj.project) logger.log(`Project name is: ${obj.project}`);

if (obj.customBuildName) logger.log(`Build name is: ${obj.customBuildName}`);
Expand Down Expand Up @@ -106,18 +110,24 @@ const validate = (bsConfig, args) => {
// if parallels specified via arguments validate only arguments
if (!Utils.isUndefined(args) && !Utils.isUndefined(args.parallels) && !Utils.isParallelValid(args.parallels)) reject(Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION);

if (!fs.existsSync(path.join(bsConfig.run_settings.cypress_proj_dir, 'cypress.json'))) reject(Constants.validationMessages.CYPRESS_JSON_NOT_FOUND + bsConfig.run_settings.cypress_proj_dir);
// validate if config file provided exists or not when cypress_config_file provided
// validate existing cypress_proj_dir key otherwise.
let cypressConfigFilePath = bsConfig.run_settings.cypressConfigFilePath;

if (!fs.existsSync(cypressConfigFilePath) && bsConfig.run_settings.cypress_config_filename !== 'false') reject(Constants.validationMessages.CYPRESS_JSON_NOT_FOUND + cypressConfigFilePath);

try {
let cypressJson = fs.readFileSync(path.join(bsConfig.run_settings.cypress_proj_dir, 'cypress.json'));
cypressJson = JSON.parse(cypressJson);
// Cypress Json Base Url & Local true check
if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET);

// Detect if the user is not using the right directory structure, and throw an error
if (!Utils.isUndefined(cypressJson.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypress_proj_dir,cypressJson.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);

} catch (error) {
if (bsConfig.run_settings.cypress_config_filename !== 'false') {
let cypressJsonContent = fs.readFileSync(cypressConfigFilePath);
cypressJson = JSON.parse(cypressJsonContent);

// Cypress Json Base Url & Local true check
if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET);

// Detect if the user is not using the right directory structure, and throw an error
if (!Utils.isUndefined(cypressJson.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypress_proj_dir,cypressJson.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
}
} catch(error){
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
}

Expand Down
2 changes: 2 additions & 0 deletions bin/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ const cliMessages = {
PARALLEL_DESC: "The maximum number of parallels to use to run your test suite",
INFO: "Run your tests on BrowserStack.",
DESC: "Path to BrowserStack config",
CYPRESS_DESC: "Path to Cypress config file",
CONFIG_DEMAND: "config file is required",
CYPRESS_CONFIG_DEMAND: "Cypress config file is required",
BUILD_NAME: "The build name you want to use to name your test runs"
},
COMMON: {
Expand Down
31 changes: 31 additions & 0 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,37 @@ exports.setBuildName = (bsConfig, args) => {
}
}

exports.searchForOption = (option) => {
return (process.argv.indexOf(option) > -1);
}

exports.verifyCypressConfigFileOption = () => {
let ccfOptionsSet = (this.searchForOption('-ccf') || this.searchForOption('--ccf'));
let cypressConfigFileSet = (this.searchForOption('-cypress-config-file') || this.searchForOption('--cypress-config-file'));
let cypressConfigOptionsSet = (this.searchForOption('-cypressConfigFile') || this.searchForOption('--cypressConfigFile'));
return (ccfOptionsSet || cypressConfigFileSet || cypressConfigOptionsSet);
}

// TODO: Remove when cleaningup cypress_proj_dir
//
// 1. Remove demand from runner.js for --ccf option.
// 2. Remove the strict check functions: verifyCypressConfigFileOption
// 3. Just use the args.cypressConfigFile for checking the value for cypress config file.
exports.setCypressConfigFilename = (bsConfig, args) => {
let userProvidedCypessConfigFile = this.verifyCypressConfigFileOption();

bsConfig.run_settings.userProvidedCypessConfigFile = (userProvidedCypessConfigFile || (!this.isUndefined(bsConfig.run_settings.cypress_config_file)));

if (userProvidedCypessConfigFile || this.isUndefined(bsConfig.run_settings.cypress_config_file)) {
bsConfig.run_settings.cypress_config_file = args.cypressConfigFile;
bsConfig.run_settings.cypress_config_filename = path.basename(args.cypressConfigFile);
} else if (!this.isUndefined(bsConfig.run_settings.cypress_config_file)) {
bsConfig.run_settings.cypress_config_filename = path.basename(bsConfig.run_settings.cypress_config_file);
}

bsConfig.run_settings.cypressConfigFilePath = bsConfig.run_settings.userProvidedCypessConfigFile ? bsConfig.run_settings.cypress_config_file : path.join(bsConfig.run_settings.cypress_proj_dir, 'cypress.json');
}

exports.isUndefined = value => (value === undefined || value === null);

exports.isFloat = value => (Number(value) && Number(value) % 1 !== 0);
Expand Down
9 changes: 9 additions & 0 deletions bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ var argv = yargs
demand: true,
demand: Constants.cliMessages.RUN.CONFIG_DEMAND
},
'ccf': {
alias: 'cypress-config-file',
describe: Constants.cliMessages.RUN.CYPRESS_DESC,
default: './cypress.json',
type: 'string',
nargs: 1,
demand: true,
demand: Constants.cliMessages.RUN.CYPRESS_CONFIG_DEMAND
},
'disable-usage-reporting': {
default: undefined,
description: Constants.cliMessages.COMMON.DISABLE_USAGE_REPORTING,
Expand Down
2 changes: 1 addition & 1 deletion bin/templates/configTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = function () {
}
],
"run_settings": {
"cypress_proj_dir" : "/path/to/directory-that-contains-<cypress.json>-file",
"cypress_config_file" : "/path/to/<cypress config file>.json",
"project_name": "project-name",
"build_name": "build-name",
"parallels": "Here goes the number of parallels you want to run",
Expand Down
10 changes: 10 additions & 0 deletions test/unit/bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe("runs", () => {
setUsernameStub = sandbox.stub();
setAccessKeyStub = sandbox.stub();
setBuildNameStub = sandbox.stub();
setCypressConfigFilenameStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
sendUsageReportStub = sandbox.stub().callsFake(function () {
Expand Down Expand Up @@ -116,6 +117,7 @@ describe("runs", () => {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
setBuildName: setBuildNameStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
Expand Down Expand Up @@ -165,6 +167,7 @@ describe("runs", () => {
setAccessKeyStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setBuildNameStub = sandbox.stub();
setCypressConfigFilenameStub = sandbox.stub();
validateBstackJsonStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
sendUsageReportStub = sandbox.stub().callsFake(function () {
Expand Down Expand Up @@ -196,6 +199,7 @@ describe("runs", () => {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
setBuildName: setBuildNameStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
Expand Down Expand Up @@ -255,6 +259,7 @@ describe("runs", () => {
setUsernameStub = sandbox.stub();
setAccessKeyStub = sandbox.stub();
setBuildNameStub = sandbox.stub();
setCypressConfigFilenameStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
sendUsageReportStub = sandbox.stub().callsFake(function () {
Expand Down Expand Up @@ -287,6 +292,7 @@ describe("runs", () => {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
setBuildName: setBuildNameStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
Expand Down Expand Up @@ -353,6 +359,7 @@ describe("runs", () => {
setUsernameStub = sandbox.stub();
setAccessKeyStub = sandbox.stub();
setBuildNameStub = sandbox.stub();
setCypressConfigFilenameStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
sendUsageReportStub = sandbox.stub().callsFake(function () {
Expand Down Expand Up @@ -386,6 +393,7 @@ describe("runs", () => {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
setBuildName: setBuildNameStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
Expand Down Expand Up @@ -463,6 +471,7 @@ describe("runs", () => {
setUsernameStub = sandbox.stub();
setAccessKeyStub = sandbox.stub();
setBuildNameStub = sandbox.stub();
setCypressConfigFilenameStub = sandbox.stub();
getConfigPathStub = sandbox.stub();
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
sendUsageReportStub = sandbox.stub().callsFake(function () {
Expand Down Expand Up @@ -499,6 +508,7 @@ describe("runs", () => {
setUsername: setUsernameStub,
setAccessKey: setAccessKeyStub,
setBuildName: setBuildNameStub,
setCypressConfigFilename: setCypressConfigFilenameStub,
setUsageReportingFlag: setUsageReportingFlagStub,
setParallels: setParallelsStub,
getConfigPath: getConfigPathStub,
Expand Down
37 changes: 35 additions & 2 deletions test/unit/bin/helpers/capabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ describe("capabilityHelper.js", () => {
},
],
run_settings: {
cypress_proj_dir: "random path"
cypress_proj_dir: "random path",
cypressConfigFilePath: "random path"
},
};
});
Expand Down Expand Up @@ -651,7 +652,8 @@ describe("capabilityHelper.js", () => {
},
],
run_settings: {
cypress_proj_dir: "random path"
cypress_proj_dir: "random path",
cypressConfigFilePath: "random path"
},
};
});
Expand Down Expand Up @@ -732,6 +734,37 @@ describe("capabilityHelper.js", () => {
fs.readFileSync.restore();
});
});

context("cypress config file set to false", () => {
beforeEach(function() {
readFileSpy = sinon.stub(fs, 'readFileSync');
jsonParseSpy = sinon.stub(JSON, 'parse');
});

afterEach(function() {
readFileSpy.restore();
jsonParseSpy.restore();
});

it("does not validate with cypress config filename set to false", () => {
// sinon.stub(fs, 'existsSync').returns(false);
bsConfig.run_settings.cypressConfigFilePath = 'false';
bsConfig.run_settings.cypress_config_filename = 'false';

return capabilityHelper
.validate(bsConfig, {})
.then(function (data) {
sinon.assert.notCalled(readFileSpy);
sinon.assert.notCalled(jsonParseSpy);
})
.catch((error) => {
chai.assert.equal(
error,
Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE
);
});
})
});
});
});
});
Loading