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

Remove old and redundant functions #2520

Merged
merged 13 commits into from
Jul 1, 2024
225 changes: 0 additions & 225 deletions src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,6 @@ function Invoke-Pester {
This parameter is ignored in v5, and is only present for backwards compatibility
when migrating from v4.

Sets advanced options for the test execution. Enter a PesterOption object,
such as one that you create by using the New-PesterOption cmdlet, or a hash table
in which the keys are option names and the values are option values.
For more information on the options available, see the help for New-PesterOption.

.PARAMETER Quiet
(Deprecated v4)
The parameter Quiet is deprecated since Pester v4.0 and will be deleted
Expand Down Expand Up @@ -1102,226 +1097,6 @@ function Convert-PesterLegacyParameterSet ($BoundParameters) {
return $Configuration
}

function New-PesterOption {
#TODO: move those options, right now I am just not exposing this function and added the testSuiteName
<#
.SYNOPSIS
Creates an object that contains advanced options for Invoke-Pester
.DESCRIPTION
By using New-PesterOption you can set options what allow easier integration with external applications or
modifies output generated by Invoke-Pester.
The result of New-PesterOption need to be assigned to the parameter 'PesterOption' of the Invoke-Pester function.
.PARAMETER IncludeVSCodeMarker
When this switch is set, an extra line of output will be written to the console for test failures, making it easier
for VSCode's parser to provide highlighting / tooltips on the line where the error occurred.
.PARAMETER TestSuiteName
When generating NUnit XML output, this controls the name assigned to the root "test-suite" element. Defaults to "Pester".
.PARAMETER ScriptBlockFilter
Filters scriptblock based on the path and line number. This is intended for integration with external tools so we don't rely on names (strings) that can have expandable variables in them.
.PARAMETER Experimental
Enables experimental features of Pester to be enabled.
.PARAMETER ShowScopeHints
EXPERIMENTAL: Enables debugging output for debugging transitions among scopes. (Experimental flag needs to be used to enable this.)

.INPUTS
None
You cannot pipe input to this command.
.OUTPUTS
System.Management.Automation.PSObject
.EXAMPLE
PS > $Options = New-PesterOption -TestSuiteName "Tests - Set A"

PS > Invoke-Pester -PesterOption $Options -Outputfile ".\Results-Set-A.xml" -OutputFormat NUnitXML

The result of commands will be execution of tests and saving results of them in a NUnitMXL file where the root "test-suite"
will be named "Tests - Set A".
.LINK
https://github.com/pester/Pester/wiki/New-PesterOption

.LINK
Invoke-Pester
#>
[CmdletBinding()]
param (
[switch] $IncludeVSCodeMarker,

[ValidateNotNullOrEmpty()]
[string] $TestSuiteName = 'Pester',

[switch] $Experimental,

[switch] $ShowScopeHints,

[hashtable[]] $ScriptBlockFilter
)

# in PowerShell 2 Add-Member can attach properties only to
# PSObjects, I could work around this by capturing all instances
# in checking them during runtime, but that would bring a lot of
# object management problems - so let's just not allow this in PowerShell 2
if ($Experimental -and $ShowScopeHints) {
if ($PSVersionTable.PSVersion.Major -lt 3) {
throw "Scope hints cannot be used on PowerShell 2 due to limitations of Add-Member."
}

$script:DisableScopeHints = $false
}
else {
$script:DisableScopeHints = $true
}

return [PSCustomObject]@{
ReadMe = "New-PesterOption is deprecated and kept only for backwards compatibility when executing Pester v5 using the " +
"legacy parameter set. When the object is used with Invoke-Pester -PesterOption it will be ignored."
IncludeVSCodeMarker = [bool] $IncludeVSCodeMarker
TestSuiteName = $TestSuiteName
ShowScopeHints = $ShowScopeHints
Experimental = $Experimental
ScriptBlockFilter = $ScriptBlockFilter
}
}

function ResolveTestScripts {
param ([object[]] $Path)

$resolvedScriptInfo = @(
foreach ($object in $Path) {
if ($object -is [System.Collections.IDictionary]) {
$unresolvedPath = Get-DictionaryValueFromFirstKeyFound -Dictionary $object -Key 'Path', 'p'
$script = Get-DictionaryValueFromFirstKeyFound -Dictionary $object -Key 'Script'
$arguments = @(Get-DictionaryValueFromFirstKeyFound -Dictionary $object -Key 'Arguments', 'args', 'a')
$parameters = Get-DictionaryValueFromFirstKeyFound -Dictionary $object -Key 'Parameters', 'params'

if ($null -eq $Parameters) {
$Parameters = @{}
}

if ($unresolvedPath -isnot [string] -or $unresolvedPath -notmatch '\S' -and ($script -isnot [string] -or $script -notmatch '\S')) {
throw 'When passing hashtables to the -Path parameter, the Path key is mandatory, and must contain a single string.'
}

if ($null -ne $parameters -and $parameters -isnot [System.Collections.IDictionary]) {
throw 'When passing hashtables to the -Path parameter, the Parameters key (if present) must be assigned an IDictionary object.'
}
}
else {
$unresolvedPath = [string] $object
$script = [string] $object
$arguments = @()
$parameters = @{}
}

if (-not [string]::IsNullOrEmpty($unresolvedPath)) {
if ($unresolvedPath -notmatch '[\*\?\[\]]' -and
(& $script:SafeCommands['Test-Path'] -LiteralPath $unresolvedPath -PathType Leaf) -and
(& $script:SafeCommands['Get-Item'] -LiteralPath $unresolvedPath) -is [System.IO.FileInfo]) {
$extension = [System.IO.Path]::GetExtension($unresolvedPath)
if ($extension -ne '.ps1') {
& $script:SafeCommands['Write-Error'] "Script path '$unresolvedPath' is not a ps1 file."
}
else {
[PSCustomObject]@{
Path = $unresolvedPath
Script = $null
Arguments = $arguments
Parameters = $parameters
}
}
}
else {
# World's longest pipeline?

& $script:SafeCommands['Resolve-Path'] -Path $unresolvedPath |
& $script:SafeCommands['Where-Object'] { $_.Provider.Name -eq 'FileSystem' } |
& $script:SafeCommands['Select-Object'] -ExpandProperty ProviderPath |
& $script:SafeCommands['Get-ChildItem'] -Include *.Tests.ps1 -Recurse |
& $script:SafeCommands['Where-Object'] { -not $_.PSIsContainer } |
& $script:SafeCommands['Select-Object'] -ExpandProperty FullName -Unique |
& $script:SafeCommands['ForEach-Object'] {
[PSCustomObject]@{
Path = $_
Script = $null
Arguments = $arguments
Parameters = $parameters
}
}
}
}
elseif (-not [string]::IsNullOrEmpty($script)) {
[PSCustomObject]@{
Path = $null
Script = $script
Arguments = $arguments
Parameters = $parameters
}
}
}
)

# Here, we have the option of trying to weed out duplicate file paths that also contain identical
# Parameters / Arguments. However, we already make sure that each object in $Path didn't produce
# any duplicate file paths, and if the caller happens to pass in a set of parameters that produce
# dupes, maybe that's not our problem. For now, just return what we found.

$resolvedScriptInfo
}

function Get-DictionaryValueFromFirstKeyFound {
param ([System.Collections.IDictionary] $Dictionary, [object[]] $Key)

foreach ($keyToTry in $Key) {
if ($Dictionary.Contains($keyToTry)) {
return $Dictionary[$keyToTry]
}
}
}

function Set-PesterStatistics($Node) {
if ($null -eq $Node) {
$Node = $pester.TestActions
}

foreach ($action in $Node.Actions) {
if ($action.Type -eq 'TestGroup') {
Set-PesterStatistics -Node $action

$Node.TotalCount += $action.TotalCount
$Node.PassedCount += $action.PassedCount
$Node.FailedCount += $action.FailedCount
$Node.SkippedCount += $action.SkippedCount
$Node.InconclusiveCount += $action.InconclusiveCount
}
elseif ($action.Type -eq 'TestCase') {
$node.TotalCount++

switch ($action.Result) {
Passed {
$Node.PassedCount++; break;
}
Failed {
$Node.FailedCount++; break;
}
Skipped {
$Node.SkippedCount++; break;
}
Inconclusive {
$Node.InconclusiveCount++; break;
}
}
}
}
}

function Contain-AnyStringLike ($Filter, $Collection) {
foreach ($item in $Collection) {
foreach ($value in $Filter) {
if ($item -like $value) {
return $true
}
}
}
return $false
}

function ConvertTo-Pester4Result {
<#
Expand Down
Loading