This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
AppVeyor.psm1
1309 lines (1110 loc) · 53.2 KB
/
AppVeyor.psm1
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
<#
.SYNOPSIS
This module provides functions for building and testing DSC Resources in AppVeyor.
These functions will only work if called within an AppVeyor CI build task.
#>
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'TestHelper.psm1') -Force
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'DscResource.CodeCoverage')
# Import the module containing the container helper functions.
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'DscResource.Container')
# Load the test helper module.
$testHelperPath = Join-Path -Path $PSScriptRoot -ChildPath 'TestHelper.psm1'
Import-Module -Name $testHelperPath -Force
<#
.SYNOPSIS
Prepares the AppVeyor build environment to perform tests and packaging on a
DSC Resource module.
Performs the following tasks:
1. Installs Nuget Package Provider DLL.
2. Installs Nuget.exe to the AppVeyor Build Folder.
3. Installs the Pester PowerShell Module.
4. Creates a self-signed certificate for encrypting credentials in configurations.
5. Executes Invoke-CustomAppveyorInstallTask if defined in .AppVeyor\CustomAppVeyorTasks.psm1
in resource module repository.
.EXAMPLE
Invoke-AppveyorInstallTask -PesterMaximumVersion 3.4.3
#>
function Invoke-AppveyorInstallTask
{
[CmdletBinding(DefaultParameterSetName = 'Default')]
param
(
[Parameter()]
[Version]
$PesterMaximumVersion
)
<#
Parameter ForceBootstrap automatically installs the NuGet package provider
if your computer does not have the NuGet package provider installed.
#>
Write-Info -Message 'Installing the latest NuGet package provider.'
$getPackageProviderResult = Get-PackageProvider -Name NuGet -ForceBootstrap
$getPackageProviderResult | Format-Table -Property @('Name', 'ProviderName', 'Version')
Write-Info -Message 'Installing the latest PowerShellGet from the PowerShell Gallery.'
Install-Module -Name PowerShellGet -Force -Repository PSGallery -AllowClobber
$nuGetExePath = Join-Path -Path $env:TEMP -ChildPath 'nuget.exe'
Write-Info -Message 'Installing nuget.exe to enable package creation.'
Install-NugetExe -OutFile $nuGetExePath -RequiredVersion '3.4.4'
Write-Info -Message 'Installing the latest Pester module.'
$installPesterParameters = @{
Name = 'Pester'
Force = $true
}
$installModuleSupportsSkipPublisherCheck = (Get-Command Install-Module).Parameters['SkipPublisherCheck']
if ($installModuleSupportsSkipPublisherCheck)
{
$installPesterParameters['SkipPublisherCheck'] = $true
}
if ($PesterMaximumVersion)
{
$installPesterParameters['MaximumVersion'] = $PesterMaximumVersion
}
Install-Module @installPesterParameters
Write-Info -Message 'Create a self-signed certificate for encrypting DSC configuration credentials.'
$null = New-DscSelfSignedCertificate
Write-Info -Message 'Install Task Complete.'
}
<#
.SYNOPSIS
Executes the tests on a DSC Resource in the AppVeyor build environment.
Executes Start-CustomAppveyorTestTask if defined in .AppVeyor\CustomAppVeyorTasks.psm1
in resource module repository.
Creates a self-signed certificate for encrypting credentials in configurations
if it doesn't already exist.
.PARAMETER Type
This controls the method of running the tests.
To use execute tests using a test harness function specify 'Harness', otherwise
leave empty to use default value 'Default'.
.PARAMETER MainModulePath
This is the relative path of the folder that contains the module manifest.
If not specified it will default to the root folder of the repository.
.PARAMETER CodeCoverage
This will switch on Code Coverage evaluation in Pester.
.PARAMETER ExcludeTag
This is the list of tags that will be used to prevent tests from being run if
the tag is set in the describe block of the test.
.PARAMETER HarnessModulePath
This is the full path and filename of the test harness module.
If not specified it will default to 'Tests\TestHarness.psm1'.
.PARAMETER HarnessFunctionName
This is the function name in the harness module to call to execute tests.
If not specified it will default to 'Invoke-TestHarness'.
.PARAMETER CodeCovIo
This will switch on reporting of code coverage to codecov.io.
Require -CodeCoverage when running with -type default.
.PARAMETER DisableConsistency
This will switch off monitoring (consistency) for the Local Configuration
Manager (LCM), setting ConfigurationMode to 'ApplyOnly', on the node
running tests.
.PARAMETER RunTestInOrder
This will cause the integration tests to be run in order. First, the
common tests will run, followed by the unit tests. Finally the integration
tests will be run in the order defined.
Each integration test configuration file ('*.config.ps1') must be decorated
with an attribute `Microsoft.DscResourceKit.IntegrationTest` containing
a named attribute argument 'OrderNumber' and be assigned a numeric value
(`1`, `2`, `3`,..). If the integration test is not decorated with the
attribute, then that test will run among the last tests, after all the
integration test with a specific order has run.
This will also enable running unit tests and integration tests in a
Docker Windows container.
.PARAMETER CodeCoveragePath
One or more relative paths to PowerShell modules, from the root module
folder. For each relative folder it will recursively search the first
level subfolders for PowerShell module files (.psm1).
Default to 'DSCResources', 'DSCClassResources', and 'Modules'.
This parameter is ignored when testing the DscResource.Tests repository
since that repository is treated differently, and has hard-coded paths.
#>
function Invoke-AppveyorTestScriptTask
{
[CmdletBinding(DefaultParameterSetName = 'Default')]
param
(
[Parameter()]
[ValidateSet('Default', 'Harness')]
[String]
$Type = 'Default',
[Parameter(ParameterSetName = 'Harness')]
[ValidateNotNullOrEmpty()]
[String]
$MainModulePath = $env:APPVEYOR_BUILD_FOLDER,
[Parameter(ParameterSetName = 'Default')]
[Parameter(ParameterSetName = 'Harness')]
[Switch]
$CodeCoverage,
[Parameter(ParameterSetName = 'Default')]
[Parameter(ParameterSetName = 'Harness')]
[Switch]
$CodeCovIo,
[Parameter(ParameterSetName = 'Default')]
[String[]]
$ExcludeTag,
[Parameter(ParameterSetName = 'Harness')]
[String]
$HarnessModulePath = 'Tests\TestHarness.psm1',
[Parameter(ParameterSetName = 'Harness')]
[String]
$HarnessFunctionName = 'Invoke-TestHarness',
[Parameter()]
[Switch]
$DisableConsistency,
[Parameter(ParameterSetName = 'Default')]
[Switch]
$RunTestInOrder,
[Parameter(ParameterSetName = 'Default')]
[String[]]
$CodeCoveragePath = @(
'DSCResources',
'DSCClassResources',
'Modules'
)
)
# Convert the Main Module path into an absolute path if it is relative
if (-not ([System.IO.Path]::IsPathRooted($MainModulePath)))
{
$MainModulePath = Join-Path -Path $env:APPVEYOR_BUILD_FOLDER `
-ChildPath $MainModulePath
}
# Create a self-signed certificate for encrypting configuration credentials if it doesn't exist
$null = New-DscSelfSignedCertificate
$testResultsFile = Join-Path -Path $env:APPVEYOR_BUILD_FOLDER `
-ChildPath 'TestsResults.xml'
Initialize-LocalConfigurationManager -Encrypt:$true -DisableConsistency:$DisableConsistency
$moduleName = Split-Path -Path $env:APPVEYOR_BUILD_FOLDER -Leaf
$testsPath = Join-Path -Path $env:APPVEYOR_BUILD_FOLDER -ChildPath 'Tests'
if (Test-Path -Path $testsPath)
{
$configurationFiles = Get-ChildItem -Path $testsPath -Include '*.config.ps1' -Recurse
foreach ($configurationFile in $configurationFiles)
{
# Get the list of additional modules required by the example
$requiredModules = Get-ResourceModulesInConfiguration -ConfigurationPath $configurationFile.FullName |
Where-Object -Property Name -ne $moduleName
if ($requiredModules)
{
Install-DependentModule -Module $requiredModules
}
}
}
else
{
Write-Warning -Message 'The ''Tests'' folder is missing, the test framework will only run the common tests.'
}
<#
Initiate the test container array so that even if no containers is
started the logic can correctly evaluate that using $testContainer.Count
#>
$testContainer = @()
switch ($Type)
{
'Default'
{
# Execute the standard tests using Pester.
$pesterParameters = @{
OutputFormat = 'NUnitXML'
OutputFile = $testResultsFile
PassThru = $True
}
if ($ExcludeTag.Count -gt 0)
{
$pesterParameters += @{
ExcludeTag = $ExcludeTag
}
}
if ($CodeCoverage)
{
<#
This code path is used to get the correct files for code
coverage for the tests that are run by the build worker.
If the container testing functionality is enabled by passing
the switch parameter 'RunTestInOrder', then the container
logic will handle the gathering of the files that are used
for the correct CodeCoverage.
Although even if the container logic is handling code coverage,
it will still depend on the $codeCoveragePaths array below to
determine the possible paths that can be used to gather the
files that will be used for code coverage.
The container logic will make sure those files that are not
used for code coverage in a container will be used for the
tests that run in the build worker.
#>
Write-Warning -Message 'Code coverage statistics are being calculated. This will slow the start of the tests while the code matrix is built. Please be patient.'
# Only add code path for DSCResources if they exist.
$codeCoveragePaths = @(
"$env:APPVEYOR_BUILD_FOLDER\*.psm1"
)
if (Test-IsRepositoryDscResourceTests)
{
<#
The repository being tested is DscResource.Tests.
DscResource.Tests need a different set of paths for
code coverage.
#>
$codeCoveragePaths += "$env:APPVEYOR_BUILD_FOLDER\**\*.psm1"
}
else
{
<#
Define the folders to check, if found add the path for
code coverage.
#>
$possibleModulePaths = $CodeCoveragePath
foreach ($possibleModulePath in $possibleModulePaths)
{
if (Test-Path -Path "$env:APPVEYOR_BUILD_FOLDER\$possibleModulePath")
{
$codeCoveragePaths += "$env:APPVEYOR_BUILD_FOLDER\$possibleModulePath\*.psm1"
$codeCoveragePaths += "$env:APPVEYOR_BUILD_FOLDER\$possibleModulePath\**\*.psm1"
}
}
}
$pesterParameters += @{
CodeCoverage = $codeCoveragePaths
}
}
$getChildItemParameters = @{
Path = $env:APPVEYOR_BUILD_FOLDER
Recurse = $true
}
# Get all tests '*.Tests.ps1'.
$getChildItemParameters['Filter'] = '*.Tests.ps1'
$testFiles = Get-ChildItem @getChildItemParameters
<#
If it is another repository other than DscResource.Tests
then remove the DscResource.Tests unit tests from the list
of tests to run. Issue #189.
#>
if (-not (Test-IsRepositoryDscResourceTests))
{
$testFiles = $testFiles | Where-Object -FilterScript {
$_.FullName -notmatch 'DSCResource.Tests\\Tests'
}
}
if ($RunTestInOrder.IsPresent)
{
<#
This is an array of test files containing path
and optional order number.
#>
$testObjects = @()
<#
Add all tests to the $testObjects array with properties set
to $null.
This array will be used to run tests in order and the correct
container if specified.
#>
foreach ($testFile in $testFiles)
{
$testObjects += @(
[PSCustomObject] @{
TestPath = $testFile.FullName
OrderNumber = $null
ContainerName = $null
ContainerImage = $null
}
)
}
<#
Make sure all common tests are always run first
by setting order number to zero (0).
#>
$testObjects | Where-Object -FilterScript {
$_.TestPath -match 'DSCResource.Tests'
} | ForEach-Object -Process {
$_.OrderNumber = 0
}
<#
In each file, search for existens of attribute 'IntegrationTest'
or 'UnitTest' with named attribute arguments.
#>
foreach ($testObject in $testObjects)
{
# Only check for order number if it is an integration test.
if ($testObject.TestPath -match '\.Integration\.')
{
$orderNumber = Get-DscIntegrationTestOrderNumber `
-Path $testObject.TestPath
if ($orderNumber)
{
$testObject.OrderNumber = $orderNumber
}
}
$containerInformation = Get-DscTestContainerInformation `
-Path $testObject.TestPath
if ($containerInformation)
{
$testObject.ContainerName = $containerInformation.ContainerName
$testObject.ContainerImage = $containerInformation.ContainerImage
}
}
<#
This is an array of the test files in the correct
order they will be run.
- First the common tests will always run.
- Secondly the tests that use mocks will run (unit tests),
unless they should be run in a container.
- Finally, those the tests that actually changes things
(integration tests) will run in order.
#>
$testObjectOrder = @()
<#
Add tests that have OrderNumber -eq 0 and are not assigned a
container. This is the common tests.
#>
$testObjectOrder += $testObjects | Where-Object -FilterScript {
$_.OrderNumber -eq 0 `
-and $null -eq $_.ContainerName
}
<#
Get all tests that have a container assigned so those can be
started.
#>
$testObjectUsingContainer = $testObjects | Where-Object -FilterScript {
$null -ne $_.ContainerName
}
<#
If we should run tests in one or more Docker Windows containers,
then those should be kicked off first.
#>
if ($testObjectUsingContainer)
{
# Get unique container names with the corresponding container image.
$uniqueContainersFromTestObjects = $testObjectUsingContainer |
Sort-Object -Property 'ContainerName' -Unique
# Build all container objects
foreach ($uniqueContainer in $uniqueContainersFromTestObjects)
{
$testContainer += @(
[PSCustomObject] @{
ContainerName = $uniqueContainer.ContainerName
ContainerImage = $uniqueContainer.ContainerImage
ContainerIdentifier = $null
PesterResult = $null
TranscriptPath = $null
}
)
}
Write-Info -Message 'Using one or more Docker Windows containers to run tests.'
<#
Read all module files. This array list will end up
with only the module files that should be used for
code coverage in the build worker.
The $codeCoveragePaths array is built at the beginning
where the switch-statement start. This is so that
we only have one location where those are specified.
#>
[System.Collections.ArrayList] $moduleFile = `
Get-ChildItem -Path $codeCoveragePaths
foreach ($currentContainer in $testContainer)
{
Write-Info -Message (
'Building container ''{0}'' using image ''{1}''.' `
-f $currentContainer.ContainerName, $currentContainer.ContainerImage
)
<#
Filter out tests that should be run in the current
container, also sorts the tests in the correct order
if any has been set to run in specific order.
#>
$containerTestObjectOrder = $testObjectUsingContainer | Where-Object -FilterScript {
$_.ContainerName -eq $currentContainer.ContainerName
} | Sort-Object -Property 'OrderNumber'
$containerName = $currentContainer.ContainerName
$newContainerParameters = @{
Name = $containerName
Image = $currentContainer.ContainerImage
TestPath = $containerTestObjectOrder.TestPath
ProjectPath = $env:APPVEYOR_BUILD_FOLDER
}
<#
If code coverage was chosen, then evaluate the files
needed to be able to calculate coverage for the files
tested in the container. The rest of the files are left
for the tests running in the build worker.
#>
if ($CodeCoverage)
{
# Read all the test files to get an object for each.
$testFilePath = Get-ChildItem -Path $containerTestObjectOrder.TestPath -File
<#
This will contain all the modules files that will be
used for code coverage in the container.
#>
$codeCoverageFile = @()
foreach ($currentTestFilePath in $testFilePath)
{
<#
Get the base name of the test file, in other
words the resource name, so we can match it
against it's module script file.
#>
if ($currentTestFilePath.BaseName -match '\.Integration\.Tests')
{
# integration test
$scriptBaseName = $currentTestFilePath.BaseName -replace '\.Integration\.Tests'
}
else
{
# Unit test
$scriptBaseName = $currentTestFilePath.BaseName -replace '\.Tests'
}
$coverageFile = $moduleFile | Where-Object -FilterScript {
$_.FullName -match "$scriptBaseName\.psm1"
}
if ($coverageFile)
{
$codeCoverageFile += $coverageFile.FullName
<#
Remove the module script file since it should not
be used for code coverage on the build worker,
it should only be used for code coverage in the
container.
#>
$moduleFile.Remove($coverageFile)
}
}
<#
The container gets the module files it needs for
calculating code coverage.
#>
$newContainerParameters['CodeCoverage'] = $codeCoverageFile
}
<#
Create the new Docker container and assign the identifier
to the hash table object.
#>
$currentContainer.ContainerIdentifier = New-Container @newContainerParameters
<#
This will always start the container. If for some reason
the container fails, the problem will be handled after
waiting for the container to finish (or fail). At that
point if the container exits with a code other than 0,
then the docker logs will be gathered and sent as an
artifact. If PowerShell.exe returned an error record then
that will be thrown.
We could have waited here for X seconds to check
whether the container seems to have started the task
(and not exited with an error code). But to save seconds
we assume that the container will always be able to start
the task successfully.
#>
Start-Container -ContainerIdentifier $currentContainer.ContainerIdentifier | Out-Null
}
<#
If we run in a container then the result file that is
generated by the test running in the build worker should
use a different name than the default, to differentiate
it from the container test result files.
#>
$testResultsFile = Join-Path -Path $env:APPVEYOR_BUILD_FOLDER `
-ChildPath 'worker_TestsResults.xml'
$pesterParameters['OutputFile'] = $testResultsFile
<#
The array $moduleFile will contain all the module files
that wasn't added for code coverage in a container.
The build worker gets the remaining module files to
calculate code coverage on.
#>
$pesterParameters['CodeCoverage'] = $moduleFile.FullName
Write-Info -Message 'One or more containers have been started. Build worker will continue to run other tests.'
}
<#
Add tests that uses mocks (unit tests) which does not have an
order number, nor have a container assigned.
#>
$testObjectOrder += $testObjects | Where-Object -FilterScript {
$null -eq $_.OrderNumber `
-and $null -eq $_.ContainerName `
-and $_.TestPath -notmatch 'Integration\.Tests'
}
<#
Add integration tests that must run in the correct order.
These test have an order number higher than 0, and contain
'Integration.Tests' in the filename, but does not have a
container assigned.
#>
$testObjectOrder += $testObjects | Where-Object -FilterScript {
$null -eq $_.ContainerName `
-and $_.OrderNumber -gt 0 `
-and $_.TestPath -match 'Integration\.Tests'
} | Sort-Object -Property 'OrderNumber'
<#
Finally add integration tests that can run unordered.
These tests do not have an order number, and do not have
a container assigned, but do contain 'Integration.Tests' in
the filename.
#>
$testObjectOrder += $testObjects | Where-Object -FilterScript {
$null -eq $_.OrderNumber `
-and $null -eq $_.ContainerName `
-and $_.TestPath -match 'Integration\.Tests'
}
# Add all the paths to the Invoke-Pester Path parameter.
$pesterParameters += @{
Path = $testObjectOrder.TestPath
}
<#
This runs the tests on the build worker.
If the option was to run tests in a container, then this
will only run the remaining tests. The name of the result
file that is generated by this test run was changed by the
container logic, to differentiate the test result from the
container test result.
#>
$results = Invoke-Pester @pesterParameters
<#
If we ran unit test in a Docker Windows container, then
we need to wait for the container to finish running tests.
#>
if ($testContainer.Count -gt 0)
{
foreach ($currentContainer in $testContainer)
{
$waitContainerParameters = @{
ContainerIdentifier = $currentContainer.ContainerIdentifier
<#
Wait 1 hour for the container to finish the tests.
If the container has not returned before that time,
the test will fail.
#>
Timeout = 3600
}
$containerExitCode = Wait-Container @waitContainerParameters
if ($containerExitCode -ne 0)
{
$containerErrorObject = Get-ContainerLog -ContainerIdentifier $currentContainer.ContainerIdentifier
# Upload the Docker Windows container log.
$containerDockerLogFileName = '{0}-DockerLog.txt' -f $currentContainer.ContainerName
$containerDockerLogPath = Join-Path -Path $env:APPVEYOR_BUILD_FOLDER -ChildPath $containerDockerLogFileName
$containerErrorObject | Out-File -FilePath $containerDockerLogPath -Encoding ascii -Force
Push-TestArtifact -Path $containerDockerLogPath
Write-Warning -Message ('The container named ''{0}'' failed with exit code {1}. See artifact ''{2}'' for the logs. Throwing the error reported by Docker (in the log output):' -f $currentContainer.ContainerName, $containerExitCode, $containerDockerLogFileName)
<#
Loop thru the output and throw if PowerShell, that was
started in the container, returned an error record.
All other other output is ignored (sent to Out-Null).
#>
$containerErrorObject | ForEach-Object -Process {
if ($_ -is [System.Management.Automation.ErrorRecord])
{
throw $_
}
} | Out-Null
<#
No error record was found that could be thrown above.
Write a warning that we couldn't find an error.
#>
Write-Warning -Message 'Container exited with an error, but no error record was found in the container log, so the error could not be caught.'
}
Write-Info -Message ('Container named ''{0}'' has finish running tests.' -f $currentContainer.ContainerName)
<#
Get the <container>_Transcript.txt from the container
and upload it as an artifact.
#>
$currentContainer.TranscriptPath = Join-Path `
-Path $env:APPVEYOR_BUILD_FOLDER `
-ChildPath ('{0}_Transcript.txt' -f $currentContainer.ContainerName)
$copyItemFromContainerParameters = @{
ContainerIdentifier = $currentContainer.ContainerIdentifier
Path = $currentContainer.TranscriptPath
Destination = $env:APPVEYOR_BUILD_FOLDER
}
Copy-ItemFromContainer @copyItemFromContainerParameters
Push-TestArtifact -Path $currentContainer.TranscriptPath
<#
Get the <container>TestsResults.xml from the container
and upload it as an artifact.
#>
$containerTestsResultsFilePath = Join-Path `
-Path $env:APPVEYOR_BUILD_FOLDER `
-ChildPath ('{0}_TestsResults.xml' -f $currentContainer.ContainerName)
$copyItemFromContainerParameters['Path'] = $containerTestsResultsFilePath
Copy-ItemFromContainer @copyItemFromContainerParameters
Push-TestArtifact -Path $containerTestsResultsFilePath
<#
Get the <container>TestsResults.json from the container
and upload it as an artifact.
#>
$containerTestsResultsJsonPath = Join-Path `
-Path $env:APPVEYOR_BUILD_FOLDER `
-ChildPath ('{0}_TestsResults.json' -f $currentContainer.ContainerName)
$copyItemFromContainerParameters['Path'] = $containerTestsResultsJsonPath
Copy-ItemFromContainer @copyItemFromContainerParameters
Push-TestArtifact -Path $containerTestsResultsJsonPath
Write-Info -Message ('Start listing test results from container named ''{0}''.' -f $currentContainer.ContainerName)
$currentContainer.PesterResult = Get-Content -Path $containerTestsResultsJsonPath | ConvertFrom-Json
if ($currentContainer.PesterResult.TestResult)
{
# Output the final unit test results.
$outTestResultParameters = @{
TestResult = $currentContainer.PesterResult.TestResult
WaitForAppVeyorConsole = $true
Timeout = 5
}
Out-TestResult @outTestResultParameters
}
else
{
throw 'The container did not report any test result! This indicates that an error occurred in the container.'
}
# Output the missed commands when code coverage is used.
if ($CodeCoverage.IsPresent)
{
$outMissedCommandParameters = @{
MissedCommand = $currentContainer.PesterResult.CodeCoverage.MissedCommands
WaitForAppVeyorConsole = $true
Timeout = 5
}
Out-MissedCommand @outMissedCommandParameters
}
Write-Info -Message ('End of test results from container named ''{0}''.' -f $currentContainer.ContainerName)
}
}
}
else
{
$pesterParameters += @{
Path = $testFiles.FullName
}
$results = Invoke-Pester @pesterParameters
}
break
}
'Harness'
{
# Copy the DSCResource.Tests folder into the folder containing the resource PSD1 file.
$dscTestsPath = Join-Path -Path $MainModulePath `
-ChildPath 'DSCResource.Tests'
Copy-Item -Path $PSScriptRoot -Destination $MainModulePath -Recurse
$testHarnessPath = Join-Path -Path $env:APPVEYOR_BUILD_FOLDER `
-ChildPath $HarnessModulePath
# Execute the resource tests as well as the DSCResource.Tests\meta.tests.ps1
Import-Module -Name $testHarnessPath
$results = & $HarnessFunctionName -TestResultsFile $testResultsFile `
-DscTestsPath $dscTestsPath
# Delete the DSCResource.Tests folder because it is not needed
Remove-Item -Path $dscTestsPath -Force -Recurse
break
}
}
$pesterTestResult = $results.TestResult
# If tests were run in a container, add those Pester results as well.
if ($testContainer.Count -gt 0)
{
foreach ($currentContainer in $testContainer)
{
$pesterTestResult += $currentContainer.PesterResult.TestResult
}
}
Write-Info -Message 'Adding test result to AppVeyor test pane.'
foreach ($result in $pesterTestResult)
{
[string] $describeName = $result.Describe -replace '\\', '/'
[string] $contextName = $result.Context -replace '\\', '/'
$componentName = '{0}; Context: {1}' -f $describeName, $contextName
$appVeyorResult = $result.Result
# Convert any result not know by AppVeyor to an AppVeyor Result
switch ($result.Result)
{
'Pending'
{
$appVeyorResult = 'Skipped'
}
}
$addAppVeyorTestParameters = @{
Name = $result.Name
Framework = 'NUnit'
Filename = $componentName
Outcome = $appVeyorResult
Duration = $result.Time.TotalMilliseconds
}
if ($result.FailureMessage)
{
$addAppVeyorTestParameters += @{
ErrorMessage = $result.FailureMessage
ErrorStackTrace = $result.StackTrace
}
}
<#
This is a workaround until AppVeyor PowerShell module is supported
on PowerShell Core.
#>
if ($PSVersionTable.PSEdition -eq 'Core')
{
$requestObject = @{
testName = $addAppVeyorTestParameters.Name
testFramework = $addAppVeyorTestParameters.Framework
fileName = $addAppVeyorTestParameters.Filename
outcome = $addAppVeyorTestParameters.Outcome
durationMilliseconds = $addAppVeyorTestParameters.Duration
}
if ($result.FailureMessage)
{
$requestObject['ErrorMessage'] = $addAppVeyorTestParameters.ErrorMessage
$requestObject['ErrorStackTrace'] = $addAppVeyorTestParameters.ErrorStackTrace
}
<#
ConvertTo-Json will handle all escaping for us, like escaping
double quotes and backslashes.
#>
$requestBody = $requestObject | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri "$env:APPVEYOR_API_URL/api/tests" -Body $requestBody -ContentType 'application/json' | Out-Null
}
else
{
Add-AppveyorTest @addAppVeyorTestParameters
}
}
Push-TestArtifact -Path $testResultsFile
if ($CodeCovIo.IsPresent)
{
# Get the code coverage result from build worker test run.
$entireCodeCoverage = $results.CodeCoverage
# Check whether we run in a container, and the build worker reported coverage
if ($testContainer.Count -gt 0)
{
# Loop thru each container result and add it to the main coverage.
foreach ($currentContainer in $testContainer)
{
if ($entireCodeCoverage)
{
# Concatenate the code coverage result from the container test run.
$containerCodeCoverage = $currentContainer.PesterResult.CodeCoverage
if ($containerCodeCoverage)
{
$entireCodeCoverage.NumberOfCommandsAnalyzed += $containerCodeCoverage.NumberOfCommandsAnalyzed
$entireCodeCoverage.NumberOfFilesAnalyzed += $containerCodeCoverage.NumberOfFilesAnalyzed
$entireCodeCoverage.NumberOfCommandsExecuted += $containerCodeCoverage.NumberOfCommandsExecuted
$entireCodeCoverage.NumberOfCommandsMissed += $containerCodeCoverage.NumberOfCommandsMissed
$entireCodeCoverage.MissedCommands += $containerCodeCoverage.MissedCommands
$entireCodeCoverage.HitCommands += $containerCodeCoverage.HitCommands
$entireCodeCoverage.AnalyzedFiles += $containerCodeCoverage.AnalyzedFiles
}
}
else
{
# The container was the first to report code coverage.
$entireCodeCoverage = $currentContainer.PesterResult.CodeCoverage
}
}
}
if ($entireCodeCoverage)
{
Write-Info -Message 'Uploading CodeCoverage to CodeCov.io...'
$jsonPath = Export-CodeCovIoJson -CodeCoverage $entireCodeCoverage -repoRoot $env:APPVEYOR_BUILD_FOLDER
Invoke-UploadCoveCoveIoReport -Path $jsonPath
}
else
{
Write-Warning -Message 'Could not create CodeCov.io report because Pester results object did not contain a CodeCoverage object'
}
}
Write-Verbose -Message "Test result Type: $($results.GetType().FullName)"
Write-Info -Message 'Done running tests.'
$pesterFailedCount = $results.FailedCount
if ($testContainer.Count -gt 0)
{
foreach ($currentContainer in $testContainer)
{
if ($currentContainer.PesterResult.FailedCount)
{
Write-Warning -Message ('The tests that ran in the container named ''{0}'' report errors. Please look at the artifact ''{1}'' for more detailed errors.' -f $currentContainer.ContainerName, (Split-Path -Path $currentContainer.TranscriptPath -Leaf))
$pesterFailedCount += $currentContainer.PesterResult.FailedCount
}
}
}
if ($pesterFailedCount -gt 0)
{
throw ('{0} tests failed.' -f $pesterFailedCount)
}
Write-Info -Message 'Test Script Task Complete.'
}
<#
.SYNOPSIS
Performs the after tests tasks for the AppVeyor build process.
This includes:
1. Optional: Produce and upload Wiki documentation to AppVeyor.
2. Set version number in Module Manifest to build version
3. Zip up the module content and produce a checksum file and upload to AppVeyor.
4. Pack the module into a Nuget Package.
5. Upload the Nuget Package to AppVeyor.
Executes Start-CustomAppveyorAfterTestTask if defined in .AppVeyor\CustomAppVeyorTasks.psm1
in resource module repository.
.PARAMETER Type
This controls the additional processes that can be run after testing.
To produce wiki documentation specify 'Wiki', otherwise leave empty to use
default value 'Default'.
.PARAMETER MainModulePath
This is the relative path of the folder that contains the module manifest.
If not specified it will default to the root folder of the repository.
.PARAMETER ResourceModuleName
Name of the Resource Module being produced.
If not specified will default to GitHub repository name.
.PARAMETER Author
The Author string to insert into the NUSPEC file for the package.
If not specified will default to 'Microsoft'.