DscResource.Tests/Tests/Unit/AppVeyor.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
$script:ModuleName = 'AppVeyor'
$script:moduleRootPath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent

Import-Module -Name (Join-Path -Path $script:moduleRootPath -ChildPath "$($script:ModuleName).psm1") -Force

InModuleScope $script:ModuleName {
    Describe 'AppVeyor\Invoke-AppveyorTestScriptTask' {
        BeforeAll {
            <#
                Mocking $env:APPVEYOR_BUILD_FOLDER to point to $TestDrive
                to be able to test relative path.
            #>

            $originalAppVeyorBuildFolder = $env:APPVEYOR_BUILD_FOLDER
            $env:APPVEYOR_BUILD_FOLDER = $TestDrive

            New-Item -Path (Join-Path -Path $TestDrive -ChildPath 'Modules') -ItemType Directory -Force
            New-Item -Path (Join-Path -Path $TestDrive -ChildPath 'Tests\Integration') -ItemType Directory -Force
            New-Item -Path (Join-Path -Path $TestDrive -ChildPath 'Tests\Unit') -ItemType Directory -Force

            $mockModuleFolder = 'Modules'


            # Added functions that are specific to AppVeyor environment so mocks would not fail
            function Add-AppveyorTest
            {
            }

            <#
                Mocking Write-Info so the text output can be changed to
                differentiate from the real test output.
            #>

            Mock -CommandName Write-Info -MockWith {
                Write-Host -Object "[Mocked Build Info] [UTC $([System.DateTime]::UtcNow)] $message"
            }

            Mock -CommandName Add-AppveyorTest
            Mock -CommandName Push-TestArtifact
            Mock -CommandName Invoke-UploadCoveCoveIoReport
            Mock -CommandName New-DscSelfSignedCertificate
            Mock -CommandName Initialize-LocalConfigurationManager
            Mock -CommandName Write-Warning
            Mock -CommandName Write-Verbose
            Mock -CommandName Export-CodeCovIoJson -MockWith {
                return Join-Path -Path $TestDrive -ChildPath 'CodecovReport.json'
            }

            $mockMissedCommands = @(
                [PSCustomObject] @{
                    File     = 'TestFile1'
                    Function = 'TestFunction1'
                    Line     = '9999'
                    Command  = 'Command1'
                }

                [PSCustomObject] @{
                    File     = 'TestFile2'
                    Function = 'TestFunction2'
                    Line     = '8888'
                    Command  = 'Command2'
                }
            )

            $mockHitCommands = @(
                [PSCustomObject] @{
                    File     = 'TestFile1'
                    Function = 'TestFunction1'
                    Line     = '2222'
                    Command  = 'Command3'
                }
            )

            $mockAnalyzedFiles = @(
                'TestFile1'
                'TestFile2'
            )

            $mockTestResult = @{
                TestResult   = @(
                    @{
                        ErrorRecord            = $null
                        ParameterizedSuiteName = $null
                        Describe               = 'MyModule\TestFunction'
                        Parameters             = [ordered] @{}
                        Passed                 = $true
                        Show                   = 'All'
                        FailureMessage         = $null
                        Time                   = New-TimeSpan -Seconds 6
                        Name                   = 'Should return the correct test properties'
                        Result                 = 'Passed'
                        Context                = 'When called with test parameters'
                        StackTrace             = $null
                    }
                )
                FailedCount  = 0
                CodeCoverage = New-Object -TypeName Object |
                    Add-Member -MemberType NoteProperty -Name 'NumberOfCommandsAnalyzed' -Value 3 -PassThru |
                    Add-Member -MemberType NoteProperty -Name 'NumberOfFilesAnalyzed' -Value 2 -PassThru |
                    Add-Member -MemberType NoteProperty -Name 'NumberOfCommandsExecuted' -Value 1 -PassThru |
                    Add-Member -MemberType NoteProperty -Name 'NumberOfCommandsMissed' -Value 2 -PassThru |
                    Add-Member -MemberType NoteProperty -Name 'MissedCommands' -Value $mockMissedCommands -PassThru |
                    Add-Member -MemberType NoteProperty -Name 'HitCommands' -Value $mockHitCommands -PassThru |
                    Add-Member -MemberType NoteProperty -Name 'AnalyzedFiles' -Value $mockAnalyzedFiles -PassThru -Force
            }
        }

        AfterAll {
            <#
                This will reset the APPVEYOR_BUILD_FOLDER or remove the environment
                variable if it was not set to begin with.
            #>

            $env:APPVEYOR_BUILD_FOLDER = $originalAppVeyorBuildFolder
        }

        # Regression test for issue #229
        Context 'When called with model type as ''Harness''' {
            BeforeAll {
                Mock -CommandName Copy-Item
                Mock -CommandName Remove-Item
                Mock -CommandName Import-Module -ParameterFilter {
                    $Name -match 'TestHarness\.psm1'
                }

                <#
                    This is a mock of the default harness function name
                    that normally exists in the module TestHarness.psm1.
                #>

                function Invoke-TestHarness
                {
                    return [PSCustomObject] $mockTestResult
                }
            }

            It 'Should call the correct mocks' {
                $testParameters = @{
                    Type           = 'Harness'
                    MainModulePath = $mockModuleFolder
                    CodeCovIo      = $true
                }

                {
                    Invoke-AppveyorTestScriptTask @testParameters
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Add-AppveyorTest -Exactly -Times 1 -Scope It
                Assert-MockCalled -CommandName Push-TestArtifact -Exactly -Times 1 -Scope It
                Assert-MockCalled -CommandName Export-CodeCovIoJson -Exactly -Times 1 -Scope It
                Assert-MockCalled -CommandName Invoke-UploadCoveCoveIoReport -Exactly -Times 1 -Scope It
                Assert-MockCalled -CommandName New-DscSelfSignedCertificate -Exactly -Times 1 -Scope It
                Assert-MockCalled -CommandName Initialize-LocalConfigurationManager -Exactly -Times 1 -Scope It
            }
        }

        Context 'When there is no Tests folder' {
            BeforeAll {
                Mock -CommandName Copy-Item
                Mock -CommandName Remove-Item
                Mock -CommandName Import-Module -ParameterFilter {
                    $Name -match 'TestHarness\.psm1'
                }

                Mock -CommandName Test-Path -MockWith {
                    return $false
                }

                <#
                    This is a mock of the default harness function name
                    that normally exists in the module TestHarness.psm1.
                #>

                function Invoke-TestHarness
                {
                    return [PSCustomObject] $mockTestResult
                }
            }

            It 'Should call the correct mock and write a warning' {
                $testParameters = @{
                    Type           = 'Harness'
                    MainModulePath = $mockModuleFolder
                    CodeCovIo      = $true
                }

                {
                    Invoke-AppveyorTestScriptTask @testParameters
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Write-Warning -ParameterFilter {
                    $Message -eq 'The ''Tests'' folder is missing, the test framework will only run the common tests.'
                } -Exactly -Times 1 -Scope It
            }
        }

        Context 'When CodeCoverage requires additional directories' {
            $pesterReturnedValues = @{
                PassedCount = 1
                FailedCount = 0
            }

            BeforeAll {
                Mock -CommandName Test-Path -MockWith {
                    return $true
                } -ParameterFilter {
                    $Path -eq (Join-Path -Path $TestDrive -ChildPath 'Tests')
                }

                Mock -CommandName Test-Path -MockWith {
                    return $false
                }

                Mock -CommandName Get-ChildItem -MockWith {
                    return 'file.Tests.ps1'
                }

                Mock -CommandName Get-ChildItem -MockWith {
                    return $null
                } -ParameterFilter {
                    $Include -eq '*.config.ps1'
                }

                Mock -CommandName Invoke-Pester -MockWith {
                    return $pesterReturnedValues
                }

                # Making sure there is no output when performing tests
                Mock -CommandName Test-IsRepositoryDscResourceTests -MockWith {
                    return $false
                }
            } # End BeforeAll

            AfterEach {
                Assert-MockCalled -CommandName Test-Path -Times 1 -Exactly -Scope It -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCClassResources"
                }

                Assert-MockCalled -CommandName Test-Path -Times 1 -Exactly -Scope It -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCResources"
                }

                Assert-MockCalled -CommandName Test-Path -Times 1 -Exactly -Scope It -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\Modules"
                }

                Assert-MockCalled -CommandName Get-ChildItem -Times 1 -Exactly -Scope It -ParameterFilter {
                    $Include -eq '*.config.ps1'
                }

                Assert-MockCalled -CommandName Get-ChildItem -Times 2 -Exactly -Scope It
            } # End AfterEach

            It 'Should only include DSCClassResources for CodeCoverage' {
                Mock -CommandName Test-Path -MockWith {
                    return $true
                } -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCClassResources"
                }

                Mock -CommandName Test-Path -MockWith {
                    return $false
                } -ParameterFilter {
                    $Path -and $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCResources"
                }

                {
                    Invoke-AppveyorTestScriptTask -CodeCoverage
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Invoke-Pester -Times 1 -Exactly -Scope It -ParameterFilter {
                    $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\DSCClassResources\**\*.psm1"
                }

                Assert-MockCalled -CommandName Invoke-Pester -Times 0 -Exactly -Scope It -ParameterFilter {
                    $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\DSCResources\**\*.psm1"
                }
            } # End It DSCClassResources only

            It 'Should only include DSCResources for CodeCoverage' {
                Mock -CommandName Test-Path -MockWith {
                    return $false
                } -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCClassResources"
                }

                Mock -CommandName Test-Path -MockWith {
                    return $true
                } -ParameterFilter {
                    $Path -and $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCResources"
                }

                {
                    Invoke-AppveyorTestScriptTask -CodeCoverage
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Invoke-Pester -Times 0 -Exactly -Scope It -ParameterFilter {
                    $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\DSCClassResources\**\*.psm1"
                }

                Assert-MockCalled -CommandName Invoke-Pester -Times 1 -Exactly -Scope It -ParameterFilter {
                    $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\DSCResources\**\*.psm1"
                }
            } # End It DSCResources only

            It 'Should include DSCResources and DSCClassResources for CodeCoverage' {
                Mock -CommandName Test-Path -MockWith {
                    return $true
                } -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCClassResources"
                }

                Mock -CommandName Test-Path -MockWith {
                    return $true
                } -ParameterFilter {
                    $Path -and $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCResources"
                }

                {
                    Invoke-AppveyorTestScriptTask -CodeCoverage
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Invoke-Pester -Times 1 -Exactly -Scope It -ParameterFilter {
                    $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\DSCClassResources\**\*.psm1"
                }

                Assert-MockCalled -CommandName Invoke-Pester -Times 1 -Exactly -Scope It -ParameterFilter {
                    $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\DSCResources\**\*.psm1"
                }
            } # End It Both DSCResources and DSCClassResources

            It 'Should include all default paths for CodeCoverage' {
                Mock -CommandName Test-Path -MockWith {
                    return $true
                }

                {
                    Invoke-AppveyorTestScriptTask -CodeCoverage
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Invoke-Pester -Times 1 -Exactly -Scope It -ParameterFilter {
                    $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\DSCClassResources\**\*.psm1" `
                    -and $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\DSCResources\**\*.psm1" `
                    -and $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\Modules\**\*.psm1"
                }
            } # End It Both DSCResources and DSCClassResources
        } # End Context When CodeCoverage requires additional directories

        Context 'When Invoke-AppveyorTestScriptTask is called with parameter CodeCoverage and CodeCoveragePath ' {
            $pesterReturnedValues = @{
                PassedCount = 1
                FailedCount = 0
            }

            BeforeAll {
                Mock -CommandName Test-Path -MockWith {
                    return $false
                }

                Mock -CommandName Get-ChildItem -MockWith {
                    return 'file.Tests.ps1'
                }

                Mock -CommandName Get-ChildItem -MockWith {
                    return $null
                } -ParameterFilter {
                    $Include -eq '*.config.ps1'
                }

                Mock -CommandName Invoke-Pester -MockWith {
                    return $pesterReturnedValues
                }

                # Making sure there is no output when performing tests
                Mock -CommandName Test-IsRepositoryDscResourceTests -MockWith {
                    return $false
                }
            } # End BeforeAll

            AfterEach {
                Assert-MockCalled -CommandName Test-Path -Times 0 -Exactly -Scope It -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCClassResources"
                }

                Assert-MockCalled -CommandName Test-Path -Times 0 -Exactly -Scope It -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\DSCResources"
                }

                Assert-MockCalled -CommandName Test-Path -Times 0 -Exactly -Scope It -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\Modules"
                }

                Assert-MockCalled -CommandName Test-Path -Times 1 -Exactly -Scope It -ParameterFilter {
                    $Path -eq "$env:APPVEYOR_BUILD_FOLDER\OtherPath"
                }

                Assert-MockCalled -CommandName Get-ChildItem -Times 1 -Exactly -Scope It -ParameterFilter {
                    $Include -eq '*.config.ps1'
                }

                Assert-MockCalled -CommandName Get-ChildItem -Times 2 -Exactly -Scope It
            } # End AfterEach

            It 'Should only include the correct specified path for CodeCoverage' {
                Mock -CommandName Test-Path -MockWith { return $true }

                {
                    Invoke-AppveyorTestScriptTask -CodeCoverage -CodeCoveragePath @('OtherPath')
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Invoke-Pester -Times 1 -Exactly -Scope It -ParameterFilter {
                    $CodeCoverage -contains "$env:APPVEYOR_BUILD_FOLDER\OtherPath\**\*.psm1"
                }
            }
        } # End Context When CodeCoverage requires additional directories

        Context 'When called with model type as ''Default''' {
            BeforeAll {
                Mock -CommandName Write-Warning
                Mock -CommandName Invoke-Pester -MockWith {
                    return $mockTestResult
                }
            }

            AfterEach {
                Assert-MockCalled -CommandName Invoke-Pester -Exactly -Times 1 -Scope It
            }

            Context 'When called with default values' {
                It 'Should not throw exception and call the correct mocks' {
                    {
                        Invoke-AppveyorTestScriptTask
                    } | Should -Not -Throw

                    Assert-MockCalled -CommandName Add-AppveyorTest -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Push-TestArtifact -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName New-DscSelfSignedCertificate -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Initialize-LocalConfigurationManager -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Export-CodeCovIoJson -Exactly -Times 0 -Scope It
                    Assert-MockCalled -CommandName Invoke-UploadCoveCoveIoReport -Exactly -Times 0 -Scope It
                    Assert-MockCalled -CommandName Write-Warning -Exactly -Times 0 -Scope It
                }

                Context 'When configuration needs a module to compile' {
                    BeforeAll {
                        New-Item -Path (Join-Path -Path $TestDrive -ChildPath 'Tests\Integration\MockResource1.config.ps1') -ItemType File -Force

                        Mock -CommandName Get-ResourceModulesInConfiguration -MockWith {
                            return @{
                                Name = 'MyModule'
                            }
                        }

                        Mock -CommandName Install-DependentModule
                    }

                    It 'Should not throw exception and call the correct mocks' {
                        {
                            Invoke-AppveyorTestScriptTask
                        } | Should -Not -Throw

                        Assert-MockCalled -CommandName Get-ResourceModulesInConfiguration -Exactly -Times 1 -Scope It
                        Assert-MockCalled -CommandName Install-DependentModule -Exactly -Times 1 -Scope It
                    }
                }
            }

            Context 'When called with the parameters CodeCoverage and CodeCovIo ' {
                It 'Should not throw exception and call the correct mocks' {
                    $testParameters = @{
                        CodeCoverage = $true
                        CodeCovIo    = $true
                    }

                    {
                        Invoke-AppveyorTestScriptTask @testParameters
                    } | Should -Not -Throw

                    Assert-MockCalled -CommandName Add-AppveyorTest -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Push-TestArtifact -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Export-CodeCovIoJson -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Invoke-UploadCoveCoveIoReport -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName New-DscSelfSignedCertificate -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Initialize-LocalConfigurationManager -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Write-Warning -Exactly -Times 1 -Scope It
                }
            }

            Context 'When called with the parameters ExcludeTag' {
                It 'Should not throw exception and call the correct mocks' {
                    $testParameters = @{
                        ExcludeTag = @('Markdown')
                    }

                    {
                        Invoke-AppveyorTestScriptTask @testParameters
                    } | Should -Not -Throw

                    Assert-MockCalled -CommandName Add-AppveyorTest -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Push-TestArtifact -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Export-CodeCovIoJson -Exactly -Times 0 -Scope It
                    Assert-MockCalled -CommandName Invoke-UploadCoveCoveIoReport -Exactly -Times 0 -Scope It
                    Assert-MockCalled -CommandName New-DscSelfSignedCertificate -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Initialize-LocalConfigurationManager -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Write-Warning -Exactly -Times 0 -Scope It
                    Assert-MockCalled -CommandName Invoke-Pester -ParameterFilter {
                        $ExcludeTag[0] -eq 'Markdown'
                    } -Exactly -Times 1
                }
            }

            Context 'When called with parameters RunTestInOrder' {
                BeforeAll {
                    '
                    [Microsoft.DscResourceKit.IntegrationTest(OrderNumber = 1)]
                    param()
                    '
 | Set-Content -Path (Join-Path -Path $TestDrive -ChildPath 'Tests\Integration\MockResource1.Integration.Tests.ps1') -Encoding UTF8 -Force

                    '
                    [Microsoft.DscResourceKit.IntegrationTest(OrderNumber = 2)]
                    param()
                    '
 | Set-Content -Path (Join-Path -Path $TestDrive -ChildPath 'Tests\Integration\MockResource2.Integration.Tests.ps1') -Encoding UTF8 -Force
                }

                It 'Should not throw exception and call the correct mocks' {
                    $testParameters = @{
                        RunTestInOrder = $true
                    }

                    {
                        Invoke-AppveyorTestScriptTask @testParameters
                    } | Should -Not -Throw

                    Assert-MockCalled -CommandName Add-AppveyorTest -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Push-TestArtifact -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Export-CodeCovIoJson -Exactly -Times 0 -Scope It
                    Assert-MockCalled -CommandName Invoke-UploadCoveCoveIoReport -Exactly -Times 0 -Scope It
                    Assert-MockCalled -CommandName New-DscSelfSignedCertificate -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Initialize-LocalConfigurationManager -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Write-Warning -Exactly -Times 0 -Scope It
                }
            }

            Context 'When called with parameters RunTestInOrder and CodeCoverage, and using containers' {
                BeforeAll {
                    # Mock the Meta.Tests.ps1 from DscResource.Tests.
                    New-Item -Path (Join-Path -Path $TestDrive -ChildPath 'DSCResource.Tests\Meta.Tests.ps1') -ItemType File -Force

                    <#
                        Mock some empty resource module files. These files are
                        used by the tested code, to mock coverage.
                    #>

                    New-Item -Path (Join-Path -Path $TestDrive -ChildPath 'MockResource1.psm1') -ItemType File -Force
                    New-Item -Path (Join-Path -Path $TestDrive -ChildPath 'MockResource2.psm1') -ItemType File -Force

                    <#
                        Mock some resource integration and unit tests files.
                        Two containers are mocked, each container is running
                        one unit test, and one integration test.
                    #>

                    $containerName1 = 'Container1'
                    $containerIdentifier1 = 1111
                    $containerName2 = 'Container2'
                    $containerIdentifier2 = 2222

                    ('
                    [Microsoft.DscResourceKit.UnitTest(ContainerName = ''{0}'', ContainerImage = ''microsoft/windowsservercore'')]
                    param()
                    '
 -f $containerName1) | Set-Content -Path (Join-Path -Path $TestDrive -ChildPath 'Tests\Unit\MockResource1.Tests.ps1') -Encoding UTF8 -Force

                    ('
                    [Microsoft.DscResourceKit.UnitTest(ContainerName = ''{0}'', ContainerImage = ''microsoft/windowsservercore'')]
                    param()
                    '
 -f $containerName2)| Set-Content -Path (Join-Path -Path $TestDrive -ChildPath 'Tests\Unit\MockResource2.Tests.ps1') -Encoding UTF8 -Force

                    ('
                    [Microsoft.DscResourceKit.IntegrationTest(OrderNumber = 1, ContainerName = ''{0}'', ContainerImage = ''microsoft/windowsservercore'')]
                    param()
                    '
 -f $containerName1) | Set-Content -Path (Join-Path -Path $TestDrive -ChildPath 'Tests\Integration\MockResource1.Integration.Tests.ps1') -Encoding UTF8 -Force

                    ('
                    [Microsoft.DscResourceKit.IntegrationTest(OrderNumber = 2, ContainerName = ''{0}'', ContainerImage = ''microsoft/windowsservercore'')]
                    param()
                    '
 -f $containerName2) | Set-Content -Path (Join-Path -Path $TestDrive -ChildPath 'Tests\Integration\MockResource2.Integration.Tests.ps1') -Encoding UTF8 -Force

                    Mock -CommandName New-Container -MockWith {
                        return $containerIdentifier1
                    } -ParameterFilter {
                        $Name -eq $containerName1
                    }

                    Mock -CommandName New-Container -MockWith {
                        return $containerIdentifier2
                    } -ParameterFilter {
                        $Name -eq $containerName2
                    }

                    Mock -CommandName Start-Container
                    Mock -CommandName Get-ContainerLog
                    Mock -CommandName Copy-ItemFromContainer
                    Mock -CommandName Out-TestResult
                    Mock -CommandName Out-MissedCommand
                    Mock -CommandName Wait-Container -MockWith {
                        # Mock an error.
                        return 1
                    }

                    Mock -CommandName Get-Content -MockWith {
                        return ($mockTestResult | ConvertTo-Json)
                    }

                    Mock -CommandName ConvertFrom-Json -MockWith {
                        return $mockTestResult
                    }
                }

                It 'Should not throw exception and call the correct mocks' {
                    $testParameters = @{
                        CodeCoverage   = $true
                        CodeCovIo      = $true
                        RunTestInOrder = $true
                    }

                    {
                        Invoke-AppveyorTestScriptTask @testParameters
                    } | Should -Not -Throw

                    Assert-MockCalled -CommandName Add-AppveyorTest -Exactly -Times 3 -Scope It
                    Assert-MockCalled -CommandName Push-TestArtifact -Exactly -Times 9 -Scope It
                    Assert-MockCalled -CommandName Export-CodeCovIoJson -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Invoke-UploadCoveCoveIoReport -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName New-DscSelfSignedCertificate -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Initialize-LocalConfigurationManager -Exactly -Times 1 -Scope It
                    Assert-MockCalled -CommandName Write-Warning -Exactly -Times 5 -Scope It
                    Assert-MockCalled -CommandName Get-Content -Exactly -Times 2 -Scope It
                    Assert-MockCalled -CommandName Out-TestResult -Exactly -Times 2 -Scope It
                    Assert-MockCalled -CommandName Out-MissedCommand -Exactly -Times 2 -Scope It
                    Assert-MockCalled -CommandName New-Container -ParameterFilter {
                        $Name -eq $containerName1
                    } -Exactly -Times 1 -Scope It

                    Assert-MockCalled -CommandName New-Container -ParameterFilter {
                        $Name -eq $containerName2
                    } -Exactly -Times 1 -Scope It

                    Assert-MockCalled -CommandName Start-Container -ParameterFilter {
                        $ContainerIdentifier -eq $containerIdentifier1
                    } -Exactly -Times 1 -Scope It

                    Assert-MockCalled -CommandName Start-Container -ParameterFilter {
                        $ContainerIdentifier -eq $containerIdentifier2
                    } -Exactly -Times 1 -Scope It

                    Assert-MockCalled -CommandName Wait-Container -ParameterFilter {
                        $ContainerIdentifier -eq $containerIdentifier1
                    } -Exactly -Times 1 -Scope It

                    Assert-MockCalled -CommandName Wait-Container -ParameterFilter {
                        $ContainerIdentifier -eq $containerIdentifier2
                    } -Exactly -Times 1 -Scope It

                    Assert-MockCalled -CommandName Copy-ItemFromContainer -ParameterFilter {
                        $ContainerIdentifier -eq $containerIdentifier1
                    } -Exactly -Times 3 -Scope It

                    Assert-MockCalled -CommandName Copy-ItemFromContainer -ParameterFilter {
                        $ContainerIdentifier -eq $containerIdentifier2
                    } -Exactly -Times 3 -Scope It
                }

                Context 'When container reports an error' {
                    BeforeAll {
                        $errorMessage = 'MockError'

                        Mock -CommandName Get-ContainerLog -MockWith {
                            $invalidOperationException = New-Object -TypeName 'InvalidOperationException' -ArgumentList @( $errorMessage )
                            $newObjectParameters = @{
                                TypeName = 'System.Management.Automation.ErrorRecord'
                                ArgumentList = @(
                                    $invalidOperationException.ToString(),
                                    'MachineStateIncorrect',
                                    'InvalidOperation',
                                    $null
                                )
                            }

                            return @(
                                New-Object @newObjectParameters
                            )
                        }
                    }

                    It 'Should throw the correct error' {
                        $testParameters = @{
                            CodeCoverage   = $true
                            CodeCovIo      = $true
                            RunTestInOrder = $true
                        }

                        {
                            Invoke-AppveyorTestScriptTask @testParameters
                        } | Should -Throw $errorMessage

                    }
                }

                Context 'When container does not return any result' {
                    BeforeAll {
                        Mock -CommandName Get-Content
                    }

                    It 'Should throw the correct error' {
                        $testParameters = @{
                            CodeCoverage   = $true
                            CodeCovIo      = $true
                            RunTestInOrder = $true
                        }

                        {
                            Invoke-AppveyorTestScriptTask @testParameters
                        } | Should -Throw 'The container did not report any test result! This indicates that an error occurred in the container.'
                    }
                }
            }
        }
    } # End Describe AppVeyor\Invoke-AppveyorTestScriptTask

    Describe 'AppVeyor\Invoke-AppVeyorDeployTask' {
        BeforeAll {
            # Stub for the function so we don't need to import the module.
            function Start-GalleryDeploy
            {
            }

            function Publish-WikiContent
            {
            }

            Mock -CommandName Write-Info
            Mock -CommandName Start-GalleryDeploy
            Mock -CommandName Publish-WikiContent
            Mock -CommandName Import-Module -ParameterFilter {
                $Name -match 'DscResource\.GalleryDeploy'
            }
            Mock -CommandName Import-Module -ParameterFilter {
                $Name -match 'DscResource\.DocumentationHelper'
            }

            $mockBranchName = 'MockTestBranch'

            # Change build environment variables and remember the previous setting.
            $originalAppVeyorBuildFolder = $env:APPVEYOR_BUILD_FOLDER
            $oldAppVeyorRepoBranch = $env:APPVEYOR_REPO_BRANCH
            $env:APPVEYOR_BUILD_FOLDER = $TestDrive
            $env:APPVEYOR_REPO_BRANCH = $mockBranchName
        }

        AfterAll {
            <#
                This will reset the APPVEYOR_REPO_BRANCH or remove the environment
                variable if it was not set to begin with.
            #>

            $env:APPVEYOR_REPO_BRANCH = $oldAppVeyorRepoBranch

            <#
                This will reset the APPVEYOR_BUILD_FOLDER or remove the environment
                variable if it was not set to begin with.
            #>

            $env:APPVEYOR_BUILD_FOLDER = $originalAppVeyorBuildFolder
        }

        Context 'When not opt-in for publishing examples' {
            It 'Should not call Start-GalleryDeploy' {
                {
                    Invoke-AppVeyorDeployTask -OptIn @() -Branch $mockBranchName
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Import-Module -ParameterFilter {
                    $Name -match 'DscResource\.GalleryDeploy'
                } -Exactly -Times 0 -Scope It

                Assert-MockCalled -CommandName Start-GalleryDeploy -Exactly -Times 0 -Scope It
            }
        }

        Context 'When opt-in for publishing examples, but building on the wrong branch' {
            It 'Should not call Start-GalleryDeploy' {
                {
                    Invoke-AppVeyorDeployTask -OptIn @('PublishExample') -Branch 'WrongBranch'
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Import-Module -ParameterFilter {
                    $Name -match 'DscResource\.GalleryDeploy'
                } -Exactly -Times 0 -Scope It

                Assert-MockCalled -CommandName Start-GalleryDeploy -Exactly -Times 0 -Scope It
            }
        }

        Context 'When opt-in for publishing examples and building on the correct branch' {
            It 'Should call Start-GalleryDeploy' {
                {
                    Invoke-AppVeyorDeployTask -OptIn @('PublishExample') -Branch $mockBranchName
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Import-Module -ParameterFilter {
                    $Name -match 'DscResource\.GalleryDeploy'
                } -Exactly -Times 1 -Scope It

                Assert-MockCalled -CommandName Start-GalleryDeploy -Exactly -Times 1 -Scope It
            }
        }

        Context 'When not opt-in for publishing Wiki Content' {
            It 'Should not call Publish-WikiContent' {
                {
                    Invoke-AppVeyorDeployTask -OptIn @() -Branch $mockBranchName
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Import-Module -ParameterFilter {
                    $Name -match 'DscResource\.DocumentationHelper'
                } -Exactly -Times 0 -Scope It

                Assert-MockCalled -CommandName Publish-WikiContent -Exactly -Times 0 -Scope It
            }
        }

        Context 'When opt-in for publishing Wiki Content, but building on the wrong branch' {
            It 'Should not call Publish-WikiContent' {
                {
                    Invoke-AppVeyorDeployTask -OptIn @('PublishWikiContent') -Branch 'WrongBranch'
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Import-Module -ParameterFilter {
                    $Name -match 'DscResource\.DocumentationHelper'
                } -Exactly -Times 0 -Scope It

                Assert-MockCalled -CommandName Publish-WikiContent -Exactly -Times 0 -Scope It
            }
        }

        Context 'When opt-in for publishing Wiki Content and building on the correct branch' {
            It 'Should call Publish-WikiContent' {
                {
                    Invoke-AppVeyorDeployTask -OptIn @('PublishWikiContent') -Branch $mockBranchName
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Import-Module -ParameterFilter {
                    $Name -match 'DscResource\.DocumentationHelper'
                } -Exactly -Times 1 -Scope It

                Assert-MockCalled -CommandName Publish-WikiContent -Exactly -Times 1 -Scope It
            }
        }
    }

    Describe 'AppVeyor\Invoke-AppveyorInstallTask' {
        BeforeAll {
            Mock -CommandName Write-Info
            Mock -CommandName Get-PackageProvider
            Mock -CommandName Install-Module
            Mock -CommandName Install-NugetExe
            Mock -CommandName New-DscSelfSignedCertificate
        }

        Context 'When installing tests prerequisites using default parameter values' {
            It 'Should call the correct mocks' {
                {
                    Invoke-AppveyorInstallTask
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Get-PackageProvider -Exactly -Times 1 -Scope It
                Assert-MockCalled -CommandName Install-NugetExe -Exactly -Times 1 -Scope It
                Assert-MockCalled -CommandName New-DscSelfSignedCertificate -Exactly -Times 1 -Scope It

                Assert-MockCalled -CommandName Install-Module -ParameterFilter {
                    $Name -eq 'PowerShellGet'
                } -Exactly -Times 1 -Scope It

                Assert-MockCalled -CommandName Install-Module -ParameterFilter {
                    $Name -eq 'Pester' `
                    -and $PSBoundParameters.ContainsKey('MaximumVersion') -eq $false
                } -Exactly -Times 1 -Scope It
            }
        }

        Context 'When installing tests prerequisites using specific Pester version' {
            BeforeEach {
                $mockPesterMaximumVersion = '1.0.0'
            }

            It 'Should call the correct mocks' {
                {
                    Invoke-AppveyorInstallTask -PesterMaximumVersion $mockPesterMaximumVersion
                } | Should -Not -Throw

                Assert-MockCalled -CommandName Get-PackageProvider -Exactly -Times 1 -Scope It
                Assert-MockCalled -CommandName Install-NugetExe -Exactly -Times 1 -Scope It
                Assert-MockCalled -CommandName New-DscSelfSignedCertificate -Exactly -Times 1 -Scope It

                Assert-MockCalled -CommandName Install-Module -ParameterFilter {
                    $Name -eq 'PowerShellGet'
                } -Exactly -Times 1 -Scope It

                Assert-MockCalled -CommandName Install-Module -ParameterFilter {
                    $Name -eq 'Pester' `
                    -and $MaximumVersion -eq [Version] $mockPesterMaximumVersion
                } -Exactly -Times 1 -Scope It
            }
        }
    }
} # End InModuleScope