Skip to content

Commit

Permalink
Add option to disable v5 syntax (#2500)
Browse files Browse the repository at this point in the history
* Add option to disable v5 syntax

* Remove extra output

* Update src/functions/assertions/Should.ps1

Co-authored-by: Frode Flaten <[email protected]>

* Check message, so we don't write error to pipeline

* Check message, so we don't write error to pipeline

* Don't format my error

---------

Co-authored-by: Frode Flaten <[email protected]>
  • Loading branch information
nohwnd and fflaten authored Jun 14, 2024
1 parent c8bc967 commit c60fd55
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/Pester.RSpec.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ function New-PesterConfiguration {
ErrorAction: Controls if Should throws on error. Use 'Stop' to throw on error, or 'Continue' to fail at the end of the test.
Default value: 'Stop'
DisableV5: Disables usage of Should -Be assertions, that are replaced by Should-Be in version 6.
Default value: $false
Debug:
ShowFullErrors: Show full errors including Pester internal stack. This property is deprecated, and if set to true it will override Output.StackTraceVerbosity to 'Full'.
Default value: $false
Expand Down
19 changes: 19 additions & 0 deletions src/csharp/Pester/ShouldConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Pester
public class ShouldConfiguration : ConfigurationSection
{
private StringOption _errorAction;
private BoolOption _disableV5;

public static ShouldConfiguration Default { get { return new ShouldConfiguration(); } }

Expand All @@ -33,11 +34,13 @@ public static ShouldConfiguration ShallowClone(ShouldConfiguration configuration
public ShouldConfiguration() : base("Should configuration.")
{
ErrorAction = new StringOption("Controls if Should throws on error. Use 'Stop' to throw on error, or 'Continue' to fail at the end of the test.", "Stop");
DisableV5 = new BoolOption("Disables usage of Should -Be assertions, that are replaced by Should-Be in version 6.", false);
}

public ShouldConfiguration(IDictionary configuration) : this()
{
configuration?.AssignObjectIfNotNull<string>(nameof(ErrorAction), v => ErrorAction = v);
configuration?.AssignValueIfNotNull<bool>(nameof(DisableV5), v => DisableV5 = v);
}

public StringOption ErrorAction
Expand All @@ -55,5 +58,21 @@ public StringOption ErrorAction
}
}
}

public BoolOption DisableV5
{
get { return _disableV5; }
set
{
if (_disableV5 == null)
{
_disableV5 = value;
}
else
{
_disableV5 = new BoolOption(_disableV5, value.Value);
}
}
}
}
}
20 changes: 12 additions & 8 deletions src/functions/assertions/Should.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,22 @@ function Should {
$shouldThrow = 'Stop' -eq $PSBoundParameters["ErrorAction"]
}

# first check if we are in the context of Pester, if not we will always throw, and won't disable Should:
# This check is slightly hacky, here we are reaching out the caller session state and
# look for $______parameters which we know we are using inside of the Pester runtime to
# keep the current invocation context, when we find it, we are able to add non-terminating
# errors without throwing and terminating the test.
$pesterRuntimeInvocationContext = $PSCmdlet.SessionState.PSVariable.GetValue('______parameters')
$isInsidePesterRuntime = $null -ne $pesterRuntimeInvocationContext

if ($isInsidePesterRuntime -and $pesterRuntimeInvocationContext.Configuration.Should.DisableV5.Value) {
throw "Pester Should -Be syntax is disabled. Use Should-Be (without space), or enable it by setting: `$PesterPreference.Should.DisableV5 = `$false"
}

if ($null -eq $shouldThrow -or -not $shouldThrow) {
# we are sure that we either:
# - should not throw because of explicit ErrorAction, and need to figure out a place where to collect the error
# - or we don't know what to do yet and need to figure out what to do based on the context and settings

# first check if we are in the context of Pester, if not we will always throw:
# this is slightly hacky, here we are reaching out the the caller session state and
# look for $______parameters which we know we are using inside of the Pester runtime to
# keep the current invocation context, when we find it, we are able to add non-terminating
# errors without throwing and terminating the test
$pesterRuntimeInvocationContext = $PSCmdlet.SessionState.PSVariable.GetValue('______parameters')
$isInsidePesterRuntime = $null -ne $pesterRuntimeInvocationContext
if (-not $isInsidePesterRuntime) {
$shouldThrow = $true
}
Expand Down
52 changes: 47 additions & 5 deletions tst/Pester.RSpec.Configuration.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ i -PassThru:$PassThru {
[PesterConfiguration]::Default.Should.ErrorAction.Value | Verify-Equal 'Stop'
}

t "Should.DisableV5 is `$false" {
[PesterConfiguration]::Default.Should.DisableV5.Value | Verify-Equal $false
}

# Debug configuration
t "Debug.ShowFullErrors is `$false" {
[PesterConfiguration]::Default.Debug.ShowFullErrors.Value | Verify-False
Expand Down Expand Up @@ -1140,7 +1144,7 @@ i -PassThru:$PassThru {
Throw = $true
}
Output = @{
CIFormat = 'None'
CIFormat = 'None'
CILogLevel = 'Something'
}
}
Expand All @@ -1152,7 +1156,7 @@ i -PassThru:$PassThru {
b 'Output.RenderMode' {
t 'Output.RenderMode is Plaintext when set to Auto (default) and env:NO_COLOR is set' {
$c = [PesterConfiguration] @{
Run = @{
Run = @{
ScriptBlock = { }
PassThru = $true
}
Expand Down Expand Up @@ -1225,7 +1229,7 @@ i -PassThru:$PassThru {
$pesterPath = Get-Module Pester | Select-Object -ExpandProperty Path
try {
$ps = [PowerShell]::Create()
$ps.AddCommand('Set-StrictMode').AddParameter('Version','Latest') > $null
$ps.AddCommand('Set-StrictMode').AddParameter('Version', 'Latest') > $null
$ps.AddStatement().AddScript("Import-Module '$pesterPath' -Force") > $null
$ps.AddStatement().AddScript('$c = [PesterConfiguration]@{Run = @{ScriptBlock={ describe "d1" { it "i1" { } } };PassThru=$true};Output=@{RenderMode="Auto"}}') > $null
$ps.AddStatement().AddScript('Invoke-Pester -Configuration $c') > $null
Expand All @@ -1243,7 +1247,7 @@ i -PassThru:$PassThru {

t 'Each non-Auto option can be set and updated' {
$c = [PesterConfiguration] @{
Run = @{
Run = @{
ScriptBlock = { }
PassThru = $true
}
Expand Down Expand Up @@ -1277,7 +1281,8 @@ i -PassThru:$PassThru {
try {
Invoke-Pester -Configuration $c
$true | Verify-False # Should not get here
} catch {
}
catch {
$_.Exception.Message -match "Output.RenderMode must be .* it was 'Something'" | Verify-True
$failed = $true
}
Expand Down Expand Up @@ -1323,4 +1328,41 @@ i -PassThru:$PassThru {
{ Invoke-Pester -Configuration $c } | Verify-Throw
}
}

b "Should.DisableV5" {
t "Disabling V5 assertions makes Should -Be throw" {
$c = [PesterConfiguration]@{
Run = @{
ScriptBlock = { Describe 'a' { It 'b' { 1 | Should -Be 1 } } }
PassThru = $true
}
Should = @{
DisableV5 = $true
}
Output = @{
CIFormat = 'None'
}
}

$r = Invoke-Pester -Configuration $c
$err = $r.Containers.Blocks.Tests.ErrorRecord

$err.Exception.Message | Verify-Equal 'Pester Should -Be syntax is disabled. Use Should-Be (without space), or enable it by setting: $PesterPreference.Should.DisableV5 = $false'
}

t "Enabling V5 assertions makes Should -Be pass" {
$c = [PesterConfiguration]@{
Run = @{
ScriptBlock = { Describe 'a' { It 'b' { 1 | Should -Be 1 } } }
PassThru = $true
}
Should = @{
DisableV5 = $false
}
}

$r = Invoke-Pester -Configuration $c
$r.Result | Verify-Equal "Passed"
}
}
}

0 comments on commit c60fd55

Please sign in to comment.