forked from PowerShell/DscResource.Tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeta.Tests.ps1
384 lines (309 loc) · 15.8 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
<#
.SYNOPSIS
Common tests for all resource modules in the DSC Resource Kit.
#>
Set-StrictMode -Version 'Latest'
$errorActionPreference = 'Stop'
$testHelperModulePath = Join-Path -Path $PSScriptRoot -ChildPath 'TestHelper.psm1'
Import-Module -Name $testHelperModulePath
$moduleRootFilePath = Split-Path -Path $PSScriptRoot -Parent
$dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'
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. Use fixer function 'Get-TextFilesList `$pwd | ConvertTo-SpaceIndentation'."
$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
}
}
Describe 'Common Tests - .psm1 File Parsing' {
$psm1Files = Get-Psm1FileList -FilePath $moduleRootFilePath
foreach ($psm1File in $psm1Files)
{
Context $psm1File.Name {
It 'Should not contain parse errors' {
$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 - Module Manifest' {
$containsClassResource = Test-ModuleContainsClassResource -ModulePath $moduleRootFilePath
if ($containsClassResource)
{
$minimumPSVersion = [Version]'5.0'
}
else
{
$minimumPSVersion = [Version]'4.0'
}
$moduleName = (Get-Item -Path $moduleRootFilePath).Name
$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-ClassResourceNames -ModulePath $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
Only the first and last tests here will pass/fail correctly at the moment. The other 3 tests
will currently always pass, but print warnings based on the problems they find.
These automatic passes are here to give contributors time to fix the PSSA
problems before we turn on these tests. These 'automatic passes' should be removed
along with the first test (which is replaced by the following 3) around Jan-Feb
2017.
#>
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-Warning -Message 'Error-level PSSA rule(s) did not pass.'
Write-Warning -Message 'The following PSScriptAnalyzer errors need to be fixed:'
foreach ($errorPssaRuleOutput in $errorPssaRulesOutput)
{
Write-Warning -Message "$($errorPssaRuleOutput.ScriptName) (Line $($errorPssaRuleOutput.Line)): $($errorPssaRuleOutput.Message)"
}
Write-Warning -Message 'For instructions on how to run PSScriptAnalyzer on your own machine, please go to https://github.com/powershell/PSScriptAnalyzer'
}
$errorPssaRulesOutput | Should Be $null
}
It 'Should pass all required PS Script Analyzer rules' {
$requiredPssaRulesOutput = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters -IncludeRule $requiredPssaRuleNames
if ($null -ne $requiredPssaRulesOutput) {
Write-Warning -Message 'Required PSSA rule(s) did not pass.'
Write-Warning -Message 'The following PSScriptAnalyzer errors need to be fixed:'
foreach ($requiredPssaRuleOutput in $requiredPssaRulesOutput)
{
Write-Warning -Message "$($requiredPssaRuleOutput.ScriptName) (Line $($requiredPssaRuleOutput.Line)): $($requiredPssaRuleOutput.Message)"
}
Write-Warning -Message 'For instructions on how to run PSScriptAnalyzer on your own machine, please go to https://github.com/powershell/PSScriptAnalyzer'
}
<#
Automatically passing this test since it may break several resource modules at the moment.
Automatic pass to be removed Jan-Feb 2017.
#>
$requiredPssaRulesOutput = $null
$requiredPssaRulesOutput | Should Be $null
}
It 'Should pass all flagged PS Script Analyzer rules' {
$flaggedPssaRulesOutput = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters -IncludeRule $flaggedPssaRuleNames
if ($null -ne $flaggedPssaRulesOutput) {
Write-Warning -Message 'Flagged PSSA rule(s) did not pass.'
Write-Warning -Message 'The following PSScriptAnalyzer errors need to be fixed or approved to be suppressed:'
foreach ($flaggedPssaRuleOutput in $flaggedPssaRulesOutput)
{
Write-Warning -Message "$($flaggedPssaRuleOutput.ScriptName) (Line $($flaggedPssaRuleOutput.Line)): $($flaggedPssaRuleOutput.Message)"
}
Write-Warning -Message 'For instructions on how to run PSScriptAnalyzer on your own machine, please go to https://github.com/powershell/PSScriptAnalyzer'
}
<#
Automatically passing this test since it may break several resource modules at the moment.
Automatic pass to be removed Jan-Feb 2017.
#>
$flaggedPssaRulesOutput = $null
$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-Warning -Message 'Recently-added, error-level PSSA rule(s) did not pass.'
Write-Warning -Message 'The following PSScriptAnalyzer errors need to be fixed or approved to be suppressed:'
foreach ($newErrorPssaRuleOutput in $newErrorPssaRulesOutput)
{
Write-Warning -Message "$($newErrorPssaRuleOutput.ScriptName) (Line $($newErrorPssaRuleOutput.Line)): $($newErrorPssaRuleOutput.Message)"
}
Write-Warning -Message 'For instructions on how to run PSScriptAnalyzer on your own machine, please go to https://github.com/powershell/PSScriptAnalyzer'
}
<#
Automatically passing this test since it may break several resource modules at the moment.
Automatic pass to be removed Jan-Feb 2017.
#>
$newErrorPssaRulesOutput = $null
$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
}
}
}
}
else
{
Write-Warning -Message 'PS Script Analyzer could not run on this machine. Please run tests on a machine with WMF 5.0+.'
}
}