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
/
Copy pathMeta.Tests.ps1
1593 lines (1330 loc) · 67.1 KB
/
Meta.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
<#
.SYNOPSIS
Common tests for all resource modules in the DSC Resource Kit.
#>
# Suppressing this because we need to generate a mocked credentials that will be passed along to the examples that are needed in the tests.
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
param ()
Set-StrictMode -Version 'Latest'
$errorActionPreference = 'Stop'
$testHelperModulePath = Join-Path -Path $PSScriptRoot -ChildPath 'TestHelper.psm1'
Import-Module -Name $testHelperModulePath
# Check if there is a request to override the common tests in this file.
$skipAllCommonTests = Get-Item -Path 'env:\SkipAllCommonTests' -ErrorAction 'SilentlyContinue'
if ($null -ne $skipAllCommonTests -and $skipAllCommonTests.Value -eq $true)
{
Write-Warning -Message 'DEBUG WARNING: Overriding common test because environment variable OverrideCommonTests is set to $true.'
return
}
$moduleRootFilePath = Split-Path -Path $PSScriptRoot -Parent
<#
This is a workaround to be able to run these common test on DscResource.Tests
module, for testing itself.
#>
if (Test-IsRepositoryDscResourceTests)
{
$moduleRootFilePath = $PSScriptRoot
<#
Because the repository name of 'DscResource.Tests' has punctuation in
the name, AppVeyor replaces that with a dash when it creates the folder
structure, so the folder name becomes 'dscresource-tests'.
This sets the module name to the correct name.
If the name can be detected in a better way, for DscResource.Tests
and all other modules, then this could be removed.
#>
$moduleName = 'DscResource.Tests'
}
else
{
$moduleName = (Get-Item -Path $moduleRootFilePath).Name
}
$dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'
if (-not (Test-Path -Path $dscResourcesFolderFilePath))
{
$dscResourcesFolderFilePath = $moduleRootFilePath
}
# Identify the repository root path of the resource module
$repoRootPath = $moduleRootFilePath
$repoRootPathFound = $false
while (-not $repoRootPathFound `
-and -not ([String]::IsNullOrEmpty((Split-Path -Path $repoRootPath -Parent))))
{
if (Get-ChildItem -Path $repoRootPath -Filter '.git' -Directory -Force)
{
$repoRootPathFound = $true
break
}
else
{
$repoRootPath = Split-Path -Path $repoRootPath -Parent
}
}
if (-not $repoRootPathFound)
{
Write-Warning -Message ('The root folder of the DSC Resource repository could ' + `
'not be located. This may prevent some markdown files from being checked for ' + `
'errors. Please ensure this repository has been cloned using Git.')
$repoRootPath = $moduleRootFilePath
}
$testOptInFilePath = Join-Path -Path $repoRootPath -ChildPath '.MetaTestOptIn.json'
# .MetaTestOptIn.json should be in the following format
# [
# "Common Tests - Validate Module Files",
# "Common Tests - Validate Markdown Files",
# "Common Tests - Validate Example Files",
# "Common Tests - Validate Script Files"
# ]
$optIns = @()
if (Test-Path $testOptInFilePath)
{
$optIns = Get-Content -LiteralPath $testOptInFilePath | ConvertFrom-Json
}
Describe 'Common Tests - Relative Path Length' {
$optIn = Get-PesterDescribeOptInStatus -OptIns $optIns
Context -Name 'When the resource should be used to compile a configuration in Azure Automation' {
<#
129 characters is the current maximum for a relative path to be
able to compile configurations in Azure Automation.
#>
$fullPathHardLimit = 129
$allModuleFiles = Get-ChildItem -Path $moduleRootFilePath -Recurse
$allModuleFiles = $allModuleFiles | Where-Object -FilterScript {
# Skip all files under DscResource.Tests.
$_.FullName -notmatch 'DscResource\.Tests'
}
$testCaseModuleFile = @()
$allModuleFiles | ForEach-Object -Process {
$testCaseModuleFile += @(
@{
FullRelativePath = $_.FullName -replace ($moduleRootFilePath -replace '\\', '\\')
}
)
}
It 'The length of the relative full path <FullRelativePath> should not exceed the max hard limit' -TestCases $testCaseModuleFile -Skip:(!$optIn) {
param
(
[Parameter()]
[System.String]
$FullRelativePath
)
$FullRelativePath.Length | Should -Not -BeGreaterThan $fullPathHardLimit
}
}
}
Describe 'Common Tests - File Formatting' {
$textFiles = Get-TextFilesList $moduleRootFilePath
It "Should not contain any files with Unicode file encoding" {
$containsUnicodeFile = $false
foreach ($textFile in $textFiles)
{
if (Test-FileInUnicode $textFile)
{
if ($textFile.Extension -ieq '.mof')
{
Write-Warning -Message "File $($textFile.FullName) should be converted to ASCII. Use fixer function 'Get-UnicodeFilesList `$pwd | ConvertTo-ASCII'."
}
else
{
Write-Warning -Message "File $($textFile.FullName) should be converted to UTF-8. Use fixer function 'Get-UnicodeFilesList `$pwd | ConvertTo-UTF8'."
}
$containsUnicodeFile = $true
}
}
$containsUnicodeFile | Should -Be $false
}
It 'Should not contain any files with tab characters' {
$containsFileWithTab = $false
foreach ($textFile in $textFiles)
{
$fileName = $textFile.FullName
$fileContent = Get-Content -Path $fileName -Raw
$tabCharacterMatches = $fileContent | Select-String "`t"
if ($null -ne $tabCharacterMatches)
{
Write-Warning -Message "Found tab character(s) in $fileName."
$containsFileWithTab = $true
}
}
$containsFileWithTab | Should -Be $false
}
It 'Should not contain empty files' {
$containsEmptyFile = $false
foreach ($textFile in $textFiles)
{
$fileContent = Get-Content -Path $textFile.FullName -Raw
if ([String]::IsNullOrWhiteSpace($fileContent))
{
Write-Warning -Message "File $($textFile.FullName) is empty. Please remove this file."
$containsEmptyFile = $true
}
}
$containsEmptyFile | Should -Be $false
}
It 'Should not contain files without a newline at the end' {
$containsFileWithoutNewLine = $false
foreach ($textFile in $textFiles)
{
$fileContent = Get-Content -Path $textFile.FullName -Raw
if (-not [String]::IsNullOrWhiteSpace($fileContent) -and $fileContent[-1] -ne "`n")
{
if (-not $containsFileWithoutNewLine)
{
Write-Warning -Message 'Each file must end with a new line.'
}
Write-Warning -Message "$($textFile.FullName) does not end with a new line. Use fixer function 'Add-NewLine'"
$containsFileWithoutNewLine = $true
}
}
$containsFileWithoutNewLine | Should -Be $false
}
Context 'When repository contains markdown files' {
$markdownFileExtensions = @('.md')
$markdownFiles = $textFiles |
Where-Object { $markdownFileExtensions -contains $_.Extension }
foreach ($markdownFile in $markdownFiles)
{
$filePathOutputName = Get-RelativePathFromModuleRoot `
-FilePath $markdownFile.FullName `
-ModuleRootFilePath $moduleRootFilePath
It ('Markdown file ''{0}'' should not have Byte Order Mark (BOM)' -f $filePathOutputName) {
$markdownFileHasBom = Test-FileHasByteOrderMark -FilePath $markdownFile.FullName
if ($markdownFileHasBom)
{
Write-Warning -Message "$filePathOutputName contain Byte Order Mark (BOM). Use fixer function 'ConvertTo-ASCII'."
}
$markdownFileHasBom | Should -Be $false
}
}
}
}
Describe 'Common Tests - Validate Script Files' -Tag 'Script' {
$optIn = Get-PesterDescribeOptInStatus -OptIns $optIns
$scriptFilesFilterScript = {
'.ps1' -eq $_.Extension
}
$scriptFiles = Get-TextFilesList -Root $moduleRootFilePath | Where-Object -FilterScript $scriptFilesFilterScript
foreach ($scriptFile in $scriptFiles)
{
$filePathOutputName = Get-RelativePathFromModuleRoot `
-FilePath $scriptFile.FullName `
-ModuleRootFilePath $moduleRootFilePath
Context $filePathOutputName {
It ('Script file ''{0}'' should not have Byte Order Mark (BOM)' -f $filePathOutputName) -Skip:(!$optIn) {
$scriptFileHasBom = Test-FileHasByteOrderMark -FilePath $scriptFile.FullName
if ($scriptFileHasBom)
{
Write-Warning -Message "$filePathOutputName contain Byte Order Mark (BOM). Use fixer function 'ConvertTo-ASCII'."
}
$scriptFileHasBom | Should -Be $false
}
}
}
}
Describe 'Common Tests - .psm1 File Parsing' {
$psm1Files = Get-Psm1FileList -FilePath $moduleRootFilePath
foreach ($psm1File in $psm1Files)
{
$filePathOutputName = Get-RelativePathFromModuleRoot `
-FilePath $psm1File.FullName `
-ModuleRootFilePath $moduleRootFilePath
Context $filePathOutputName {
It ('Module file ''{0}'' should not contain parse errors' -f $filePathOutputName) {
$containsParseErrors = $false
$parseErrors = Get-FileParseErrors -FilePath $psm1File.FullName
if ($null -ne $parseErrors)
{
Write-Warning -Message "There are parse errors in $($psm1File.FullName):"
Write-Warning -Message ($parseErrors | Format-List | Out-String)
$containsParseErrors = $true
}
$containsParseErrors | Should -Be $false
}
}
}
}
Describe 'Common Tests - Validate Module Files' -Tag 'Module' {
$optIn = Get-PesterDescribeOptInStatus -OptIns $optIns
$moduleFiles = Get-Psm1FileList -FilePath $moduleRootFilePath
foreach ($moduleFile in $moduleFiles)
{
$filePathOutputName = Get-RelativePathFromModuleRoot `
-FilePath $moduleFile.FullName `
-ModuleRootFilePath $moduleRootFilePath
Context $filePathOutputName {
It ('Module file ''{0}'' should not have Byte Order Mark (BOM)' -f $filePathOutputName) -Skip:(!$optIn) {
$moduleFileHasBom = Test-FileHasByteOrderMark -FilePath $moduleFile.FullName
if ($moduleFileHasBom)
{
Write-Warning -Message "$filePathOutputName contain Byte Order Mark (BOM). Use fixer function 'ConvertTo-ASCII'."
}
$moduleFileHasBom | Should -Be $false
}
}
}
}
Describe 'Common Tests - Module Manifest' {
$containsClassResource = Test-ModuleContainsClassResource -ModulePath $moduleRootFilePath
if ($containsClassResource)
{
$minimumPSVersion = [Version]'5.0'
}
else
{
$minimumPSVersion = [Version]'4.0'
}
$moduleManifestPath = Join-Path -Path $moduleRootFilePath -ChildPath "$moduleName.psd1"
<#
ErrorAction specified as SilentelyContinue because this call will throw an error
on machines with an older PS version than the manifest requires. WMF 5.1 machines
are not yet available on AppVeyor, so modules that require 5.1 (PSDscResources)
would always crash this test.
#>
$moduleManifestProperties = Test-ModuleManifest -Path $moduleManifestPath -ErrorAction 'SilentlyContinue'
It "Should contain a PowerShellVersion property of at least $minimumPSVersion based on resource types" {
$moduleManifestProperties.PowerShellVersion -ge $minimumPSVersion | Should -Be $true
}
if ($containsClassResource)
{
$classResourcesInModule = Get-ClassResourceNameFromFile -FilePath $moduleRootFilePath
Context 'Requirements for manifest of module with class-based resources' {
foreach ($classResourceInModule in $classResourcesInModule)
{
It "Should explicitly export $classResourceInModule in DscResourcesToExport" {
$moduleManifestProperties.ExportedDscResources -contains $classResourceInModule | Should -Be $true
}
It "Should include class module $classResourceInModule.psm1 in NestedModules" {
$moduleManifestProperties.NestedModules.Name -contains $classResourceInModule | Should -Be $true
}
}
}
}
}
Describe 'Common Tests - Script Resource Schema Validation' {
Import-xDscResourceDesigner
$scriptResourceNames = Get-ModuleScriptResourceNames -ModulePath $moduleRootFilePath
foreach ($scriptResourceName in $scriptResourceNames)
{
Context $scriptResourceName {
$scriptResourcePath = Join-Path -Path $dscResourcesFolderFilePath -ChildPath $scriptResourceName
It 'Should pass Test-xDscResource' {
Test-xDscResource -Name $scriptResourcePath | Should -Be $true
}
It 'Should pass Test-xDscSchema' {
$mofSchemaFilePath = Join-Path -Path $scriptResourcePath -ChildPath "$scriptResourceName.schema.mof"
Test-xDscSchema -Path $mofSchemaFilePath | Should -Be $true
}
}
}
}
<#
PSSA = PS Script Analyzer
The following PSSA tests will always fail if any violations are found:
- Common Tests - Error-Level Script Analyzer Rules
- Common Tests - Custom Script Analyzer Rules
The following PSSA tests will only fail if a violation is found and
a matching option is found in the opt-in file.
- Common Tests - Required Script Analyzer Rules
- Common Tests - Flagged Script Analyzer Rules
- Common Tests - New Error-Level Script Analyzer Rules
- Common Tests - Custom Script Analyzer Rules
#>
Describe 'Common Tests - PS Script Analyzer on Resource Files' {
# PSScriptAnalyzer requires PowerShell 5.0 or higher
if ($PSVersionTable.PSVersion.Major -ge 5)
{
Import-PSScriptAnalyzer
$requiredPssaRuleNames = @(
'PSAvoidDefaultValueForMandatoryParameter',
'PSAvoidDefaultValueSwitchParameter',
'PSAvoidInvokingEmptyMembers',
'PSAvoidNullOrEmptyHelpMessageAttribute',
'PSAvoidUsingCmdletAliases',
'PSAvoidUsingComputerNameHardcoded',
'PSAvoidUsingDeprecatedManifestFields',
'PSAvoidUsingEmptyCatchBlock',
'PSAvoidUsingInvokeExpression',
'PSAvoidUsingPositionalParameters',
'PSAvoidShouldContinueWithoutForce',
'PSAvoidUsingWMICmdlet',
'PSAvoidUsingWriteHost',
'PSDSCReturnCorrectTypesForDSCFunctions',
'PSDSCStandardDSCFunctionsInResource',
'PSDSCUseIdenticalMandatoryParametersForDSC',
'PSDSCUseIdenticalParametersForDSC',
'PSMissingModuleManifestField',
'PSPossibleIncorrectComparisonWithNull',
'PSProvideCommentHelp',
'PSReservedCmdletChar',
'PSReservedParams',
'PSUseApprovedVerbs',
'PSUseCmdletCorrectly',
'PSUseOutputTypeCorrectly'
)
$flaggedPssaRuleNames = @(
'PSAvoidGlobalVars',
'PSAvoidUsingConvertToSecureStringWithPlainText',
'PSAvoidUsingPlainTextForPassword',
'PSAvoidUsingUsernameAndPasswordParams',
'PSDSCUseVerboseMessageInDSCResource',
'PSShouldProcess',
'PSUseDeclaredVarsMoreThanAssigments',
'PSUsePSCredentialType'
)
$ignorePssaRuleNames = @(
'PSDSCDscExamplesPresent',
'PSDSCDscTestsPresent',
'PSUseBOMForUnicodeEncodedFile',
'PSUseShouldProcessForStateChangingFunctions',
'PSUseSingularNouns',
'PSUseToExportFieldsInManifest',
'PSUseUTF8EncodingForHelpFile'
)
$dscResourcesPsm1Files = Get-Psm1FileList -FilePath $dscResourcesFolderFilePath
foreach ($dscResourcesPsm1File in $dscResourcesPsm1Files)
{
$invokeScriptAnalyzerParameters = @{
Path = $dscResourcesPsm1File.FullName
ErrorAction = 'SilentlyContinue'
Recurse = $true
}
Context $dscResourcesPsm1File.Name {
It 'Should pass all error-level PS Script Analyzer rules' {
$errorPssaRulesOutput = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters -Severity 'Error'
if ($null -ne $errorPssaRulesOutput)
{
Write-PsScriptAnalyzerWarning -PssaRuleOutput $errorPssaRulesOutput -RuleType 'Error-Level'
}
$errorPssaRulesOutput | Should -Be $null
}
It 'Should pass all required PS Script Analyzer rules' {
$requiredPssaRulesOutput = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters -IncludeRule $requiredPssaRuleNames
if ($null -ne $requiredPssaRulesOutput)
{
Write-PsScriptAnalyzerWarning -PssaRuleOutput $requiredPssaRulesOutput -RuleType 'Required'
}
if ($null -ne $requiredPssaRulesOutput -and (Get-OptInStatus -OptIns $optIns -Name 'Common Tests - Required Script Analyzer Rules'))
{
<#
If opted into 'Common Tests - Required Script Analyzer Rules' then
test that there were no violations
#>
$requiredPssaRulesOutput | Should -Be $null
}
}
It 'Should pass all flagged PS Script Analyzer rules' {
$flaggedPssaRulesOutput = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters -IncludeRule $flaggedPssaRuleNames
if ($null -ne $flaggedPssaRulesOutput)
{
Write-PsScriptAnalyzerWarning -PssaRuleOutput $flaggedPssaRulesOutput -RuleType 'Flagged'
}
if ($null -ne $flaggedPssaRulesOutput -and (Get-OptInStatus -OptIns $optIns -Name 'Common Tests - Flagged Script Analyzer Rules'))
{
<#
If opted into 'Common Tests - Flagged Script Analyzer Rules' then
test that there were no violations
#>
$flaggedPssaRulesOutput | Should -Be $null
}
}
It 'Should pass any recently-added, error-level PS Script Analyzer rules' {
$knownPssaRuleNames = $requiredPssaRuleNames + $flaggedPssaRuleNames + $ignorePssaRuleNames
$newErrorPssaRulesOutput = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters -ExcludeRule $knownPssaRuleNames -Severity 'Error'
if ($null -ne $newErrorPssaRulesOutput)
{
Write-PsScriptAnalyzerWarning -PssaRuleOutput $newErrorPssaRulesOutput -RuleType 'Recently-added'
}
if ($null -ne $newErrorPssaRulesOutput -and (Get-OptInStatus -OptIns $optIns -Name 'Common Tests - New Error-Level Script Analyzer Rules'))
{
<#
If opted into 'Common Tests - New Error-Level Script Analyzer Rules' then
test that there were no violations
#>
$newErrorPssaRulesOutput | Should -Be $null
}
}
It 'Should not suppress any required PS Script Analyzer rules' {
$requiredRuleIsSuppressed = $false
$suppressedRuleNames = Get-SuppressedPSSARuleNameList -FilePath $dscResourcesPsm1File.FullName
foreach ($suppressedRuleName in $suppressedRuleNames)
{
$suppressedRuleNameNoQuotes = $suppressedRuleName.Replace("'", '')
if ($requiredPssaRuleNames -icontains $suppressedRuleNameNoQuotes)
{
Write-Warning -Message "The file $($dscResourcesPsm1File.Name) contains a suppression of the required PS Script Analyser rule $suppressedRuleNameNoQuotes. Please remove the rule suppression."
$requiredRuleIsSuppressed = $true
}
}
$requiredRuleIsSuppressed | Should -Be $false
}
It 'Should pass all custom DSC Resource Kit PSSA rules' {
$customDscResourceAnalyzerRulesPath = Join-Path -Path $PSScriptRoot -ChildPath 'DscResource.AnalyzerRules'
$customPssaRulesOutput = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters `
-CustomRulePath $customDscResourceAnalyzerRulesPath `
-Severity 'Warning'
if ($null -ne $customPssaRulesOutput)
{
Write-PsScriptAnalyzerWarning -PssaRuleOutput $customPssaRulesOutput -RuleType 'Custom DSC Resource Kit'
}
if ($null -ne $customPssaRulesOutput -and (Get-OptInStatus -OptIns $optIns -Name 'Common Tests - Custom Script Analyzer Rules'))
{
<#
If opted into 'Common Tests - Custom Script Analyzer Rules' then
test that there were no violations
#>
$customPssaRulesOutput | Should -Be $null
}
}
}
}
}
else
{
Write-Warning -Message 'PS Script Analyzer could not run on this machine. Please run tests on a machine with WMF 5.0+.'
}
}
Describe 'Common Tests - Validate Example Files' -Tag 'Examples' {
$optIn = Get-PesterDescribeOptInStatus -OptIns $optIns
$examplesPath = Join-Path -Path $moduleRootFilePath -ChildPath 'Examples'
if (Test-Path -Path $examplesPath)
{
<#
For Appveyor builds copy the module to the system modules directory so it falls in to a PSModulePath folder and is
picked up correctly.
For a user to run the test, they need to make sure that the module exists in one of the paths in env:PSModulePath, i.e.
'%USERPROFILE%\Documents\WindowsPowerShell\Modules'.
No copying is done when a user runs the test, because that could potentially be destructive.
#>
if ($env:APPVEYOR -eq $true)
{
$powershellModulePath = Copy-ResourceModuleToPSModulePath -ResourceModuleName $moduleName -ModuleRootPath $moduleRootFilePath
}
$exampleFile = Get-ChildItem -Path (Join-Path -Path $moduleRootFilePath -ChildPath 'Examples') -Filter '*.ps1' -Recurse
foreach ($exampleToValidate in $exampleFile)
{
$exampleDescriptiveName = Join-Path -Path (Split-Path $exampleToValidate.Directory -Leaf) -ChildPath (Split-Path $exampleToValidate -Leaf)
Context -Name $exampleDescriptiveName {
It "Should compile MOFs for example correctly" -Skip:(!$optIn) {
{
$mockPassword = ConvertTo-SecureString '&iPm%M5q3K$Hhq=wcEK' -AsPlainText -Force
$mockCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @('username', $mockPassword)
$mockConfigurationData = @{
AllNodes = @(
@{
NodeName = 'localhost'
CertificateFile = $env:DscPublicCertificatePath
}
)
}
try
{
<#
Set this first because it is used in the final block,
and must be set otherwise it fails on not being assigned.
#>
$existingCommandName = $null
# Get the list of additional modules required by the example
$requiredModules = Get-ResourceModulesInConfiguration -ConfigurationPath $exampleToValidate.FullName |
Where-Object -Property Name -ne $moduleName
if ($requiredModules)
{
Install-DependentModule -Module $requiredModules
}
. $exampleToValidate.FullName
<#
Test for either a configuration named 'Example',
or parse the name from the filename and try that
as the configuration name (requirement for Azure
Automation).
#>
$commandName = @('Example')
$azureCommandName = Get-PublishFileName -Path $exampleToValidate.FullName
$commandName += $azureCommandName
# Get the first one that matches.
$existingCommand = Get-ChildItem -Path 'function:' |
Where-Object { $_.Name -in $commandName } |
Select-Object -First 1
if ($existingCommand)
{
$existingCommandName = $existingCommand.Name
$exampleCommand = Get-Command -Name $existingCommandName -ErrorAction SilentlyContinue
if ($exampleCommand)
{
$exampleParameters = @{ }
# Remove any common parameters that are available.
$commandParameters = $exampleCommand.Parameters.Keys | Where-Object -FilterScript {
($_ -notin [System.Management.Automation.PSCmdlet]::CommonParameters) -and `
($_ -notin [System.Management.Automation.PSCmdlet]::OptionalCommonParameters)
}
foreach ($parameterName in $commandParameters)
{
$parameterType = $exampleCommand.Parameters[$parameterName].ParameterType.FullName
<#
Each credential parameter in the Example function is assigned the
mocked credential. 'PsDscRunAsCredential' is not assigned because
that brakes the example.
#>
if ($parameterName -ne 'PsDscRunAsCredential' `
-and $parameterType -eq 'System.Management.Automation.PSCredential')
{
$exampleParameters.Add($parameterName, $mockCredential)
}
else
{
<#
Check for mandatory parameters.
Assume the parameters are all in the 'all' parameter set.
#>
$isParameterMandatory = $exampleCommand.Parameters[$parameterName].ParameterSets['__AllParameterSets'].IsMandatory
if ($isParameterMandatory)
{
<#
Convert '1' to the type that the parameter expects.
Using '1' since it can be converted to String, Numeric
and Boolean.
#>
$exampleParameters.Add($parameterName, ('1' -as $parameterType))
}
}
}
<#
If there is a $ConfigurationData variable that was dot-sources.
Then use that as the configuration data instead of the mocked configuration data.
#>
if (Get-Item -Path variable:ConfigurationData -ErrorAction SilentlyContinue)
{
# Adding certificate to the examples configuration data.
foreach ($node in $ConfigurationData.AllNodes.GetEnumerator())
{
if ($node.ContainsKey('PSDscAllowPlainTextPassword') -eq $true -and $node.PSDscAllowPlainTextPassword -eq $true)
{
Write-Warning -Message ('The example ''{0}'' is using PSDscAllowPlainTextPassword = $true in the configuration data for node name ''{1}'', this should be removed so the example is secure. PSDscAllowPlainTextPassword was overridden in the tests so the example can be tested securely.' -f $exampleDescriptiveName, $node.NodeName)
# Override PSDscAllowPlainTextPassword.
$node.PSDscAllowPlainTextPassword = $false
}
$node.CertificateFile = $env:DscPublicCertificatePath
}
$mockConfigurationData = $ConfigurationData
}
& $exampleCommand.Name @exampleParameters -ConfigurationData $mockConfigurationData -OutputPath 'TestDrive:\' -ErrorAction Continue -WarningAction SilentlyContinue | Out-Null
}
}
else
{
throw "The example '$exampleDescriptiveName' does not contain a configuration named 'Example' or '$azureCommandName'."
}
}
finally
{
# Remove the function we dot-sourced so next example file doesn't use the previous Example-function.
if ($existingCommandName)
{
Remove-Item -Path "function:$existingCommandName" -ErrorAction SilentlyContinue
}
# Remove the variable $ConfigurationData if it existed in the file we dot-sourced so next example file doesn't use the previous examples configuration.
Remove-Item -Path variable:ConfigurationData -ErrorAction SilentlyContinue
}
} | Should -Not -Throw
}
}
}
if ($env:APPVEYOR -eq $true)
{
Remove-Item -Path $powershellModulePath -Recurse -Force -Confirm:$false
# Restore the load of the module to ensure future tests have access to it
Import-Module -Name (Join-Path -Path $moduleRootFilePath `
-ChildPath "$moduleName.psd1") `
-Global
}
}
}
Describe 'Common Tests - Validate Example Files To Be Published' -Tag 'Examples' {
$optIn = Get-PesterDescribeOptInStatus -OptIns $optIns
$examplesPath = Join-Path -Path $moduleRootFilePath -ChildPath 'Examples'
# Due to speed, only run these test if opt-in and the examples folder exist.
if ($optIn -and (Test-Path -Path $examplesPath))
{
# We need helper functions from this module.
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'DscResource.GalleryDeploy')
<#
For Appveyor builds copy the module to the system modules directory so it falls in to a PSModulePath folder and is
picked up correctly.
For a user to run the test, they need to make sure that the module exists in one of the paths in env:PSModulePath, i.e.
'%USERPROFILE%\Documents\WindowsPowerShell\Modules'.
No copying is done when a user runs the test, because that could potentially be destructive.
#>
if ($env:APPVEYOR -eq $true)
{
$powershellModulePath = Copy-ResourceModuleToPSModulePath -ResourceModuleName $moduleName -ModuleRootPath $moduleRootFilePath
}
Context 'When there are examples that should be published' {
$exampleScriptFiles = Get-ChildItem -Path (Join-Path -Path $examplesPath -ChildPath '*Config.ps1')
It 'Should not contain any duplicate GUID is script file metadata' -Skip:(!$optIn) {
$exampleScriptMetadata = $exampleScriptFiles | ForEach-Object -Process {
<#
The cmdlet Test-ScriptFileInfo ignores the parameter ErrorAction and $ErrorActionPreference.
Instead a try-catch need to be used to ignore files that does not have the correct metadata.
#>
try
{
Test-ScriptFileInfo -Path $_.FullName
}
catch
{
# Intentionally left blank. Files with missing metadata will be caught in the next test.
}
}
$duplicateGuid = $exampleScriptMetadata |
Group-Object -Property Guid |
Where-Object { $_.Count -gt 1 }
if ($duplicateGuid -and $duplicateGuid.Count -gt 0)
{
$duplicateGuid |
ForEach-Object -Process {
Write-Warning -Message ('Guid {0} is duplicated in the files ''{1}''' -f $_.Name, ($_.Group.Name -join ', '))
}
}
$duplicateGuid | Should -BeNullOrEmpty
}
foreach ($exampleToValidate in $exampleScriptFiles)
{
$exampleDescriptiveName = Join-Path -Path (Split-Path -Path $exampleToValidate.Directory -Leaf) `
-ChildPath (Split-Path -Path $exampleToValidate -Leaf)
Context -Name "When publishing example '$exampleDescriptiveName'" {
It 'Should pass testing of script file metadata' -Skip:(!$optIn) {
{
Test-ScriptFileInfo -Path $exampleToValidate.FullName
} | Should -Not -Throw
}
It 'Should have the correct naming convention, and the same file name as the configuration name' -Skip:(!$optIn) {
$result = Test-ConfigurationName -Path $exampleToValidate.FullName
$result | Should -Be $true
}
}
}
}
if ($env:APPVEYOR -eq $true)
{
Remove-Item -Path $powershellModulePath -Recurse -Force -Confirm:$false
# Restore the load of the module to ensure future tests have access to it
Import-Module -Name (Join-Path -Path $moduleRootFilePath `
-ChildPath "$moduleName.psd1") `
-Global
}
}
}
Describe 'Common Tests - Validate Markdown Files' -Tag 'Markdown' {
$optIn = Get-PesterDescribeOptInStatus -OptIns $optIns
if (Get-Command -Name 'npm' -ErrorAction SilentlyContinue)
{
$npmParametersForStartProcess = @{
FilePath = 'npm'
ArgumentList = ''
WorkingDirectory = $PSScriptRoot
Wait = $true
WindowStyle = 'Hidden'
}
Context 'When installing markdown validation dependencies' {
It 'Should not throw an error when installing package Gulp in global scope' {
{
<#
gulp; gulp is a toolkit that helps you automate painful or time-consuming tasks in your development workflow.
gulp must be installed globally to be able to be called through Start-Process
#>
$npmParametersForStartProcess['ArgumentList'] = 'install -g gulp'
Start-Process @npmParametersForStartProcess
} | Should -Not -Throw
}
It 'Should not throw an error when installing package Gulp in local scope' {
{
# gulp must also be installed locally to be able to be referenced in the javascript file.
$npmParametersForStartProcess['ArgumentList'] = 'install gulp'
Start-Process @npmParametersForStartProcess
} | Should -Not -Throw
}
It 'Should not throw an error when installing package through2' {
{
# Used in gulpfile.js; A tiny wrapper around Node streams2 Transform to avoid explicit sub classing noise
$npmParametersForStartProcess['ArgumentList'] = 'install through2'
Start-Process @npmParametersForStartProcess
} | Should -Not -Throw
}
It 'Should not throw an error when installing package markdownlint' {
{
# Used in gulpfile.js; A Node.js style checker and lint tool for Markdown/CommonMark files.
$npmParametersForStartProcess['ArgumentList'] = 'install markdownlint'
Start-Process @npmParametersForStartProcess
} | Should -Not -Throw
}
It 'Should not throw an error when installing package gulp-concat as a dev-dependency' {
{
# gulp-concat is installed as devDependencies. Used in gulpfile.js; Concatenates files
$npmParametersForStartProcess['ArgumentList'] = 'install gulp-concat -D'
Start-Process @npmParametersForStartProcess
} | Should -Not -Throw
}
}
Context 'When there are markdown files' {
if (Test-Path -Path (Join-Path -Path $repoRootPath -ChildPath '.markdownlint.json'))
{
Write-Verbose -Message ('Using markdownlint settings file from repository folder ''{0}''.' -f $repoRootPath) -Verbose
$markdownlintSettingsFilePath = Join-Path -Path $repoRootPath -ChildPath '.markdownlint.json'
}
else
{
Write-Verbose -Message 'Using markdownlint settings file from DscResource.Test repository.' -Verbose
$markdownlintSettingsFilePath = $null
}
It "Should not have errors in any markdown files" {
$mdErrors = 0
try
{
if ($dscResourcesFolderFilePath -eq $moduleRootFilePath)
{
# Reverting back to defaults
$dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'
}
$gulpArgumentList = @(
'test-mdsyntax',
'--silent',
'--rootpath',
$repoRootPath,
'--dscresourcespath',
$dscResourcesFolderFilePath
)
if ($markdownlintSettingsFilePath)
{
$gulpArgumentList += @(
'--settingspath',
$markdownlintSettingsFilePath
)
}
Start-Process -FilePath 'gulp' -ArgumentList $gulpArgumentList `
-Wait -WorkingDirectory $PSScriptRoot -PassThru -NoNewWindow
Start-Sleep -Seconds 3
$mdIssuesPath = Join-Path -Path $PSScriptRoot -ChildPath 'markdownissues.txt'
if ((Test-Path -Path $mdIssuesPath) -eq $true)
{
Get-Content -Path $mdIssuesPath | ForEach-Object -Process {
if ([string]::IsNullOrEmpty($_) -eq $false)
{
Write-Warning -Message $_
$mdErrors ++
}
}
}
Remove-Item -Path $mdIssuesPath -Force -ErrorAction SilentlyContinue
}
catch [System.Exception]
{
Write-Warning -Message ('Unable to run gulp to test markdown files. Please ' + `
'be sure that you have installed nodejs and have ' + `
'run ''npm install -g gulp'' in order to have this ' + `
'test execute.')
}
if ($optIn)
{
$mdErrors | Should -Be 0
}
}
}
<#
We're uninstalling the dependencies, in reverse order, so that the
node_modules folder do not linger on a users computer if run locally.
Also, this fixes so that when there is a apostrophe in the path for
$PSScriptRoot, the node_modules folder is correctly removed.
#>
Context 'When uninstalling markdown validation dependencies' {
It 'Should not throw an error when uninstalling package gulp-concat as a dev-dependency' {
{
# gulp-concat is installed as devDependencies. Used in gulpfile.js; Concatenates files