Skip to content

Commit

Permalink
Merge pull request #769 from 07souravkunda/update_requiremodulev3_mon…
Browse files Browse the repository at this point in the history
…orepo

Update requiremodulev3 monorepo
  • Loading branch information
bstack-security-github committed Jan 9, 2024
2 parents 07321fc + 541f268 commit f443bb6
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 26 deletions.
4 changes: 4 additions & 0 deletions bin/testObservability/helper/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const path = require('path');

exports.consoleHolder = Object.assign({},console);
exports.BATCH_SIZE = 1000;
exports.BATCH_INTERVAL = 2000;
Expand Down Expand Up @@ -28,3 +30,5 @@ exports.OBSERVABILITY_ENV_VARS = [
];

exports.TEST_OBSERVABILITY_REPORTER = 'browserstack-cypress-cli/bin/testObservability/reporter';

exports.TEST_OBSERVABILITY_REPORTER_LOCAL = path.join(__dirname, '..', 'reporter');
113 changes: 87 additions & 26 deletions bin/testObservability/helper/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ const GLOBAL_MODULE_PATH = execSync('npm root -g').toString().trim();
const { name, version } = require('../../../package.json');

const { CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS } = require('../../helpers/constants');
const { consoleHolder, API_URL, TEST_OBSERVABILITY_REPORTER } = require('./constants');
const { consoleHolder, API_URL, TEST_OBSERVABILITY_REPORTER, TEST_OBSERVABILITY_REPORTER_LOCAL } = require('./constants');

const ALLOWED_MODULES = [
'cypress/package.json',
'mocha/lib/reporters/base.js',
'mocha/lib/utils.js',
'mocha'
];

exports.pending_test_uploads = {
count: 0
};
Expand Down Expand Up @@ -715,32 +723,85 @@ exports.getOSDetailsFromSystem = async (product) => {
};
}

exports.requireModule = (module, internal = false) => {
logger.debug(`Getting ${module} from ${process.cwd()}`);
let local_path = "";
if(process.env["browserStackCwd"]){
local_path = path.join(process.env["browserStackCwd"], 'node_modules', module);
} else if(internal) {
local_path = path.join(process.cwd(), 'node_modules', 'browserstack-cypress-cli', 'node_modules', module);
} else {
local_path = path.join(process.cwd(), 'node_modules', module);
}
if(!fs.existsSync(local_path)) {
logger.debug(`${module} doesn\'t exist at ${process.cwd()}`);
logger.debug(`Getting ${module} from ${GLOBAL_MODULE_PATH}`);

let global_path;
if(['jest-runner', 'jest-runtime'].includes(module))
global_path = path.join(GLOBAL_MODULE_PATH, 'jest', 'node_modules', module);
else
global_path = path.join(GLOBAL_MODULE_PATH, module);
if(!fs.existsSync(global_path)) {
throw new Error(`${module} doesn't exist.`);
let WORKSPACE_MODULE_PATH;

exports.requireModule = (module) => {
const modulePath = exports.resolveModule(module);
if (modulePath.error) {
throw new Error(`${module} doesn't exist.`);
}

return require(modulePath.path);
};

exports.resolveModule = (module) => {
if (!ALLOWED_MODULES.includes(module)) {
throw new Error('Invalid module name');
}

if (WORKSPACE_MODULE_PATH == undefined) {
try {
WORKSPACE_MODULE_PATH = execSync('npm ls').toString().trim();
WORKSPACE_MODULE_PATH = WORKSPACE_MODULE_PATH.split('\n')[0].split(' ')[1];
} catch (e) {
WORKSPACE_MODULE_PATH = null;
exports.debug(`Could not locate npm module path with error ${e}`);
}
return require(global_path);
}
return require(local_path);
}

/*
Modules will be resolved in the following order,
current working dir > workspaces dir > NODE_PATH env var > global node modules path
*/

try {
exports.debug('requireModuleV2');

return {path: require.resolve(module), foundAt: 'resolve'};
} catch (_) {
/* Find from current working directory */
exports.debug(`Getting ${module} from ${process.cwd()}`);
let local_path = path.join(process.cwd(), 'node_modules', module);
if (!fs.existsSync(local_path)) {
exports.debug(`${module} doesn't exist at ${process.cwd()}`);

/* Find from workspaces */
if (WORKSPACE_MODULE_PATH) {
exports.debug(`Getting ${module} from path ${WORKSPACE_MODULE_PATH}`);
let workspace_path = null;
workspace_path = path.join(WORKSPACE_MODULE_PATH, 'node_modules', module);
if (workspace_path && fs.existsSync(workspace_path)) {
exports.debug(`Found ${module} from ${WORKSPACE_MODULE_PATH}`);

return {path: workspace_path, foundAt: 'workspaces'};
}
}

/* Find from node path */
let node_path = null;
if (!exports.isUndefined(process.env.NODE_PATH)) {
node_path = path.join(process.env.NODE_PATH, module);
}
if (node_path && fs.existsSync(node_path)) {
exports.debug(`Getting ${module} from ${process.env.NODE_PATH}`);

return {path: node_path, foundAt: 'nodePath'};
}

/* Find from global node modules path */
exports.debug(`Getting ${module} from ${GLOBAL_MODULE_PATH}`);

let global_path = path.join(GLOBAL_MODULE_PATH, module);
if (!global_path || !fs.existsSync(global_path)) {
return {error: 'module_not_found'};
}

return {path: global_path, foundAt: 'local'};
}

return {path: local_path, foundAt: 'global'};
}
};

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

const getLocalSessionReporter = () => {
if(this.isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
return ['--reporter', TEST_OBSERVABILITY_REPORTER];
return ['--reporter', TEST_OBSERVABILITY_REPORTER_LOCAL];
} else {
return [];
}
Expand Down

0 comments on commit f443bb6

Please sign in to comment.