browserstack-cypress-cli 1.6.0 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  'use strict';
2
2
  const path = require('path');
3
+ var sandbox = require('sinon').createSandbox();
3
4
 
5
+ const request = require('request');
4
6
  const chai = require('chai'),
5
7
  expect = chai.expect,
6
8
  sinon = require('sinon'),
@@ -8,13 +10,14 @@ const chai = require('chai'),
8
10
  glob = require('glob'),
9
11
  chalk = require('chalk'),
10
12
  fs = require('fs');
11
-
13
+ const getmac = require('getmac').default;
14
+ const usageReporting = require('../../../../bin/helpers/usageReporting');
12
15
  const utils = require('../../../../bin/helpers/utils'),
13
16
  constant = require('../../../../bin/helpers/constants'),
14
17
  logger = require('../../../../bin/helpers/logger').winstonLogger,
15
18
  testObjects = require('../../support/fixtures/testObjects'),
16
19
  syncLogger = require("../../../../bin/helpers/logger").syncCliLogger;
17
-
20
+ const browserstack = require('browserstack-local');
18
21
  chai.use(chaiAsPromised);
19
22
  logger.transports['console.info'].silent = true;
20
23
 
@@ -73,6 +76,31 @@ describe('utils', () => {
73
76
  'Please use --config-file <path to browserstack.json>.'
74
77
  )
75
78
  ).to.eq('bstack_json_path_invalid');
79
+ expect(
80
+ utils.getErrorCodeFromMsg(
81
+ constant.validationMessages.INVALID_LOCAL_IDENTIFIER
82
+ )
83
+ ).to.eq('invalid_local_identifier');
84
+ expect(
85
+ utils.getErrorCodeFromMsg(
86
+ constant.validationMessages.INVALID_CLI_LOCAL_IDENTIFIER
87
+ )
88
+ ).to.eq('invalid_local_identifier');
89
+ expect(
90
+ utils.getErrorCodeFromMsg(
91
+ constant.validationMessages.INVALID_LOCAL_MODE
92
+ )
93
+ ).to.eq('invalid_local_mode');
94
+ expect(
95
+ utils.getErrorCodeFromMsg(
96
+ constant.validationMessages.INVALID_LOCAL_CONFIG_FILE
97
+ )
98
+ ).to.eq('invalid_local_config_file');
99
+ expect(
100
+ utils.getErrorCodeFromMsg(
101
+ "Invalid browserstack.json file."
102
+ )
103
+ ).to.eq("bstack_json_invalid");
76
104
  });
77
105
  });
78
106
 
@@ -145,7 +173,7 @@ describe('utils', () => {
145
173
  sandbox = sinon.createSandbox();
146
174
  sandbox.stub(utils,'getBrowserCombinations').returns(['a','b']);
147
175
  });
148
-
176
+
149
177
  afterEach(() => {
150
178
  sandbox.restore();
151
179
  sinon.restore();
@@ -330,7 +358,6 @@ describe('utils', () => {
330
358
  let args = testObjects.initSampleArgs;
331
359
 
332
360
  it('should call sendUsageReport', () => {
333
- let sandbox = sinon.createSandbox();
334
361
  sendUsageReportStub = sandbox
335
362
  .stub(utils, 'sendUsageReport')
336
363
  .callsFake(function () {
@@ -338,6 +365,8 @@ describe('utils', () => {
338
365
  });
339
366
  utils.configCreated(args);
340
367
  sinon.assert.calledOnce(sendUsageReportStub);
368
+ sandbox.restore();
369
+ sinon.restore();
341
370
  });
342
371
  });
343
372
 
@@ -545,6 +574,32 @@ describe('utils', () => {
545
574
  });
546
575
  });
547
576
 
577
+ describe('setHeaded', () => {
578
+ it('sets the headless to false', () => {
579
+ let args = {
580
+ headed: true
581
+ };
582
+ let bsConfig = {
583
+ run_settings: {}
584
+ };
585
+
586
+ utils.setHeaded(bsConfig, args);
587
+ expect(bsConfig.run_settings.headless).to.be.eq(false);
588
+ });
589
+
590
+ it('sets the headless to false', () => {
591
+ let args = {
592
+ headed: false
593
+ };
594
+ let bsConfig = {
595
+ run_settings: {}
596
+ };
597
+
598
+ utils.setHeaded(bsConfig, args);
599
+ expect(bsConfig.run_settings.headless).to.be.eq(undefined);
600
+ });
601
+ });
602
+
548
603
  describe('exportResults', () => {
549
604
  it('should export results to log/build_results.txt', () => {
550
605
  sinon.stub(fs, 'writeFileSync').returns(true);
@@ -616,12 +671,21 @@ describe('utils', () => {
616
671
  });
617
672
 
618
673
  describe('setLocal', () => {
619
- beforeEach(function () {
674
+ afterEach(function () {
675
+ sinon.restore();
620
676
  delete process.env.BROWSERSTACK_LOCAL;
621
677
  });
622
678
 
623
- afterEach(function () {
624
- delete process.env.BROWSERSTACK_LOCAL;
679
+ it('bsconfig connection_settings local_inferred as true if args local-mode true', () => {
680
+ let bsConfig = {
681
+ connection_settings: {
682
+ }
683
+ };
684
+ let args = {
685
+ localMode: "always-on"
686
+ };
687
+ utils.setLocal(bsConfig,args);
688
+ expect(bsConfig.connection_settings.local_inferred).to.be.eq(true);
625
689
  });
626
690
 
627
691
  it('should not change local in bsConfig if process.env.BROWSERSTACK_LOCAL is undefined', () => {
@@ -630,7 +694,21 @@ describe('utils', () => {
630
694
  local: true,
631
695
  },
632
696
  };
633
- utils.setLocal(bsConfig);
697
+ let args = {};
698
+ utils.setLocal(bsConfig,args);
699
+ expect(bsConfig.connection_settings.local).to.be.eq(true);
700
+ });
701
+
702
+ it('should change local to true in bsConfig if process.env.BROWSERSTACK_LOCAL is set to true', () => {
703
+ let bsConfig = {
704
+ connection_settings: {
705
+ local: false,
706
+ },
707
+ };
708
+ let args = {
709
+ };
710
+ process.env.BROWSERSTACK_LOCAL = true;
711
+ utils.setLocal(bsConfig,args);
634
712
  expect(bsConfig.connection_settings.local).to.be.eq(true);
635
713
  });
636
714
 
@@ -640,30 +718,369 @@ describe('utils', () => {
640
718
  local: true,
641
719
  },
642
720
  };
721
+ let args = {};
643
722
  process.env.BROWSERSTACK_LOCAL = false;
644
- utils.setLocal(bsConfig);
723
+ utils.setLocal(bsConfig,args);
645
724
  expect(bsConfig.connection_settings.local).to.be.eq(false);
646
725
  });
647
726
 
648
- it('should change local to true in bsConfig if process.env.BROWSERSTACK_LOCAL is set to true', () => {
727
+ it('should change local to true in bsConfig if args.local is set to true', () => {
649
728
  let bsConfig = {
650
729
  connection_settings: {
651
730
  local: false,
652
731
  },
653
732
  };
654
- process.env.BROWSERSTACK_LOCAL = true;
655
- utils.setLocal(bsConfig);
733
+ let args = { local: true };
734
+ utils.setLocal(bsConfig,args);
656
735
  expect(bsConfig.connection_settings.local).to.be.eq(true);
657
736
  });
658
737
 
659
- it('should set local to true in bsConfig if process.env.BROWSERSTACK_LOCAL is set to true & local is not set in bsConfig', () => {
738
+ it('should set local to true in bsConfig if args is set to true & local is not set in bsConfig', () => {
660
739
  let bsConfig = {
661
740
  connection_settings: {},
662
741
  };
663
- process.env.BROWSERSTACK_LOCAL = true;
664
- utils.setLocal(bsConfig);
742
+ let args = {
743
+ local: true
744
+ }
745
+ utils.setLocal(bsConfig,args);
746
+ expect(bsConfig.connection_settings.local).to.be.eq(true);
747
+ });
748
+
749
+ it('should set local to true in bsConfig if local is passed as string in bsConfig', () => {
750
+ let bsConfig = {
751
+ connection_settings: {
752
+ local: "true"
753
+ },
754
+ };
755
+ let args = {
756
+ };
757
+ utils.setLocal(bsConfig, args);
665
758
  expect(bsConfig.connection_settings.local).to.be.eq(true);
666
759
  });
760
+
761
+ });
762
+
763
+ describe('setLocalMode', () => {
764
+
765
+ afterEach(() =>{
766
+ sinon.restore();
767
+ })
768
+
769
+ it('if bsconfig local is true and args localMode is always-on then local_mode should be always-on' , () => {
770
+ let bsConfig = {
771
+ connection_settings: {
772
+ local: true,
773
+ local_mode: "on-demand"
774
+ },
775
+ };
776
+ let args = {
777
+ localMode: "always-on"
778
+ };
779
+ utils.setLocalMode(bsConfig,args);
780
+ expect(bsConfig['connection_settings']['local_mode']).to.be.eq("always-on");
781
+ });
782
+
783
+ it('if bsconfig local mode is not always-on then local_mode should be on-demand', () => {
784
+ let bsConfig = {
785
+ connection_settings: {
786
+ local: true,
787
+ },
788
+ };
789
+ let args = {};
790
+ utils.setLocalMode(bsConfig,args);
791
+ expect(bsConfig['connection_settings']['local_mode']).to.be.eq("on-demand");
792
+ });
793
+
794
+ it('setLocalMode should end up setting args.sync and sync_inferred as true', () => {
795
+ let bsConfig = {
796
+ connection_settings: {
797
+ local: true
798
+ },
799
+ };
800
+ let args = {
801
+ localMode: "always-on"
802
+ }
803
+ utils.setLocalMode(bsConfig,args);
804
+ expect(args.sync).to.be.eq(true);
805
+ expect(bsConfig.connection_settings.sync_inferred).to.be.eq(true);
806
+ });
807
+
808
+ it('if local_mode is not provided then the bsConfig local_mode_inferred changes to local_mode', () => {
809
+ let bsConfig = {
810
+ connection_settings: {
811
+ local: true
812
+ },
813
+ };
814
+ let args = {
815
+ }
816
+ let searchForOptionStub = sinon.stub(utils,"searchForOption");
817
+ searchForOptionStub.returns(false);
818
+ utils.setLocalMode(bsConfig,args);
819
+ expect(bsConfig.connection_settings.local_mode_inferred).to.be.eq("on-demand");
820
+ });
821
+
822
+ it('if local_mode is provided then the bsConfig local_mode_inferred remains unchanged', () => {
823
+ let bsConfig = {
824
+ connection_settings: {
825
+ local: true,
826
+ local_mode: "always-on"
827
+ },
828
+ };
829
+ let args = {
830
+ }
831
+ let searchForOptionStub = sinon.stub(utils,"searchForOption");
832
+ searchForOptionStub.returns(true);
833
+ utils.setLocalMode(bsConfig,args);
834
+ expect(bsConfig.connection_settings.local_mode_inferred).to.be.undefined;
835
+ });
836
+ });
837
+
838
+ describe('setupLocalTesting' ,() => {
839
+
840
+ beforeEach(function () {
841
+ sinon.restore();
842
+ sandbox.restore();
843
+ });
844
+
845
+ afterEach(function () {
846
+ sinon.restore();
847
+ sandbox.restore();
848
+ });
849
+
850
+ it('if local is true and localIdentifier is not running and start error is raised', () => {
851
+ let bsConfig = {
852
+ auth: {
853
+ access_key: "xyz"
854
+ },
855
+ connection_settings: {
856
+ local: true,
857
+ local_identifier: "xyz"
858
+ },
859
+ };
860
+ let args = {};
861
+ let checkLocalIdentifierRunningStub = sinon.stub(utils, "checkLocalIdentifierRunning");
862
+ checkLocalIdentifierRunningStub.returns(Promise.resolve(false));
863
+ let setLocalArgsStub = sinon.stub(utils,"setLocalArgs");
864
+ setLocalArgsStub.returns({});
865
+ let localBinaryStartStub = sandbox.stub().yields(
866
+ 'Key is required to start local testing!'
867
+ );
868
+ let getLocalBinaryStub = sandbox.stub(utils, 'getLocalBinary').returns({
869
+ start: localBinaryStartStub
870
+ });
871
+ let sendUsageReportStub = sandbox
872
+ .stub(utils, 'sendUsageReport')
873
+ .callsFake(function () {
874
+ return 'end';
875
+ });
876
+ utils.setupLocalTesting(bsConfig,args).catch((error) => {
877
+ expect(error).to.eq(constant.userMessages.LOCAL_START_FAILED);
878
+ sinon.assert.calledOnce(sendUsageReportStub);
879
+ sinon.assert.calledOnce(getLocalBinaryStub);
880
+ });
881
+ });
882
+
883
+ it('if local is true and localIdentifier is not running and start error is not raised', () => {
884
+ let bsConfig = {
885
+ auth: {
886
+ access_key: 'xyz',
887
+ },
888
+ connection_settings: {
889
+ local: true,
890
+ local_identifier: 'xyz',
891
+ },
892
+ };
893
+ let args = {};
894
+ let localArgs = {
895
+ key: 'abc',
896
+ localIdentifier: 'abc',
897
+ daemon: true,
898
+ };
899
+ let checkLocalIdentifierRunningStub = sinon.stub(
900
+ utils,
901
+ 'checkLocalIdentifierRunning'
902
+ );
903
+ checkLocalIdentifierRunningStub.returns(Promise.resolve(false));
904
+ let setLocalArgsStub = sinon.stub(utils, 'setLocalArgs');
905
+ setLocalArgsStub.returns(localArgs);
906
+
907
+ let localBinaryStartStub = sandbox.stub().yields(undefined);
908
+
909
+ let getLocalBinaryStub = sandbox.stub(utils, 'getLocalBinary').returns({
910
+ start: localBinaryStartStub,
911
+ });
912
+
913
+ let sendUsageReportStub = sandbox
914
+ .stub(utils, 'sendUsageReport')
915
+ .callsFake(function () {
916
+ return 'end';
917
+ });
918
+ utils.setupLocalTesting(bsConfig, args).catch((result) => {
919
+ expect(result).to.eq(undefined);
920
+ sinon.assert.calledOnce(getLocalBinaryStub);
921
+ });
922
+ });
923
+
924
+ it('if bsconfig local is true then promise should return a browserstack local object', () => {
925
+ let bsConfig = {
926
+ auth: {
927
+ access_key: "xyz"
928
+ },
929
+ connection_settings: {
930
+ local: true,
931
+ local_identifier: "xyz"
932
+ },
933
+ };
934
+ let args = {};
935
+ let checkLocalIdentifierRunningStub = sinon.stub(utils, "checkLocalIdentifierRunning");
936
+ checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
937
+ return utils.setupLocalTesting(bsConfig,args).then((result) => {
938
+ expect(result).to.be.eq(undefined);
939
+ });
940
+ });
941
+ });
942
+
943
+ describe('setLocalArgs', () => {
944
+ it('setting up local args and returning a local_args hash', () => {
945
+ let bsConfig = {
946
+ auth: {
947
+ access_key: "xyz"
948
+ },
949
+ connection_settings: {
950
+ local: true,
951
+ local_identifier: "on-demand",
952
+ local_config_file: "./local.yml"
953
+ },
954
+ };
955
+ let args = {};
956
+ let cliVersionPathStub = sinon.stub(usageReporting, "cli_version_and_path").withArgs(bsConfig);
957
+ cliVersionPathStub.returns("abc");
958
+ let local_args = utils.setLocalArgs(bsConfig, args);
959
+ expect(local_args["key"]).to.be.eq(bsConfig['auth']['access_key']);
960
+ expect(local_args["localIdentifier"]).to.be.eq(bsConfig["connection_settings"]["local_identifier"]);
961
+ expect(local_args["daemon"]).to.be.eq(true);
962
+ expect(local_args["enable-logging-for-api"]).to.be.eq(true);
963
+ expect(local_args['config-file']).to.be.eq(path.resolve('./local.yml'));
964
+ sinon.restore();
965
+ });
966
+ });
967
+
968
+ describe('stopLocalBinary' , () => {
969
+ afterEach(function () {
970
+ sinon.restore();
971
+ sandbox.restore();
972
+ });
973
+ it('stopLocalBinary promise gets resolve with undefined' ,() => {
974
+ let bsConfig = {
975
+ connection_settings: {
976
+ local: true
977
+ }
978
+ };
979
+ let checkLocalIdentifierRunningStub = sinon.stub(utils, "checkLocalIdentifierRunning");
980
+ checkLocalIdentifierRunningStub.returns(Promise.resolve(false));
981
+ let sendUsageReportStub = sandbox
982
+ .stub(utils, 'sendUsageReport')
983
+ .callsFake(function () {
984
+ return 'end';
985
+ });
986
+ return utils.stopLocalBinary(bsConfig).then((result) => {
987
+ expect(result).to.be.eq(undefined);
988
+ sinon.assert.calledOnce(sendUsageReportStub);
989
+ });
990
+ });
991
+
992
+ it('stopLocalBinary promise resolves with undefined if the bs_local isRunning is false' ,() => {
993
+ let bsConfig = {
994
+ connection_settings: {
995
+ local_mode: "on-demand"
996
+ }
997
+ };
998
+ let isRunningStub = sandbox.stub().returns(false);
999
+ let bs_local = {
1000
+ isRunning: isRunningStub,
1001
+ };
1002
+ let checkLocalIdentifierRunningStub = sinon.stub(utils, "checkLocalIdentifierRunning");
1003
+ checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
1004
+ return utils.stopLocalBinary(bsConfig, bs_local).then((result) => {
1005
+ expect(result).to.be.eq(undefined);
1006
+ });
1007
+ });
1008
+
1009
+ it('if the bs_local isRunning is true and local_mode is always-on, then gets resolve with undefined' ,() => {
1010
+ let bsConfig = {
1011
+ connection_settings: {
1012
+ local_mode: "always-on"
1013
+ }
1014
+ };
1015
+ let isRunningStub = sandbox.stub().returns(true);
1016
+ let bs_local = {
1017
+ isRunning: isRunningStub,
1018
+ }
1019
+ let checkLocalIdentifierRunningStub = sinon.stub(utils, "checkLocalIdentifierRunning");
1020
+ checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
1021
+ return utils.stopLocalBinary(bsConfig, bs_local).then((result) => {
1022
+ expect(result).to.be.eq(undefined);
1023
+ });
1024
+ });
1025
+
1026
+ it('if the bs_local isRunning is true and local_mode is not always-on and there is no stop error, then gets resolve with undefined' ,() => {
1027
+ let bsConfig = {
1028
+ connection_settings: {
1029
+ local_mode: "on-demand"
1030
+ }
1031
+ };
1032
+ let isRunningStub = sandbox.stub().returns(true);
1033
+ let stopStub = sandbox.stub().yields(undefined);
1034
+ let bs_local = {
1035
+ isRunning: isRunningStub,
1036
+ stop: stopStub
1037
+ }
1038
+ let checkLocalIdentifierRunningStub = sinon.stub(utils, "checkLocalIdentifierRunning");
1039
+ checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
1040
+ return utils.stopLocalBinary(bsConfig, bs_local).then((result) => {
1041
+ expect(result).to.be.eq(undefined);
1042
+ });
1043
+ });
1044
+
1045
+ it('if the bs_local isRunning is true and local_mode is not always-on and there is stop error, then gets resolve with stop error' ,() => {
1046
+ let bsConfig = {
1047
+ connection_settings: {
1048
+ local_mode: "on-demand"
1049
+ }
1050
+ };
1051
+ let isRunningStub = sandbox.stub().returns(true);
1052
+ let error = new Error('Local Stop Error');
1053
+ let stopStub = sandbox.stub().yields(error);
1054
+ let checkLocalIdentifierRunningStub = sinon.stub(utils, "checkLocalIdentifierRunning");
1055
+ checkLocalIdentifierRunningStub.returns(Promise.resolve(true));
1056
+ let bs_local = {
1057
+ isRunning: isRunningStub,
1058
+ stop: stopStub
1059
+ }
1060
+ let sendUsageReportStub = sandbox
1061
+ .stub(utils, 'sendUsageReport')
1062
+ .callsFake(function () {
1063
+ return 'end';
1064
+ });
1065
+ return utils.stopLocalBinary(bsConfig, bs_local, {}).then((result) => {
1066
+ expect(result).to.be.eq(constant.userMessages.LOCAL_STOP_FAILED);
1067
+ sinon.assert.calledOnce(sendUsageReportStub);
1068
+ sinon.assert.calledOnce(stopStub);
1069
+ });
1070
+ });
1071
+
1072
+ });
1073
+
1074
+ describe('generateLocalIdentifier', () => {
1075
+
1076
+ it('if the mode is always-on it returns getmac() as local-identifier', () => {
1077
+ expect(utils.generateLocalIdentifier("always-on")).to.be.eq(Buffer.from(getmac()).toString("base64"));
1078
+ });
1079
+ it('if the mode is not always-on it returns random uuidv4 as local-identifier', () => {
1080
+ let uuidv41 = utils.generateLocalIdentifier("abc");
1081
+ let uuidv42 = utils.generateLocalIdentifier("abc");
1082
+ expect(uuidv41 != uuidv42).to.be.eq(true);
1083
+ });
667
1084
  });
668
1085
 
669
1086
  describe('setLocalIdentifier', () => {
@@ -674,15 +1091,18 @@ describe('utils', () => {
674
1091
  afterEach(function () {
675
1092
  delete process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
676
1093
  });
677
- it('should not change local identifier in bsConfig if process.env.BROWSERSTACK_LOCAL_IDENTIFIER is undefined', () => {
1094
+ it('should generate local_identifier if args.localIdentifier & process.env.BROWSERSTACK_LOCAL_IDENTIFIER is undefined', () => {
678
1095
  let bsConfig = {
679
1096
  connection_settings: {
680
- local_identifier: 'local_identifier',
1097
+ local: true
681
1098
  },
682
1099
  };
683
- utils.setLocalIdentifier(bsConfig);
1100
+ let args = {};
1101
+ let generateLocalIdentifierStub = sinon.stub(utils,"generateLocalIdentifier");
1102
+ generateLocalIdentifierStub.returns("abc");
1103
+ utils.setLocalIdentifier(bsConfig,args);
684
1104
  expect(bsConfig.connection_settings.local_identifier).to.be.eq(
685
- 'local_identifier'
1105
+ "abc"
686
1106
  );
687
1107
  });
688
1108
 
@@ -692,8 +1112,9 @@ describe('utils', () => {
692
1112
  local_identifier: 'test',
693
1113
  },
694
1114
  };
1115
+ let args = {};
695
1116
  process.env.BROWSERSTACK_LOCAL_IDENTIFIER = 'local_identifier';
696
- utils.setLocalIdentifier(bsConfig);
1117
+ utils.setLocalIdentifier(bsConfig,args);
697
1118
  expect(bsConfig.connection_settings.local_identifier).to.be.eq(
698
1119
  'local_identifier'
699
1120
  );
@@ -703,12 +1124,41 @@ describe('utils', () => {
703
1124
  let bsConfig = {
704
1125
  connection_settings: {},
705
1126
  };
1127
+ let args = {};
706
1128
  process.env.BROWSERSTACK_LOCAL_IDENTIFIER = 'local_identifier';
707
- utils.setLocalIdentifier(bsConfig);
1129
+ utils.setLocalIdentifier(bsConfig,args);
708
1130
  expect(bsConfig.connection_settings.local_identifier).to.be.eq(
709
1131
  'local_identifier'
710
1132
  );
711
1133
  });
1134
+
1135
+ it('if args localIdentifier is defined then it gets assigned to bsConfig connection_settings local_identifier' , () => {
1136
+ let bsConfig = {
1137
+ local: true,
1138
+ connection_settings: {
1139
+ local_identifier: "abc"
1140
+ }
1141
+ };
1142
+ let args = {
1143
+ localIdentifier: "xyz"
1144
+ };
1145
+ utils.setLocalIdentifier(bsConfig, args);
1146
+ expect(bsConfig.connection_settings.local_identifier).to.be.eq("xyz");
1147
+ expect(bsConfig.connection_settings.local_mode).to.be.eq('always-on');
1148
+ });
1149
+
1150
+ it('if localIdentifier is defined then local_mode is set to always-on' , () => {
1151
+ let bsConfig = {
1152
+ connection_settings: {
1153
+ local: true,
1154
+ local_identifier: 'abc',
1155
+ },
1156
+ };
1157
+ let args = {}
1158
+ utils.setLocalIdentifier(bsConfig, args);
1159
+ expect(bsConfig.connection_settings.local_identifier).to.be.eq("abc");
1160
+ expect(bsConfig['connection_settings']['local_mode']).to.be.eq('always-on');
1161
+ });
712
1162
  });
713
1163
 
714
1164
  describe('setUsername', () => {
@@ -880,6 +1330,10 @@ describe('utils', () => {
880
1330
  };
881
1331
  });
882
1332
 
1333
+ afterEach(function(){
1334
+ sinon.restore();
1335
+ });
1336
+
883
1337
  it('has user provided ccf flag', () => {
884
1338
  ccfBool = true;
885
1339
 
@@ -984,6 +1438,23 @@ describe('utils', () => {
984
1438
  expect(utils.isUndefined(bsConfig.auth)).to.be.true;
985
1439
  expect(utils.isUndefined(bsConfig.run_settings.npm_dependencies)).to.be.false;
986
1440
  });
1441
+
1442
+ it ('should set connection_settings if bsConfig.connection_settings is undefined' , () => {
1443
+ let bsConfig = { run_settings: {} };
1444
+ utils.setDefaults(bsConfig, {});
1445
+ expect(utils.isUndefined(bsConfig.connection_settings)).to.be.false;
1446
+ });
1447
+
1448
+ it('should not set connection_settings if bsConfig.connection_settings is defined ', () => {
1449
+ let bsConfig = {
1450
+ run_settings: {},
1451
+ connection_settings: {
1452
+ local: "false"
1453
+ }
1454
+ };
1455
+ utils.setDefaults(bsConfig, {});
1456
+ expect(bsConfig.connection_settings).to.deep.equal({local: "false"});
1457
+ });
987
1458
  });
988
1459
 
989
1460
  describe('getNumberOfSpecFiles', () => {
@@ -1063,6 +1534,21 @@ describe('utils', () => {
1063
1534
 
1064
1535
  });
1065
1536
 
1537
+ describe('sanitizeSpecsPattern', () => {
1538
+
1539
+ it('should wrap pattern around {} when input is csv', () => {
1540
+ expect(utils.sanitizeSpecsPattern("pattern1,pattern2")).to.eq("{pattern1,pattern2}");
1541
+ });
1542
+
1543
+ it('should not wrap pattern around {} when input is single glob pattern', () => {
1544
+ expect(utils.sanitizeSpecsPattern("pattern3")).to.eq("pattern3");
1545
+ });
1546
+
1547
+ it('should return undefined when --spec is undefined', () => {
1548
+ expect(utils.sanitizeSpecsPattern(undefined)).to.eq(undefined);
1549
+ });
1550
+ });
1551
+
1066
1552
  describe('getBrowserCombinations', () => {
1067
1553
 
1068
1554
  it('returns correct number of browserCombinations for one combination', () => {
@@ -1146,4 +1632,139 @@ describe('utils', () => {
1146
1632
  expect(utils.versionChangedMessage(preferredVersion, actualVersion)).to.eq(message)
1147
1633
  });
1148
1634
  })
1635
+
1636
+ describe('#isJSONInvalid', () => {
1637
+ it('JSON is valid when error is parallel misconfiguration', () => {
1638
+ let error = constant.validationMessages.INVALID_PARALLELS_CONFIGURATION;
1639
+ let args = {"parallels": 4}
1640
+ expect(utils.isJSONInvalid(error, args)).to.eq(false)
1641
+ });
1642
+
1643
+ it('JSON is valid when local is not set for localhost url', () => {
1644
+ let error = constant.validationMessages.LOCAL_NOT_SET.replace("<baseUrlValue>", "localhost:4000");
1645
+ expect(utils.isJSONInvalid(error, {})).to.eq(false)
1646
+ });
1647
+
1648
+ it('JSON is invalid for errors apart from Local or Prallell misconfiguration', () => {
1649
+ let error = constant.validationMessages.INCORRECT_AUTH_PARAMS;
1650
+ expect(utils.isJSONInvalid(error, {})).to.eq(true)
1651
+ });
1652
+
1653
+ it('JSON is invalid if local identifier is invalid', () =>{
1654
+ let error = constant.validationMessages.INVALID_CLI_LOCAL_IDENTIFIER;
1655
+ expect(utils.isJSONInvalid(error,{})).to.eq(false);
1656
+ });
1657
+
1658
+ it('JSON is invalid if local mode is invalid', () =>{
1659
+ let error = constant.validationMessages.INVALID_LOCAL_MODE;
1660
+ expect(utils.isJSONInvalid(error,{})).to.eq(false);
1661
+ });
1662
+ })
1663
+
1664
+ describe('#deleteBaseUrlFromError', () => {
1665
+ it('Replace baseUrl in Local error string', () => {
1666
+ let error = constant.validationMessages.LOCAL_NOT_SET;
1667
+ expect(utils.deleteBaseUrlFromError(error)).to.match(/To test on BrowserStack/)
1668
+ });
1669
+
1670
+ it('should not replace baseUrl in other error string', () => {
1671
+ let error = constant.validationMessages.NOT_VALID_JSON;
1672
+ expect(utils.deleteBaseUrlFromError(error)).not.to.match(/To test on BrowserStack/)
1673
+ });
1674
+ });
1675
+
1676
+ describe('#checkLocalIdentifierRunning', () => {
1677
+ afterEach(() =>{
1678
+ sinon.restore();
1679
+ });
1680
+ it('if the bsConfig localIdentifier is not present within the response body then function should resolve with false' , () => {
1681
+ const responseObject = {
1682
+ statusCode: 200,
1683
+ headers: {
1684
+ 'content-type': 'application/json'
1685
+ }
1686
+ };
1687
+ const responseBody = {
1688
+ status: 'success',
1689
+ instances: [
1690
+ {
1691
+ localIdentifier: 'abcdef',
1692
+ },
1693
+ {
1694
+ localIdentifier: 'ghij',
1695
+ },
1696
+ {
1697
+ localIdentifier: 'lmno',
1698
+ }
1699
+ ]
1700
+ };
1701
+ sinon.stub(request, 'get')
1702
+ .yields(undefined, responseObject, JSON.stringify(responseBody));
1703
+
1704
+ let bsConfig = {
1705
+ auth: {
1706
+ access_key: "abcd",
1707
+ username: "abcd"
1708
+ }
1709
+ };
1710
+
1711
+ let localIdentifier = "abcd";
1712
+ return utils.checkLocalIdentifierRunning(bsConfig, localIdentifier).then((result) => {
1713
+ expect(result).to.be.eq(false);
1714
+ });
1715
+ });
1716
+
1717
+ it('if the bsConfig localIdentifier if present within the response body then the function should resolve with true' , () => {
1718
+ const responseObject = {
1719
+ statusCode: 200,
1720
+ headers: {
1721
+ 'content-type': 'application/json'
1722
+ }
1723
+ };
1724
+ const responseBody = {
1725
+ status: 'success',
1726
+ instances: [
1727
+ {
1728
+ localIdentifier: 'abcdef',
1729
+ },
1730
+ {
1731
+ localIdentifier: 'ghij',
1732
+ },
1733
+ {
1734
+ localIdentifier: 'lmno',
1735
+ }
1736
+ ]
1737
+ };
1738
+ sinon.stub(request, 'get')
1739
+ .yields(undefined, responseObject, JSON.stringify(responseBody));
1740
+
1741
+ let bsConfig = {
1742
+ auth: {
1743
+ access_key: "abcd",
1744
+ username: "abcd"
1745
+ }
1746
+ };
1747
+
1748
+ let localIdentifier = "lmno";
1749
+ return utils.checkLocalIdentifierRunning(bsConfig, localIdentifier).then((result) => {
1750
+ expect(result).to.be.eq(true);
1751
+ });
1752
+ });
1753
+ });
1754
+
1755
+ describe('setLocalConfigFile', () => {
1756
+ it('the args localConfigfile should be assigned to bsconfig connection_settigs local_config_file', () => {
1757
+ let bsConfig = {
1758
+ connection_settings: {
1759
+ local_config_file: "efgh"
1760
+ }
1761
+ };
1762
+ let args = {
1763
+ localConfigFile: "abcd"
1764
+ };
1765
+ utils.setLocalConfigFile(bsConfig, args);
1766
+ expect(args.localConfigFile).to.be.eql(bsConfig.connection_settings.local_config_file);
1767
+ });
1768
+ });
1769
+
1149
1770
  });