Skip to content

Avoid using deprecated manifest fields #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 19, 2015
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
132 changes: 132 additions & 0 deletions Rules/AvoidUsingDeprecatedManifestFields.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// Copyright (c) Microsoft Corporation.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Management.Automation.Language;
using System.Management.Automation;
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
using System.ComponentModel.Composition;
using System.Globalization;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// AvoidUsingDeprecatedManifestFields: Run Test Module Manifest to check that no deprecated fields are being used.
/// </summary>
[Export(typeof(IScriptRule))]
public class AvoidUsingDeprecatedManifestFields : IScriptRule
{
/// <summary>
/// AnalyzeScript: Run Test Module Manifest to check that no deprecated fields are being used.
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The script's file name</param>
/// <returns>A List of diagnostic results of this rule</returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null)
{
throw new ArgumentNullException(Strings.NullAstErrorMessage);
}

if (String.Equals(System.IO.Path.GetExtension(fileName), ".psd1", StringComparison.OrdinalIgnoreCase))
{
var ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);
IEnumerable<PSObject> result = null;
try
{
ps.AddCommand("Test-ModuleManifest");
ps.AddParameter("Path", fileName);

// Suppress warnings emitted during the execution of Test-ModuleManifest
// ModuleManifest rule must catch any violations (warnings/errors) and generate DiagnosticRecord(s)
ps.AddParameter("WarningAction", ActionPreference.SilentlyContinue);
ps.AddParameter("WarningVariable", "Message");
ps.AddScript("$Message");
result = ps.Invoke();

}
catch
{}

if (result != null)
{
foreach (var warning in result)
{
if (warning.BaseObject != null)
{
yield return
new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, warning.BaseObject.ToString()), ast.Extent,
GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}

}

}

/// <summary>
/// GetName: Retrieves the name of this rule.
/// </summary>
/// <returns>The name of this rule</returns>
public string GetName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingDeprecatedManifestFieldsName);
}

/// <summary>
/// GetCommonName: Retrieves the common name of this rule.
/// </summary>
/// <returns>The common name of this rule</returns>
public string GetCommonName()
{
return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsCommonName);
}

/// <summary>
/// GetDescription: Retrieves the description of this rule.
/// </summary>
/// <returns>The description of this rule</returns>
public string GetDescription()
{
return String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDeprecatedManifestFieldsDescription);
}

/// <summary>
/// Method: Retrieves the type of the rule: builtin, managed or module.
/// </summary>
public SourceType GetSourceType()
{
return SourceType.Builtin;
}

/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}

/// <summary>
/// Method: Retrieves the module/assembly name the rule is from.
/// </summary>
public string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}
}
}
1 change: 1 addition & 0 deletions Rules/ScriptAnalyzerBuiltinRules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="AvoidReservedParams.cs" />
<Compile Include="AvoidShouldContinueWithoutForce.cs" />
<Compile Include="AvoidTrapStatement.cs" />
<Compile Include="AvoidUsingDeprecatedManifestFields.cs" />
<Compile Include="ProvideDefaultParameterValue.cs" />
<Compile Include="AvoidUninitializedVariable.cs" />
<Compile Include="AvoidUsernameAndPasswordParams.cs" />
Expand Down
33 changes: 30 additions & 3 deletions Rules/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Rules/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -726,4 +726,13 @@
<data name="ProvideDefaultParameterValueName" xml:space="preserve">
<value>ProvideDefaultParameterValue</value>
</data>
<data name="AvoidUsingDeprecatedManifestFieldsCommonName" xml:space="preserve">
<value>Avoid Using Deprecated Manifest Fields</value>
</data>
<data name="AvoidUsingDeprecatedManifestFieldsDescription" xml:space="preserve">
<value>"ModuleToProcess" is obsolete in the latest PowerShell version. Please update with the latest field "RootModule" in manifest files to avoid PowerShell version inconsistency.</value>
</data>
<data name="AvoidUsingDeprecatedManifestFieldsName" xml:space="preserve">
<value>AvoidUsingDeprecatedManifestFields</value>
</data>
</root>
19 changes: 19 additions & 0 deletions Tests/Rules/AvoidUsingDeprecatedManifestFields.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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}

Describe "AvoidUsingDeprecatedManifestFields" {
Context "When there are violations" {
It "has 1 violations" {
$violations.Count | Should Be 1
}
}

Context "When there are no violations" {
It "returns no violations" {
$noViolations.Count | Should Be 0
}
}
}
Binary file modified Tests/Rules/TestBadModule/TestBadModule.psd1
Binary file not shown.
120 changes: 120 additions & 0 deletions Tests/Rules/TestBadModule/TestDeprecatedManifestFields.psd1
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 = ''

# 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