Skip to content

Commit edd49c0

Browse files
author
Quoc Truong
committed
Merge pull request #446 from PowerShell/FixDeprecatedManifestFields
Fix a bug where deprecated manifest fields are raised even if powershellversion 2.0 is used
2 parents 55f38b3 + ae54808 commit edd49c0

File tree

4 files changed

+172
-4
lines changed

4 files changed

+172
-4
lines changed

Rules/AvoidUsingDeprecatedManifestFields.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
1818
using System.ComponentModel.Composition;
1919
using System.Globalization;
20+
using System.Linq;
2021

2122
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2223
{
@@ -43,6 +44,49 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4344
{
4445
var ps = System.Management.Automation.PowerShell.Create();
4546
IEnumerable<PSObject> result = null;
47+
48+
// hash table in psd1
49+
var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault();
50+
51+
// no hash table means not a module manifest
52+
if (hashTableAst == null)
53+
{
54+
yield break;
55+
}
56+
57+
var table = hashTableAst as HashtableAst;
58+
59+
// needs to find the PowerShellVersion key
60+
foreach (var kvp in table.KeyValuePairs)
61+
{
62+
if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst)
63+
{
64+
var key = (kvp.Item1 as StringConstantExpressionAst).Value;
65+
66+
// find the powershellversion key in the hashtable
67+
if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null)
68+
{
69+
// get the string value of the version
70+
var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false);
71+
72+
if (value != null)
73+
{
74+
Version psVersion = null;
75+
76+
// get the version
77+
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
78+
{
79+
// if version exists and version less than 3, don't raise rule
80+
if (psVersion.Major < 3)
81+
{
82+
yield break;
83+
}
84+
}
85+
}
86+
}
87+
}
88+
}
89+
4690
try
4791
{
4892
ps.AddCommand("Test-ModuleManifest");
@@ -54,7 +98,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5498
ps.AddParameter("WarningVariable", "Message");
5599
ps.AddScript("$Message");
56100
result = ps.Invoke();
57-
58101
}
59102
catch
60103
{}

Tests/Rules/AvoidUsingDeprecatedManifestFields.tests.ps1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Import-Module PSScriptAnalyzer
22
$violationName = "PSAvoidUsingDeprecatedManifestFields"
33
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
4-
$violations = Invoke-ScriptAnalyzer $directory\TestBadModule\TestDeprecatedManifestFields.psd1 | Where-Object {$_.RuleName -eq $violationName}
5-
$noViolations = Invoke-ScriptAnalyzer $directory\TestGoodModule\TestGoodModule.psd1 | Where-Object {$_.RuleName -eq $violationName}
4+
$violations = Invoke-ScriptAnalyzer "$directory\TestBadModule\TestDeprecatedManifestFields.psd1" | Where-Object {$_.RuleName -eq $violationName}
5+
$noViolations = Invoke-ScriptAnalyzer "$directory\TestGoodModule\TestGoodModule.psd1" | Where-Object {$_.RuleName -eq $violationName}
6+
$noViolations2 = Invoke-ScriptAnalyzer "$directory\TestGoodModule\TestDeprecatedManifestFieldsWithVersion2.psd1" | Where-Object {$_.RuleName -eq $violationName}
67

78
Describe "AvoidUsingDeprecatedManifestFields" {
89
Context "When there are violations" {
@@ -12,7 +13,11 @@ Describe "AvoidUsingDeprecatedManifestFields" {
1213
}
1314

1415
Context "When there are no violations" {
15-
It "returns no violations" {
16+
It "returns no violations if no deprecated fields are used" {
17+
$noViolations.Count | Should Be 0
18+
}
19+
20+
It "returns no violations if deprecated fields are used but psVersion is less than 3.0" {
1621
$noViolations.Count | Should Be 0
1722
}
1823
}

Tests/Rules/TestGoodModule/TestDeprecatedManifestFieldsWithVersion2.psd1

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#
2+
# Module manifest for module 'Deprecated Module manifest fields"
3+
#
4+
# Generated by: Microsoft PowerShell Team
5+
#
6+
# Generated on: 5/18/2015
7+
#
8+
9+
@{
10+
11+
# Script module or binary module file associated with this manifest.
12+
ModuleToProcess ='psscriptanalyzer'
13+
14+
# Version number of this module.
15+
ModuleVersion = '1.0'
16+
17+
# ID used to uniquely identify this module
18+
GUID = 'a9f79c02-4503-4300-a022-5e8c01f3449f'
19+
20+
# Author of this module
21+
Author = ''
22+
23+
# Company or vendor of this module
24+
CompanyName = ''
25+
26+
# Copyright statement for this module
27+
Copyright = '(c) 2015 Microsoft. All rights reserved.'
28+
29+
# Description of the functionality provided by this module
30+
# Description = ''
31+
32+
# Minimum version of the Windows PowerShell engine required by this module
33+
PowerShellVersion = '2.0'
34+
35+
# Name of the Windows PowerShell host required by this module
36+
# PowerShellHostName = ''
37+
38+
# Minimum version of the Windows PowerShell host required by this module
39+
# PowerShellHostVersion = ''
40+
41+
# Minimum version of Microsoft .NET Framework required by this module
42+
# DotNetFrameworkVersion = ''
43+
44+
# Minimum version of the common language runtime (CLR) required by this module
45+
# CLRVersion = ''
46+
47+
# Processor architecture (None, X86, Amd64) required by this module
48+
# ProcessorArchitecture = ''
49+
50+
# Modules that must be imported into the global environment prior to importing this module
51+
# RequiredModules = @()
52+
53+
# Assemblies that must be loaded prior to importing this module
54+
# RequiredAssemblies = @()
55+
56+
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
57+
# ScriptsToProcess = @()
58+
59+
# Type files (.ps1xml) to be loaded when importing this module
60+
# TypesToProcess = @()
61+
62+
# Format files (.ps1xml) to be loaded when importing this module
63+
# FormatsToProcess = @()
64+
65+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
66+
# NestedModules = @()
67+
68+
# Functions to export from this module
69+
FunctionsToExport = '*'
70+
71+
# Cmdlets to export from this module
72+
CmdletsToExport = '*'
73+
74+
# Variables to export from this module
75+
VariablesToExport = '*'
76+
77+
# Aliases to export from this module
78+
AliasesToExport = '*'
79+
80+
# DSC resources to export from this module
81+
# DscResourcesToExport = @()
82+
83+
# List of all modules packaged with this module
84+
# ModuleList = @()
85+
86+
# List of all files packaged with this module
87+
# FileList = @()
88+
89+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable
90+
#with additional module metadata used by PowerShell.
91+
PrivateData = @{
92+
93+
PSData = @{
94+
95+
# Tags applied to this module. These help with module discovery in online galleries.
96+
# Tags = @()
97+
98+
# A URL to the license for this module.
99+
# LicenseUri = ''
100+
101+
# A URL to the main website for this project.
102+
# ProjectUri = ''
103+
104+
# A URL to an icon representing this module.
105+
# IconUri = ''
106+
107+
# ReleaseNotes of this module
108+
# ReleaseNotes = ''
109+
110+
} # End of PSData hashtable
111+
112+
} # End of PrivateData hashtable
113+
114+
# HelpInfo URI of this module
115+
# HelpInfoURI = ''
116+
117+
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
118+
# DefaultCommandPrefix = ''
119+
120+
}
2 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)