diff --git a/Functions/Assertions/Should.Tests.ps1 b/Functions/Assertions/Should.Tests.ps1 index 89b36bf84..e72fd0235 100644 --- a/Functions/Assertions/Should.Tests.ps1 +++ b/Functions/Assertions/Should.Tests.ps1 @@ -109,7 +109,7 @@ InModuleScope Pester { It "ensures all assertion functions provide failure messages" { $assertionFunctions = @("PesterBe", "PesterThrow", "PesterBeNullOrEmpty", "PesterExist", "PesterMatch", "PesterContain") - $assertionFunctions | % { + $assertionFunctions | ForEach { "function:$($_)FailureMessage" | Should Exist "function:Not$($_)FailureMessage" | Should Exist "function:$($_)FailureMessage" | Should -Exist @@ -138,8 +138,8 @@ InModuleScope Pester { <# It 'All failure message functions are present' { $assertionFunctions = Get-Command -CommandType Function -Module Pester | - Select-Object -ExpandProperty Name | - Where-Object { $_ -like 'Pester*' -and $_ -notlike '*FailureMessage' } + Select -ExpandProperty Name | + Where { $_ -like 'Pester*' -and $_ -notlike '*FailureMessage' } $missingFunctions = @( foreach ($assertionFunction in $assertionFunctions) diff --git a/Functions/GherkinHook.ps1 b/Functions/GherkinHook.ps1 index d81915032..ff31ab779 100644 --- a/Functions/GherkinHook.ps1 +++ b/Functions/GherkinHook.ps1 @@ -16,7 +16,7 @@ function BeforeEachFeature { param( # Optional tags. If set, this hook only runs for features with matching tags [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")] - [String[]]$Tags = @(), + [String[]]$Tags, # The ScriptBlock to run for the hook [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")] @@ -45,7 +45,7 @@ function AfterEachFeature { param( # Optional tags. If set, this hook only runs for features with matching tags [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")] - [String[]]$Tags = @(), + [String[]]$Tags, # The ScriptBlock to run for the hook [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")] @@ -76,7 +76,7 @@ function BeforeEachScenario { param( # Optional tags. If set, this hook only runs for features with matching tags [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")] - [String[]]$Tags = @(), + [String[]]$Tags, # The ScriptBlock to run for the hook [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")] @@ -105,7 +105,7 @@ function AfterEachScenario { param( # Optional tags. If set, this hook only runs for features with matching tags [Parameter(Mandatory=$True, Position=0, ParameterSetName="Tags")] - [String[]]$Tags = @(), + [String[]]$Tags, # The ScriptBlock to run for the hook [Parameter(Mandatory=$True, Position=1, ParameterSetName="Tags")] diff --git a/Functions/Mock.Tests.ps1 b/Functions/Mock.Tests.ps1 index d4474e4b2..4ddfec682 100644 --- a/Functions/Mock.Tests.ps1 +++ b/Functions/Mock.Tests.ps1 @@ -257,7 +257,7 @@ Describe "When calling Mock on existing cmdlet to handle pipelined input" { } $result = '' - "a", "b" | Get-ChildItem | % { $result += $_ } + "a", "b" | Get-ChildItem | ForEach { $result += $_ } It "Should process the pipeline in the mocked script" { $result | Should Be "AABB" @@ -1302,7 +1302,7 @@ Describe 'Parameter Filters and Common Parameters' { Describe "Mocking Get-ItemProperty" { Mock Get-ItemProperty { New-Object -typename psobject -property @{ Name = "fakeName" } } It "Does not fail with NotImplementedException" { - Get-ItemProperty -Path "HKLM:\Software\Key\" -Name "Property" | Select -ExpandProperty Name | Should Be fakeName + Get-ItemProperty -Path "HKLM:\Software\Key\" -Name "Property" | Select-Object -ExpandProperty Name | Should Be fakeName } } @@ -1397,7 +1397,7 @@ Describe 'Mocking a function taking input from pipeline' { context 'when calling original function with an array' { $result = @(1,2) | PipelineInputFunction it 'Returns actual implementation' { - $result[0].keys | % { + $result[0].keys | ForEach { $result[0][$_] | Should Be $noMockArrayResult[0][$_] $result[1][$_] | Should Be $noMockArrayResult[1][$_] } @@ -1407,7 +1407,7 @@ Describe 'Mocking a function taking input from pipeline' { context 'when calling original function with an int' { $result = 1 | PipelineInputFunction it 'Returns actual implementation' { - $result.keys | % { + $result.keys | ForEach { $result[$_] | Should Be $noMockIntResult[$_] } } @@ -1416,7 +1416,7 @@ Describe 'Mocking a function taking input from pipeline' { context 'when calling original function with a string' { $result = '1' | PipelineInputFunction it 'Returns actual implementation' { - $result.keys | % { + $result.keys | ForEach { $result[$_] | Should Be $noMockStringResult[$_] } } @@ -1425,7 +1425,7 @@ Describe 'Mocking a function taking input from pipeline' { context 'when calling original function and pipeline is bound by property name' { $result = $psobj | PipelineInputFunction -PipeStr 'val' it 'Returns actual implementation' { - $result.keys | % { + $result.keys | ForEach { $result[$_] | Should Be $noMockResultByProperty[$_] } } @@ -1448,7 +1448,7 @@ Describe 'Mocking a function taking input from pipeline' { context 'when calling original function and pipeline is bound by property name with array values' { $result = $psArrayobj | PipelineInputFunction -PipeStr 'val' it 'Returns actual implementation' { - $result.keys | % { + $result.keys | ForEach { $result[$_] | Should Be $noMockArrayResultByProperty[$_] } } diff --git a/Functions/New-Fixture.Tests.ps1 b/Functions/New-Fixture.Tests.ps1 index bd3978361..63c6acde6 100644 --- a/Functions/New-Fixture.Tests.ps1 +++ b/Functions/New-Fixture.Tests.ps1 @@ -10,9 +10,9 @@ Describe "New-Fixture" { $name = "Test-Fixture" $path = "TestDrive:\" - pushd $path + Push-Location -Path $path New-Fixture -Name $name | Out-Null - popd + Pop-Location Join-Path -Path $path -ChildPath "$name.ps1" | Should Exist Join-Path -Path $path -ChildPath "$name.Tests.ps1" | Should Exist @@ -40,9 +40,9 @@ Describe "New-Fixture" { $name = "Relative1-Fixture" $path = "TestDrive:\" - pushd $path + Push-Location -Path $path New-Fixture -Name $name -Path relative | Out-Null - popd + Pop-Location Join-Path -Path "$path\relative" -ChildPath "$name.ps1" | Should Exist Join-Path -Path "$path\relative" -ChildPath "$name.Tests.ps1" | Should Exist @@ -51,9 +51,9 @@ Describe "New-Fixture" { $name = "Relative2-Fixture" $path = "TestDrive:\" - pushd $path + Push-Location -Path $path New-Fixture -Name $name -Path . | Out-Null - popd + Pop-Location Join-Path -Path "$path" -ChildPath "$name.ps1" | Should Exist Join-Path -Path "$path" -ChildPath "$name.Tests.ps1" | Should Exist @@ -62,9 +62,9 @@ Describe "New-Fixture" { $name = "Relative3-Fixture" $path = "TestDrive:\" - pushd $path - New-Fixture -Name $name -Path (pwd) | Out-Null - popd + Push-Location -Path $path + New-Fixture -Name $name -Path (Get-Location) | Out-Null + Pop-Location Join-Path -Path "$path" -ChildPath "$name.ps1" | Should Exist Join-Path -Path "$path" -ChildPath "$name.Tests.ps1" | Should Exist diff --git a/Functions/Output.ps1 b/Functions/Output.ps1 index e0a3e4d6c..435adea0a 100644 --- a/Functions/Output.ps1 +++ b/Functions/Output.ps1 @@ -99,7 +99,7 @@ function Write-Describe { & $SafeCommands['Write-Host'] "${margin}${Text}" -ForegroundColor $ReportTheme.Describe # If the feature has a longer description, write that too if($Describe.PSObject.Properties['Description'] -and $Describe.Description) { - $Describe.Description -split '\n' | % { + $Describe.Description -split '\n' | ForEach { & $SafeCommands['Write-Host'] ($ReportStrings.Margin * ($pester.IndentLevel + 1)) $_ -ForegroundColor $ReportTheme.DescribeDetail } } @@ -123,7 +123,7 @@ function Write-Context { & $SafeCommands['Write-Host'] ($ReportStrings.Margin + $Text) -ForegroundColor $ReportTheme.Context # If the scenario has a longer description, write that too if($Context.PSObject.Properties['Description'] -and $Context.Description) { - $Context.Description -split '\n' | % { + $Context.Description -split '\n' | ForEach { & $SafeCommands['Write-Host'] (" " * $ReportStrings.Context.Length) $_ -ForegroundColor $ReportTheme.ContextDetail } } diff --git a/Functions/TestResults.Tests.ps1 b/Functions/TestResults.Tests.ps1 index 9086741da..91b63e88b 100644 --- a/Functions/TestResults.Tests.ps1 +++ b/Functions/TestResults.Tests.ps1 @@ -271,22 +271,22 @@ InModuleScope Pester { Describe "GetFullPath" { It "Resolves non existing path correctly" { - pushd TestDrive:\ + Push-Location -Path TestDrive:\ $p = GetFullPath notexistingfile.txt - popd + Pop-Location $p | Should Be (Join-Path $TestDrive notexistingfile.txt) } It "Resolves existing path correctly" { - pushd TestDrive:\ + Push-Location -Path TestDrive:\ New-Item -ItemType File -Name existingfile.txt $p = GetFullPath existingfile.txt - popd + Pop-Location $p | Should Be (Join-Path $TestDrive existingfile.txt) } It "Resolves full path correctly" { - $powershellPath = Get-Command 'powershell' | select -ExpandProperty 'Definition' + $powershellPath = Get-Command 'powershell' | Select -ExpandProperty 'Definition' $powershellPath | Should -Not -BeNullOrEmpty GetFullPath $powershellPath | Should Be $powershellPath diff --git a/Functions/TestsRunningInCleanRunspace.Tests.ps1 b/Functions/TestsRunningInCleanRunspace.Tests.ps1 index db7299f5b..4db531886 100644 --- a/Functions/TestsRunningInCleanRunspace.Tests.ps1 +++ b/Functions/TestsRunningInCleanRunspace.Tests.ps1 @@ -27,7 +27,7 @@ #TODO: how should this handle errors? #$job.Error | foreach { throw $_.Exception } $job.Output - $job.ChildJobs| foreach { + $job.ChildJobs | ForEach { $childJob = $_ #$childJob.Error | foreach { throw $_.Exception } $childJob.Output diff --git a/Pester.Tests.ps1 b/Pester.Tests.ps1 index 8f2de962d..330003d35 100644 --- a/Pester.Tests.ps1 +++ b/Pester.Tests.ps1 @@ -88,7 +88,7 @@ if ($PSVersionTable.PSVersion.Major -ge 3) $path = $ExecutionContext.SessionState.Module.ModuleBase $filesToCheck = Get-ChildItem -Path $path -Recurse -Include *.ps1,*.psm1 -Exclude *.Tests.ps1 $callsToSafeCommands = @( - foreach ($file in $files) + foreach ($file in $filesToCheck) { $tokens = $parseErrors = $null $ast = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref] $tokens, [ref] $parseErrors) @@ -106,7 +106,11 @@ if ($PSVersionTable.PSVersion.Major -ge 3) $uniqueSafeCommands = $callsToSafeCommands | ForEach-Object { $_.CommandElements[0].Index.Value } | Select-Object -Unique - $missingSafeCommands = $uniqueSafeCommands | Where-Object { -not $script:SafeCommands.ContainsKey($_) } + $missingSafeCommands = $uniqueSafeCommands | Where { -not $script:SafeCommands.ContainsKey($_) } + + # These commands are conditionally added to the safeCommands table due to Nano / Core versus PSv2 compatibility; one will always + # be missing, and can be ignored. + $missingSafeCommands = $missingSafeCommands | Where { @('Get-WmiObject', 'Get-CimInstance') -notcontains $_ } It 'The SafeCommands table contains all commands that are called from the module' { $missingSafeCommands | Should Be $null diff --git a/Pester.psm1 b/Pester.psm1 index 63b190db2..f3f482ec6 100644 --- a/Pester.psm1 +++ b/Pester.psm1 @@ -218,7 +218,7 @@ function Add-AssertionOperator $script:AssertionOperators[$Name] = $entry - foreach ($string in $Alias | where { -not (Test-NullOrWhiteSpace $_)}) + foreach ($string in $Alias | Where { -not (Test-NullOrWhiteSpace $_)}) { Assert-ValidAssertionAlias -Alias $string $script:AssertionAliases[$string] = $Name @@ -233,7 +233,7 @@ function Assert-AssertionOperatorNameIsUnique [string[]] $Name ) - foreach ($string in $name | where { -not (Test-NullOrWhiteSpace $_)}) + foreach ($string in $name | Where { -not (Test-NullOrWhiteSpace $_)}) { Assert-ValidAssertionName -Name $string @@ -342,8 +342,6 @@ function Get-AssertionDynamicParams } $Script:PesterRoot = & $SafeCommands['Split-Path'] -Path $MyInvocation.MyCommand.Path -$moduleRoot = & $script:SafeCommands['Split-Path'] -Path $MyInvocation.MyCommand.Path - "$PesterRoot\Functions\*.ps1", "$PesterRoot\Functions\Assertions\*.ps1" | & $script:SafeCommands['Resolve-Path'] | & $script:SafeCommands['Where-Object'] { -not ($_.ProviderPath.ToLower().Contains(".tests.")) } | diff --git a/build.psake.ps1 b/build.psake.ps1 index d6fae2a43..3f99faf49 100644 --- a/build.psake.ps1 +++ b/build.psake.ps1 @@ -23,8 +23,8 @@ Task Version-Module{ $v = git.exe describe --abbrev=0 --tags $changeset=(git.exe log -1 $($v + '..') --pretty=format:%H) (Get-Content "$baseDir\Pester.psm1") ` - | % {$_ -replace "\`$version\`$", "$version" } ` - | % {$_ -replace "\`$sha\`$", "$changeset" } ` + | Where {$_ -replace "\`$version\`$", "$version" } ` + | Where {$_ -replace "\`$sha\`$", "$changeset" } ` | Set-Content "$baseDir\Pester.psm1" }