1- Import-Module PSScriptAnalyzer
1+ Import-Module PSScriptAnalyzer
22$missingMessage = " The member 'ModuleVersion' is not present in the module manifest."
3- $missingName = " PSMissingModuleManifestField"
3+ $missingMemberRuleName = " PSMissingModuleManifestField"
44$directory = Split-Path - Parent $MyInvocation.MyCommand.Path
55$violationFilepath = Join-Path $directory " TestBadModule\TestBadModule.psd1"
6- $violations = Invoke-ScriptAnalyzer $violationFilepath | Where-Object {$_.RuleName -eq $missingName }
7- $noViolations = Invoke-ScriptAnalyzer $directory \TestGoodModule\TestGoodModule.psd1 | Where-Object {$_.RuleName -eq $missingName }
6+ $violations = Invoke-ScriptAnalyzer $violationFilepath | Where-Object {$_.RuleName -eq $missingMemberRuleName }
7+ $noViolations = Invoke-ScriptAnalyzer $directory \TestGoodModule\TestGoodModule.psd1 | Where-Object {$_.RuleName -eq $missingMemberRuleName }
8+ $noHashtableFilepath = Join-Path $directory " TestBadModule\NoHashtable.psd1"
89
910Describe " MissingRequiredFieldModuleManifest" {
1011 BeforeAll {
1112 Import-Module (Join-Path $directory " PSScriptAnalyzerTestHelper.psm1" )
1213 }
13-
14+
1415 AfterAll{
1516 Remove-Module PSScriptAnalyzerTestHelper
16- }
17-
17+ }
18+
1819 Context " When there are violations" {
1920 It " has 1 missing required field module manifest violation" {
2021 $violations.Count | Should Be 1
@@ -23,27 +24,33 @@ Describe "MissingRequiredFieldModuleManifest" {
2324 It " has the correct description message" {
2425 $violations.Message | Should Match $missingMessage
2526 }
26-
27+
2728 $numExpectedCorrections = 1
2829 It " has $numExpectedCorrections suggested corrections" {
2930 $violations.SuggestedCorrections.Count | Should Be $numExpectedCorrections
3031 }
31-
3232
33- It " has the right suggested correction" {
33+
34+ It " has the right suggested correction" {
3435 $expectedText = @'
3536# Version number of this module.
3637ModuleVersion = '1.0.0.0'
3738'@
3839 $violations [0 ].SuggestedCorrections[0 ].Text | Should Match $expectedText
3940 Get-ExtentText $violations [0 ].SuggestedCorrections[0 ] $violationFilepath | Should Match " "
40- }
41- }
41+ }
42+ }
4243
4344 Context " When there are no violations" {
4445 It " returns no violations" {
4546 $noViolations.Count | Should Be 0
4647 }
4748 }
49+
50+ Context " When an .psd1 file doesn't contain a hashtable" {
51+ It " does not throw exception" {
52+ {Invoke-ScriptAnalyzer - Path $noHashtableFilepath - IncludeRule $missingMemberRuleName } | Should Not Throw
53+ }
54+ }
4855}
4956
0 commit comments