Skip to content

Commit

Permalink
Fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Apr 6, 2024
1 parent 911a465 commit 0bf4752
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 41 deletions.
31 changes: 19 additions & 12 deletions src/Format2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
if ($Pretty) {
$separator = ",`n"
}
($Value | ForEach-Object { Format-Nicely2 -Value $_ -Pretty:$Pretty }) -join $separator

$o = foreach ($v in $Value) {
Format-Nicely2 -Value $v -Pretty:$Pretty
}

$o -join $separator
}

function Format-Object2 ($Value, $Property, [switch]$Pretty) {
if ($null -eq $Property) {
$Property = $Value.PSObject.Properties | Select-Object -ExpandProperty Name
$Property = foreach ($p in $Value.PSObject.Properties) { $p.Name }
}
$orderedProperty = $Property |
Sort-Object |
$orderedProperty = foreach ($p in $Property | & $SafeCommands['Sort-Object']) {
# force the values to be strings for powershell v2
ForEach-Object { "$_" }
"$p"
}

$valueType = Get-ShortType $Value
$valueFormatted = [string]([PSObject]$Value | Select-Object -Property $orderedProperty)
$valueFormatted = [string]([PSObject]$Value | & $SafeCommands['Select-Object'] -Property $orderedProperty)

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

if ($Pretty) {
$margin = " "
Expand Down Expand Up @@ -50,9 +55,10 @@ function Format-Hashtable2 ($Value) {
$head = '@{'
$tail = '}'

$entries = $Value.Keys | Sort-Object | ForEach-Object {
$formattedValue = Format-Nicely2 $Value.$_
"$_=$formattedValue" }
$entries = foreach ($v in $Value.Keys | & $SafeCommands['Sort-Object']) {
$formattedValue = Format-Nicely2 $Value.$v
"$v=$formattedValue"
}

$head + ( $entries -join '; ') + $tail
}
Expand All @@ -61,9 +67,10 @@ function Format-Dictionary2 ($Value) {
$head = 'Dictionary{'
$tail = '}'

$entries = $Value.Keys | Sort-Object | ForEach-Object {
$formattedValue = Format-Nicely2 $Value.$_
"$_=$formattedValue" }
$entries = foreach ($v in $Value.Keys | & $SafeCommands['Sort-Object'] ) {
$formattedValue = Format-Nicely2 $Value.$v
"$v=$formattedValue"
}

$head + ( $entries -join '; ') + $tail
}
Expand Down
15 changes: 8 additions & 7 deletions src/functions/assert/Collection/Assert-All.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
# we are jumping between modules so I need to explicitly pass the _ variable
# simply using '&' won't work
# see: https://blogs.msdn.microsoft.com/sergey_babkins_blog/2014/10/30/calling-the-script-blocks-in-powershell/
$actualFiltered = $Actual | ForEach-Object {
#
# Do NOT replace this Foreach-Object with foreach keyword, you will break the $_ variable.
$actualFiltered = $Actual | & $SafeCommands['ForEach-Object'] {

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.
# powershell v4 code where we have InvokeWithContext available
# $underscore = Get-Variable _
# $pass = $FilterScript.InvokeWithContext($null, $underscore, $null)

# polyfill for PowerShell v2
$PSCmdlet.SessionState.PSVariable.Set("_", $_)
$pass = & $FilterScript
$underscore = Get-Variable _

Check warning

Code scanning / PSScriptAnalyzer

Unsafe call to 'Get-Variable' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1. Warning

Unsafe call to 'Get-Variable' found. Pester module defines a $SafeCommands dictionary for external commands to avoid hijacking. To fix a violation of this rule, update the call to use SafeCommands-variant, ex. & $SafeCommands['CommandName'] -Param1 Value1.
$pass = $FilterScript.InvokeWithContext($null, $underscore, $null)

# # polyfill for PowerShell v2
# $PSCmdlet.SessionState.PSVariable.Set("_", $_)
# $pass = & $FilterScript

if (-not $pass) { $_ }
}
Expand Down
7 changes: 3 additions & 4 deletions src/functions/assert/Collection/Assert-Any.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
function Assert-Any {
param (
[Parameter(ValueFromPipeline=$true, Position=1)]
[Parameter(ValueFromPipeline = $true, Position = 1)]
$Actual,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
[Parameter(Position=0, Mandatory=$true)]
[Parameter(Position = 0, Mandatory = $true)]
[scriptblock]$FilterScript,
[String]$CustomMessage
)

$Expected = $FilterScript
$Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input
if (-not ($Actual | Where-Object -FilterScript $FilterScript))
{
if (-not ($Actual | & $SafeCommands['Where-Object'] -FilterScript $FilterScript)) {

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.
$Message = Get-AssertionMessage -Expected $Expected -Actual $Actual -CustomMessage $CustomMessage -DefaultMessage "Expected at least one item in collection '<actual>' to pass filter '<expected>', but none of the items passed the filter."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
}
Expand Down
17 changes: 6 additions & 11 deletions src/functions/assert/Common/Get-AssertionMessage.ps1
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
function Get-AssertionMessage ($Expected, $Actual, $Option, [hashtable]$Data = @{}, $CustomMessage, $DefaultMessage, [switch]$Pretty)
{
if (-not $CustomMessage)
{
function Get-AssertionMessage ($Expected, $Actual, $Option, [hashtable]$Data = @{}, $CustomMessage, $DefaultMessage, [switch]$Pretty) {
if (-not $CustomMessage) {
$CustomMessage = $DefaultMessage
}

$expectedFormatted = Format-Nicely2 -Value $Expected -Pretty:$Pretty
$actualFormatted = Format-Nicely2 -Value $Actual -Pretty:$Pretty

$optionMessage = $null;
if ($null -ne $Option -and $option.Length -gt 0)
{
if ($null -ne $Option -and $option.Length -gt 0) {
if (-not $Pretty) {
$optionMessage = "Used options: $($Option -join ", ")."
}
else {
if ($Pretty) {
$optionMessage = "Used options:$($Option | ForEach-Object { "`n$_" })."
$optionMessage = "Used options:$(foreach ($o in $Option) { "`n$o" })."
}
}
}
Expand All @@ -28,16 +25,14 @@
$CustomMessage = $CustomMessage.Replace('<actualType>', (Get-ShortType2 -Value $Actual))
$CustomMessage = $CustomMessage.Replace('<options>', $optionMessage)

foreach ($pair in $Data.GetEnumerator())
{
foreach ($pair in $Data.GetEnumerator()) {
$CustomMessage = $CustomMessage.Replace("<$($pair.Key)>", (Format-Nicely2 -Value $pair.Value))
}

if (-not $Pretty) {
$CustomMessage
}
else
{
else {
$CustomMessage + "`n`n"
}
}
2 changes: 1 addition & 1 deletion src/functions/assert/Compatibility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function Invoke-WithContext {
# making it invoke in the same scope as $ScriptBlock
$scriptBlockWithContext.GetType().GetProperty('SessionStateInternal', $flags).SetValue($scriptBlockWithContext, $SessionStateInternal, $null)

& $scriptBlockWithContext @{ ScriptBlock = $ScriptBlock; Variables = $Variables }
& $scriptBlockWithContext @{ ScriptBlock = $ScriptBlock; Variables = $Variables }
}

function Test-NullOrWhiteSpace ($Value) {
Expand Down
8 changes: 3 additions & 5 deletions src/functions/assert/Exception/Assert-Throw.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ function Assert-Throw {
if ($AllowNonTerminatingError) {
$p = 'continue'
}
# compatibility fix for powershell v2
# $eap = New-Object -TypeName psvariable "erroractionpreference", $p
# $null = $ScriptBlock.InvokeWithContext($null, $eap, $null) 2>&1

$null = (Invoke-WithContext -ScriptBlock $ScriptBlock -Variables @{ ErrorActionPreference = $p }) 2>&1
$eap = [PSVariable]::new("erroractionpreference", $p)
$null = $ScriptBlock.InvokeWithContext($null, $eap, $null) 2>&1
}
catch {
$errorThrown = $true
Expand Down Expand Up @@ -86,7 +84,7 @@ function Get-Error ($ErrorRecord) {
else {
$e = $ErrorRecord
}
New-Object -TypeName PSObject -Property @{
[PSCustomObject] @{
ErrorRecord = $e
ExceptionMessage = $e.Exception.Message
Exception = $e.Exception
Expand Down
4 changes: 3 additions & 1 deletion tst/functions/assert/Exception/Assert-Throw.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ InPesterModuleScope {
$sb = {
Get-Item "/non-existing"
}
Invoke-WithContext $sb -Variables @{ ErrorActionPreference = "Stop" }

$eap = [PSVariable]::new("erroractionpreference", 'Stop')
$null = $sb.InvokeWithContext($null, $eap, $null) 2>&1
}
catch {
$e = $_
Expand Down

0 comments on commit 0bf4752

Please sign in to comment.