Skip to content

Commit

Permalink
Use functions instead of aliases and patch warning (#2499)
Browse files Browse the repository at this point in the history
* Use functions instead of aliases and patch warning

* Skip description check on Should-*

* Somehow the sleep sleeps for 0.5 ms, cannot repro
  • Loading branch information
nohwnd authored Jun 13, 2024
1 parent 4bc6b64 commit c8bc967
Show file tree
Hide file tree
Showing 41 changed files with 160 additions and 225 deletions.
122 changes: 24 additions & 98 deletions src/Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,15 @@ $script:SafeCommands['Set-DynamicParameterVariable'] = $ExecutionContext.Session
& $SafeCommands['Set-Alias'] 'Add-AssertionOperator' 'Add-ShouldOperator'
& $SafeCommands['Set-Alias'] 'Get-AssertionOperator' 'Get-ShouldOperator'

& $SafeCommands['Set-Alias'] 'Should-BeFalse' 'Assert-False'
& $SafeCommands['Set-Alias'] 'Should-BeTrue' 'Assert-True'
& $SafeCommands['Set-Alias'] 'Should-BeFalsy' 'Assert-Falsy'
& $SafeCommands['Set-Alias'] 'Should-BeTruthy' 'Assert-Truthy'
& $SafeCommands['Set-Alias'] 'Should-All' 'Assert-All'
& $SafeCommands['Set-Alias'] 'Should-Any' 'Assert-Any'
& $SafeCommands['Set-Alias'] 'Should-BeCollection' 'Assert-Collection'
& $SafeCommands['Set-Alias'] 'Should-ContainCollection' 'Assert-Contain'
& $SafeCommands['Set-Alias'] 'Should-NotContainCollection' 'Assert-NotContain'
& $SafeCommands['Set-Alias'] 'Should-BeEquivalent' 'Assert-Equivalent'
& $SafeCommands['Set-Alias'] 'Should-Throw' 'Assert-Throw'
& $SafeCommands['Set-Alias'] 'Should-Be' 'Assert-Equal'
& $SafeCommands['Set-Alias'] 'Should-BeGreaterThan' 'Assert-GreaterThan'
& $SafeCommands['Set-Alias'] 'Should-BeGreaterThanOrEqual' 'Assert-GreaterThanOrEqual'
& $SafeCommands['Set-Alias'] 'Should-BeLessThan' 'Assert-LessThan'
& $SafeCommands['Set-Alias'] 'Should-BeLessThanOrEqual' 'Assert-LessThanOrEqual'
& $SafeCommands['Set-Alias'] 'Should-NotBe' 'Assert-NotEqual'
& $SafeCommands['Set-Alias'] 'Should-NotBeNull' 'Assert-NotNull'
& $SafeCommands['Set-Alias'] 'Should-NotBeSame' 'Assert-NotSame'
& $SafeCommands['Set-Alias'] 'Should-NotHaveType' 'Assert-NotType'
& $SafeCommands['Set-Alias'] 'Should-BeNull' 'Assert-Null'
& $SafeCommands['Set-Alias'] 'Should-BeSame' 'Assert-Same'
& $SafeCommands['Set-Alias'] 'Should-HaveType' 'Assert-Type'

& $SafeCommands['Set-Alias'] 'Should-BeString' 'Assert-StringEqual'
& $SafeCommands['Set-Alias'] 'Should-NotBeString' 'Assert-StringNotEqual'
& $SafeCommands['Set-Alias'] 'Should-BeLikeString' 'Assert-Like'
& $SafeCommands['Set-Alias'] 'Should-NotBeLikeString' 'Assert-NotLike'

& $SafeCommands['Set-Alias'] 'Should-BeEmptyString' 'Assert-StringEmpty'
& $SafeCommands['Set-Alias'] 'Should-NotBeNullOrWhiteSpaceString' 'Assert-StringNotWhiteSpace'
& $SafeCommands['Set-Alias'] 'Should-NotBeNullOrEmptyString' 'Assert-StringNotEmpty'

& $SafeCommands['Set-Alias'] 'Should-BeFasterThan' 'Assert-Faster'
& $SafeCommands['Set-Alias'] 'Should-BeSlowerThan' 'Assert-Slower'
& $SafeCommands['Set-Alias'] 'Should-BeBefore' 'Assert-Before'
& $SafeCommands['Set-Alias'] 'Should-BeAfter' 'Assert-After'



& $SafeCommands['Update-TypeData'] -TypeName PesterConfiguration -TypeConverter 'PesterConfigurationDeserializer' -SerializationDepth 5 -Force
& $SafeCommands['Update-TypeData'] -TypeName 'Deserialized.PesterConfiguration' -TargetTypeForDeserialization PesterConfiguration -Force

& $script:SafeCommands['Export-ModuleMember'] @(
[Pester.VerbsPatcher]::AllowShouldVerb($PSVersionTable.PSVersion.Major)

& $script:SafeCommands['Export-ModuleMember'] -Function @(
'Invoke-Pester'

# blocks
Expand Down Expand Up @@ -80,62 +45,6 @@ $script:SafeCommands['Set-DynamicParameterVariable'] = $ExecutionContext.Session
'New-PesterConfiguration'

# assert
'Assert-False'
'Assert-True'
'Assert-Falsy'
'Assert-Truthy'
'Assert-All'
'Assert-Any'
'Assert-Contain'
'Assert-NotContain'
'Assert-Collection'
'Assert-Equivalent'
'Assert-Throw'
'Assert-Equal'
'Assert-GreaterThan'
'Assert-GreaterThanOrEqual'
'Assert-LessThan'
'Assert-LessThanOrEqual'
'Assert-NotEqual'
'Assert-NotNull'
'Assert-NotSame'
'Assert-NotType'
'Assert-Null'
'Assert-Same'
'Assert-Type'

'Assert-Like'
'Assert-NotLike'
'Assert-StringEqual'
'Assert-StringNotEqual'

'Assert-StringEmpty'
'Assert-StringNotWhiteSpace'
'Assert-StringNotEmpty'

'Assert-Faster'
'Assert-Slower'
'Assert-Before'
'Assert-After'

'Get-EquivalencyOption'

# export
'Export-NUnitReport'
'ConvertTo-NUnitReport'
'Export-JUnitReport'
'ConvertTo-JUnitReport'
'ConvertTo-Pester4Result'

# helpers
'New-MockObject'
'New-Fixture'
'Set-ItResult'
) -Alias @(
'Add-AssertionOperator'
'Get-AssertionOperator'

# assertion functions
# bool
'Should-BeFalse'
'Should-BeTrue'
Expand All @@ -145,9 +54,9 @@ $script:SafeCommands['Set-DynamicParameterVariable'] = $ExecutionContext.Session
# collection
'Should-All'
'Should-Any'
'Should-BeCollection'
'Should-ContainCollection'
'Should-NotContainCollection'
'Should-BeCollection'
'Should-BeEquivalent'
'Should-Throw'
'Should-Be'
Expand All @@ -168,15 +77,32 @@ $script:SafeCommands['Set-DynamicParameterVariable'] = $ExecutionContext.Session
'Should-NotBeString'

'Should-BeEmptyString'
'Should-NotBeNullOrWhiteSpaceString'
'Should-NotBeNullOrEmptyString'

'Should-NotBeWhiteSpaceString'
'Should-NotBeEmptyString'

'Should-BeLikeString'
'Should-NotBeLikeString'

# time
'Should-BeFasterThan'
'Should-BeSlowerThan'
'Should-BeBefore'
'Should-BeAfter'

'Get-EquivalencyOption'

# export
'Export-NUnitReport'
'ConvertTo-NUnitReport'
'Export-JUnitReport'
'ConvertTo-JUnitReport'
'ConvertTo-Pester4Result'

# helpers
'New-MockObject'
'New-Fixture'
'Set-ItResult'
) -Alias @(
'Add-AssertionOperator'
'Get-AssertionOperator'
)
78 changes: 21 additions & 57 deletions src/Pester.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -67,61 +67,6 @@
'New-PesterConfiguration'

# assert
'Assert-False'
'Assert-True'
'Assert-Falsy'
'Assert-Truthy'
'Assert-All'
'Assert-Any'
'Assert-Contain'
'Assert-NotContain'
'Assert-Collection'
'Assert-Equivalent'
'Assert-Throw'
'Assert-Equal'
'Assert-GreaterThan'
'Assert-GreaterThanOrEqual'
'Assert-LessThan'
'Assert-LessThanOrEqual'
'Assert-NotEqual'
'Assert-NotNull'
'Assert-NotSame'
'Assert-NotType'
'Assert-Null'
'Assert-Same'
'Assert-Type'
'Assert-Like'
'Assert-NotLike'
'Assert-StringEqual'
'Assert-StringNotEqual'
'Assert-StringEmpty'
'Assert-StringNotWhiteSpace'
'Assert-StringNotEmpty'
'Assert-Faster'
'Assert-Slower'
'Assert-Before'
'Assert-After'

'Get-EquivalencyOption'

# helpers
'New-MockObject'
'New-Fixture'
'Set-ItResult'
)

# # Cmdlets to export from this module
CmdletsToExport = ''

# Variables to export from this module
VariablesToExport = @()

# # Aliases to export from this module
AliasesToExport = @(
'Add-AssertionOperator'
'Get-AssertionOperator'

# assertion functions
# bool
'Should-BeFalse'
'Should-BeTrue'
Expand Down Expand Up @@ -155,8 +100,8 @@

'Should-BeEmptyString'

'Should-NotBeNullOrWhiteSpaceString'
'Should-NotBeNullOrEmptyString'
'Should-NotBeWhiteSpaceString'
'Should-NotBeEmptyString'

'Should-BeLikeString'
'Should-NotBeLikeString'
Expand All @@ -165,6 +110,25 @@
'Should-BeSlowerThan'
'Should-BeBefore'
'Should-BeAfter'

'Get-EquivalencyOption'

# helpers
'New-MockObject'
'New-Fixture'
'Set-ItResult'
)

# # Cmdlets to export from this module
CmdletsToExport = ''

# Variables to export from this module
VariablesToExport = @()

# # Aliases to export from this module
AliasesToExport = @(
'Add-AssertionOperator'
'Get-AssertionOperator'
)


Expand Down
45 changes: 45 additions & 0 deletions src/csharp/Pester/VerbsPatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;

namespace Pester
{
/// <summary>
/// Modifies that list of recommended Verbs, so we can export Should-* functions directly without
/// showing a warning to the user. Reverts the change after few seconds.
/// </summary>
public static class VerbsPatcher
{
// Keep the tasks we started so they finish and are not garbage collected.
// Concurrent bag in case we start this multiple times, and god forbid in parallel.
private static ConcurrentBag<Task> s_tasks = new ConcurrentBag<Task>();

public static void AllowShouldVerb(int powershellVersion)
{
var should = "Should";

var fieldName = powershellVersion == 5 ? "validVerbs" : "s_validVerbs";
var verbsType = typeof(System.Management.Automation.VerbsCommon).Assembly.GetType("System.Management.Automation.Verbs");
var verbsField = verbsType.GetField(fieldName, BindingFlags.Static | BindingFlags.NonPublic);

// private static readonly Dictionary<string, bool> s_validVerbs;
Dictionary<string, bool> validVerbs = (Dictionary<string, bool>)verbsField.GetValue(null);
// Overwrite when we call this multiple times.
validVerbs[should] = true; // The bool does not matter.

s_tasks.Add(Task.Run(async () =>
{
await Task.Delay(5_000);
try
{
if (validVerbs.ContainsKey(should))
{
validVerbs.Remove(should);
}
}
catch { }
}));
}
}
}
2 changes: 1 addition & 1 deletion src/functions/assert/Boolean/Should-BeFalse.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-False {
function Should-BeFalse {
<#
.SYNOPSIS
Compares the actual value to a boolean $false. It does not convert input values to boolean, and will fail for any value that is not $false.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/Boolean/Should-BeFalsy.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-Falsy {
function Should-BeFalsy {
<#
.SYNOPSIS
Compares the actual value to a boolean $false or a falsy value: 0, "", $null or @(). It converts the input value to a boolean.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/Boolean/Should-BeTrue.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-True {
function Should-BeTrue {
<#
.SYNOPSIS
Compares the actual value to a boolean $true. It does not convert input values to boolean, and will fail for any value is not $true.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/Boolean/Should-BeTruthy.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-Truthy {
function Should-BeTruthy {
<#
.SYNOPSIS
Compares the actual value to a boolean $true. It converts input values to boolean, and will fail for any value is not $true, or truthy.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/Collection/Should-All.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-All {
function Should-All {
<#
.SYNOPSIS
Compares all items in a collection to a filter script. If the filter returns true, or does not throw for all the items in the collection, the assertion passes.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/Collection/Should-Any.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-Any {
function Should-Any {
<#
.SYNOPSIS
Compares all items in a collection to a filter script. If the filter returns true, or does not throw for any of the items in the collection, the assertion passes.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/Collection/Should-BeCollection.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-Collection {
function Should-BeCollection {
<#
.SYNOPSIS
Compares collections for equality, by comparing their sizes and each item in them. It does not compare the types of the input collections.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-Contain {
function Should-ContainCollection {
<#
.SYNOPSIS
Compares collections to see if the expected collection is present in the provided collection. It does not compare the types of the input collections.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-NotContain {
function Should-NotContainCollection {
<#
.SYNOPSIS
Compares collections to ensure that the expected collection is not present in the provided collection. It does not compare the types of the input collections.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/Equivalence/Should-BeEquivalent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ function Compare-Equivalent {
Compare-ObjectEquivalent -Expected $Expected -Actual $Actual -Property $Path -Options $Options
}

function Assert-Equivalent {
function Should-BeEquivalent {
<#
.SYNOPSIS
Compares two objects for equivalency, by recursively comparing their properties for equivalency.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/Exception/Should-Throw.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-Throw {
function Should-Throw {
<#
.SYNOPSIS
Asserts that a script block throws an exception.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/General/Should-Be.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-Equal {
function Should-Be {
<#
.SYNOPSIS
Compares the expected value to actual value, to see if they are equal.
Expand Down
2 changes: 1 addition & 1 deletion src/functions/assert/General/Should-BeGreaterThan.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-GreaterThan {
function Should-BeGreaterThan {
<#
.SYNOPSIS
Compares the expected value to actual value, to see if the actual value is greater than the expected value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Assert-GreaterThanOrEqual {
function Should-BeGreaterThanOrEqual {
<#
.SYNOPSIS
Compares the expected value to actual value, to see if the actual value is greater than or equal to the expected value.
Expand Down
Loading

0 comments on commit c8bc967

Please sign in to comment.