Skip to content

Commit

Permalink
Revert test, fix safe commands etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed May 20, 2024
1 parent 90a887d commit 29f5cb8
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 126 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ vendor/packages/
.vscode/
# But don't exclude settings.json
!.vscode/settings.json
!.vscode/launch.json

coverage.xml
testResults.xml
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ stages:
targetType: inline
pwsh: $(pwsh)
script: |
& ./test.ps1 -CI -NoBuild
& ./test.ps1 -CI -PassThru -NoBuild
workingDirectory: '$(Build.SourcesDirectory)'
- task: PublishCodeCoverageResults@2
inputs:
Expand Down
3 changes: 2 additions & 1 deletion src/functions/assert/Common/Collect-Input.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
# It is always $null or object[] containing all the received items.
$PipelineInput,
# This tell us if we were called by | syntax or not. Caller needs to pass in $MyInvocation.ExpectingInput.
$IsPipelineInput,
[Parameter(Mandatory)]
[bool] $IsPipelineInput,
# This unwraps input provided by |. The effect of this is that we get single item input directly,
# and not wrapped in array. E.g. 1 | Should-Be -> 1, and not 1 | Should-Be -> @(1).
#
Expand Down
36 changes: 18 additions & 18 deletions src/functions/assert/Equivalence/Should-BeEquivalent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function Compare-DataTableEquivalent ($Expected, $Actual, $Property, $Options) {
}
$Expected = Format-Nicely2 -Value $Expected
$Actual = Format-Nicely2 -Value $Actual
$notFoundFormatted = Format-Nicely2 -Value ( $notFound | ForEach-Object { Format-Nicely2 -Value $_ } )
$notFoundFormatted = Format-Nicely2 -Value ( $notFound | & $SafeCommands['ForEach-Object'] { Format-Nicely2 -Value $_ } )

if ($notFound) {
$propertyMessage = if ($Property) { " in property $Property which is" }
Expand Down Expand Up @@ -290,24 +290,24 @@ function Compare-HashtableEquivalent ($Actual, $Expected, $Property, $Options) {

if (!$Options.ExcludePathsNotOnExpected) {
# fix for powershell 2 where the array needs to be explicit
$keysNotInExpected = @( $actualKeys | Where-Object { $expectedKeys -notcontains $_ })
$keysNotInExpected = @( $actualKeys | & $SafeCommands['Where-Object'] { $expectedKeys -notcontains $_ })

$filteredKeysNotInExpected = @( $keysNotInExpected | Test-IncludedPath -PathSelector Hashtable -Path $Property -Options $Options)

# fix for powershell v2 where foreach goes once over null
if ($filteredKeysNotInExpected | Where-Object { $_ }) {
if ($filteredKeysNotInExpected | & $SafeCommands['Where-Object'] { $_ }) {
v -Difference "`$Actual has $($filteredKeysNotInExpected.Count) keys that were not found on `$Expected: $(Format-Nicely2 @($filteredKeysNotInExpected))."
}
else {
v "`$Actual has no keys that we did not find on `$Expected."
}

foreach ($k in $filteredKeysNotInExpected | Where-Object { $_ }) {
foreach ($k in $filteredKeysNotInExpected | & $SafeCommands['Where-Object'] { $_ }) {
$result += "Expected is missing key '$k' that the other object has."
}
}

if ($result | Where-Object { $_ }) {
if ($result | & $SafeCommands['Where-Object'] { $_ }) {
v -Difference "Hashtables `$Actual and `$Expected are not equivalent."
$expectedFormatted = Format-Nicely2 -Value $Expected
$actualFormatted = Format-Nicely2 -Value $Actual
Expand Down Expand Up @@ -355,18 +355,18 @@ function Compare-DictionaryEquivalent ($Actual, $Expected, $Property, $Options)
}
if (!$Options.ExcludePathsNotOnExpected) {
# fix for powershell 2 where the array needs to be explicit
$keysNotInExpected = @( $actualKeys | Where-Object { $expectedKeys -notcontains $_ } )
$keysNotInExpected = @( $actualKeys | & $SafeCommands['Where-Object'] { $expectedKeys -notcontains $_ } )
$filteredKeysNotInExpected = @( $keysNotInExpected | Test-IncludedPath -PathSelector Hashtable -Path $Property -Options $Options )

# fix for powershell v2 where foreach goes once over null
if ($filteredKeysNotInExpected | Where-Object { $_ }) {
if ($filteredKeysNotInExpected | & $SafeCommands['Where-Object'] { $_ }) {
v -Difference "`$Actual has $($filteredKeysNotInExpected.Count) keys that were not found on `$Expected: $(Format-Nicely2 @($filteredKeysNotInExpected))."
}
else {
v "`$Actual has no keys that we did not find on `$Expected."
}

foreach ($k in $filteredKeysNotInExpected | Where-Object { $_ }) {
foreach ($k in $filteredKeysNotInExpected | & $SafeCommands['Where-Object'] { $_ }) {
$result += "Expected is missing key '$k' that the other object has."
}
}
Expand Down Expand Up @@ -403,7 +403,7 @@ function Compare-ObjectEquivalent ($Actual, $Expected, $Property, $Options) {
}

$propertyName = $p.Name
$actualProperty = $actualProperties | Where-Object { $_.Name -eq $propertyName }
$actualProperty = $actualProperties | & $SafeCommands['Where-Object'] { $_.Name -eq $propertyName }
if (-not $actualProperty) {
v -Difference "Property '$propertyName' was not found on `$Actual."
"Expected has property '$PropertyName' that the other object does not have."
Expand All @@ -424,7 +424,7 @@ function Compare-ObjectEquivalent ($Actual, $Expected, $Property, $Options) {
#check if there are any extra actual object props
$expectedPropertyNames = $expectedProperties | Select-Object -ExpandProperty Name

$propertiesNotInExpected = @( $actualProperties | Where-Object { $expectedPropertyNames -notcontains $_.name })
$propertiesNotInExpected = @( $actualProperties | & $SafeCommands['Where-Object'] { $expectedPropertyNames -notcontains $_.name })

# fix for powershell v2 we need to make the array explicit
$filteredPropertiesNotInExpected = $propertiesNotInExpected |
Expand All @@ -438,7 +438,7 @@ function Compare-ObjectEquivalent ($Actual, $Expected, $Property, $Options) {
}

# fix for powershell v2 where foreach goes once over null
foreach ($p in $filteredPropertiesNotInExpected | Where-Object { $_ }) {
foreach ($p in $filteredPropertiesNotInExpected | & $SafeCommands['Where-Object'] { $_ }) {
"Expected is missing property '$($p.Name)' that the other object has."
}
}
Expand All @@ -456,12 +456,12 @@ function Compare-DataRowEquivalent ($Actual, $Expected, $Property, $Options) {
return "Expected DataRow '$expectedFormatted', but got '$actualFormatted'."
}

$actualProperties = $Actual.PsObject.Properties | Where-Object { 'RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors' -notcontains $_.Name }
$expectedProperties = $Expected.PsObject.Properties | Where-Object { 'RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors' -notcontains $_.Name }
$actualProperties = $Actual.PsObject.Properties | & $SafeCommands['Where-Object'] { 'RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors' -notcontains $_.Name }
$expectedProperties = $Expected.PsObject.Properties | & $SafeCommands['Where-Object'] { 'RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors' -notcontains $_.Name }

foreach ($p in $expectedProperties) {
$propertyName = $p.Name
$actualProperty = $actualProperties | Where-Object { $_.Name -eq $propertyName }
$actualProperty = $actualProperties | & $SafeCommands['Where-Object'] { $_.Name -eq $propertyName }
if (-not $actualProperty) {
"Expected has property '$PropertyName' that the other object does not have."
continue
Expand All @@ -473,10 +473,10 @@ function Compare-DataRowEquivalent ($Actual, $Expected, $Property, $Options) {
#check if there are any extra actual object props
$expectedPropertyNames = $expectedProperties | Select-Object -ExpandProperty Name

$propertiesNotInExpected = @($actualProperties | Where-Object { $expectedPropertyNames -notcontains $_.name })
$propertiesNotInExpected = @($actualProperties | & $SafeCommands['Where-Object'] { $expectedPropertyNames -notcontains $_.name })

# fix for powershell v2 where foreach goes once over null
foreach ($p in $propertiesNotInExpected | Where-Object { $_ }) {
foreach ($p in $propertiesNotInExpected | & $SafeCommands['Where-Object'] { $_ }) {
"Expected is missing property '$($p.Name)' that the other object has."
}
}
Expand Down Expand Up @@ -750,7 +750,7 @@ function Test-IncludedPath {
}

function Format-EquivalencyOptions ($Options) {
$Options.ExcludedPaths | ForEach-Object { "Exclude path '$_'" }
$Options.ExcludedPaths | & $SafeCommands['ForEach-Object'] { "Exclude path '$_'" }
if ($Options.ExcludePathsNotOnExpected) { "Excluding all paths not found on Expected" }
}

Expand All @@ -761,7 +761,7 @@ function Like-Any {
[String] $Path
)
process {
foreach ($pathFilter in $PathFilters | Where-Object { $_ }) {
foreach ($pathFilter in $PathFilters | & $SafeCommands['Where-Object'] { $_ }) {
$r = $Path -like $pathFilter
if ($r) {
v -Skip "Path '$Path' matches filter '$pathFilter'."
Expand Down
6 changes: 3 additions & 3 deletions src/functions/assert/Exception/Should-Throw.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Assert-Throw {
.EXAMPLE
```powershell
$err = { throw 'error' } | Should-Throw -PassThru
$err = { throw 'error' } | Should-Throw
$err.Exception.Message | Should-BeLike '*err*'
```
Expand Down Expand Up @@ -75,7 +75,7 @@ function Assert-Throw {
}
catch {
$errorThrown = $true
$err = Get-Error $_
$err = Get-ErrorObject $_
}

$buts = @()
Expand Down Expand Up @@ -127,7 +127,7 @@ function Assert-Throw {
$err.ErrorRecord
}

function Get-Error ($ErrorRecord) {
function Get-ErrorObject ($ErrorRecord) {

if ($ErrorRecord.Exception -like '*"InvokeWithContext"*') {
$e = $ErrorRecord.Exception.InnerException.ErrorRecord
Expand Down
45 changes: 5 additions & 40 deletions test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@
so leaving it in code would run only one test from the file on the server.
.PARAMETER SkipPTests
Skips P tests. Skip the tests written using the P module, Unit
Skips Passthrough P tests. Skip the tests written using the P module, Unit
Tests for the Runtime, and Acceptance Tests for Pester
.PARAMETER SkipPesterTests
Skips Pester tests, but not P tests.
.PARAMETER NoBuild
Skips running build.ps1. Do not build the underlying csharp components.
Used in CI pipeline since a clean build has already been run prior to Test.
Expand All @@ -32,22 +29,16 @@
done, but makes local debugging difficult. When -CI is used, inlining is
forced.
.PARAMETER VSCode
Set when calling from VSCode laucher file so we automatically figure out
what to run or what to skip.
.NOTES
Tests are excluded with Tags VersionChecks, StyleRules, Help.
#>
[CmdletBinding()]
param (
# force P to fail when I leave `dt` in the tests
[switch] $CI,
[switch] $SkipPTests,
[switch] $SkipPesterTests,
[switch] $NoBuild,
[switch] $Inline,
[switch] $VSCode,
[string[]] $File = @()
[string[]] $File
)

Set-StrictMode -Version Latest
Expand All @@ -57,21 +48,6 @@ $ErrorView = "NormalView"
"Using PS: $($PsVersionTable.PSVersion)"
"In path: $($pwd.Path)"

if ($VSCode) {
# Detect which tests to skip from the filenames.
$anyFile = 0 -lt $File.Count
$anyPesterTests = [bool]@($File | Where-Object { $_ -like "*.Tests.ps1" })
$anyPTests = [bool]@($File | Where-Object { $_ -like "*.ts.ps1" })

if ($SkipPTests -or ($anyFile -and -not $anyPTests)) {
$SkipPTests = $true
}

if ($SkipPesterTests -or ($anyFile -and -not $anyPesterTests)) {
$SkipPesterTests = $true
}
}

if (-not $NoBuild) {
if ($CI) {
& "$PSScriptRoot/build.ps1" -Inline
Expand All @@ -81,10 +57,6 @@ if (-not $NoBuild) {
}
}

# if ($CI -and ($SkipPTests -or $SkipPesterTests)) {
# throw "Cannot skip tests in CI mode!"
# }

# remove pester because we will be reimporting it in multiple other places
Get-Module Pester | Remove-Module

Expand Down Expand Up @@ -146,7 +118,7 @@ New-Module -Name TestHelpers -ScriptBlock {
}

function New-Dictionary ([hashtable]$Hashtable) {
$d = new-object "Collections.Generic.Dictionary[string,object]"
$d = [System.Collections.Generic.Dictionary[string, object]]::new()
$Hashtable.GetEnumerator() | ForEach-Object { $d.Add($_.Key, $_.Value) }

$d
Expand All @@ -155,15 +127,8 @@ New-Module -Name TestHelpers -ScriptBlock {
function Clear-WhiteSpace ($Text) {
"$($Text -replace "(`t|`n|`r)"," " -replace "\s+"," ")".Trim()
}

function New-PSObject ([hashtable]$Property) {
New-Object -Type PSObject -Property $Property
}
} | Out-Null

if ($SkipPesterTests) {
return
}

$configuration = [PesterConfiguration]::Default

Expand All @@ -189,8 +154,8 @@ if ($CI) {
$configuration.Run.Exit = $true

# not using code coverage, it is still very slow
$configuration.CodeCoverage.Enabled = $true
$configuration.CodeCoverage.Path = "$PSScriptRoot/bin/*"
$configuration.CodeCoverage.Enabled = $false
$configuration.CodeCoverage.Path = "$PSScriptRoot/src/*"

# experimental, uses the Profiler based tracer to do code coverage without using breakpoints
$configuration.CodeCoverage.UseBreakpoints = $false
Expand Down
8 changes: 4 additions & 4 deletions tst/Format2.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ InPesterModuleScope {

Describe "Format-Object2" {
It "Formats object '<value>' to '<expected>'" -TestCases @(
@{ Value = (New-PSObject @{Name = 'Jakub'; Age = 28 }); Expected = "PSObject{Age=28; Name='Jakub'}" },
@{ Value = ([PSCustomObject]@{Name = 'Jakub'; Age = 28 }); Expected = "PSObject{Age=28; Name='Jakub'}" },
@{ Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28 }); Expected = "Assertions.TestType.Person{Age=28; Name='Jakub'}" }
) {
param ($Value, $Expected)
Format-Object2 -Value $Value | Verify-Equal $Expected
}

It "Formats object '<value>' with selected properties '<selectedProperties>' to '<expected>'" -TestCases @(
@{ Value = (New-PSObject @{Name = 'Jakub'; Age = 28 }); SelectedProperties = "Age"; Expected = "PSObject{Age=28}" },
@{ Value = ([PSCustomObject]@{Name = 'Jakub'; Age = 28 }); SelectedProperties = "Age"; Expected = "PSObject{Age=28}" },
@{
Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28 })
SelectedProperties = 'Name'
Expand Down Expand Up @@ -159,7 +159,7 @@ InPesterModuleScope {
@{ Value = (1, 2, 3); Expected = '@(1, 2, 3)' },
@{ Value = 1.1; Expected = '1.1' },
@{ Value = [int]; Expected = '[int]' }
@{ Value = New-PSObject @{ Name = "Jakub" }; Expected = "PSObject{Name='Jakub'}" },
@{ Value = [PSCustomObject]@{ Name = "Jakub" }; Expected = "PSObject{Name='Jakub'}" },
@{ Value = (New-Object -Type Assertions.TestType.Person -Property @{Name = 'Jakub'; Age = 28 }); Expected = "Assertions.TestType.Person{Age=28; Name='Jakub'}" }
@{ Value = @{Name = 'Jakub'; Age = 28 }; Expected = "@{Age=28; Name='Jakub'}" }
@{ Value = New-Dictionary @{Age = 28; Name = 'Jakub' }; Expected = "Dictionary{Age=28; Name='Jakub'}" }
Expand Down Expand Up @@ -199,7 +199,7 @@ InPesterModuleScope {
@{ Value = 1.1; Expected = '[double]' },
@{ Value = 'a' ; Expected = '[string]' },
@{ Value = $null ; Expected = '[null]' },
@{ Value = New-PSObject @{Name = 'Jakub' } ; Expected = '[PSObject]' },
@{ Value = [PSCustomObject]@{Name = 'Jakub' } ; Expected = '[PSObject]' },
@{ Value = [Object[]]1, 2, 3 ; Expected = '[collection]' }
) {
param($Value, $Expected)
Expand Down
4 changes: 2 additions & 2 deletions tst/functions/assert/Common/Get-AssertionMessage.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ InPesterModuleScope {
It "returns correct message when complex objects are provided" {
$expected = "We expected string to be PSObject{Age=28; Name='Jakub'}, but got 2."
$customMessage = "We expected string to be <expected>, but got <actual>."
Get-AssertionMessage -CustomMessage $customMessage -Expected (New-PSObject @{Name = 'Jakub'; Age = 28 }) -Actual 2 | Verify-Equal $expected
Get-AssertionMessage -CustomMessage $customMessage -Expected ([PSCustomObject]@{Name = 'Jakub'; Age = 28 }) -Actual 2 | Verify-Equal $expected
}

It "returns correct message when type tokens are provided" {
$expected = "We expected string to be [PSObject], but got [int]."
$customMessage = "We expected string to be <expectedType>, but got <actualType>."
Get-AssertionMessage -CustomMessage $customMessage -Expected (New-PSObject @{Name = 'Jakub'; Age = 28 }) -Actual 2 | Verify-Equal $expected
Get-AssertionMessage -CustomMessage $customMessage -Expected ([PSCustomObject]@{Name = 'Jakub'; Age = 28 }) -Actual 2 | Verify-Equal $expected
}

It "returns correct type message when `$null is provided" {
Expand Down
Loading

0 comments on commit 29f5cb8

Please sign in to comment.