forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup alias and missing SafeCommands (pester#1745)
* Added UnsafeCommands custom analyzer rule * Updated and renamed custom rules module * Updated PSScriptAnalyzer settings * Removed alias-usage and replaced unsafe commands * Removed temp debug code * Remove BOM * SafeCommands cleanup in /src/functions * Warn or suppress SafeCommand-rule * Apply suggestions from code review Co-authored-by: Jakub Jareš <[email protected]> * Fixed sort alias usage * typos * Safecommands in mock-wrapper * Revert BOM-removal in Mock.ps1 Co-authored-by: Jakub Jareš <[email protected]>
- Loading branch information
Showing
23 changed files
with
289 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@{ | ||
Severity = @('Error','Warning') | ||
IncludeDefaultRules = $true | ||
CustomRulePath = './Pester.BuildAnalyzerRules' | ||
ExcludeRules=@( | ||
'PSUseShouldProcessForStateChangingFunctions' | ||
'PSUseApprovedVerbs' | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
@{ | ||
|
||
# Script module or binary module file associated with this manifest. | ||
RootModule = 'Pester.BuildAnalyzerRules.psm1' | ||
|
||
# Version number of this module. | ||
ModuleVersion = '0.0.1' | ||
|
||
# ID used to uniquely identify this module | ||
GUID = '7e04f341-3ce0-4f3c-9177-1a7de9daaddf' | ||
|
||
# Author of this module | ||
Author = 'Pester Team' | ||
|
||
# Company or vendor of this module | ||
CompanyName = 'Pester' | ||
|
||
# Copyright statement for this module | ||
Copyright = 'Copyright (c) 2020 by Pester Team, licensed under Apache 2.0 License.' | ||
|
||
# Description of the functionality provided by this module | ||
Description = 'This module contains custom script analyzer rules used for validation during build of the Pester module.' | ||
|
||
# Minimum version of the PowerShell engine required by this module | ||
PowerShellVersion = '3.0' | ||
|
||
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. | ||
FunctionsToExport = 'Measure-*' | ||
|
||
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. | ||
CmdletsToExport = @() | ||
|
||
# Variables to export from this module | ||
VariablesToExport = @() | ||
|
||
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. | ||
AliasesToExport = @() | ||
|
||
# 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 = '' | ||
|
||
# Prerelease string of this module | ||
# Prerelease = '' | ||
|
||
# Flag to indicate whether the module requires explicit user acceptance for install/update/save | ||
# RequireLicenseAcceptance = $false | ||
|
||
# External dependent modules of this module | ||
# ExternalModuleDependencies = @() | ||
|
||
} # 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 = '' | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Get list of SafeCommands | ||
$SafeCommands = & { . "$PSScriptRoot/../src/functions/Pester.SafeCommands.ps1"; $Script:SafeCommands } | ||
# Workaround as RuleSuppressionID-based suppression is bugged. returns error. | ||
# Should be replaced with the following line when PSScriptAnalyzer is fixed. See Invoke-Pester | ||
# [Diagnostics.CodeAnalysis.SuppressMessageAttribute('Pester.BuildAnalyzerRules\Measure-SafeComands', 'Remove-Variable')] | ||
$IgnoreUnsafeCommands = @('Remove-Variable') | ||
function Measure-SafeComands { | ||
<# | ||
.SYNOPSIS | ||
Should use $SafeCommand-variant of external function when available. | ||
.DESCRIPTION | ||
Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCoomands variant, ex. `& $SafeCommands['CommandName'] -Param1 Value1`. | ||
.EXAMPLE | ||
Measure-SafeComands -CommandAst $CommandAst | ||
.INPUTS | ||
[System.Management.Automation.Language.CommandAst] | ||
.OUTPUTS | ||
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]] | ||
.NOTES | ||
None | ||
#> | ||
[CmdletBinding()] | ||
[OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] | ||
Param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.Management.Automation.Language.CommandAst] | ||
$CommandAst | ||
) | ||
|
||
Process { | ||
$results = @() | ||
try { | ||
$commandName = $CommandAst.GetCommandName() | ||
|
||
# If command exists in $SafeCommands, write error | ||
if ($null -ne $commandName -and $commandName -in $SafeCommands.Keys -and $commandName -notin $IgnoreUnsafeCommands) { | ||
foreach ($cmd in $CommandAst.CommandElements) { | ||
# Find extent for command name only | ||
if(($cmd -is [System.Management.Automation.Language.StringConstantExpressionAst]) -and $cmd.Value -eq $commandName) { | ||
|
||
#Define fix-action | ||
[int]$startLineNumber = $cmd.Extent.StartLineNumber | ||
[int]$endLineNumber = $cmd.Extent.EndLineNumber | ||
[int]$startColumnNumber = $cmd.Extent.StartColumnNumber | ||
[int]$endColumnNumber = $cmd.Extent.EndColumnNumber | ||
[string]$correction = "& `$SafeCommands['$commandName']" | ||
[string]$file = $MyInvocation.MyCommand.Definition | ||
[string]$description = 'Replacing with SafeCommands-type' | ||
$correctionExtent = New-Object 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent' $startLineNumber, $endLineNumber, $startColumnNumber, $endColumnNumber, $correction, $file, $description | ||
$suggestedCorrections = New-Object System.Collections.ObjectModel.Collection['Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent'] | ||
$suggestedCorrections.add($correctionExtent) > $null | ||
|
||
# Output error | ||
$result = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ | ||
'Message' = "Unsafe call to '$commandName' found. $((Get-Help $MyInvocation.MyCommand.Name).Description.Text)" | ||
'Extent' = $cmd.Extent | ||
'RuleName' = $PSCmdlet.MyInvocation.InvocationName | ||
'Severity' = 'Warning' | ||
'RuleSuppressionID' = $commandName | ||
"SuggestedCorrections" = $suggestedCorrections | ||
} | ||
$results += $result | ||
} | ||
} | ||
} | ||
return $results | ||
} | ||
catch { | ||
$PSCmdlet.ThrowTerminatingError($PSItem) | ||
} | ||
} | ||
} | ||
|
||
Export-ModuleMember -Function 'Measure-*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
if ($PSVersionTable.PSVersion.Major -ge 6) { | ||
Add-Type -Path "$PSScriptRoot/bin/netstandard2.0/Pester.dll" | ||
& $SafeCommands['Add-Type'] -Path "$PSScriptRoot/bin/netstandard2.0/Pester.dll" | ||
} | ||
else { | ||
Add-Type -Path "$PSScriptRoot/bin/net452/Pester.dll" | ||
& $SafeCommands['Add-Type'] -Path "$PSScriptRoot/bin/net452/Pester.dll" | ||
} |
Oops, something went wrong.