@@ -107,7 +107,9 @@ const nodeRequest = (type, url, data, config) => {
107
107
try {
108
108
if ( typeof ( body ) !== 'object' ) body = JSON . parse ( body ) ;
109
109
} catch ( e ) {
110
- reject ( 'Not a JSON response from BrowserStack Server' ) ;
110
+ if ( ! url . includes ( '/stop' ) ) {
111
+ reject ( 'Not a JSON response from BrowserStack Server' ) ;
112
+ }
111
113
}
112
114
resolve ( {
113
115
data : body
@@ -157,29 +159,6 @@ exports.getTestEnv = () => {
157
159
}
158
160
}
159
161
160
- // exports.extractValuesWithRegexKeyMatch = (obj) => {
161
- // let values = [];
162
- // let customTagRegex = new RegExp("^CUSTOM_TAG_\\d+$", 'i');
163
- // Object.keys(obj)
164
- // .filter(key => customTagRegex.test(key))
165
- // .forEach(key => values.push(obj[key]));
166
- // return values;
167
- // }
168
-
169
- // exports.getCustomTags = (config) => {
170
- // let tags = [];
171
-
172
- // let tag = config.customTag || process.env.CUSTOM_TAG;
173
- // if (tag) {
174
- // tags.push(tag);
175
- // }
176
-
177
- // tags.push(...this.extractValuesWithRegexKeyMatch(process.env));
178
- // tags.push(...this.extractValuesWithRegexKeyMatch(config));
179
-
180
- // return tags;
181
- // }
182
-
183
162
exports . getFileSeparatorData = ( ) => {
184
163
const fileSeparatorRegex = / ^ w i n / . test ( process . platform ) ? "\\\\" : "/" ;
185
164
const fileSeparator = / ^ w i n / . test ( process . platform ) ? "\\" : "/" ;
@@ -414,7 +393,6 @@ const getBuildDetails = (bsConfig) => {
414
393
let buildName = '' ,
415
394
projectName = '' ,
416
395
buildDescription = '' ,
417
- buildIdentifier = null ,
418
396
buildTags = [ ] ;
419
397
420
398
/* Pick from environment variables */
@@ -426,7 +404,6 @@ const getBuildDetails = (bsConfig) => {
426
404
buildName = buildName || bsConfig [ "testObservabilityOptions" ] [ "buildName" ] ;
427
405
projectName = projectName || bsConfig [ "testObservabilityOptions" ] [ "projectName" ] ;
428
406
buildTags = [ ...buildTags , ...bsConfig [ "testObservabilityOptions" ] [ "buildTag" ] ] ;
429
- buildIdentifier = buildIdentifier || bsConfig [ "testObservabilityOptions" ] [ "buildIdentifier" ] ;
430
407
buildDescription = buildDescription || bsConfig [ "testObservabilityOptions" ] [ "buildDescription" ] ;
431
408
}
432
409
@@ -440,18 +417,28 @@ const getBuildDetails = (bsConfig) => {
440
417
return {
441
418
buildName,
442
419
projectName,
443
- buildIdentifier,
444
420
buildDescription,
445
421
buildTags
446
422
} ;
447
423
}
448
424
425
+ const setBrowserstackCypressCliDependency = ( bsConfig ) => {
426
+ const runSettings = bsConfig . run_settings ;
427
+ if ( runSettings . npm_dependencies !== undefined &&
428
+ Object . keys ( runSettings . npm_dependencies ) . length !== 0 &&
429
+ typeof runSettings . npm_dependencies === 'object' ) {
430
+ if ( ! ( "browserstack-cypress-cli" in runSettings . npm_dependencies ) ) {
431
+ logger . warn ( "Missing browserstack-cypress-cli not found in npm_dependencies" ) ;
432
+ runSettings . npm_dependencies [ 'browserstack-cypress-cli' ] = "latest" ;
433
+ logger . warn ( `Adding browserstack-cypress-cli in npm_dependencies` ) ;
434
+ }
435
+ }
436
+ }
437
+
449
438
exports . launchTestSession = async ( user_config ) => {
450
- // const obsUserName = user_config["auth"]["username"];
451
- // const obsAccessKey = user_config["auth"]["access_key"];
439
+ const obsUserName = user_config [ "auth" ] [ "username" ] ;
440
+ const obsAccessKey = user_config [ "auth" ] [ "access_key" ] ;
452
441
453
- const obsUserName = process . env . OBS_USERNAME || user_config [ "auth" ] [ "username" ] ;
454
- const obsAccessKey = process . env . OBS_ACCESS_KEY || user_config [ "auth" ] [ "access_key" ] ;
455
442
const BSTestOpsToken = `${ obsUserName || '' } :${ obsAccessKey || '' } ` ;
456
443
if ( BSTestOpsToken === '' ) {
457
444
exports . debug ( 'EXCEPTION IN BUILD START EVENT : Missing authentication token' ) ;
@@ -462,15 +449,13 @@ exports.launchTestSession = async (user_config) => {
462
449
const {
463
450
buildName,
464
451
projectName,
465
- buildIdentifier,
466
452
buildDescription,
467
453
buildTags
468
454
} = getBuildDetails ( user_config ) ;
469
455
const data = {
470
456
'format' : 'json' ,
471
457
'project_name' : projectName ,
472
458
'name' : buildName ,
473
- 'build_identifier' : buildIdentifier ,
474
459
'description' : buildDescription ,
475
460
'start_time' : ( new Date ( ) ) . toISOString ( ) ,
476
461
'tags' : buildTags ,
@@ -482,6 +467,7 @@ exports.launchTestSession = async (user_config) => {
482
467
arch : os . arch ( )
483
468
} ,
484
469
'ci_info' : getCiInfo ( ) ,
470
+ 'build_run_identifier' : process . env . BROWSERSTACK_BUILD_RUN_IDENTIFIER ,
485
471
'failed_tests_rerun' : process . env . BROWSERSTACK_RERUN || false ,
486
472
'version_control' : await getGitMetaData ( ) ,
487
473
'observability_version' : {
@@ -506,7 +492,7 @@ exports.launchTestSession = async (user_config) => {
506
492
process . env . BS_TESTOPS_BUILD_COMPLETED = true ;
507
493
setEnvironmentVariablesForRemoteReporter ( response . data . jwt , response . data . build_hashed_id , response . data . allow_screenshots , data . observability_version . sdkVersion ) ;
508
494
setEventListeners ( ) ;
509
- // return [response.data.jwt, response.data.build_hashed_id, response.data.allow_screenshots] ;
495
+ if ( this . isBrowserstackInfra ( ) ) setBrowserstackCypressCliDependency ( user_config ) ;
510
496
} catch ( error ) {
511
497
if ( error . response ) {
512
498
exports . debug ( `EXCEPTION IN BUILD START EVENT : ${ error . response . status } ${ error . response . statusText } ${ JSON . stringify ( error . response . data ) } ` ) ;
@@ -515,7 +501,6 @@ exports.launchTestSession = async (user_config) => {
515
501
}
516
502
process . env . BS_TESTOPS_BUILD_COMPLETED = false ;
517
503
setEnvironmentVariablesForRemoteReporter ( null , null , null ) ;
518
- // return [null, null, null];
519
504
}
520
505
}
521
506
}
@@ -727,7 +712,7 @@ exports.stopBuildUpstream = async (buildStartWaitRun = 0, testUploadWaitRun = 0,
727
712
728
713
try {
729
714
const response = await nodeRequest ( 'PUT' , `api/v1/builds/${ process . env . BS_TESTOPS_BUILD_HASHED_ID } /stop` , data , config ) ;
730
- if ( response . data . error ) {
715
+ if ( response . data && response . data . error ) {
731
716
throw ( { message : response . data . error } ) ;
732
717
} else {
733
718
exports . debug ( `stopBuildUpstream buildStartWaitRun[${ buildStartWaitRun } ] testUploadWaitRun[${ testUploadWaitRun } ] event successfull!` )
@@ -755,10 +740,6 @@ exports.stopBuildUpstream = async (buildStartWaitRun = 0, testUploadWaitRun = 0,
755
740
}
756
741
}
757
742
758
- // exports.getUpstreamConfig = () => {
759
- // return bsSetupHelper.readConfig(bsSetupHelper.getConfigPath());
760
- // }
761
-
762
743
exports . getPlatformVersion = ( isBstack ) => {
763
744
if ( isBstack ) {
764
745
try {
@@ -820,7 +801,7 @@ const getMacOSVersion = () => {
820
801
return execSync ( "awk '/SOFTWARE LICENSE AGREEMENT FOR macOS/' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk -F 'macOS ' '{print $NF}' | awk '{print substr($0, 0, length($0)-1)}'" ) . toString ( ) . trim ( )
821
802
}
822
803
823
- exports . getOSDetailsFromSystem = async ( ) => {
804
+ exports . getOSDetailsFromSystem = async ( product ) => {
824
805
let platformName = getPlatformName ( ) ;
825
806
let platformVersion = os . release ( ) . toString ( ) ;
826
807
@@ -848,32 +829,18 @@ exports.getOSDetailsFromSystem = async () => {
848
829
}
849
830
850
831
return {
851
- os : platformName ,
832
+ os : product == 'automate' && platformName == 'Linux' ? 'OS X' : platformName ,
852
833
os_version : platformVersion
853
834
} ;
854
835
}
855
836
856
- // exports.getOSDetailsFromSystem = (product) => {
857
- // var opsys = os.platform();
858
- // if (opsys == "darwin") {
859
- // opsys = "MacOS";
860
- // } else if (opsys == "win32" || opsys == "win64") {
861
- // opsys = "Windows";
862
- // } else if (opsys == "linux") {
863
- // opsys = product === "automate" ? "MacOS" : "Linux";
864
- // }
865
-
866
- // return {
867
- // os: opsys,
868
- // os_version: os.version()
869
- // }
870
- // }
871
-
872
- exports . requireModule = ( module ) => {
837
+ exports . requireModule = ( module , internal = false ) => {
873
838
logger . debug ( `Getting ${ module } from ${ process . cwd ( ) } ` ) ;
874
839
let local_path = "" ;
875
840
if ( process . env [ "browserStackCwd" ] ) {
876
841
local_path = path . join ( process . env [ "browserStackCwd" ] , 'node_modules' , module ) ;
842
+ } else if ( internal ) {
843
+ local_path = path . join ( process . cwd ( ) , 'node_modules' , 'browserstack-cypress-cli' , 'node_modules' , module ) ;
877
844
} else {
878
845
local_path = path . join ( process . cwd ( ) , 'node_modules' , module ) ;
879
846
}
@@ -894,24 +861,40 @@ exports.requireModule = (module) => {
894
861
return require ( local_path ) ;
895
862
}
896
863
864
+ const getReRunSpecs = ( rawArgs ) => {
865
+ if ( this . isTestObservabilitySession ( ) && this . shouldReRunObservabilityTests ( ) ) {
866
+ let startIdx = - 1 , numEle = 0 ;
867
+ for ( let idx = 0 ; idx < rawArgs . length ; idx ++ ) {
868
+ if ( rawArgs [ idx ] == '--spec' ) {
869
+ startIdx = idx ;
870
+ } else if ( rawArgs [ idx ] . includes ( '--' ) && startIdx != - 1 ) {
871
+ break ;
872
+ } else if ( startIdx != - 1 ) {
873
+ numEle ++ ;
874
+ }
875
+ }
876
+ if ( startIdx != - 1 ) rawArgs . splice ( startIdx , numEle + 1 ) ;
877
+ return [ ...rawArgs , '--spec' , process . env . BROWSERSTACK_RERUN_TESTS ] ;
878
+ } else {
879
+ return rawArgs ;
880
+ }
881
+ }
882
+
897
883
exports . runCypressTestsLocally = ( bsConfig , args , rawArgs ) => {
898
- logger . info ( `\n >>> LOCAL CMD : npx cypress run ${ rawArgs . slice ( 1 ) } --reporter 'browserstack-cypress-cli/bin/testObservability/reporter' \n` ) ;
899
- // const index = rawArgs.findIndex((arg) => (arg === '--reporter' || arg === '-r'));
900
-
884
+ logger . info ( `Running npx cypress run ${ getReRunSpecs ( rawArgs . slice ( 1 ) ) . join ( ' ' ) } --reporter 'browserstack-cypress-cli/bin/testObservability/reporter'` ) ;
901
885
const cypressProcess = spawn (
902
886
'npx' ,
903
- [ 'cypress' , 'run' , ...rawArgs . slice ( 1 ) , '--reporter' , 'browserstack-cypress-cli/bin/testObservability/reporter' ] ,
887
+ [ 'cypress' , 'run' , ...getReRunSpecs ( rawArgs . slice ( 1 ) ) , '--reporter' , 'browserstack-cypress-cli/bin/testObservability/reporter' ] ,
904
888
{ stdio : 'inherit' , cwd : process . cwd ( ) , env : process . env }
905
889
) ;
906
-
907
890
cypressProcess . on ( 'close' , async ( code ) => {
908
891
logger . info ( `Cypress process exited with code ${ code } ` ) ;
909
892
await this . printBuildLink ( ) ;
910
893
} ) ;
911
894
912
895
cypressProcess . on ( 'error' , ( err ) => {
913
- logger . info ( `Cypress process error ${ err } ` ) ;
914
- } )
896
+ logger . info ( `Cypress process encountered an error ${ err } ` ) ;
897
+ } ) ;
915
898
}
916
899
917
900
class PathHelper {
0 commit comments