Skip to content

Commit 8e2b07d

Browse files
author
Kapil Borle
committed
Merge pull request #542 from PowerShell/FixMissingModuleManifestMemberRule
Fix null reference exception in MissingModuleManifestField rule
2 parents d1bba28 + cd86fa1 commit 8e2b07d

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

Rules/MissingModuleManifestField.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5050
System.Diagnostics.Debug.Assert(
5151
errorRecord.Exception != null && !String.IsNullOrWhiteSpace(errorRecord.Exception.Message),
5252
Strings.NullErrorMessage);
53-
var hashTableAst = ast.Find(x => x is HashtableAst, false);
53+
var hashTableAst = ast.Find(x => x is HashtableAst, false);
54+
if (hashTableAst == null)
55+
{
56+
yield break;
57+
}
5458
yield return new DiagnosticRecord(
5559
errorRecord.Exception.Message,
5660
hashTableAst.Extent,
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
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

910
Describe "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.
3637
ModuleVersion = '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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$x = "No hash table here"

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ clone_folder: c:\projects\psscriptanalyzer
66

77
# Install Pester
88
install:
9-
- cinst -y pester --version 3.3.13
9+
- cinst -y pester --version 3.4.0
1010

1111
# Build PSScriptAnalyzer using msbuild
1212
build: true

0 commit comments

Comments
 (0)