Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve operator-help returned from Get-ShouldOperator #1988

Merged
merged 16 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-vscode.powershell",
"ms-dotnettools.csharp"
"ms-dotnettools.csharp",
"redhat.vscode-xml"
],
// Use 'postCreateCommand' to run commands after the container is created.
// Restoring the c# projects
Expand Down
15 changes: 14 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,18 @@
"yaml.format.singleQuote": true,
"[powershell]": {
"files.encoding": "utf8bom"
}
},
"files.associations": {
"*.ps1xml": "xml"
},
"xml.fileAssociations": [
{
"systemId": "https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/Schemas/Format.xsd",
"pattern": "**/*.Format.ps1xml"
},
{
"systemId": "https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/Schemas/Types.xsd",
"pattern": "**/*.Types.ps1xml"
}
]
}
1 change: 1 addition & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ $content = @(
, ("$PSScriptRoot/src/report.dtd", "$PSScriptRoot/bin/")
, ("$PSScriptRoot/src/Pester.ps1", "$PSScriptRoot/bin/")
, ("$PSScriptRoot/src/Pester.psd1", "$PSScriptRoot/bin/")
, ("$PSScriptRoot/src/Pester.Format.ps1xml", "$PSScriptRoot/bin/")
)

if ($Clean) {
Expand Down
200 changes: 200 additions & 0 deletions src/Pester.Format.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>PesterAssertionOperatorHelp</Name>
<ViewSelectedBy>
<TypeName>PesterAssertionOperatorHelp</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<Text>NAME</Text>
<NewLine />
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<ExpressionBinding>
<PropertyName>Name</PropertyName>
</ExpressionBinding>
<NewLine />
</CustomItem>
</Frame>
<NewLine />
<ExpressionBinding>
<ScriptBlock>$_</ScriptBlock>
<ItemSelectionCondition>
<PropertyName>Aliases</PropertyName>
</ItemSelectionCondition>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<Text>ALIASES</Text>
<NewLine />
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<ExpressionBinding>
<PropertyName>Aliases</PropertyName>
<EnumerateCollection />
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>$_</ScriptBlock>
</ExpressionBinding>
<NewLine />
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</ExpressionBinding>
</CustomItem>
</Frame>
<NewLine />
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</ExpressionBinding>
<ExpressionBinding>
<PropertyName>Help</PropertyName>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<Text>SYNTAX</Text>
<NewLine />
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<ExpressionBinding>
<PropertyName>syntax</PropertyName>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<PropertyName>syntaxitem</PropertyName>
<EnumerateCollection />
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>"$($_.name) $($_.DisplayParameterSet)"</ScriptBlock>
</ExpressionBinding>
<NewLine />
<NewLine />
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</ExpressionBinding>
</CustomItem>
</Frame>
<Text>SYNOPSIS</Text>
<NewLine />
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<ExpressionBinding>
<PropertyName>synopsis</PropertyName>
</ExpressionBinding>
<NewLine />
</CustomItem>
</Frame>
<NewLine />
<Text>EXAMPLES</Text>
<NewLine />
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<ExpressionBinding>
<PropertyName>Examples</PropertyName>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<PropertyName>Example</PropertyName>
<EnumerateCollection />
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<PropertyName>title</PropertyName>
</ExpressionBinding>
<NewLine />
<NewLine />
<ExpressionBinding>
<PropertyName>introduction</PropertyName>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<PropertyName>Text</PropertyName>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</ExpressionBinding>
<ExpressionBinding>
<PropertyName>code</PropertyName>
<EnumerateCollection />
</ExpressionBinding>
<NewLine />
<NewLine />
<ExpressionBinding>
<PropertyName>remarks</PropertyName>
<EnumerateCollection />
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<PropertyName>Text</PropertyName>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</ExpressionBinding>
<NewLine />
<NewLine />
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</ExpressionBinding>
<NewLine />
</CustomItem>
</Frame>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
</ViewDefinitions>
</Configuration>
2 changes: 1 addition & 1 deletion src/Pester.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = @('PesterConfiguration.Format.ps1xml')
FormatsToProcess = @('Pester.Format.ps1xml', 'PesterConfiguration.Format.ps1xml')

# Functions to export from this module
FunctionsToExport = @(
Expand Down
17 changes: 15 additions & 2 deletions src/functions/Get-ShouldOperator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,26 @@
END {
if ($Name) {
$operator = $AssertionOperators.Values | & $SafeCommands['Where-Object'] { $Name -eq $_.Name -or $_.Alias -contains $Name }
$help = Get-Help $operator.InternalName -Examples -ErrorAction SilentlyContinue
$commandInfo = & $SafeCommands['Get-Command'] -Name $operator.InternalName -ErrorAction SilentlyContinue
$help = & $SafeCommands['Get-Help'] -Name $operator.InternalName -Examples -ErrorAction SilentlyContinue

if (($help | & $SafeCommands['Measure-Object']).Count -ne 1) {
& $SafeCommands['Write-Warning'] ("No help found for Should operator '{0}'" -f ((Get-AssertionOperatorEntry $Name).InternalName))
}
else {
$help
# Update syntax to use Should -Operator as command-name and pretty printed parameter set
for ($i = 0; $i -lt $commandInfo.ParameterSets.Count; $i++) {
$help.syntax.syntaxItem[$i].name = "Should -$($operator.Name)"
$prettyParameterSet = $commandInfo.ParameterSets[$i].ToString() -replace '-Negate', '-Not' -replace '\[+-CallerSessionState\]? <.*?>\]?\s?'
$help.syntax.syntaxItem[$i].PSObject.Properties.Add([Pester.Factory]::CreateNoteProperty('DisplayParameterSet', $prettyParameterSet))
}

[PSCustomObject]@{
PSTypeName = 'PesterAssertionOperatorHelp'
Name = $operator.Name
Aliases = @($operator.Alias)
Help = $help
}
}
}
else {
Expand Down
1 change: 1 addition & 0 deletions src/functions/Pester.SafeCommands.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ $script:SafeCommands = @{
'Get-ChildItem' = & $Get_Command -Name Get-ChildItem -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-Content' = & $Get_Command -Name Get-Content -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-Date' = & $Get_Command -Name Get-Date -Module Microsoft.PowerShell.Utility @safeCommandLookupParameters
'Get-Help' = & $Get_Command -Name Get-Help -Module Microsoft.PowerShell.Core @safeCommandLookupParameters
'Get-Item' = & $Get_Command -Name Get-Item -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-ItemProperty' = & $Get_Command -Name Get-ItemProperty -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
'Get-Location' = & $Get_Command -Name Get-Location -Module Microsoft.PowerShell.Management @safeCommandLookupParameters
Expand Down
16 changes: 8 additions & 8 deletions src/functions/Pester.SessionState.Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -770,38 +770,38 @@ Context, the command will identify all calls to the mocked command in the
current Describe / Context block, as well as all child scopes of that block.

.EXAMPLE
C:\PS>Mock Set-Content {}
Mock Set-Content {}

{... Some Code ...}

C:\PS>Should -Invoke Set-Content
Should -Invoke Set-Content

This will throw an exception and cause the test to fail if Set-Content is not called in Some Code.

.EXAMPLE
C:\PS>Mock Set-Content -parameterFilter {$path.StartsWith("$env:temp\")}
Mock Set-Content -parameterFilter {$path.StartsWith("$env:temp\")}

{... Some Code ...}

C:\PS>Should -Invoke Set-Content 2 { $path -eq "$env:temp\test.txt" }
Should -Invoke Set-Content 2 { $path -eq "$env:temp\test.txt" }

This will throw an exception if some code calls Set-Content on $path=$env:temp\test.txt less than 2 times

.EXAMPLE
C:\PS>Mock Set-Content {}
Mock Set-Content {}

{... Some Code ...}

C:\PS>Should -Invoke Set-Content 0
Should -Invoke Set-Content 0

This will throw an exception if some code calls Set-Content at all

.EXAMPLE
C:\PS>Mock Set-Content {}
Mock Set-Content {}

{... Some Code ...}

C:\PS>Should -Invoke Set-Content -Exactly 2
Should -Invoke Set-Content -Exactly 2

This will throw an exception if some code does not call Set-Content Exactly two times.

Expand Down
8 changes: 4 additions & 4 deletions src/functions/assertions/Be.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ and throws if the two objects are not the same.

.EXAMPLE
$actual = "Actual value"
PS C:\>$actual | Should -Be "actual value"
$actual | Should -Be "actual value"

This test will pass. -Be is not case sensitive.
For a case sensitive assertion, see -BeExactly.

.EXAMPLE
$actual = "Actual value"
PS C:\>$actual | Should -Be "not actual value"
$actual | Should -Be "not actual value"

This test will fail, as the two strings are not identical.
#>
Expand Down Expand Up @@ -78,13 +78,13 @@ two objects are not the same. This comparison is case sensitive.

.EXAMPLE
$actual = "Actual value"
PS C:\>$actual | Should -Be "Actual value"
$actual | Should -Be "Actual value"

This test will pass. The two strings are identical.

.EXAMPLE
$actual = "Actual value"
PS C:\>$actual | Should -Be "actual value"
$actual | Should -Be "actual value"

This test will fail, as the two strings do not match case sensitivity.
#>
Expand Down
3 changes: 3 additions & 0 deletions src/functions/assertions/BeGreaterThan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Uses PowerShell's -gt operator to compare the two values.

.EXAMPLE
2 | Should -BeGreaterThan 0

This test passes, as PowerShell evaluates `2 -gt 0` as true.
#>
if ($Negate) {
Expand Down Expand Up @@ -33,10 +34,12 @@ Uses PowerShell's -le operator to compare the two values.

.EXAMPLE
1 | Should -BeLessOrEqual 10

This test passes, as PowerShell evaluates `1 -le 10` as true.

.EXAMPLE
10 | Should -BeLessOrEqual 10

This test also passes, as PowerShell evaluates `10 -le 10` as true.
#>
if ($Negate) {
Expand Down
1 change: 1 addition & 0 deletions src/functions/assertions/BeIn.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Uses PowerShell's -contains operator to confirm.

.EXAMPLE
1 | Should -BeIn @(1,2,3,'a','b','c')

This test passes, as 1 exists in the provided collection.
#>
[bool] $succeeded = $ExpectedValue -contains $ActualValue
Expand Down