@@ -11,6 +11,7 @@ const fs = require('fs');
11
11
const BinaryControl = require ( '../src/binaryControl' ) ;
12
12
const ArtifactsManager = require ( '../src/artifacts' ) ;
13
13
const constants = require ( '../config/constants' ) ;
14
+ const Utils = require ( '../src/utils' ) ;
14
15
15
16
const {
16
17
BINARY_LINKS ,
@@ -22,6 +23,9 @@ const {
22
23
ALLOWED_INPUT_VALUES : {
23
24
LOCAL_TESTING ,
24
25
} ,
26
+ ENV_VARS : {
27
+ BROWSERSTACK_LOCAL_LOGS_FILE ,
28
+ } ,
25
29
} = constants ;
26
30
27
31
describe ( 'Binary Control Operations' , ( ) => {
@@ -84,18 +88,51 @@ describe('Binary Control Operations', () => {
84
88
os . platform . restore ( ) ;
85
89
} ) ;
86
90
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
+ ) ;
91
135
} ) ;
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 ( ) ;
99
136
} ) ;
100
137
101
138
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', () => {
104
141
sinon . stub ( github , 'context' ) . value ( {
105
142
job : 'someJobName' ,
106
143
} ) ;
144
+ sinon . stub ( Date , 'now' ) . returns ( 'now' ) ;
145
+ sinon . stub ( core , 'exportVariable' ) ;
107
146
} ) ;
108
147
109
148
afterEach ( ( ) => {
110
149
os . platform . restore ( ) ;
150
+ Date . now . restore ( ) ;
151
+ core . exportVariable . restore ( ) ;
111
152
} ) ;
112
153
113
154
context ( 'Start Operation' , ( ) => {
@@ -120,7 +161,7 @@ describe('Binary Control Operations', () => {
120
161
localTesting : 'start' ,
121
162
} ;
122
163
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' ) } ` ;
124
165
const binaryControl = new BinaryControl ( stateForBinary ) ;
125
166
binaryControl . _generateArgsForBinary ( ) ;
126
167
expect ( binaryControl . binaryArgs ) . to . eq ( expectedFinalArgs ) ;
@@ -274,6 +315,7 @@ describe('Binary Control Operations', () => {
274
315
} ) ;
275
316
276
317
it ( 'Downloads and sets the binary path without any error' , async ( ) => {
318
+ sinon . stub ( Utils , 'checkToolInCache' ) . returns ( false ) ;
277
319
sinon . stub ( tc , 'downloadTool' ) . returns ( 'downloadPath' ) ;
278
320
sinon . stub ( tc , 'extractZip' ) . returns ( 'extractedPath' ) ;
279
321
sinon . stub ( tc , 'cacheDir' ) . returns ( 'cachedPath' ) ;
@@ -282,16 +324,36 @@ describe('Binary Control Operations', () => {
282
324
tc . downloadTool . restore ( ) ;
283
325
tc . extractZip . restore ( ) ;
284
326
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 ( ) ;
285
345
} ) ;
286
346
287
347
it ( 'Throws error if download of Binary fails' , async ( ) => {
348
+ sinon . stub ( Utils , 'checkToolInCache' ) . returns ( false ) ;
288
349
sinon . stub ( tc , 'downloadTool' ) . throws ( Error ( 'someError' ) ) ;
289
350
try {
290
351
await binaryControl . downloadBinary ( ) ;
291
352
} catch ( e ) {
292
353
expect ( e . message ) . to . eq ( 'BrowserStackLocal binary could not be downloaded due to someError' ) ;
293
354
}
294
355
tc . downloadTool . restore ( ) ;
356
+ Utils . checkToolInCache . restore ( ) ;
295
357
} ) ;
296
358
} ) ;
297
359
@@ -467,6 +529,7 @@ describe('Binary Control Operations', () => {
467
529
sinon . stub ( binaryControl , '_generateLogFileMetadata' ) ;
468
530
sinon . stub ( io , 'rmRF' ) ;
469
531
sinon . stub ( ArtifactsManager , 'uploadArtifacts' ) . returns ( true ) ;
532
+ sinon . stub ( Utils , 'clearEnvironmentVariable' ) ;
470
533
binaryControl . logFilePath = 'somePath' ;
471
534
binaryControl . logFileName = 'someName' ;
472
535
binaryControl . binaryFolder = 'someFolderPath' ;
@@ -475,6 +538,7 @@ describe('Binary Control Operations', () => {
475
538
afterEach ( ( ) => {
476
539
io . rmRF . restore ( ) ;
477
540
ArtifactsManager . uploadArtifacts . restore ( ) ;
541
+ Utils . clearEnvironmentVariable . restore ( ) ;
478
542
} ) ;
479
543
480
544
it ( 'Uploads the log files if they exists' , async ( ) => {
@@ -487,6 +551,7 @@ describe('Binary Control Operations', () => {
487
551
'someFolderPath' ,
488
552
) ;
489
553
sinon . assert . calledWith ( io . rmRF , 'somePath' ) ;
554
+ sinon . assert . calledWith ( Utils . clearEnvironmentVariable , BROWSERSTACK_LOCAL_LOGS_FILE ) ;
490
555
fs . existsSync . restore ( ) ;
491
556
} ) ;
492
557
@@ -495,6 +560,7 @@ describe('Binary Control Operations', () => {
495
560
await binaryControl . uploadLogFilesIfAny ( ) ;
496
561
sinon . assert . notCalled ( ArtifactsManager . uploadArtifacts ) ;
497
562
sinon . assert . notCalled ( io . rmRF ) ;
563
+ sinon . assert . calledWith ( Utils . clearEnvironmentVariable , BROWSERSTACK_LOCAL_LOGS_FILE ) ;
498
564
fs . existsSync . restore ( ) ;
499
565
} ) ;
500
566
} ) ;
0 commit comments