Skip to content

Commit b943634

Browse files
author
Kapil Borle
committed
Add tests for MissingModuleManifestFields
* Modifies the implmentation to add description and other minor changes * Adds test cases to verify the correction extent of the rule
1 parent 5cd8a3a commit b943634

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

Rules/MissingModuleManifestField.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,31 @@ private List<CorrectionExtent> GetCorrectionExtent(HashtableAst ast)
8181
}
8282

8383
var correctionExtents = new List<CorrectionExtent>();
84-
string moduleVersionText = @"
85-
# Version number of this module.
86-
ModuleVersion = '1.0.0.0'";
84+
string fieldName = "ModuleVersion";
85+
string fieldValue = "1.0.0.0";
8786
int startLineNumber = ast.Extent.StartLineNumber;
8887
int startColumnNumber = ast.Extent.StartColumnNumber + 2; // 2 for "@{",
89-
var correctionText = new StringBuilder();
90-
correctionText.AppendLine();
91-
correctionText.Append(moduleVersionText);
92-
correctionText.AppendLine();
93-
correctionText.AppendLine();
88+
string description = string.Format(
89+
CultureInfo.CurrentCulture,
90+
Strings.MissingModuleManifestFieldCorrectionDescription,
91+
fieldName,
92+
fieldValue);
93+
var correctionTextTemplate = @"
94+
# Version number of this module.
95+
{0} = {1}
96+
";
97+
var correctionText = string.Format(
98+
correctionTextTemplate,
99+
fieldName,
100+
fieldValue);
94101
var correctionExtent = new CorrectionExtent(
95102
startLineNumber,
96103
startLineNumber,
97104
startColumnNumber,
98105
startColumnNumber + 1,
99-
correctionText.ToString(),
100-
ast.Extent.File);
106+
correctionText,
107+
ast.Extent.File,
108+
description);
101109
correctionExtents.Add(correctionExtent);
102110
return correctionExtents;
103111
}

Rules/Strings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rules/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,9 @@
816816
<data name="AvoidUsingPlainTextForPasswordCorrectionDescription" xml:space="preserve">
817817
<value>Set {0} type to SecureString</value>
818818
</data>
819+
<data name="MissingModuleManifestFieldCorrectionDescription" xml:space="preserve">
820+
<value>Add {0} = {1} to the module manifest</value>
821+
</data>
819822
<data name="UseToExportFieldsInManifestCorrectionDescription" xml:space="preserve">
820823
<value>Replace {0} with {1}</value>
821824
</data>

Tests/Rules/AvoidUnloadableModuleOrMissingRequiredFieldInManifest.tests.ps1

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22
$missingMessage = "The member 'ModuleVersion' is not present in the module manifest."
33
$missingName = "PSMissingModuleManifestField"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
5-
$violations = Invoke-ScriptAnalyzer $directory\TestBadModule\TestBadModule.psd1 | Where-Object {$_.RuleName -eq $missingName}
5+
$violationFilepath = Join-Path $directory "TestBadModule\TestBadModule.psd1"
6+
$violations = Invoke-ScriptAnalyzer $violationFilepath | Where-Object {$_.RuleName -eq $missingName}
67
$noViolations = Invoke-ScriptAnalyzer $directory\TestGoodModule\TestGoodModule.psd1 | Where-Object {$_.RuleName -eq $missingName}
78

89
Describe "MissingRequiredFieldModuleManifest" {
10+
BeforeAll {
11+
Import-Module (Join-Path $directory "PSScriptAnalyzerTestHelper.psm1")
12+
}
13+
14+
AfterAll{
15+
Remove-Module PSScriptAnalyzerTestHelper
16+
}
17+
918
Context "When there are violations" {
1019
It "has 1 missing required field module manifest violation" {
1120
$violations.Count | Should Be 1
@@ -14,7 +23,23 @@ Describe "MissingRequiredFieldModuleManifest" {
1423
It "has the correct description message" {
1524
$violations.Message | Should Match $missingMessage
1625
}
17-
}
26+
27+
$expectedCorrections = 1
28+
It "has $expectedCorrections suggested corrections" {
29+
$violations.SuggestedCorrections.Count | Should Be 1
30+
}
31+
32+
33+
It "has the right suggested correction" {
34+
$expectedText = @'
35+
36+
# Version number of this module.
37+
ModuleVersion = 1.0.0.0
38+
'@
39+
$violations[0].SuggestedCorrections[0].Text | Should Match $expectedText
40+
Get-ExtentText $violations[0].SuggestedCorrections[0] $violationFilepath | Should Match ""
41+
}
42+
}
1843

1944
Context "When there are no violations" {
2045
It "returns no violations" {

0 commit comments

Comments
 (0)