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

Add option to disable v5 syntax #2500

Merged
merged 7 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 the caller session state and
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
# 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) {
fflaten marked this conversation as resolved.
Show resolved Hide resolved
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
49 changes: 44 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,38 @@ 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
}
}

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

$err | 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"
}
}
}
Loading