1
- Import-Module PSScriptAnalyzer
1
+ Import-Module PSScriptAnalyzer
2
2
$missingMessage = " The member 'ModuleVersion' is not present in the module manifest."
3
- $missingName = " PSMissingModuleManifestField"
3
+ $missingMemberRuleName = " PSMissingModuleManifestField"
4
4
$directory = Split-Path - Parent $MyInvocation.MyCommand.Path
5
5
$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"
8
9
9
10
Describe " MissingRequiredFieldModuleManifest" {
10
11
BeforeAll {
11
12
Import-Module (Join-Path $directory " PSScriptAnalyzerTestHelper.psm1" )
12
13
}
13
-
14
+
14
15
AfterAll{
15
16
Remove-Module PSScriptAnalyzerTestHelper
16
- }
17
-
17
+ }
18
+
18
19
Context " When there are violations" {
19
20
It " has 1 missing required field module manifest violation" {
20
21
$violations.Count | Should Be 1
@@ -23,27 +24,33 @@ Describe "MissingRequiredFieldModuleManifest" {
23
24
It " has the correct description message" {
24
25
$violations.Message | Should Match $missingMessage
25
26
}
26
-
27
+
27
28
$numExpectedCorrections = 1
28
29
It " has $numExpectedCorrections suggested corrections" {
29
30
$violations.SuggestedCorrections.Count | Should Be $numExpectedCorrections
30
31
}
31
-
32
32
33
- It " has the right suggested correction" {
33
+
34
+ It " has the right suggested correction" {
34
35
$expectedText = @'
35
36
# Version number of this module.
36
37
ModuleVersion = '1.0.0.0'
37
38
'@
38
39
$violations [0 ].SuggestedCorrections[0 ].Text | Should Match $expectedText
39
40
Get-ExtentText $violations [0 ].SuggestedCorrections[0 ] $violationFilepath | Should Match " "
40
- }
41
- }
41
+ }
42
+ }
42
43
43
44
Context " When there are no violations" {
44
45
It " returns no violations" {
45
46
$noViolations.Count | Should Be 0
46
47
}
47
48
}
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
+ }
48
55
}
49
56
0 commit comments