From abea95a63ffb00ae445861cfa17451b00210f20d Mon Sep 17 00:00:00 2001 From: Samarjeet Date: Tue, 21 Nov 2023 17:35:15 +0530 Subject: [PATCH 1/3] Omit CLI args on local runs --- bin/helpers/constants.js | 2 +- bin/helpers/runnerArgs.js | 203 +++++++++++++++++++++++++ bin/runner.js | 203 +------------------------ bin/testObservability/helper/helper.js | 42 ++++- 4 files changed, 244 insertions(+), 206 deletions(-) create mode 100644 bin/helpers/runnerArgs.js diff --git a/bin/helpers/constants.js b/bin/helpers/constants.js index 70dac61c..0dc748f4 100644 --- a/bin/helpers/constants.js +++ b/bin/helpers/constants.js @@ -135,7 +135,7 @@ const validationMessages = { EMPTY_CYPRESS_CONFIG_FILE: "cypress_config_file is not set in run_settings. See https://www.browserstack.com/docs/automate/cypress/configuration-file to learn more.", VALIDATED: "browserstack.json file is validated", - NOT_VALID: "browerstack.json is not valid", + NOT_VALID: "browserstack.json is not valid", NOT_VALID_JSON: "browerstack.json is not a valid json", INVALID_EXTENSION: "Invalid files, please remove these files and try again.", INVALID_PARALLELS_CONFIGURATION: diff --git a/bin/helpers/runnerArgs.js b/bin/helpers/runnerArgs.js new file mode 100644 index 00000000..860f403f --- /dev/null +++ b/bin/helpers/runnerArgs.js @@ -0,0 +1,203 @@ +const Constants = require('./constants'); + +const disableUsageReportingOptions = { + 'disable-usage-reporting': { + default: undefined, + description: Constants.cliMessages.COMMON.DISABLE_USAGE_REPORTING, + type: "boolean" + }, +} + +const usernameOptions = { + 'u': { + alias: 'username', + describe: Constants.cliMessages.COMMON.USERNAME, + type: "string", + default: undefined + }, +} + +const accessKeyOptions = { + 'k': { + alias: 'key', + describe: Constants.cliMessages.COMMON.ACCESS_KEY, + type: "string", + default: undefined + }, +} + +const configFileOptions = { + 'cf': { + alias: 'config-file', + describe: Constants.cliMessages.COMMON.CONFIG_FILE_PATH, + default: 'browserstack.json', + type: 'string', + nargs: 1, + demand: true, + demand: Constants.cliMessages.COMMON.CONFIG_DEMAND + }, +} + +const debugModeOptions = { + 'cli-debug': { + default: false, + describe: Constants.cliMessages.COMMON.DEBUG, + type: "boolean" + }, +} + +exports.commonBuildOptions = { + ...configFileOptions, + ...disableUsageReportingOptions, + ...usernameOptions, + ...accessKeyOptions, + ...debugModeOptions, +} + +exports.runOptions = { + ...this.commonBuildOptions, + '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 + }, + 'gl': { + alias: 'geolocation', + describe: Constants.cliMessages.RUN.CYPRESS_GEO_LOCATION, + default: undefined, + type: 'string' + }, + 'p': { + alias: ['parallels', 'parallel'], + describe: Constants.cliMessages.RUN.PARALLEL_DESC, + type: "number", + default: undefined + }, + 'b': { + alias: ['build-name', 'ci-build-id'], + describe: Constants.cliMessages.RUN.BUILD_NAME, + type: "string", + default: undefined + }, + 'e': { + alias: 'exclude', + describe: Constants.cliMessages.RUN.EXCLUDE, + type: "string", + default: undefined + }, + 's': { + alias: ['specs', 'spec'], + describe: Constants.cliMessages.RUN.SPECS_DESCRIPTION, + type: "string", + default: undefined + }, + 'env': { + describe: Constants.cliMessages.RUN.ENV_DESCRIPTION, + type: "string", + default: undefined + }, + 't': { + alias: ['specTimeout'], + default: undefined, + describe: Constants.cliMessages.RUN.SPEC_TIMEOUT, + type: "string" + }, + 'disable-npm-warning': { + default: false, + description: Constants.cliMessages.COMMON.NO_NPM_WARNING, + type: "boolean" + }, + 'sync': { + default: true, + describe: Constants.cliMessages.RUN.SYNC_DESCRIPTION, + type: "boolean" + }, + 'async': { + default: false, + describe: Constants.cliMessages.RUN.ASYNC_DESCRIPTION, + type: "boolean" + }, + 'force-upload': { + default: false, + describe: Constants.cliMessages.COMMON.FORCE_UPLOAD, + type: "boolean" + }, + 'headed': { + default: false, + describe: Constants.cliMessages.RUN.HEADED, + type: "boolean" + }, + 'local': { + describe: Constants.cliMessages.RUN.LOCAL, + type: "boolean" + }, + 'local-identifier': { + describe: Constants.cliMessages.RUN.LOCAL_IDENTIFIER, + type: "string" + }, + 'local-mode': { + describe: Constants.cliMessages.RUN.LOCAL_MODE, + type: "string" + }, + 'local-config-file': { + describe: Constants.cliMessages.RUN.LOCAL_CONFIG_FILE, + type: "string" + }, + 'no-wrap': { + default: false, + describe: Constants.cliMessages.RUN.SYNC_NO_WRAP, + type: "boolean" + }, + 'browser': { + describe: Constants.cliMessages.RUN.BROWSER_DESCRIPTION, + type: "string", + default: undefined + }, + 'c': { + alias: 'config', + describe: Constants.cliMessages.RUN.CONFIG_DESCRIPTION, + type: "string", + default: undefined + }, + 'r': { + alias: 'reporter', + default: undefined, + describe: Constants.cliMessages.RUN.REPORTER, + type: "string" + }, + 'o': { + alias: 'reporter-options', + default: undefined, + describe: Constants.cliMessages.RUN.REPORTER_OPTIONS, + type: "string" + }, + 'record': { + describe: Constants.cliMessages.RUN.RECORD, + type: "boolean" + }, + 'record-key': { + default: undefined, + describe: Constants.cliMessages.RUN.RECORD_KEY, + type: "string" + }, + 'projectId': { + default: undefined, + describe: Constants.cliMessages.RUN.PROJECT_ID, + type: "string" + }, + 'nv': { + alias: ['node-version', 'nodeVersion'], + default: undefined, + describe: Constants.cliMessages.RUN.NODE_VERSION, + type: "string" + }, + 'build-tag': { + default: undefined, + describe: Constants.cliMessages.RUN.BUILD_TAG, + type: "string" + } +} \ No newline at end of file diff --git a/bin/runner.js b/bin/runner.js index 1f93ed01..f0967597 100755 --- a/bin/runner.js +++ b/bin/runner.js @@ -2,63 +2,10 @@ 'use strict'; const yargs = require('yargs'), logger = require("./helpers/logger").winstonLogger, - Constants = require('./helpers/constants'); + Constants = require('./helpers/constants'), + { commonBuildOptions, runOptions } = require('./helpers/runnerArgs'); -const disableUsageReportingOptions = { - 'disable-usage-reporting': { - default: undefined, - description: Constants.cliMessages.COMMON.DISABLE_USAGE_REPORTING, - type: "boolean" - }, -} - -const usernameOptions = { - 'u': { - alias: 'username', - describe: Constants.cliMessages.COMMON.USERNAME, - type: "string", - default: undefined - }, -} - -const accessKeyOptions = { - 'k': { - alias: 'key', - describe: Constants.cliMessages.COMMON.ACCESS_KEY, - type: "string", - default: undefined - }, -} - -const configFileOptions = { - 'cf': { - alias: 'config-file', - describe: Constants.cliMessages.COMMON.CONFIG_FILE_PATH, - default: 'browserstack.json', - type: 'string', - nargs: 1, - demand: true, - demand: Constants.cliMessages.COMMON.CONFIG_DEMAND - }, -} - -const debugModeOptions = { - 'cli-debug': { - default: false, - describe: Constants.cliMessages.COMMON.DEBUG, - type: "boolean" - }, -} - -const commonBuildOptions = { - ...configFileOptions, - ...disableUsageReportingOptions, - ...usernameOptions, - ...accessKeyOptions, - ...debugModeOptions, -} - function checkCommands(yargs, argv, numRequired) { if (argv._.length < numRequired) { yargs.showHelp() @@ -132,151 +79,7 @@ var argv = yargs argv = yargs .usage('usage: $0 run ') .options({ - ...commonBuildOptions, - '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 - }, - 'gl': { - alias: 'geolocation', - describe: Constants.cliMessages.RUN.CYPRESS_GEO_LOCATION, - default: undefined, - type: 'string' - }, - 'p': { - alias: ['parallels', 'parallel'], - describe: Constants.cliMessages.RUN.PARALLEL_DESC, - type: "number", - default: undefined - }, - 'b': { - alias: ['build-name', 'ci-build-id'], - describe: Constants.cliMessages.RUN.BUILD_NAME, - type: "string", - default: undefined - }, - 'e': { - alias: 'exclude', - describe: Constants.cliMessages.RUN.EXCLUDE, - type: "string", - default: undefined - }, - 's': { - alias: ['specs', 'spec'], - describe: Constants.cliMessages.RUN.SPECS_DESCRIPTION, - type: "string", - default: undefined - }, - 'env': { - describe: Constants.cliMessages.RUN.ENV_DESCRIPTION, - type: "string", - default: undefined - }, - 't': { - alias: ['specTimeout'], - default: undefined, - describe: Constants.cliMessages.RUN.SPEC_TIMEOUT, - type: "string" - }, - 'disable-npm-warning': { - default: false, - description: Constants.cliMessages.COMMON.NO_NPM_WARNING, - type: "boolean" - }, - 'sync': { - default: true, - describe: Constants.cliMessages.RUN.SYNC_DESCRIPTION, - type: "boolean" - }, - 'async': { - default: false, - describe: Constants.cliMessages.RUN.ASYNC_DESCRIPTION, - type: "boolean" - }, - 'force-upload': { - default: false, - describe: Constants.cliMessages.COMMON.FORCE_UPLOAD, - type: "boolean" - }, - 'headed': { - default: false, - describe: Constants.cliMessages.RUN.HEADED, - type: "boolean" - }, - 'local': { - describe: Constants.cliMessages.RUN.LOCAL, - type: "boolean" - }, - 'local-identifier': { - describe: Constants.cliMessages.RUN.LOCAL_IDENTIFIER, - type: "string" - }, - 'local-mode': { - describe: Constants.cliMessages.RUN.LOCAL_MODE, - type: "string" - }, - 'local-config-file': { - describe: Constants.cliMessages.RUN.LOCAL_CONFIG_FILE, - type: "string" - }, - 'no-wrap': { - default: false, - describe: Constants.cliMessages.RUN.SYNC_NO_WRAP, - type: "boolean" - }, - 'browser': { - describe: Constants.cliMessages.RUN.BROWSER_DESCRIPTION, - type: "string", - default: undefined - }, - 'c': { - alias: 'config', - describe: Constants.cliMessages.RUN.CONFIG_DESCRIPTION, - type: "string", - default: undefined - }, - 'r': { - alias: 'reporter', - default: undefined, - describe: Constants.cliMessages.RUN.REPORTER, - type: "string" - }, - 'o': { - alias: 'reporter-options', - default: undefined, - describe: Constants.cliMessages.RUN.REPORTER_OPTIONS, - type: "string" - }, - 'record': { - describe: Constants.cliMessages.RUN.RECORD, - type: "boolean" - }, - 'record-key': { - default: undefined, - describe: Constants.cliMessages.RUN.RECORD_KEY, - type: "string" - }, - 'projectId': { - default: undefined, - describe: Constants.cliMessages.RUN.PROJECT_ID, - type: "string" - }, - 'nv': { - alias: ['node-version', 'nodeVersion'], - default: undefined, - describe: Constants.cliMessages.RUN.NODE_VERSION, - type: "string" - }, - 'build-tag': { - default: undefined, - describe: Constants.cliMessages.RUN.BUILD_TAG, - type: "string" - } + ...runOptions, }) .help('help') .wrap(null) diff --git a/bin/testObservability/helper/helper.js b/bin/testObservability/helper/helper.js index 2588095d..a06d9795 100644 --- a/bin/testObservability/helper/helper.js +++ b/bin/testObservability/helper/helper.js @@ -9,6 +9,7 @@ const { promisify } = require('util'); const gitconfig = require('gitconfiglocal'); const { spawn, execSync } = require('child_process'); const glob = require('glob'); +const { runOptions } = require('../../helpers/runnerArgs') const pGitconfig = promisify(gitconfig); @@ -766,13 +767,44 @@ const getLocalSessionReporter = () => { } const cleanupTestObservabilityFlags = (rawArgs) => { - const newRawArgs = []; - for(let idx=0; idx { + const curr = runOptions[key]; + if (curr.alias) { + const aliases = Array.isArray(curr.alias) ? curr.alias : [curr.alias] + for (const alias of aliases) { + acc[alias] = curr; + } + } + return acc; + }, {}) + + const cliArgs = { + ...runOptions, + ...aliasMap + } + + // these flags are present in cypress too, but in some the same cli and + // cypress flags have different meaning. In that case, we assume user has + // given cypress related args + const retain = ['c', 'p', 'b', 'o', 's', 'specs', 'spec'] + + for (let i = 0;i < rawArgs.length;i++) { + const arg = rawArgs[i]; + const next = i + 1 < rawArgs.length ? rawArgs[i+1] : '' + if (arg.startsWith('-')) { + const argName = arg.length > 1 && arg[1] == '-' ? arg.slice(2) : arg.slice(1); + // If this argument belongs to cli, we omit it and its value + if (cliArgs[argName] && !retain.includes(argName)) { + if (cliArgs[argName].type && cliArgs[argName].type !== 'boolean' && !next.startsWith('-')) { + i++; + } + continue; + } } + newArgs.push(rawArgs[i]); } - return newRawArgs; + return newArgs; } exports.runCypressTestsLocally = (bsConfig, args, rawArgs) => { From e343aeb571bcba953f19c9d15a89dcf646b1289d Mon Sep 17 00:00:00 2001 From: Samarjeet Date: Tue, 21 Nov 2023 20:16:21 +0530 Subject: [PATCH 2/3] comment and eof --- bin/helpers/runnerArgs.js | 2 +- bin/testObservability/helper/helper.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/bin/helpers/runnerArgs.js b/bin/helpers/runnerArgs.js index 860f403f..28349ce6 100644 --- a/bin/helpers/runnerArgs.js +++ b/bin/helpers/runnerArgs.js @@ -200,4 +200,4 @@ exports.runOptions = { describe: Constants.cliMessages.RUN.BUILD_TAG, type: "string" } -} \ No newline at end of file +} diff --git a/bin/testObservability/helper/helper.js b/bin/testObservability/helper/helper.js index a06d9795..cc329125 100644 --- a/bin/testObservability/helper/helper.js +++ b/bin/testObservability/helper/helper.js @@ -791,12 +791,13 @@ const cleanupTestObservabilityFlags = (rawArgs) => { for (let i = 0;i < rawArgs.length;i++) { const arg = rawArgs[i]; - const next = i + 1 < rawArgs.length ? rawArgs[i+1] : '' if (arg.startsWith('-')) { const argName = arg.length > 1 && arg[1] == '-' ? arg.slice(2) : arg.slice(1); - // If this argument belongs to cli, we omit it and its value + // If this flag belongs to cli, we omit it and its value if (cliArgs[argName] && !retain.includes(argName)) { - if (cliArgs[argName].type && cliArgs[argName].type !== 'boolean' && !next.startsWith('-')) { + const nextArg = i + 1 < rawArgs.length ? rawArgs[i+1] : '' + // if the flag is bound to have a value, we ignore it + if (cliArgs[argName].type && cliArgs[argName].type !== 'boolean' && !nextArg.startsWith('-')) { i++; } continue; From c6b51f1cd36493b08f6fa2e1f357f0d8ecae49dc Mon Sep 17 00:00:00 2001 From: Samarjeet Date: Wed, 29 Nov 2023 19:08:57 +0530 Subject: [PATCH 3/3] fix missing arg --- bin/helpers/runnerArgs.js | 18 +++++++++--------- bin/runner.js | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/bin/helpers/runnerArgs.js b/bin/helpers/runnerArgs.js index 28349ce6..287f45dc 100644 --- a/bin/helpers/runnerArgs.js +++ b/bin/helpers/runnerArgs.js @@ -1,13 +1,5 @@ const Constants = require('./constants'); -const disableUsageReportingOptions = { - 'disable-usage-reporting': { - default: undefined, - description: Constants.cliMessages.COMMON.DISABLE_USAGE_REPORTING, - type: "boolean" - }, -} - const usernameOptions = { 'u': { alias: 'username', @@ -46,9 +38,17 @@ const debugModeOptions = { }, } +exports.disableUsageReportingOptions = { + 'disable-usage-reporting': { + default: undefined, + description: Constants.cliMessages.COMMON.DISABLE_USAGE_REPORTING, + type: "boolean" + }, +} + exports.commonBuildOptions = { ...configFileOptions, - ...disableUsageReportingOptions, + ...this.disableUsageReportingOptions, ...usernameOptions, ...accessKeyOptions, ...debugModeOptions, diff --git a/bin/runner.js b/bin/runner.js index f0967597..6a320ca9 100755 --- a/bin/runner.js +++ b/bin/runner.js @@ -3,7 +3,7 @@ const yargs = require('yargs'), logger = require("./helpers/logger").winstonLogger, Constants = require('./helpers/constants'), - { commonBuildOptions, runOptions } = require('./helpers/runnerArgs'); + { disableUsageReportingOptions, commonBuildOptions, runOptions } = require('./helpers/runnerArgs'); function checkCommands(yargs, argv, numRequired) {