Skip to content

Commit

Permalink
Merge branch 'it-praktyk-V4_PSScriptAnalyzer_Clean_Code_2'
Browse files Browse the repository at this point in the history
  • Loading branch information
dlwyatt committed Jul 31, 2017
2 parents 2541ef9 + 45187b5 commit c3d84de
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 39 deletions.
6 changes: 3 additions & 3 deletions Functions/Assertions/Should.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Functions/GherkinHook.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down
14 changes: 7 additions & 7 deletions Functions/Mock.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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][$_]
}
Expand All @@ -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[$_]
}
}
Expand All @@ -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[$_]
}
}
Expand All @@ -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[$_]
}
}
Expand All @@ -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[$_]
}
}
Expand Down
18 changes: 9 additions & 9 deletions Functions/New-Fixture.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Functions/Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand Down
10 changes: 5 additions & 5 deletions Functions/TestResults.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Functions/TestsRunningInCleanRunspace.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions Pester.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions Pester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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.")) } |
Expand Down
4 changes: 2 additions & 2 deletions build.psake.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down

0 comments on commit c3d84de

Please sign in to comment.