Skip to content

Commit 8059853

Browse files
author
Kapil Borle
committed
Add version parameter for checking module manifest
1 parent b746866 commit 8059853

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

Engine/Helper.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,8 +1604,11 @@ public bool GetNamedArgumentAttributeValue(NamedAttributeArgumentAst namedAttrib
16041604

16051605
public static IEnumerable<string> GetModuleManifestKeys(Version powershellVersion)
16061606
{
1607+
if (powershellVersion == null)
1608+
{
1609+
throw new ArgumentNullException("powershellVersion");
1610+
}
16071611
var keys = new List<string>();
1608-
16091612
var keysCommon = new List<string> {
16101613
"RootModule",
16111614
"ModuleVersion",
@@ -1635,25 +1638,31 @@ public static IEnumerable<string> GetModuleManifestKeys(Version powershellVersio
16351638
"PrivateData",
16361639
"HelpInfoURI",
16371640
"DefaultCommandPrefix"};
1638-
16391641
keys.AddRange(keysCommon);
1640-
1641-
// default to version 5.0
1642-
if (powershellVersion == null || powershellVersion.Equals(new Version("5.0")))
1642+
if (powershellVersion.Equals(new Version("5.0")))
16431643
{
16441644
keys.Add("DscResourcesToExport");
16451645
}
1646+
else if (!powershellVersion.Equals(new Version("4.0"))
1647+
&& !powershellVersion.Equals(new Version("3.0")))
1648+
{
1649+
throw new ArgumentException("Invalid PowerShell version. Choose from 3.0, 4.0 or 5.0");
1650+
}
16461651
return keys;
16471652
}
16481653

1649-
public static bool IsModuleManifest(string filepath)
1654+
public static bool IsModuleManifest(string filepath, Version powershellVersion)
16501655
{
16511656
Token[] tokens;
16521657
ParseError[] errors;
16531658
if (filepath == null)
16541659
{
16551660
throw new ArgumentNullException("filepath");
16561661
}
1662+
if (powershellVersion == null)
1663+
{
1664+
throw new ArgumentNullException("powershellVersion");
1665+
}
16571666
if (!Path.GetExtension(filepath).Equals(".psd1", StringComparison.OrdinalIgnoreCase))
16581667
{
16591668
return false;
@@ -1667,7 +1676,9 @@ public static bool IsModuleManifest(string filepath)
16671676
{
16681677
return false;
16691678
}
1670-
var keys = GetModuleManifestKeys(null);
1679+
var keys = GetModuleManifestKeys(powershellVersion);
1680+
1681+
// check if all the keys in hast.keyvaluepairs are present in keys
16711682
int matchCount = 0;
16721683
foreach(var pair in hast.KeyValuePairs)
16731684
{

Tests/Rules/AvoidUnloadableModuleOrMissingRequiredFieldInManifest.tests.ps1

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,35 @@ ModuleVersion = '1.0.0.0'
5656
Context "Validate the contents of a .psd1 file" {
5757
It "detects a valid module manifest file" {
5858
$filepath = Join-Path $directory "TestManifest/ManifestGood.psd1"
59-
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper]::IsModuleManifest($filepath) | Should Be $true
59+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper]::IsModuleManifest($filepath, [version]"5.0") | Should Be $true
6060
}
6161

6262
It "detects a .psd1 file which is not module manifest" {
6363
$filepath = Join-Path $directory "TestManifest/PowerShellDataFile.psd1"
64-
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper]::IsModuleManifest($filepath) | Should Be $false
64+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper]::IsModuleManifest($filepath, [version]"5.0") | Should Be $false
6565
}
66+
67+
It "detects valid module manifest file for PSv5" {
68+
$filepath = Join-Path $directory "TestManifest/ManifestGoodPSv5.psd1"
69+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper]::IsModuleManifest($filepath, [version]"5.0") | Should Be $true
70+
}
71+
72+
It "does not validate PSv5 module manifest file for PSv3 check" {
73+
$filepath = Join-Path $directory "TestManifest/ManifestGoodPSv5.psd1"
74+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper]::IsModuleManifest($filepath, [version]"3.0") | Should Be $false
75+
}
76+
77+
It "detects valid module manifest file for PSv4" {
78+
$filepath = Join-Path $directory "TestManifest/ManifestGoodPSv4.psd1"
79+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper]::IsModuleManifest($filepath, [version]"4.0") | Should Be $true
80+
}
81+
82+
It "detects valid module manifest file for PSv3" {
83+
$filepath = Join-Path $directory "TestManifest/ManifestGoodPSv3.psd1"
84+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper]::IsModuleManifest($filepath, [version]"3.0") | Should Be $true
85+
}
86+
87+
6688
}
6789
}
6890

-4 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)