forked from chris-dalrymple/posh-gvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.Tests.ps1
1128 lines (853 loc) · 33.6 KB
/
Utils.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
. .\Utils.ps1
. .\TestUtils.ps1
Describe 'Check-GVM-API-Version' {
Context 'API offline' {
$Script:GVM_AVAILABLE = $true
$Script:GVM_API_NEW_VERSION = $false
Mock Get-GVM-API-Version
Mock Invoke-API-Call { throw 'error' } -parameterFilter { $Path -eq 'app/Version' }
Check-GVM-API-Version
It 'the error handling set the app in offline mode' {
$Script:GVM_AVAILABLE | Should be $false
}
It 'does not informs about new version' {
$Script:GVM_API_NEW_VERSION | Should Be $false
}
}
Context 'No new version' {
$backup_Global_PGVM_AUTO_SELFUPDTE = $Global:PGVM_AUTO_SELFUPDATE
$Global:PGVM_AUTO_SELFUPDATE = $true
$Script:GVM_API_NEW_VERSION = $false
Mock Get-GVM-API-Version { 1.2.2 }
Mock Invoke-API-Call { 1.2.2 } -parameterFilter { $Path -eq 'app/Version' }
Mock Invoke-Self-Update
Check-GVM-API-Version
It 'do nothing' {
Assert-MockCalled Invoke-Self-Update 0
}
It 'does not informs about new version' {
$Script:GVM_API_NEW_VERSION | Should Be $false
}
$Global:PGVM_AUTO_SELFUPDATE = $backup_Global_PGVM_AUTO_SELFUPDTE
}
Context 'New version and no auto selfupdate' {
$backup_Global_PGVM_AUTO_SELFUPDTE = $Global:PGVM_AUTO_SELFUPDATE
$Global:PGVM_AUTO_SELFUPDATE = $false
$Script:GVM_API_NEW_VERSION = $false
Mock Get-GVM-API-Version { '1.2.2' }
Mock Invoke-API-Call { '1.2.3' } -parameterFilter { $Path -eq 'broker/version' }
Check-GVM-API-Version
It 'informs about new version' {
$Script:GVM_API_NEW_VERSION | Should Be $true
}
It 'write a warning about needed update' {
Assert-VerifiableMocks
}
$Global:PGVM_AUTO_SELFUPDATE = $backup_Global_PGVM_AUTO_SELFUPDTE
}
Context 'New version and auto selfupdate' {
$backup_Global_PGVM_AUTO_SELFUPDTE = $Global:PGVM_AUTO_SELFUPDATE
$Global:PGVM_AUTO_SELFUPDATE = $true
$Script:GVM_API_NEW_VERSION = $false
Mock Get-GVM-API-Version { '1.2.2' }
Mock Invoke-API-Call { '1.2.3' } -parameterFilter { $Path -eq 'broker/version' }
Mock Invoke-Self-Update -verifiable
Check-GVM-API-Version
It 'updates self' {
Assert-VerifiableMocks
}
It 'does not informs about new version' {
$Script:GVM_API_NEW_VERSION | Should Be $false
}
$Global:PGVM_AUTO_SELFUPDATE = $backup_Global_PGVM_AUTO_SELFUPDTE
}
}
Describe 'Check-Posh-Gvm-Version' {
Context 'No new Version' {
$backup_Global_PGVM_AUTO_SELFUPDTE = $Global:PGVM_AUTO_SELFUPDATE
$Global:PGVM_AUTO_SELFUPDATE = $false
$Script:PGVM_NEW_VERSION = $false
Mock Is-New-Posh-GVM-Version-Available { $false }
Mock Invoke-Self-Update
Check-Posh-Gvm-Version
It 'does not update itself' {
Assert-MockCalled Invoke-Self-Update -Times 0
}
It 'does not informs about new version' {
$Script:PGVM_NEW_VERSION | Should Be $false
}
$Global:PGVM_AUTO_SELFUPDATE = $backup_Global_PGVM_AUTO_SELFUPDTE
}
Context 'New version and no auto selfupdate' {
$backup_Global_PGVM_AUTO_SELFUPDTE = $Global:PGVM_AUTO_SELFUPDATE
$Global:PGVM_AUTO_SELFUPDATE = $false
$Script:PGVM_NEW_VERSION = $false
Mock Is-New-Posh-GVM-Version-Available { $true }
Mock Invoke-Self-Update
Check-Posh-Gvm-Version
It 'informs about new version' {
$Script:PGVM_NEW_VERSION | Should Be $true
}
It 'does not update itself' {
Assert-MockCalled Invoke-Self-Update -Times 0
}
$Global:PGVM_AUTO_SELFUPDATE = $backup_Global_PGVM_AUTO_SELFUPDTE
}
Context 'New version and auto selfupdate' {
$backup_Global_PGVM_AUTO_SELFUPDTE = $Global:PGVM_AUTO_SELFUPDATE
$Global:PGVM_AUTO_SELFUPDATE = $true
$Script:PGVM_NEW_VERSION = $false
Mock Is-New-Posh-GVM-Version-Available { $true }
Mock Invoke-Self-Update -verifiable
Check-Posh-Gvm-Version
It 'updates self' {
Assert-VerifiableMocks
}
It 'does not informs about new version' {
$Script:PGVM_NEW_VERSION | Should Be $false
}
$Global:PGVM_AUTO_SELFUPDATE = $backup_Global_PGVM_AUTO_SELFUPDTE
}
}
Describe 'Is-New-Posh-GVM-Version-Available' {
Context 'New version available' {
$Script:PGVM_VERSION_SERVICE = 'blub'
$Script:PGVM_VERSION_PATH = 'TestDrive:VERSION.txt'
Set-Content $Script:PGVM_VERSION_PATH '1.1.1'
Mock Invoke-RestMethod { '1.2.1' } -parameterFilter { $Uri -eq 'blub' }
$result = Is-New-Posh-GVM-Version-Available
It 'returns $true' {
$result | Should Be $true
}
}
Context 'No new version available' {
$Script:PGVM_VERSION_SERVICE = 'blub'
$Script:PGVM_VERSION_PATH = 'TestDrive:VERSION.txt'
Set-Content $Script:PGVM_VERSION_PATH '1.1.1'
Mock Invoke-RestMethod { '1.1.1' } -parameterFilter { $Uri -eq 'blub' }
$result = Is-New-Posh-GVM-Version-Available
It 'returns $false' {
$result | Should Be $false
}
}
Context 'Version service error' {
$Script:PGVM_VERSION_SERVICE = 'blub'
$Script:PGVM_VERSION_PATH = 'TestDrive:VERSION.txt'
Set-Content $Script:PGVM_VERSION_PATH '1.1.1'
Mock Invoke-RestMethod { throw 'error' } -parameterFilter { $Uri -eq 'blub' }
$result = Is-New-Posh-GVM-Version-Available
It 'returns $false' {
$result | Should Be $false
}
}
}
Describe 'Get-GVM-API-Version' {
Context 'No cached version' {
$Script:GVM_API_VERSION_PATH = 'TestDrive:version.txt'
It 'returns `$null' {
Get-GVM-API-Version | Should Be $null
}
}
Context 'No cached version' {
$Script:GVM_API_VERSION_PATH = 'TestDrive:version.txt'
Set-Content $Script:GVM_API_VERSION_PATH '1.1.1'
It 'returns $null' {
Get-GVM-API-Version | Should Be 1.1.1
}
}
}
Describe 'Check-Available-Broadcast' {
Context 'Last execution was online, still online' {
$Script:GVM_ONLINE = $true
$Script:GVM_AVAILABLE = $true
Mock Get-GVM-API-Version { '1.2.3' }
Mock Invoke-Broadcast-API-Call { 'Broadcast message' }
Mock Handle-Broadcast -verifiable -parameterFilter { $Command -eq $null -and $Broadcast -eq 'Broadcast message' }
Mock Write-Offline-Broadcast
Mock Write-Online-Broadcast
Check-Available-Broadcast
It 'does not announce any mode changes' {
Assert-MockCalled Write-Offline-Broadcast 0
Assert-MockCalled Write-Online-Broadcast 0
}
It 'calls Handle-Broadcast' {
Assert-VerifiableMocks
}
}
Context 'Last execution was online, now offline' {
$Script:GVM_ONLINE = $true
$Script:GVM_AVAILABLE = $false
Mock Get-GVM-API-Version { '1.2.4' }
Mock Invoke-Broadcast-API-Call { $null }
Mock Handle-Broadcast
Mock Write-Offline-Broadcast
Mock Write-Online-Broadcast
Check-Available-Broadcast
It 'does announce offline mode' {
Assert-MockCalled Write-Offline-Broadcast 1
Assert-MockCalled Write-Online-Broadcast 0
}
It 'does not call Handle-Broadcast' {
Assert-MockCalled Handle-Broadcast 0
}
}
Context 'Last execution was offline, still offline' {
$Script:GVM_ONLINE = $false
$Script:GVM_AVAILABLE = $false
Mock Get-GVM-API-Version { '1.2.4' }
Mock Invoke-Broadcast-API-Call { $null }
Mock Handle-Broadcast
Mock Write-Offline-Broadcast
Mock Write-Online-Broadcast
Check-Available-Broadcast
It 'does not announce any mode changes' {
Assert-MockCalled Write-Offline-Broadcast 0
Assert-MockCalled Write-Online-Broadcast 0
}
It 'does not call Handle-Broadcast' {
Assert-MockCalled Handle-Broadcast 0
}
}
Context 'Last execution was offline, now online' {
$Script:GVM_ONLINE = $false
$Script:GVM_AVAILABLE = $true
Mock Get-GVM-API-Version { '1.2.5' }
Mock Invoke-Broadcast-API-Call { 'Broadcast message' }
Mock Handle-Broadcast -verifiable -parameterFilter { $Command -eq $null -and $Broadcast -eq 'Broadcast message' }
Mock Write-Offline-Broadcast
Mock Write-Online-Broadcast
Check-Available-Broadcast
It 'does announce online mode' {
Assert-MockCalled Write-Offline-Broadcast 0
Assert-MockCalled Write-Online-Broadcast 1
}
It 'calls Handle-Broadcast' {
Assert-VerifiableMocks
}
}
}
Describe 'Invoke-Self-Update' {
Context 'Selfupdate will be triggered, no force, no new version' {
Mock Update-Candidates-Cache -verifiable
Mock Write-Output -verifiable
Mock Is-New-Posh-GVM-Version-Available { $false }
Mock Invoke-Posh-Gvm-Update
Invoke-Self-Update
It 'updates the candidate cache' {
Assert-VerifiableMocks
}
It 'does not updates itself' {
Assert-MockCalled Invoke-Posh-Gvm-Update -Times 0
}
}
Context 'Selfupdate will be triggered, no force, new version' {
Mock Update-Candidates-Cache -verifiable
Mock Write-Output -verifiable
Mock Is-New-Posh-GVM-Version-Available { $true }
Mock Invoke-Posh-Gvm-Update -verifiable
Invoke-Self-Update
It 'updates the candidate cache and version' {
Assert-VerifiableMocks
}
}
Context 'Selfupdate will be triggered, force, no new version' {
Mock Update-Candidates-Cache -verifiable
Mock Write-Output -verifiable
Mock Is-New-Posh-GVM-Version-Available { $false }
Mock Invoke-Posh-Gvm-Update -verifiable
Invoke-Self-Update -Force $true
It 'updates the candidate cache and version' {
Assert-VerifiableMocks
}
}
}
Describe 'Check-Candidate-Present checks if candidate parameter is valid' {
It 'throws an error if no candidate is provided' {
{ Check-Candidate-Present } | Should Throw
}
$Script:GVM_CANDIDATES = @('grails','groovy')
It 'throws error if candidate unknown' {
{ Check-Candidate-Present java } | Should Throw
}
It 'throws no error if candidate known' {
{ Check-Candidate-Present groovy } | Should Not Throw
}
}
Describe 'Check-Version-Present checks if version parameter is defined' {
It 'throws an error if no candidate is provided' {
{ Check-Version-Present } | Should Throw
}
It 'throws no error if version provided' {
{ Check-Version-Present 2.1.3 } | Should Not Throw
}
}
Describe 'Check-Candidate-Version-Available select or vadidates a version for a candidate' {
Context 'When grails version 1.1.1 is locally available' {
Mock-Check-Candidate-Grails
Mock-Grails-1.1.1-Locally-Available $true
$result = Check-Candidate-Version-Available grails 1.1.1
It 'check candidate parameter' {
Assert-VerifiableMocks
}
It 'returns the 1.1.1' {
$result | Should Be 1.1.1
}
}
Context 'When gvm is offline and the provided version is not locally available' {
Mock-Check-Candidate-Grails
Mock-Offline
Mock-Grails-1.1.1-Locally-Available $false
It 'throws an error' {
{ Check-Candidate-Version-Available grails 1.1.1 } | Should Throw
}
It 'check candidate parameter' {
Assert-VerifiableMocks
}
}
Context 'When gvm is offline and no version is provided but there is a current version' {
Mock-Check-Candidate-Grails
Mock-Offline
Mock-Current-Grails-1.2
$result = Check-Candidate-Version-Available grails
It 'check candidate parameter' {
Assert-VerifiableMocks
}
It 'returns the current version' {
$result | Should Be 1.2
}
}
Context 'When gvm is offline and no version is provided and no current version is defined' {
Mock-Check-Candidate-Grails
Mock-Offline
Mock-No-Current-Grails
It 'throws an error' {
{ Check-Candidate-Version-Available grails } | Should Throw
}
It 'check candidate parameter' {
Assert-VerifiableMocks
}
}
Context 'When gvm is online and no version is provided' {
Mock-Check-Candidate-Grails
Mock-Online
Mock-Api-Call-Default-Grails-2.2
$result = Check-Candidate-Version-Available grails
It 'the API default is returned' {
$result | Should Be 2.2
}
It 'check candidate parameter' {
Assert-VerifiableMocks
}
}
Context 'When gvm is online and the provided version is valid' {
Mock-Check-Candidate-Grails
Mock-Online
Mock-Api-Call-Grails-1.1.1-Available $true
$result = Check-Candidate-Version-Available grails 1.1.1
It 'returns the version' {
$result | Should Be 1.1.1
}
It 'check candidate parameter' {
Assert-VerifiableMocks
}
}
Context 'When gvm is online and the provided version is invalid' {
Mock-Check-Candidate-Grails
Mock-Online
Mock-Api-Call-Grails-1.1.1-Available $false
It 'throws an error' {
{ Check-Candidate-Version-Available grails 1.1.1 } | Should Throw
}
It 'check candidate parameter' {
Assert-VerifiableMocks
}
}
}
Describe 'Get-Current-Candidate-Version reads the currently linked version' {
Context 'When current is not defined' {
Mock-PGVM-Dir
It 'returns $null if current not defined' {
Get-Current-Candidate-Version grails | Should Be $null
}
Reset-PGVM-DIR
}
Context 'When current is defined' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\2.2.2" | Out-Null
Set-Junction-Via-Mklink "$Global:PGVM_DIR\grails\current" "$Global:PGVM_DIR\grails\2.2.2"
It 'returns the liked version' {
Get-Current-Candidate-Version grails | Should Be 2.2.2
}
Reset-PGVM-Dir
}
}
Describe 'Get-Env-Candidate-Version reads the version set in $Candidate-Home' {
Context 'When GRAILS_HOME is set to a specific version' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\2.2.1" | Out-Null
Mock-Grails-Home 2.2.1
It 'returns the set version' {
Get-Env-Candidate-Version grails | Should Be 2.2.1
}
Reset-Grails-Home
Reset-PGVM-Dir
}
Context 'When GRAILS_HOME is set to current' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\2.2.1" | Out-Null
Set-Junction-Via-Mklink "$Global:PGVM_DIR\grails\current" "$Global:PGVM_DIR\grails\2.2.1"
Mock-Grails-Home current
It 'returns the version linked to current' {
Get-Env-Candidate-Version grails | Should Be 2.2.1
}
Reset-Grails-Home
Reset-PGVM-Dir
}
}
Describe 'Check-Candidate-Version-Locally-Available throws error message if not available' {
Context 'Version not available' {
Mock-Grails-1.1.1-Locally-Available $false
It 'throws an error' {
{ Check-Candidate-Version-Locally-Available grails 1.1.1 } | Should Throw
}
}
Context 'Version is available' {
Mock-Grails-1.1.1-Locally-Available $true
It 'not throws any error' {
{ Check-Candidate-Version-Locally-Available grails 1.1.1 } | Should Not Throw
}
}
}
Describe 'Is-Candidate-Version-Locally-Available check the path exists' {
Context 'No version provided' {
it 'returns $false' {
Is-Candidate-Version-Locally-Available grails | Should Be $false
}
}
Context 'COC path for grails 1.1.1 is missing' {
Mock-PGVM-Dir
it 'returns $false' {
Is-Candidate-Version-Locally-Available grails 1.1.1 | Should Be $false
}
Reset-PGVM-Dir
}
Context 'COC path for grails 1.1.1 exists' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\1.1.1" | Out-Null
it 'returns $true' {
Is-Candidate-Version-Locally-Available grails 1.1.1 | Should Be $true
}
Reset-PGVM-Dir
}
}
Describe 'Get-Installed-Candidate-Version-List' {
Context 'Version 1.1, 1.3.7 and 2.2.1 of grails installed' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\1.1" | Out-Null
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\1.3.7" | Out-Null
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\2.2.1" | Out-Null
Set-Junction-Via-Mklink "$Global:PGVM_DIR\grails\current" "$Global:PGVM_DIR\grails\2.2.1"
It 'returns list of installed versions' {
Get-Installed-Candidate-Version-List grails | Should Be 1.1,1.3.7,2.2.1
}
Reset-PGVM-Dir
}
}
Describe 'Set-Env-Candidate-Version' {
Context 'Env-Version of grails is current' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\1.3.7" | Out-Null
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\2.2.1" | Out-Null
Set-Junction-Via-Mklink "$Global:PGVM_DIR\grails\current" "$Global:PGVM_DIR\grails\2.2.1"
Mock-Grails-Home current
$backupPATH = $env:Path
Set-Env-Candidate-Version grails 1.3.7
It 'sets GRAILS_HOME' {
$env:GRAILS_HOME -eq "$Global:PGVM_DIR\grails\1.3.7"
}
It 'extends the Path' {
$env:Path -eq "$Global:PGVM_DIR\grails\1.3.7\bin"
}
$env:Path = $backupPATH
Reset-Grails-Home
Reset-PGVM-Dir
}
}
Describe 'Set-Linked-Candidate-Version' {
Context 'In a initialized PGVM-Dir' {
Mock-PGVM-Dir
Mock Set-Junction-Via-Mklink -verifiable -parameterFilter { $Candidate -eq 'grails' -and $Version -eq '2.2.1' }
Set-Linked-Candidate-Version grails 2.2.1
It 'calls Set-Junction-Via-Mklink with the correct paths' {
Assert-VerifiableMocks
}
Reset-PGVM-Dir
}
}
Describe 'Set-Junction-Via-Mklink' {
Context 'No junction for the link-path exists' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\1.3.7" | Out-Null
Set-Junction-Via-Mklink "$Global:PGVM_DIR\grails\bla" "$Global:PGVM_DIR\grails\1.3.7"
It 'creates a junction to the target location' {
(Get-Junction-Target "$Global:PGVM_DIR\grails\bla").FullName -eq "$Global:PGVM_DIR\grails\1.3.7"
}
(Get-Item "$Global:PGVM_DIR\grails\bla").Delete()
Reset-PGVM-Dir
}
Context 'A Junction for the link-path exists' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\1.3.7" | Out-Null
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\1.3.8" | Out-Null
Set-Junction-Via-Mklink "$Global:PGVM_DIR\grails\bla" "$Global:PGVM_DIR\grails\1.3.8"
Set-Junction-Via-Mklink "$Global:PGVM_DIR\grails\bla" "$Global:PGVM_DIR\grails\1.3.7"
It 'creates a junction to the target location without errors' {
(Get-Junction-Target "$Global:PGVM_DIR\grails\bla").FullName -eq "$Global:PGVM_DIR\grails\1.3.7"
}
(Get-Item "$Global:PGVM_DIR\grails\bla").Delete()
Reset-PGVM-Dir
}
}
Describe 'Get-Junction-Target' {
Context 'Provided path is a junction' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\1.3.7" | Out-Null
Set-Junction-Via-Mklink "$Global:PGVM_DIR\grails\bla" "$Global:PGVM_DIR\grails\1.3.7"
It 'returns the item of the junction correctly' {
(Get-Junction-Target "$Global:PGVM_DIR\grails\bla").FullName -eq "$Global:PGVM_DIR\grails\1.3.7"
}
(Get-Item "$Global:PGVM_DIR\grails\bla").Delete()
Reset-PGVM-Dir
}
Context 'Provided path is no junction' {
Mock-PGVM-Dir
New-Item -ItemType Directory "$Global:PGVM_DIR\grails\1.3.7" | Out-Null
It 'returns correctly a null object without exception' {
Get-Junction-Target "$Global:PGVM_DIR\grails\1.3.7" -eq $null
}
Reset-PGVM-Dir
}
}
Describe 'Get-Online-Mode check the state variables for GVM-API availablitiy and for force offline mode' {
Context 'GVM-Api unavailable but may be connected' {
$Script:GVM_AVAILABLE = $false
$Script:GVM_FORCE_OFFLINE = $false
It 'returns $false' {
Get-Online-Mode | Should Be $false
}
}
Context 'GVM-Api unavailable and may not be connected' {
$Script:GVM_AVAILABLE = $false
$Script:GVM_FORCE_OFFLINE = $true
It 'returns $false' {
Get-Online-Mode | Should Be $false
}
}
Context 'GVM-Api is available and may not be connected' {
$Script:GVM_AVAILABLE = $true
$Script:GVM_FORCE_OFFLINE = $true
It 'returns $false' {
Get-Online-Mode | Should Be $false
}
}
Context 'GVM-Api is available and may be connected' {
$Script:GVM_AVAILABLE = $true
$Script:GVM_FORCE_OFFLINE = $false
It 'returns $true' {
Get-Online-Mode | Should Be $true
}
}
}
Describe 'Check-Online-Mode throws an error when offline' {
Context 'Offline' {
Mock-Offline
It 'throws an error' {
{ Check-Online-Mode } | Should Throw
}
}
Context 'Online' {
Mock-Online
It 'throws no error' {
{ Check-Online-Mode } | Should Not Throw
}
}
}
Describe 'Invoke-API-Call helps doing calls to the GVM-API' {
Context 'Successful API call only with API path' {
$Script:PGVM_SERVICE = 'blub'
Mock Invoke-RestMethod { 'called' } -parameterFilter { $Uri -eq 'blub/na/rock' }
It 'returns the result from Invoke-RestMethod' {
Invoke-API-Call 'na/rock' | Should Be 'called'
}
}
Context 'Failed API call only with API path' {
$Script:PGVM_SERVICE = 'blub'
$Script:GVM_AVAILABLE = $true
Mock Invoke-RestMethod { throw 'error' } -parameterFilter { $Uri -eq 'blub/na/rock' }
Mock Check-Online-Mode -verifiable
Invoke-API-Call 'na/rock'
It 'sets GVM_AVAILABLE to false' {
$Script:GVM_AVAILABLE | Should Be $false
}
It 'calls Check-Online-Mode which throws an error' {
Assert-VerifiableMocks
}
}
Context 'Failed API call with API path and IgnoreFailure' {
$Script:PGVM_SERVICE = 'blub'
$Script:GVM_AVAILABLE = $true
Mock Invoke-RestMethod { throw 'error' } -parameterFilter { $Uri -eq 'blub/na/rock' }
Mock Check-Online-Mode
Invoke-API-Call 'na/rock' -IgnoreFailure
It 'sets GVM_AVAILABLE to false' {
$Script:GVM_AVAILABLE | Should Be $false
}
It 'do not call Check-Online-Mode' {
Assert-MockCalled Check-Online-Mode 0
}
}
Context 'Successful API call with API path and FilePath' {
$Script:PGVM_SERVICE = 'blub'
Mock Invoke-RestMethod -verifiable -parameterFilter { $Uri -eq 'blub/na/rock' -and $OutFile -eq 'TestDrive:a.txt' }
Invoke-API-Call 'na/rock' TestDrive:a.txt
It 'calls Invoke-RestMethod with file path' {
Assert-VerifiableMocks
}
}
}
Describe 'Cleanup-Directory' {
Context 'Directory with subdirectories and files' {
New-Item -ItemType Directory TestDrive:bla | Out-Null
New-Item -ItemType Directory TestDrive:bla\a | Out-Null
New-Item -ItemType Directory TestDrive:bla\b | Out-Null
New-Item -ItemType File TestDrive:bla\c | Out-Null
New-Item -ItemType File TestDrive:bla\a\a | Out-Null
Mock Write-Output -verifiable -parameterFilter { $InputObject -eq '2 archive(s) flushed, freeing 0 MB' }
Cleanup-Directory TestDrive:bla
It 'Cleans the Test-Path file' {
Test-Path TestDrive:bla | Should Be $False
}
It 'Write info to host' {
Assert-VerifiableMocks
}
}
}
Describe 'Handle-Broadcast' {
Context 'Cache broadcast message different than new broadcast' {
Mock-PGVM-Dir
$Script:PGVM_BROADCAST_PATH = "$Global:PGVM_DIR\broadcast.txt"
Set-Content $Script:PGVM_BROADCAST_PATH 'Old Broadcast message'
Mock Write-Output -verifiable -parameterFilter { $InputObject -eq 'New Broadcast message' }
Handle-Broadcast list 'New Broadcast message'
It 'outputs the broadcast message' {
Assert-VerifiableMocks
}
It 'sets the new broadcast message in file' {
Get-Content $Script:PGVM_BROADCAST_PATH | Should Be 'New Broadcast message'
}
Reset-PGVM-Dir
}
Context 'No cached broadcast message' {
Mock-PGVM-Dir
$Script:PGVM_BROADCAST_PATH = "$Global:PGVM_DIR\broadcast.txt"
Mock Write-Output -verifiable -parameterFilter { $InputObject -eq 'New Broadcast message' }
Handle-Broadcast list 'New Broadcast message'
It 'outputs the broadcast message' {
Assert-VerifiableMocks
}
It 'sets the new broadcast message in file' {
Get-Content $Script:PGVM_BROADCAST_PATH | Should Be 'New Broadcast message'
}
Reset-PGVM-Dir
}
Context 'b do not print the new broadcast message' {
Mock-PGVM-Dir
$Script:PGVM_BROADCAST_PATH = "$Global:PGVM_DIR\broadcast.txt"
Mock Write-Output -verifiable
Handle-Broadcast b 'New Broadcast message'
It 'no Broadcast' {
Assert-MockCalled Write-Output 0
}
It 'sets the new broadcast message in file' {
Test-Path $Script:PGVM_BROADCAST_PATH | Should Be $false
}
Reset-PGVM-Dir
}
Context 'Broadcast do nOt print the new broadcast message' {
Mock-PGVM-Dir
$Script:PGVM_BROADCAST_PATH = "$Global:PGVM_DIR\broadcast.txt"
Mock Write-Output -verifiable
Handle-Broadcast broadcast 'New Broadcast message'
It 'no Broadcast' {
Assert-MockCalled Write-Output 0
}
It 'sets the new broadcast message in file' {
Test-Path $Script:PGVM_BROADCAST_PATH | Should Be $false
}
Reset-PGVM-Dir
}
Context 'selfupdate do not print the new broadcast message' {
Mock-PGVM-Dir
$Script:PGVM_BROADCAST_PATH = "$Global:PGVM_DIR\broadcast.txt"
Mock Write-Output -verifiable
Handle-Broadcast selfupdate 'New Broadcast message'
It 'no Broadcast' {
Assert-MockCalled Write-Output 0
}
It 'sets the new broadcast message in file' {
Test-Path $Script:PGVM_BROADCAST_PATH | Should Be $false
}
Reset-PGVM-Dir
}
Context 'flush do not print the new broadcast message' {
Mock-PGVM-Dir
$Script:PGVM_BROADCAST_PATH = "$Global:PGVM_DIR\broadcast.txt"
Mock Write-Output -verifiable
Handle-Broadcast flush 'New Broadcast message'
It 'no Broadcast' {
Assert-MockCalled Write-Output 0
}
It 'sets the new broadcast message in file' {
Test-Path $Script:PGVM_BROADCAST_PATH | Should Be $false
}
Reset-PGVM-Dir
}
}
Describe 'Init-Candidate-Cache' {
Context 'Candidate cache file does not exists' {
Mock-PGVM-Dir
$Script:PGVM_CANDIDATES_PATH = "$Global:PGVM_DIR\candidates.txt"
It 'throws an error' {
{ Init-Candidate-Cache } | Should Throw
}
Reset-PGVM-Dir
}
Context 'Candidate cache file does exists' {
Mock-PGVM-Dir
$Script:PGVM_CANDIDATES_PATH = "$Global:PGVM_DIR\candidates.txt"
Set-Content $Script:PGVM_CANDIDATES_PATH 'grails,groovy,test'
$Script:GVM_CANDIDATES = $null
Init-Candidate-Cache
It 'sets `$Script:GVM_CANDIDATES' {
$Script:GVM_CANDIDATEs | Should Be grails,groovy,test
}
Reset-PGVM-Dir
}
}
Describe 'Update-Candidate-Cache' {
Context 'Checks online mode and than get version and candidates from api' {
Mock-PGVM-Dir
$Script:GVM_API_VERSION_PATH = "$Global:PGVM_DIR\version.txt"
$Script:PGVM_CANDIDATES_PATH = "$Global:PGVM_DIR\candidates.txt"
Mock Check-Online-Mode -verifiable
# Mock Invoke-API-Call -verifiable -parameterFilter { $Path -eq 'app/version' -and $FileTarget -eq "$Global:PGVM_DIR\version.txt" }
Mock Invoke-API-Call -verifiable -parameterFilter { $Path -eq 'candidates/all' -and $FileTarget -eq "$Global:PGVM_DIR\candidates.txt" }
Update-Candidates-Cache
It 'calls the Check-Online-Mode and two API paths' {
Assert-VerifiableMocks
}
Reset-PGVM-Dir
}
}
Describe 'Write-Offline-Version-List' {
Context 'no versions of grails installed' {
Mock Write-Output
Mock Get-Current-Candidate-Version { $null } -parameterFilter { $Candidate -eq 'grails' }
Mock Get-Installed-Candidate-Version-List { $null } -parameterFilter { $Candidate -eq 'grails' }
Write-Offline-Version-List grails