Skip to content

Commit 3636135

Browse files
committed
minor fixes + remove unneeded code
1 parent 8beb1be commit 3636135

File tree

4 files changed

+93
-216
lines changed

4 files changed

+93
-216
lines changed

bin/helpers/utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,8 +1219,7 @@ exports.setConfig = (bsConfig, args) => {
12191219

12201220
// blindly send other passed configs with run_settings and handle at backend
12211221
exports.setOtherConfigs = (bsConfig, args) => {
1222-
/* Change to when Observability is enabled */
1223-
if(isTestObservabilitySession()) {
1222+
if(isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
12241223
bsConfig["run_settings"]["reporter"] = "browserstack-cypress-cli/bin/testObservability/reporter";
12251224
return;
12261225
}

bin/testObservability/cypress/cypressEventListeners.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Cypress.Commands.add('trace', (message, file) => {
112112
level: 'trace',
113113
message,
114114
file,
115+
}).then((res) => {
116+
}).catch((err) => {
115117
});
116118
});
117119

@@ -120,6 +122,8 @@ Cypress.Commands.add('logDebug', (message, file) => {
120122
level: 'debug',
121123
message,
122124
file,
125+
}).then((res) => {
126+
}).catch((err) => {
123127
});
124128
});
125129

@@ -128,6 +132,8 @@ Cypress.Commands.add('info', (message, file) => {
128132
level: 'info',
129133
message,
130134
file,
135+
}).then((res) => {
136+
}).catch((err) => {
131137
});
132138
});
133139

@@ -136,6 +142,8 @@ Cypress.Commands.add('warn', (message, file) => {
136142
level: 'warn',
137143
message,
138144
file,
145+
}).then((res) => {
146+
}).catch((err) => {
139147
});
140148
});
141149

@@ -144,6 +152,8 @@ Cypress.Commands.add('error', (message, file) => {
144152
level: 'error',
145153
message,
146154
file,
155+
}).then((res) => {
156+
}).catch((err) => {
147157
});
148158
});
149159

@@ -152,5 +162,7 @@ Cypress.Commands.add('fatal', (message, file) => {
152162
level: 'fatal',
153163
message,
154164
file,
165+
}).then((res) => {
166+
}).catch((err) => {
155167
});
156168
});

bin/testObservability/helper/helper.js

Lines changed: 80 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const https = require('https');
55
const request = require('request');
66
var gitLastCommit = require('git-last-commit');
77
const { v4: uuidv4 } = require('uuid');
8-
const { performance } = require('perf_hooks');
98
const os = require('os');
109
const { promisify } = require('util');
1110
const getRepoInfo = require('git-repo-info');
@@ -25,9 +24,7 @@ const GLOBAL_MODULE_PATH = execSync('npm root -g').toString().trim();
2524

2625
const { name, version } = require('../../../package.json');
2726

28-
const CHUNK_SIZE = 5000
29-
30-
const { consoleHolder, API_URL, BATCH_SIZE } = require('./constants');
27+
const { consoleHolder, API_URL } = require('./constants');
3128
exports.pending_test_uploads = {
3229
count: 0
3330
};
@@ -38,14 +35,16 @@ exports.debug = (text) => {
3835
}
3936
}
4037

41-
const httpKeepAliveAgent = new http.Agent({
38+
const supportFileContentMap = {};
39+
40+
exports.httpKeepAliveAgent = new http.Agent({
4241
keepAlive: true,
4342
timeout: 60000,
4443
maxSockets: 2,
4544
maxTotalSockets: 2
4645
});
4746

48-
const httpsKeepAliveAgent = new https.Agent({
47+
exports.httpsKeepAliveAgent = new https.Agent({
4948
keepAlive: true,
5049
timeout: 60000,
5150
maxSockets: 2,
@@ -66,8 +65,20 @@ const httpsScreenshotsKeepAliveAgent = new https.Agent({
6665
maxTotalSockets: 2
6766
});
6867

68+
const supportFileCleanup = () => {
69+
Object.keys(supportFileContentMap).forEach(file => {
70+
try {
71+
fs.writeFileSync(file, supportFileContentMap[file], {encoding: 'utf-8'});
72+
} catch(e) {
73+
exports.debug(`Error while replacing file content for ${file} with it's original content with error : ${e}`);
74+
}
75+
});
76+
}
77+
6978
exports.printBuildLink = async () => {
79+
if(!this.isTestObservabilitySession()) return;
7080
try {
81+
supportFileCleanup();
7182
await this.stopBuildUpstream();
7283
try {
7384
if(process.env.BS_TESTOPS_BUILD_HASHED_ID
@@ -77,10 +88,10 @@ exports.printBuildLink = async () => {
7788
logger.info(`Visit https://observability.browserstack.com/builds/${process.env.BS_TESTOPS_BUILD_HASHED_ID} to view build report, insights, and many more debugging information all at one place!\n`);
7889
}
7990
} catch(err) {
80-
logger.error(`[${(new Date()).toISOString()}][ OBSERVABILITY ] Build Not Found`);
91+
exports.debug('Build Not Found');
8192
}
8293
} catch(err) {
83-
logger.error(`[${(new Date()).toISOString()}][ OBSERVABILITY ] Error while stopping build : ${err}`);
94+
exports.debug(`Error while stopping build with error : ${err}`);
8495
}
8596
}
8697

@@ -91,7 +102,7 @@ const nodeRequest = (type, url, data, config) => {
91102
url: `${API_URL}/${url}`,
92103
body: data,
93104
json: config.headers['Content-Type'] === 'application/json',
94-
agent: API_URL.includes('https') ? httpsKeepAliveAgent : httpKeepAliveAgent
105+
agent: API_URL.includes('https') ? this.httpsKeepAliveAgent : this.httpKeepAliveAgent
95106
}};
96107

97108
if(url === exports.requestQueueHandler.screenshotEventUrl) {
@@ -373,19 +384,25 @@ const getCypressCommandEventListener = () => {
373384
}
374385

375386
const setEventListeners = () => {
376-
glob(process.cwd() + '/cypress/support/*.js', {}, (err, files) => {
377-
if(err) return exports.debug('EXCEPTION IN BUILD START EVENT : Unable to parse cypress support files');
378-
files.forEach(file => {
379-
if(!file.includes('commands.js')) {
380-
const defaultFileContent = fs.readFileSync(file, {encoding: 'utf-8'});
381-
let newFileContent = defaultFileContent +
382-
'\n' +
383-
getCypressCommandEventListener() +
384-
'\n'
385-
fs.writeFileSync(file, newFileContent, {encoding: 'utf-8'});
386-
}
387+
try {
388+
glob(process.cwd() + '/cypress/support/*.js', {}, (err, files) => {
389+
if(err) return exports.debug('EXCEPTION IN BUILD START EVENT : Unable to parse cypress support files');
390+
try {
391+
files.forEach(file => {
392+
if(!file.includes('commands.js')) {
393+
const defaultFileContent = fs.readFileSync(file, {encoding: 'utf-8'});
394+
395+
let newFileContent = defaultFileContent +
396+
'\n' +
397+
getCypressCommandEventListener() +
398+
'\n'
399+
fs.writeFileSync(file, newFileContent, {encoding: 'utf-8'});
400+
supportFileContentMap[file] = defaultFileContent;
401+
}
402+
});
403+
} catch(e) {}
387404
});
388-
});
405+
} catch(e) {}
389406
}
390407

391408
const getBuildDetails = (bsConfig) => {
@@ -499,6 +516,25 @@ exports.launchTestSession = async (user_config) => {
499516
} else {
500517
exports.debug(`EXCEPTION IN BUILD START EVENT : ${error.message || error}`);
501518
}
519+
520+
if(error.response) {
521+
const errorMessageJson = error.response.body ? JSON.parse(error.response.body.toString()) : null
522+
const errorMessage = errorMessageJson ? errorMessageJson.message : null, errorType = errorMessageJson ? errorMessageJson.errorType : null
523+
switch (errorType) {
524+
case 'ERROR_INVALID_CREDENTIALS':
525+
logger.error(errorMessage);
526+
break;
527+
case 'ERROR_ACCESS_DENIED':
528+
logger.info(errorMessage);
529+
break;
530+
case 'ERROR_SDK_DEPRECATED':
531+
logger.error(errorMessage);
532+
break;
533+
default:
534+
logger.error(errorMessage);
535+
}
536+
}
537+
502538
process.env.BS_TESTOPS_BUILD_COMPLETED = false;
503539
setEnvironmentVariablesForRemoteReporter(null, null, null);
504540
}
@@ -740,33 +776,6 @@ exports.stopBuildUpstream = async (buildStartWaitRun = 0, testUploadWaitRun = 0,
740776
}
741777
}
742778

743-
exports.getPlatformVersion = (isBstack) => {
744-
if(isBstack) {
745-
try {
746-
return global.__platform__.split(',')[1].trim();
747-
} catch (e) {
748-
return null;
749-
}
750-
}
751-
return null;
752-
}
753-
754-
exports.isUndefined = value => (value === undefined || value === null);
755-
756-
exports.parseFileNames = (string) => {
757-
if (exports.isUndefined(string)) {
758-
return undefined;
759-
} else {
760-
try {
761-
return string.trim().split(",");
762-
} catch (err) {
763-
// file object is empty or non parseable. run all files in this case
764-
// nothing to do in this block. caught the error just that runner doesn't raise the exception and exit
765-
return undefined;
766-
}
767-
}
768-
}
769-
770779
exports.getHookSkippedTests = (suite) => {
771780
const subSuitesSkippedTests = suite.suites.reduce((acc, subSuite) => {
772781
const subSuiteSkippedTests = exports.getHookSkippedTests(subSuite);
@@ -880,21 +889,31 @@ const getReRunSpecs = (rawArgs) => {
880889
}
881890
}
882891

892+
const getLocalSessionReporter = () => {
893+
if(this.isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
894+
return ['--reporter', 'browserstack-cypress-cli/bin/testObservability/reporter'];
895+
} else {
896+
return [];
897+
}
898+
}
899+
883900
exports.runCypressTestsLocally = (bsConfig, args, rawArgs) => {
884-
logger.info(`Running npx cypress run ${getReRunSpecs(rawArgs.slice(1)).join(' ')} --reporter 'browserstack-cypress-cli/bin/testObservability/reporter'`);
885-
const cypressProcess = spawn(
886-
'npx',
887-
['cypress', 'run', ...getReRunSpecs(rawArgs.slice(1)), '--reporter', 'browserstack-cypress-cli/bin/testObservability/reporter'],
888-
{ stdio: 'inherit', cwd: process.cwd(), env: process.env }
889-
);
890-
cypressProcess.on('close', async (code) => {
891-
logger.info(`Cypress process exited with code ${code}`);
892-
await this.printBuildLink();
893-
});
901+
try {
902+
logger.info(`Running npx cypress run ${getReRunSpecs(rawArgs.slice(1)).join(' ')} ${getLocalSessionReporter().join(' ')}`);
903+
const cypressProcess = spawn(
904+
'npx',
905+
['cypress', 'run', ...getReRunSpecs(rawArgs.slice(1)), ...getLocalSessionReporter()],
906+
{ stdio: 'inherit', cwd: process.cwd(), env: process.env }
907+
);
908+
cypressProcess.on('close', async (code) => {
909+
logger.info(`Cypress process exited with code ${code}`);
910+
await this.printBuildLink();
911+
});
894912

895-
cypressProcess.on('error', (err) => {
896-
logger.info(`Cypress process encountered an error ${err}`);
897-
});
913+
cypressProcess.on('error', (err) => {
914+
logger.info(`Cypress process encountered an error ${err}`);
915+
});
916+
} catch(e) {}
898917
}
899918

900919
class PathHelper {

0 commit comments

Comments
 (0)