@@ -11,6 +11,7 @@ const fs = require('fs');
1111const BinaryControl = require ( '../src/binaryControl' ) ;
1212const ArtifactsManager = require ( '../src/artifacts' ) ;
1313const constants = require ( '../config/constants' ) ;
14+ const Utils = require ( '../src/utils' ) ;
1415
1516const {
1617 BINARY_LINKS ,
@@ -22,6 +23,9 @@ const {
2223 ALLOWED_INPUT_VALUES : {
2324 LOCAL_TESTING ,
2425 } ,
26+ ENV_VARS : {
27+ BROWSERSTACK_LOCAL_LOGS_FILE ,
28+ } ,
2529} = constants ;
2630
2731describe ( 'Binary Control Operations' , ( ) => {
@@ -84,18 +88,51 @@ describe('Binary Control Operations', () => {
8488 os . platform . restore ( ) ;
8589 } ) ;
8690
87- it ( 'Generates log-file name and path for Binary' , ( ) => {
88- sinon . stub ( os , 'platform' ) . returns ( 'darwin' ) ;
89- sinon . stub ( github , 'context' ) . value ( {
90- job : 'someJobName' ,
91+ context ( 'Log File metadata' , ( ) => {
92+ beforeEach ( ( ) => {
93+ sinon . stub ( core , 'exportVariable' ) ;
94+ sinon . stub ( os , 'platform' ) . returns ( 'darwin' ) ;
95+ sinon . stub ( github , 'context' ) . value ( {
96+ job : 'someJobName' ,
97+ } ) ;
98+ } ) ;
99+
100+ afterEach ( ( ) => {
101+ delete process . env [ BROWSERSTACK_LOCAL_LOGS_FILE ] ;
102+ core . exportVariable . restore ( ) ;
103+ os . platform . restore ( ) ;
104+ } ) ;
105+
106+ it ( 'Generates log-file name and path for Binary' , ( ) => {
107+ sinon . stub ( Date , 'now' ) . returns ( 'now' ) ;
108+ const expectedLogFileName = `${ LOCAL_LOG_FILE_PREFIX } _${ github . context . job } _now.log` ;
109+ const expectedLogFilePath = path . resolve ( path . resolve ( process . env . HOME , 'work' , 'binary' , LOCAL_BINARY_FOLDER , 'darwin' ) , expectedLogFileName ) ;
110+ const binaryControl = new BinaryControl ( ) ;
111+ binaryControl . _generateLogFileMetadata ( ) ;
112+ expect ( binaryControl . logFileName ) . to . eq ( expectedLogFileName ) ;
113+ expect ( binaryControl . logFilePath ) . to . eq ( expectedLogFilePath ) ;
114+ sinon . assert . calledWith (
115+ core . exportVariable ,
116+ BROWSERSTACK_LOCAL_LOGS_FILE ,
117+ expectedLogFileName ,
118+ ) ;
119+ Date . now . restore ( ) ;
120+ } ) ;
121+
122+ it ( 'Fetches log-file name and generates path for Binary if logs file name was already defined' , ( ) => {
123+ process . env [ BROWSERSTACK_LOCAL_LOGS_FILE ] = `${ LOCAL_LOG_FILE_PREFIX } _${ github . context . job } _now.log` ;
124+ const expectedLogFileName = `${ LOCAL_LOG_FILE_PREFIX } _${ github . context . job } _now.log` ;
125+ const expectedLogFilePath = path . resolve ( path . resolve ( process . env . HOME , 'work' , 'binary' , LOCAL_BINARY_FOLDER , 'darwin' ) , expectedLogFileName ) ;
126+ const binaryControl = new BinaryControl ( ) ;
127+ binaryControl . _generateLogFileMetadata ( ) ;
128+ expect ( binaryControl . logFileName ) . to . eq ( expectedLogFileName ) ;
129+ expect ( binaryControl . logFilePath ) . to . eq ( expectedLogFilePath ) ;
130+ sinon . assert . calledWith (
131+ core . exportVariable ,
132+ BROWSERSTACK_LOCAL_LOGS_FILE ,
133+ expectedLogFileName ,
134+ ) ;
91135 } ) ;
92- const expectedLogFileName = `${ LOCAL_LOG_FILE_PREFIX } _${ github . context . job } .log` ;
93- const expectedLogFilePath = path . resolve ( path . resolve ( process . env . HOME , 'work' , 'binary' , LOCAL_BINARY_FOLDER , 'darwin' ) , expectedLogFileName ) ;
94- const binaryControl = new BinaryControl ( ) ;
95- binaryControl . _generateLogFileMetadata ( ) ;
96- expect ( binaryControl . logFileName ) . to . eq ( expectedLogFileName ) ;
97- expect ( binaryControl . logFilePath ) . to . eq ( expectedLogFilePath ) ;
98- os . platform . restore ( ) ;
99136 } ) ;
100137
101138 context ( 'Generates args string based on the input to Binary Control & the operation required, i.e. start/stop' , ( ) => {
@@ -104,10 +141,14 @@ describe('Binary Control Operations', () => {
104141 sinon . stub ( github , 'context' ) . value ( {
105142 job : 'someJobName' ,
106143 } ) ;
144+ sinon . stub ( Date , 'now' ) . returns ( 'now' ) ;
145+ sinon . stub ( core , 'exportVariable' ) ;
107146 } ) ;
108147
109148 afterEach ( ( ) => {
110149 os . platform . restore ( ) ;
150+ Date . now . restore ( ) ;
151+ core . exportVariable . restore ( ) ;
111152 } ) ;
112153
113154 context ( 'Start Operation' , ( ) => {
@@ -120,7 +161,7 @@ describe('Binary Control Operations', () => {
120161 localTesting : 'start' ,
121162 } ;
122163
123- const expectedFinalArgs = `--key someKey --only-automate --ci-plugin GitHubAction --arg1 val1 --arg2 val2 --local-identifier someIdentifier --verbose 1 --log-file ${ path . resolve ( process . env . HOME , 'work' , 'binary' , 'LocalBinaryFolder' , 'darwin' , 'BrowserStackLocal_someJobName .log' ) } ` ;
164+ const expectedFinalArgs = `--key someKey --only-automate --ci-plugin GitHubAction --arg1 val1 --arg2 val2 --local-identifier someIdentifier --verbose 1 --log-file ${ path . resolve ( process . env . HOME , 'work' , 'binary' , 'LocalBinaryFolder' , 'darwin' , 'BrowserStackLocal_someJobName_now .log' ) } ` ;
124165 const binaryControl = new BinaryControl ( stateForBinary ) ;
125166 binaryControl . _generateArgsForBinary ( ) ;
126167 expect ( binaryControl . binaryArgs ) . to . eq ( expectedFinalArgs ) ;
@@ -274,6 +315,7 @@ describe('Binary Control Operations', () => {
274315 } ) ;
275316
276317 it ( 'Downloads and sets the binary path without any error' , async ( ) => {
318+ sinon . stub ( Utils , 'checkToolInCache' ) . returns ( false ) ;
277319 sinon . stub ( tc , 'downloadTool' ) . returns ( 'downloadPath' ) ;
278320 sinon . stub ( tc , 'extractZip' ) . returns ( 'extractedPath' ) ;
279321 sinon . stub ( tc , 'cacheDir' ) . returns ( 'cachedPath' ) ;
@@ -282,16 +324,36 @@ describe('Binary Control Operations', () => {
282324 tc . downloadTool . restore ( ) ;
283325 tc . extractZip . restore ( ) ;
284326 tc . cacheDir . restore ( ) ;
327+ Utils . checkToolInCache . restore ( ) ;
328+ } ) ;
329+
330+ it ( 'Uses cached binary if it already exists (was already downloaded)' , async ( ) => {
331+ sinon . stub ( Utils , 'checkToolInCache' ) . returns ( true ) ;
332+ sinon . stub ( tc , 'downloadTool' ) . returns ( 'downloadPath' ) ;
333+ sinon . stub ( tc , 'extractZip' ) . returns ( 'extractedPath' ) ;
334+ sinon . stub ( tc , 'cacheDir' ) . returns ( 'cachedPath' ) ;
335+ await binaryControl . downloadBinary ( ) ;
336+ sinon . assert . calledWith ( core . info , 'BrowserStackLocal binary already exists in cache. Using that instead of downloading again...' ) ;
337+ sinon . assert . notCalled ( tc . downloadTool ) ;
338+ sinon . assert . notCalled ( tc . extractZip ) ;
339+ sinon . assert . notCalled ( tc . cacheDir ) ;
340+ sinon . assert . notCalled ( binaryControl . _makeDirectory ) ;
341+ tc . downloadTool . restore ( ) ;
342+ tc . extractZip . restore ( ) ;
343+ tc . cacheDir . restore ( ) ;
344+ Utils . checkToolInCache . restore ( ) ;
285345 } ) ;
286346
287347 it ( 'Throws error if download of Binary fails' , async ( ) => {
348+ sinon . stub ( Utils , 'checkToolInCache' ) . returns ( false ) ;
288349 sinon . stub ( tc , 'downloadTool' ) . throws ( Error ( 'someError' ) ) ;
289350 try {
290351 await binaryControl . downloadBinary ( ) ;
291352 } catch ( e ) {
292353 expect ( e . message ) . to . eq ( 'BrowserStackLocal binary could not be downloaded due to someError' ) ;
293354 }
294355 tc . downloadTool . restore ( ) ;
356+ Utils . checkToolInCache . restore ( ) ;
295357 } ) ;
296358 } ) ;
297359
@@ -467,6 +529,7 @@ describe('Binary Control Operations', () => {
467529 sinon . stub ( binaryControl , '_generateLogFileMetadata' ) ;
468530 sinon . stub ( io , 'rmRF' ) ;
469531 sinon . stub ( ArtifactsManager , 'uploadArtifacts' ) . returns ( true ) ;
532+ sinon . stub ( Utils , 'clearEnvironmentVariable' ) ;
470533 binaryControl . logFilePath = 'somePath' ;
471534 binaryControl . logFileName = 'someName' ;
472535 binaryControl . binaryFolder = 'someFolderPath' ;
@@ -475,6 +538,7 @@ describe('Binary Control Operations', () => {
475538 afterEach ( ( ) => {
476539 io . rmRF . restore ( ) ;
477540 ArtifactsManager . uploadArtifacts . restore ( ) ;
541+ Utils . clearEnvironmentVariable . restore ( ) ;
478542 } ) ;
479543
480544 it ( 'Uploads the log files if they exists' , async ( ) => {
@@ -487,6 +551,7 @@ describe('Binary Control Operations', () => {
487551 'someFolderPath' ,
488552 ) ;
489553 sinon . assert . calledWith ( io . rmRF , 'somePath' ) ;
554+ sinon . assert . calledWith ( Utils . clearEnvironmentVariable , BROWSERSTACK_LOCAL_LOGS_FILE ) ;
490555 fs . existsSync . restore ( ) ;
491556 } ) ;
492557
@@ -495,6 +560,7 @@ describe('Binary Control Operations', () => {
495560 await binaryControl . uploadLogFilesIfAny ( ) ;
496561 sinon . assert . notCalled ( ArtifactsManager . uploadArtifacts ) ;
497562 sinon . assert . notCalled ( io . rmRF ) ;
563+ sinon . assert . calledWith ( Utils . clearEnvironmentVariable , BROWSERSTACK_LOCAL_LOGS_FILE ) ;
498564 fs . existsSync . restore ( ) ;
499565 } ) ;
500566 } ) ;
0 commit comments