@@ -5,7 +5,6 @@ const https = require('https');
5
5
const request = require ( 'request' ) ;
6
6
var gitLastCommit = require ( 'git-last-commit' ) ;
7
7
const { v4 : uuidv4 } = require ( 'uuid' ) ;
8
- const { performance } = require ( 'perf_hooks' ) ;
9
8
const os = require ( 'os' ) ;
10
9
const { promisify } = require ( 'util' ) ;
11
10
const getRepoInfo = require ( 'git-repo-info' ) ;
@@ -25,9 +24,7 @@ const GLOBAL_MODULE_PATH = execSync('npm root -g').toString().trim();
25
24
26
25
const { name, version } = require ( '../../../package.json' ) ;
27
26
28
- const CHUNK_SIZE = 5000
29
-
30
- const { consoleHolder, API_URL , BATCH_SIZE } = require ( './constants' ) ;
27
+ const { consoleHolder, API_URL } = require ( './constants' ) ;
31
28
exports . pending_test_uploads = {
32
29
count : 0
33
30
} ;
@@ -38,14 +35,16 @@ exports.debug = (text) => {
38
35
}
39
36
}
40
37
41
- const httpKeepAliveAgent = new http . Agent ( {
38
+ const supportFileContentMap = { } ;
39
+
40
+ exports . httpKeepAliveAgent = new http . Agent ( {
42
41
keepAlive : true ,
43
42
timeout : 60000 ,
44
43
maxSockets : 2 ,
45
44
maxTotalSockets : 2
46
45
} ) ;
47
46
48
- const httpsKeepAliveAgent = new https . Agent ( {
47
+ exports . httpsKeepAliveAgent = new https . Agent ( {
49
48
keepAlive : true ,
50
49
timeout : 60000 ,
51
50
maxSockets : 2 ,
@@ -66,8 +65,20 @@ const httpsScreenshotsKeepAliveAgent = new https.Agent({
66
65
maxTotalSockets : 2
67
66
} ) ;
68
67
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
+
69
78
exports . printBuildLink = async ( ) => {
79
+ if ( ! this . isTestObservabilitySession ( ) ) return ;
70
80
try {
81
+ supportFileCleanup ( ) ;
71
82
await this . stopBuildUpstream ( ) ;
72
83
try {
73
84
if ( process . env . BS_TESTOPS_BUILD_HASHED_ID
@@ -77,10 +88,10 @@ exports.printBuildLink = async () => {
77
88
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` ) ;
78
89
}
79
90
} catch ( err ) {
80
- logger . error ( `[ ${ ( new Date ( ) ) . toISOString ( ) } ][ OBSERVABILITY ] Build Not Found` ) ;
91
+ exports . debug ( ' Build Not Found' ) ;
81
92
}
82
93
} catch ( err ) {
83
- logger . error ( `[ ${ ( new Date ( ) ) . toISOString ( ) } ][ OBSERVABILITY ] Error while stopping build : ${ err } `) ;
94
+ exports . debug ( ` Error while stopping build with error : ${ err } `) ;
84
95
}
85
96
}
86
97
@@ -91,7 +102,7 @@ const nodeRequest = (type, url, data, config) => {
91
102
url : `${ API_URL } /${ url } ` ,
92
103
body : data ,
93
104
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
95
106
} } ;
96
107
97
108
if ( url === exports . requestQueueHandler . screenshotEventUrl ) {
@@ -373,19 +384,25 @@ const getCypressCommandEventListener = () => {
373
384
}
374
385
375
386
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 ) { }
387
404
} ) ;
388
- } ) ;
405
+ } catch ( e ) { }
389
406
}
390
407
391
408
const getBuildDetails = ( bsConfig ) => {
@@ -499,6 +516,25 @@ exports.launchTestSession = async (user_config) => {
499
516
} else {
500
517
exports . debug ( `EXCEPTION IN BUILD START EVENT : ${ error . message || error } ` ) ;
501
518
}
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
+
502
538
process . env . BS_TESTOPS_BUILD_COMPLETED = false ;
503
539
setEnvironmentVariablesForRemoteReporter ( null , null , null ) ;
504
540
}
@@ -740,33 +776,6 @@ exports.stopBuildUpstream = async (buildStartWaitRun = 0, testUploadWaitRun = 0,
740
776
}
741
777
}
742
778
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
-
770
779
exports . getHookSkippedTests = ( suite ) => {
771
780
const subSuitesSkippedTests = suite . suites . reduce ( ( acc , subSuite ) => {
772
781
const subSuiteSkippedTests = exports . getHookSkippedTests ( subSuite ) ;
@@ -880,21 +889,31 @@ const getReRunSpecs = (rawArgs) => {
880
889
}
881
890
}
882
891
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
+
883
900
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
+ } ) ;
894
912
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 ) { }
898
917
}
899
918
900
919
class PathHelper {
0 commit comments