Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion Rules/AvoidUsingDeprecatedManifestFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
using System.ComponentModel.Composition;
using System.Globalization;
using System.Linq;

namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
Expand All @@ -43,6 +44,49 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
var ps = System.Management.Automation.PowerShell.Create();
IEnumerable<PSObject> result = null;

// hash table in psd1
var hashTableAst = ast.FindAll(item => item is HashtableAst, false).FirstOrDefault();

// no hash table means not a module manifest
if (hashTableAst == null)
{
yield break;
}

var table = hashTableAst as HashtableAst;

// needs to find the PowerShellVersion key
foreach (var kvp in table.KeyValuePairs)
{
if (kvp.Item1 != null && kvp.Item1 is StringConstantExpressionAst)
{
var key = (kvp.Item1 as StringConstantExpressionAst).Value;

// find the powershellversion key in the hashtable
if (string.Equals(key, "PowerShellVersion", StringComparison.OrdinalIgnoreCase) && kvp.Item2 != null)
{
// get the string value of the version
var value = kvp.Item2.Find(item => item is StringConstantExpressionAst, false);

if (value != null)
{
Version psVersion = null;

// get the version
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
{
// if version exists and version less than 3, don't raise rule
if (psVersion.Major < 3)
{
yield break;
}
}
}
}
}
}

try
{
ps.AddCommand("Test-ModuleManifest");
Expand All @@ -54,7 +98,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
ps.AddParameter("WarningVariable", "Message");
ps.AddScript("$Message");
result = ps.Invoke();

}
catch
{}
Expand Down
11 changes: 8 additions & 3 deletions Tests/Rules/AvoidUsingDeprecatedManifestFields.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Import-Module PSScriptAnalyzer
$violationName = "PSAvoidUsingDeprecatedManifestFields"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\TestBadModule\TestDeprecatedManifestFields.psd1 | Where-Object {$_.RuleName -eq $violationName}
$noViolations = Invoke-ScriptAnalyzer $directory\TestGoodModule\TestGoodModule.psd1 | Where-Object {$_.RuleName -eq $violationName}
$violations = Invoke-ScriptAnalyzer "$directory\TestBadModule\TestDeprecatedManifestFields.psd1" | Where-Object {$_.RuleName -eq $violationName}
$noViolations = Invoke-ScriptAnalyzer "$directory\TestGoodModule\TestGoodModule.psd1" | Where-Object {$_.RuleName -eq $violationName}
$noViolations2 = Invoke-ScriptAnalyzer "$directory\TestGoodModule\TestDeprecatedManifestFieldsWithVersion2.psd1" | Where-Object {$_.RuleName -eq $violationName}

Describe "AvoidUsingDeprecatedManifestFields" {
Context "When there are violations" {
Expand All @@ -12,7 +13,11 @@ Describe "AvoidUsingDeprecatedManifestFields" {
}

Context "When there are no violations" {
It "returns no violations" {
It "returns no violations if no deprecated fields are used" {
$noViolations.Count | Should Be 0
}

It "returns no violations if deprecated fields are used but psVersion is less than 3.0" {
$noViolations.Count | Should Be 0
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#
# Module manifest for module 'Deprecated Module manifest fields"
#
# Generated by: Microsoft PowerShell Team
#
# Generated on: 5/18/2015
#

@{

# Script module or binary module file associated with this manifest.
ModuleToProcess ='psscriptanalyzer'

# Version number of this module.
ModuleVersion = '1.0'

# ID used to uniquely identify this module
GUID = 'a9f79c02-4503-4300-a022-5e8c01f3449f'

# Author of this module
Author = ''

# Company or vendor of this module
CompanyName = ''

# Copyright statement for this module
Copyright = '(c) 2015 Microsoft. All rights reserved.'

# Description of the functionality provided by this module
# Description = ''

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '2.0'

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module
# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module
FunctionsToExport = '*'

# Cmdlets to export from this module
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module
AliasesToExport = '*'

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable
#with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
# ProjectUri = ''

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}
Expand Down
Binary file modified Tests/Rules/TestGoodModule/TestGoodModule.psd1
Binary file not shown.