Skip to content

Commit 58fa285

Browse files
Merge branch 'master' into a11y_stability_changes
2 parents 51f46b9 + 8e2f550 commit 58fa285

File tree

6 files changed

+112
-35
lines changed

6 files changed

+112
-35
lines changed

bin/accessibility-automation/helper.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ const getAccessibilityCypressCommandEventListener = () => {
194194
const setAccessibilityEventListeners = () => {
195195
try {
196196
const cypressCommandEventListener = getAccessibilityCypressCommandEventListener();
197-
glob(process.cwd() + '/cypress/support/*.js', {}, (err, files) => {
197+
198+
// Searching form command.js recursively
199+
glob(process.cwd() + '/**/cypress/support/*.js', {}, (err, files) => {
198200
if(err) return logger.debug('EXCEPTION IN BUILD START EVENT : Unable to parse cypress support files');
199201
files.forEach(file => {
200202
try {

bin/helpers/helper.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,26 @@ exports.getCiInfo = () => {
267267
return null;
268268
}
269269

270-
exports.getBuildDetails = (bsConfig) => {
270+
exports.getBuildDetails = (bsConfig, isO11y = false) => {
271+
const isTestObservabilityOptionsPresent = isO11y && !utils.isUndefined(bsConfig["testObservabilityOptions"]);
272+
271273
let buildName = '',
272274
projectName = '',
273275
buildDescription = '',
274276
buildTags = [];
275-
277+
276278
/* Pick from environment variables */
277279
buildName = process.env.BROWSERSTACK_BUILD_NAME || buildName;
278280
projectName = process.env.BROWSERSTACK_PROJECT_NAME || projectName;
279-
281+
282+
/* Pick from testObservabilityOptions */
283+
if(isTestObservabilityOptionsPresent) {
284+
buildName = buildName || bsConfig["testObservabilityOptions"]["buildName"];
285+
projectName = projectName || bsConfig["testObservabilityOptions"]["projectName"];
286+
if(!utils.isUndefined(bsConfig["testObservabilityOptions"]["buildTag"])) buildTags = [...buildTags, ...bsConfig["testObservabilityOptions"]["buildTag"]];
287+
buildDescription = buildDescription || bsConfig["testObservabilityOptions"]["buildDescription"];
288+
}
289+
280290
/* Pick from run settings */
281291
buildName = buildName || bsConfig["run_settings"]["build_name"];
282292
projectName = projectName || bsConfig["run_settings"]["project_name"];

bin/testObservability/helper/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const path = require('path');
2+
13
exports.consoleHolder = Object.assign({},console);
24
exports.BATCH_SIZE = 1000;
35
exports.BATCH_INTERVAL = 2000;
@@ -28,3 +30,5 @@ exports.OBSERVABILITY_ENV_VARS = [
2830
];
2931

3032
exports.TEST_OBSERVABILITY_REPORTER = 'browserstack-cypress-cli/bin/testObservability/reporter';
33+
34+
exports.TEST_OBSERVABILITY_REPORTER_LOCAL = path.join(__dirname, '..', 'reporter');

bin/testObservability/helper/helper.js

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ const GLOBAL_MODULE_PATH = execSync('npm root -g').toString().trim();
2525
const { name, version } = require('../../../package.json');
2626

2727
const { CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS } = require('../../helpers/constants');
28-
const { consoleHolder, API_URL, TEST_OBSERVABILITY_REPORTER } = require('./constants');
28+
const { consoleHolder, API_URL, TEST_OBSERVABILITY_REPORTER, TEST_OBSERVABILITY_REPORTER_LOCAL } = require('./constants');
29+
30+
const ALLOWED_MODULES = [
31+
'cypress/package.json',
32+
'mocha/lib/reporters/base.js',
33+
'mocha/lib/utils.js',
34+
'mocha'
35+
];
36+
2937
exports.pending_test_uploads = {
3038
count: 0
3139
};
@@ -331,7 +339,7 @@ exports.launchTestSession = async (user_config, bsConfigPath) => {
331339
projectName,
332340
buildDescription,
333341
buildTags
334-
} = helper.getBuildDetails(user_config);
342+
} = helper.getBuildDetails(user_config, true);
335343
const data = {
336344
'format': 'json',
337345
'project_name': projectName,
@@ -715,32 +723,85 @@ exports.getOSDetailsFromSystem = async (product) => {
715723
};
716724
}
717725

718-
exports.requireModule = (module, internal = false) => {
719-
logger.debug(`Getting ${module} from ${process.cwd()}`);
720-
let local_path = "";
721-
if(process.env["browserStackCwd"]){
722-
local_path = path.join(process.env["browserStackCwd"], 'node_modules', module);
723-
} else if(internal) {
724-
local_path = path.join(process.cwd(), 'node_modules', 'browserstack-cypress-cli', 'node_modules', module);
725-
} else {
726-
local_path = path.join(process.cwd(), 'node_modules', module);
727-
}
728-
if(!fs.existsSync(local_path)) {
729-
logger.debug(`${module} doesn\'t exist at ${process.cwd()}`);
730-
logger.debug(`Getting ${module} from ${GLOBAL_MODULE_PATH}`);
731-
732-
let global_path;
733-
if(['jest-runner', 'jest-runtime'].includes(module))
734-
global_path = path.join(GLOBAL_MODULE_PATH, 'jest', 'node_modules', module);
735-
else
736-
global_path = path.join(GLOBAL_MODULE_PATH, module);
737-
if(!fs.existsSync(global_path)) {
738-
throw new Error(`${module} doesn't exist.`);
726+
let WORKSPACE_MODULE_PATH;
727+
728+
exports.requireModule = (module) => {
729+
const modulePath = exports.resolveModule(module);
730+
if (modulePath.error) {
731+
throw new Error(`${module} doesn't exist.`);
732+
}
733+
734+
return require(modulePath.path);
735+
};
736+
737+
exports.resolveModule = (module) => {
738+
if (!ALLOWED_MODULES.includes(module)) {
739+
throw new Error('Invalid module name');
740+
}
741+
742+
if (WORKSPACE_MODULE_PATH == undefined) {
743+
try {
744+
WORKSPACE_MODULE_PATH = execSync('npm ls').toString().trim();
745+
WORKSPACE_MODULE_PATH = WORKSPACE_MODULE_PATH.split('\n')[0].split(' ')[1];
746+
} catch (e) {
747+
WORKSPACE_MODULE_PATH = null;
748+
exports.debug(`Could not locate npm module path with error ${e}`);
739749
}
740-
return require(global_path);
741750
}
742-
return require(local_path);
743-
}
751+
752+
/*
753+
Modules will be resolved in the following order,
754+
current working dir > workspaces dir > NODE_PATH env var > global node modules path
755+
*/
756+
757+
try {
758+
exports.debug('requireModuleV2');
759+
760+
return {path: require.resolve(module), foundAt: 'resolve'};
761+
} catch (_) {
762+
/* Find from current working directory */
763+
exports.debug(`Getting ${module} from ${process.cwd()}`);
764+
let local_path = path.join(process.cwd(), 'node_modules', module);
765+
if (!fs.existsSync(local_path)) {
766+
exports.debug(`${module} doesn't exist at ${process.cwd()}`);
767+
768+
/* Find from workspaces */
769+
if (WORKSPACE_MODULE_PATH) {
770+
exports.debug(`Getting ${module} from path ${WORKSPACE_MODULE_PATH}`);
771+
let workspace_path = null;
772+
workspace_path = path.join(WORKSPACE_MODULE_PATH, 'node_modules', module);
773+
if (workspace_path && fs.existsSync(workspace_path)) {
774+
exports.debug(`Found ${module} from ${WORKSPACE_MODULE_PATH}`);
775+
776+
return {path: workspace_path, foundAt: 'workspaces'};
777+
}
778+
}
779+
780+
/* Find from node path */
781+
let node_path = null;
782+
if (!exports.isUndefined(process.env.NODE_PATH)) {
783+
node_path = path.join(process.env.NODE_PATH, module);
784+
}
785+
if (node_path && fs.existsSync(node_path)) {
786+
exports.debug(`Getting ${module} from ${process.env.NODE_PATH}`);
787+
788+
return {path: node_path, foundAt: 'nodePath'};
789+
}
790+
791+
/* Find from global node modules path */
792+
exports.debug(`Getting ${module} from ${GLOBAL_MODULE_PATH}`);
793+
794+
let global_path = path.join(GLOBAL_MODULE_PATH, module);
795+
if (!global_path || !fs.existsSync(global_path)) {
796+
return {error: 'module_not_found'};
797+
}
798+
799+
return {path: global_path, foundAt: 'local'};
800+
}
801+
802+
return {path: local_path, foundAt: 'global'};
803+
}
804+
};
744805

745806
const getReRunSpecs = (rawArgs) => {
746807
if (this.isTestObservabilitySession() && this.shouldReRunObservabilityTests()) {
@@ -763,7 +824,7 @@ const getReRunSpecs = (rawArgs) => {
763824

764825
const getLocalSessionReporter = () => {
765826
if(this.isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
766-
return ['--reporter', TEST_OBSERVABILITY_REPORTER];
827+
return ['--reporter', TEST_OBSERVABILITY_REPORTER_LOCAL];
767828
} else {
768829
return [];
769830
}

bin/testObservability/reporter/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ class MyReporter {
156156
})
157157

158158
.on(EVENT_TEST_BEGIN, async (test) => {
159-
if (test.isSkipped) return;
159+
if (this.runStatusMarkedHash[test.testAnalyticsId]) return;
160160
if(this.testObservability == true) {
161161
await this.testStarted(test);
162162
}
163163
})
164164

165165
.on(EVENT_TEST_END, async (test) => {
166-
if (test.isSkipped) return;
166+
if (this.runStatusMarkedHash[test.testAnalyticsId]) return;
167167
if(this.testObservability == true) {
168168
if(!this.runStatusMarkedHash[test.testAnalyticsId]) {
169169
if(test.testAnalyticsId) this.runStatusMarkedHash[test.testAnalyticsId] = true;
@@ -448,7 +448,7 @@ class MyReporter {
448448
}
449449

450450
// Send pending hook finsihed events for hook starts
451-
if (eventType === 'TestRunFinished') {
451+
if (eventType === 'TestRunFinished' || eventType === 'TestRunSkipped') {
452452
Object.values(this.hooksStarted).forEach(async hookData => {
453453
hookData['event_type'] = 'HookRunFinished';
454454
hookData['hook_run'] = {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browserstack-cypress-cli",
3-
"version": "1.28.0",
3+
"version": "1.28.1",
44
"description": "BrowserStack Cypress CLI for Cypress integration with BrowserStack's remote devices.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)