From 5b929effa242afe4f639da9c5616023cea802b1b Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sat, 15 Jul 2023 23:48:10 +0000 Subject: [PATCH 1/6] Add Name-property to Container --- src/csharp/Pester/Container.cs | 2 +- src/csharp/Pester/ContainerInfo.cs | 5 ++++ src/csharp/Pester/ToStringConverter.cs | 30 ++++++++++++++-------- src/functions/Output.ps1 | 28 ++++++-------------- src/functions/TestResults.JUnit4.ps1 | 15 +++-------- src/functions/TestResults.NUnit25.ps1 | 10 ++------ src/functions/TestResults.NUnit3.ps1 | 19 ++++---------- tst/Pester.RSpec.TestResults.JUnit4.ts.ps1 | 2 +- 8 files changed, 45 insertions(+), 66 deletions(-) diff --git a/src/csharp/Pester/Container.cs b/src/csharp/Pester/Container.cs index 76bd80c69..5777ccb63 100644 --- a/src/csharp/Pester/Container.cs +++ b/src/csharp/Pester/Container.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.IO; @@ -48,6 +47,7 @@ public static Container CreateFromFile(FileInfo file) }; } + public string Name { get => ToStringConverter.ContainerItemToString(Type, Item); } public string Type { get; set; } public object Item { get; set; } public object Data { get; set; } diff --git a/src/csharp/Pester/ContainerInfo.cs b/src/csharp/Pester/ContainerInfo.cs index c66de1178..40a90ba29 100644 --- a/src/csharp/Pester/ContainerInfo.cs +++ b/src/csharp/Pester/ContainerInfo.cs @@ -14,5 +14,10 @@ public static ContainerInfo Create() public string Type { get; set; } = "File"; public object Item { get; set; } public object Data { get; set; } + + public override string ToString() + { + return ToStringConverter.ContainerInfoToString(this); + } } } diff --git a/src/csharp/Pester/ToStringConverter.cs b/src/csharp/Pester/ToStringConverter.cs index cc6f77f45..1b0673da2 100644 --- a/src/csharp/Pester/ToStringConverter.cs +++ b/src/csharp/Pester/ToStringConverter.cs @@ -1,4 +1,4 @@ -using System; +using System.IO; using System.Management.Automation; namespace Pester @@ -18,29 +18,37 @@ static string ResultToString(string result) }; } - internal static string ContainerToString(Container container) + internal static string ContainerItemToString(string Type, object Item) { string path; - switch (container.Type) + switch (Type) { case Constants.File: - path = container.Item.ToString(); + path = Item is FileInfo f ? f.FullName : Item.ToString(); break; case Constants.ScriptBlock: path = ""; - if (container.Item is ScriptBlock s) { - if (!string.IsNullOrWhiteSpace(s.File)) - { - path += $":{s.File}:{s.StartPosition.StartLine}"; - } + if (Item is ScriptBlock s && !string.IsNullOrWhiteSpace(s.File)) + { + path += $":{s.File}:{s.StartPosition.StartLine}"; } break; default: - path = $"<{container.Type}>"; + path = $"<{Type}>"; break; } - return $"{ResultToString(container.Result)} {path}"; + return path; + } + + internal static string ContainerToString(Container container) + { + return $"{ResultToString(container.Result)} {ContainerItemToString(container.Type, container.Item)}"; + } + + internal static string ContainerInfoToString(ContainerInfo containerInfo) + { + return ContainerItemToString(containerInfo.Type, containerInfo.Item); } internal static string TestToString(Test test) diff --git a/src/functions/Output.ps1 b/src/functions/Output.ps1 index 53712bb26..794fae9b7 100644 --- a/src/functions/Output.ps1 +++ b/src/functions/Output.ps1 @@ -386,19 +386,13 @@ function Write-PesterReport { if (0 -lt $RunResult.FailedContainersCount) { $cs = foreach ($container in $RunResult.FailedContainers) { - $path = if ("File" -eq $container.Type) { - $container.Item.FullName - } - elseif ("ScriptBlock" -eq $container.Type) { - "$($container.Item.File):$($container.Item.StartPosition.StartLine)" - } - else { + if ($container.Type -notin 'File', 'ScriptBlock') { throw "Container type '$($container.Type)' is not supported." } - " - $path" + " - $($container.Name)" } - Write-PesterHostMessage ("Container failed: {0}" -f $RunResult.FailedContainersCount) -Foreground $ReportTheme.Fail + Write-PesterHostMessage ('Container failed: {0}' -f $RunResult.FailedContainersCount) -Foreground $ReportTheme.Fail Write-PesterHostMessage ($cs -join [Environment]::NewLine) -Foreground $ReportTheme.Fail } # & $SafeCommands['Write-Host'] ($ReportStrings.TestsPending -f $RunResult.PendingCount) -Foreground $Pending -NoNewLine @@ -553,7 +547,7 @@ function ConvertTo-FailureLines { if ($true) { # no code # non inlined scripts will have different paths just omit everything from the src folder - $path = [regex]::Escape(($PSScriptRoot | & $SafeCommands["Split-Path"])) + $path = [regex]::Escape(($PSScriptRoot | & $SafeCommands['Split-Path'])) [String]$isPesterFunction = "^at .*, .*$path.*: line [0-9]*$" [String]$isShould = "^at (Should|Invoke-Assertion), .*$path.*: line [0-9]*$" } @@ -619,18 +613,12 @@ function Get-WriteScreenPlugin ($Verbosity) { $p.ContainerDiscoveryEnd = { param ($Context) - if ("Failed" -eq $Context.Block.Result) { - $path = if ("File" -eq $container.Type) { - $container.Item.FullName - } - elseif ("ScriptBlock" -eq $container.Type) { - "$($container.Item.File):$($container.Item.StartPosition.StartLine)" - } - else { - throw "Container type '$($container.Type)' is not supported." + if ('Failed' -eq $Context.Block.Result) { + if ($Context.BlockContainer.Type -notin 'File', 'ScriptBlock') { + throw "Context.BlockContainer type '$($Context.BlockContainer.Type)' is not supported." } - $errorHeader = "[-] Discovery in $($path) failed with:" + $errorHeader = "[-] Discovery in $($Context.BlockContainer) failed with:" $formatErrorParams = @{ Err = $Context.Block.ErrorRecord diff --git a/src/functions/TestResults.JUnit4.ps1 b/src/functions/TestResults.JUnit4.ps1 index 298bdcb90..43185e5a4 100644 --- a/src/functions/TestResults.JUnit4.ps1 +++ b/src/functions/TestResults.JUnit4.ps1 @@ -42,23 +42,16 @@ function Write-JUnitTestSuiteElements { $XmlWriter.WriteStartElement('testsuite') - if ('File' -eq $Container.Type) { - $path = $Container.Item.FullName + if ($container.Type -notin 'File', 'ScriptBlock') { + throw "Container type '$($container.Type)' is not supported." } - elseif ('ScriptBlock' -eq $Container.Type) { - $path = "$($Container.Item.File):$($Container.Item.StartPosition.StartLine)" - } - else { - throw "Container type '$($Container.Type)' is not supported." - } - - Write-JUnitTestSuiteAttributes -Action $Container -XmlWriter $XmlWriter -Package $path -Id $Id + Write-JUnitTestSuiteAttributes -Action $Container -XmlWriter $XmlWriter -Package $container.Name -Id $Id $testResults = [Pester.Factory]::CreateCollection() Fold-Container -Container $Container -OnTest { param ($t) if ($t.ShouldRun) { $testResults.Add($t) } } foreach ($t in $testResults) { - Write-JUnitTestCaseElements -TestResult $t -XmlWriter $XmlWriter -Package $path + Write-JUnitTestCaseElements -TestResult $t -XmlWriter $XmlWriter -Package $container.Name } $XmlWriter.WriteEndElement() diff --git a/src/functions/TestResults.NUnit25.ps1 b/src/functions/TestResults.NUnit25.ps1 index d0c50b4d8..ec36e0fb4 100644 --- a/src/functions/TestResults.NUnit25.ps1 +++ b/src/functions/TestResults.NUnit25.ps1 @@ -53,16 +53,10 @@ function Write-NUnitTestResultChildNodes { continue } - if ('File' -eq $container.Type) { - $path = $container.Item.FullName - } - elseif ('ScriptBlock' -eq $container.Type) { - $path = "$($container.Item.File):$($container.Item.StartPosition.StartLine)" - } - else { + if ($container.Type -notin 'File', 'ScriptBlock') { throw "Container type '$($container.Type)' is not supported." } - Write-NUnitTestSuiteElements -XmlWriter $XmlWriter -Node $container -Path $path + Write-NUnitTestSuiteElements -XmlWriter $XmlWriter -Node $container -Path $container.Name } $XmlWriter.WriteEndElement() diff --git a/src/functions/TestResults.NUnit3.ps1 b/src/functions/TestResults.NUnit3.ps1 index 3f4b9b85d..e4557644e 100644 --- a/src/functions/TestResults.NUnit3.ps1 +++ b/src/functions/TestResults.NUnit3.ps1 @@ -230,21 +230,12 @@ function Get-NUnit3TestSuiteInfo { } if ($TestSuite -is [Pester.Container]) { - switch ($TestSuite.Type) { - 'File' { - $name = $TestSuite.Item.Name - $fullname = $TestSuite.Item.FullName - break - } - 'ScriptBlock' { - $name = $TestSuite.Item.Id.Guid - $fullname = "$($TestSuite.Item.File):$($TestSuite.Item.StartPosition.StartLine)" - break - } - default { - throw "Container type '$($TestSuite.Type)' is not supported." - } + $name = switch ($TestSuite.Type) { + 'File' { $TestSuite.Item.Name; break } + 'ScriptBlock' { $TestSuite.Item.Id.Guid; break } + default { throw "Container type '$($TestSuite.Type)' is not supported." } } + $fullname = $TestSuite.Name $classname = '' } else { diff --git a/tst/Pester.RSpec.TestResults.JUnit4.ts.ps1 b/tst/Pester.RSpec.TestResults.JUnit4.ts.ps1 index b0b7ce497..1f3a3d27e 100644 --- a/tst/Pester.RSpec.TestResults.JUnit4.ts.ps1 +++ b/tst/Pester.RSpec.TestResults.JUnit4.ts.ps1 @@ -16,7 +16,7 @@ $global:PesterPreference = @{ } function Get-ScriptBlockName ($ScriptBlock) { - "$($ScriptBlock.File):$($ScriptBlock.StartPosition.StartLine)" + ":$($ScriptBlock.File):$($ScriptBlock.StartPosition.StartLine)" } $schemaPath = (Get-Module -Name Pester).Path | Split-Path | Join-Path -ChildPath 'schemas/JUnit4/junit_schema_4.xsd' From 62b94dfb26f19d27bc985b732df063cf353ab8c5 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sun, 28 Apr 2024 14:30:59 +0000 Subject: [PATCH 2/6] Validate Container Type in setter --- src/csharp/Pester/Container.cs | 17 ++++++++++++++++- src/csharp/Pester/ContainerInfo.cs | 7 ++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/csharp/Pester/Container.cs b/src/csharp/Pester/Container.cs index 5777ccb63..b3532fe11 100644 --- a/src/csharp/Pester/Container.cs +++ b/src/csharp/Pester/Container.cs @@ -48,7 +48,12 @@ public static Container CreateFromFile(FileInfo file) } public string Name { get => ToStringConverter.ContainerItemToString(Type, Item); } - public string Type { get; set; } + private string _type = Constants.File; + public string Type + { + get => _type; + set => SetContainerType(ref _type, value); + } public object Item { get; set; } public object Data { get; set; } public List Blocks { get; set; } = new List(); @@ -77,5 +82,15 @@ public override string ToString() { return ToStringConverter.ContainerToString(this); } + + internal static void SetContainerType(ref string property, string value) + { + property = value.ToLower() switch + { + "file" => Constants.File, + "scriptblock" => Constants.ScriptBlock, + _ => throw new ArgumentOutOfRangeException("value", $"Type must be '{Constants.File}' or '{Constants.ScriptBlock}'"), + }; + } } } diff --git a/src/csharp/Pester/ContainerInfo.cs b/src/csharp/Pester/ContainerInfo.cs index 40a90ba29..00dfa2743 100644 --- a/src/csharp/Pester/ContainerInfo.cs +++ b/src/csharp/Pester/ContainerInfo.cs @@ -11,7 +11,12 @@ public static ContainerInfo Create() return new ContainerInfo(); } - public string Type { get; set; } = "File"; + private string _type = Constants.File; + public string Type + { + get => _type; + set => Container.SetContainerType(ref _type, value); + } public object Item { get; set; } public object Data { get; set; } From 5dcc3eade53e33c3925da82e085bf4e52f88cca5 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sun, 28 Apr 2024 15:40:59 +0000 Subject: [PATCH 3/6] Remove redundant container type validation --- src/functions/Output.ps1 | 18 +----------------- src/functions/TestResults.JUnit4.ps1 | 10 +++------- src/functions/TestResults.NUnit25.ps1 | 9 +++------ src/functions/TestResults.NUnit3.ps1 | 9 +++++++-- src/functions/TestResults.ps1 | 12 ++++++------ 5 files changed, 20 insertions(+), 38 deletions(-) diff --git a/src/functions/Output.ps1 b/src/functions/Output.ps1 index 794fae9b7..a1d70efab 100644 --- a/src/functions/Output.ps1 +++ b/src/functions/Output.ps1 @@ -211,14 +211,6 @@ function Write-PesterStart { ScriptBlocks = 0 } - foreach ($c in $Context.Containers) { - switch ($c.Type) { - "File" { $null = $hash.Files.Add($c.Item.FullName) } - "ScriptBlock" { $null = $hash.ScriptBlocks++ } - Default { throw "$($c.Type) is not supported." } - } - } - $moduleInfo = $MyInvocation.MyCommand.ScriptBlock.Module $moduleVersion = $moduleInfo.Version.ToString() if ($moduleInfo.PrivateData.PSData.Prerelease) { @@ -306,7 +298,7 @@ function ConvertTo-PesterResult { function Write-PesterReport { param ( [Parameter(mandatory = $true, valueFromPipeline = $true)] - $RunResult + [Pester.Run] $RunResult ) # if(-not ($PesterState.Show | Has-Flag Summary)) { return } @@ -386,10 +378,6 @@ function Write-PesterReport { if (0 -lt $RunResult.FailedContainersCount) { $cs = foreach ($container in $RunResult.FailedContainers) { - if ($container.Type -notin 'File', 'ScriptBlock') { - throw "Container type '$($container.Type)' is not supported." - } - " - $($container.Name)" } Write-PesterHostMessage ('Container failed: {0}' -f $RunResult.FailedContainersCount) -Foreground $ReportTheme.Fail @@ -614,10 +602,6 @@ function Get-WriteScreenPlugin ($Verbosity) { param ($Context) if ('Failed' -eq $Context.Block.Result) { - if ($Context.BlockContainer.Type -notin 'File', 'ScriptBlock') { - throw "Context.BlockContainer type '$($Context.BlockContainer.Type)' is not supported." - } - $errorHeader = "[-] Discovery in $($Context.BlockContainer) failed with:" $formatErrorParams = @{ diff --git a/src/functions/TestResults.JUnit4.ps1 b/src/functions/TestResults.JUnit4.ps1 index 43185e5a4..63396c303 100644 --- a/src/functions/TestResults.JUnit4.ps1 +++ b/src/functions/TestResults.JUnit4.ps1 @@ -1,5 +1,5 @@ function Write-JUnitReport { - param($Result, [System.Xml.XmlWriter] $XmlWriter) + param([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) # Write the XML Declaration $XmlWriter.WriteStartDocument($false) @@ -24,7 +24,7 @@ function Write-JUnitTestResultAttributes { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')] - param($Result, [System.Xml.XmlWriter] $XmlWriter) + param([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) $XmlWriter.WriteAttributeString('xmlns', 'xsi', $null, 'http://www.w3.org/2001/XMLSchema-instance') $XmlWriter.WriteAttributeString('xsi', 'noNamespaceSchemaLocation', [Xml.Schema.XmlSchema]::InstanceNamespace , 'junit_schema_4.xsd') @@ -38,14 +38,10 @@ function Write-JUnitTestResultAttributes { function Write-JUnitTestSuiteElements { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')] - param($Container, [System.Xml.XmlWriter] $XmlWriter, [uint16] $Id) + param([Pester.Container] $Container, [System.Xml.XmlWriter] $XmlWriter, [uint16] $Id) $XmlWriter.WriteStartElement('testsuite') - if ($container.Type -notin 'File', 'ScriptBlock') { - throw "Container type '$($container.Type)' is not supported." - } - Write-JUnitTestSuiteAttributes -Action $Container -XmlWriter $XmlWriter -Package $container.Name -Id $Id $testResults = [Pester.Factory]::CreateCollection() diff --git a/src/functions/TestResults.NUnit25.ps1 b/src/functions/TestResults.NUnit25.ps1 index ec36e0fb4..6a7af0506 100644 --- a/src/functions/TestResults.NUnit25.ps1 +++ b/src/functions/TestResults.NUnit25.ps1 @@ -1,5 +1,5 @@ function Write-NUnitReport { - param($Result, [System.Xml.XmlWriter] $XmlWriter) + param([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) # Write the XML Declaration $XmlWriter.WriteStartDocument($false) @@ -14,7 +14,7 @@ function Write-NUnitTestResultAttributes { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')] - param($Result, [System.Xml.XmlWriter] $XmlWriter) + param([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) $XmlWriter.WriteAttributeString('xmlns', 'xsi', $null, 'http://www.w3.org/2001/XMLSchema-instance') $XmlWriter.WriteAttributeString('xsi', 'noNamespaceSchemaLocation', [Xml.Schema.XmlSchema]::InstanceNamespace , 'nunit_schema_2.5.xsd') @@ -33,7 +33,7 @@ function Write-NUnitTestResultAttributes { function Write-NUnitTestResultChildNodes { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')] - param($Result, [System.Xml.XmlWriter] $XmlWriter) + param([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) Write-NUnitEnvironmentInformation -Result $Result -XmlWriter $XmlWriter Write-NUnitCultureInformation -Result $Result -XmlWriter $XmlWriter @@ -53,9 +53,6 @@ function Write-NUnitTestResultChildNodes { continue } - if ($container.Type -notin 'File', 'ScriptBlock') { - throw "Container type '$($container.Type)' is not supported." - } Write-NUnitTestSuiteElements -XmlWriter $XmlWriter -Node $container -Path $container.Name } diff --git a/src/functions/TestResults.NUnit3.ps1 b/src/functions/TestResults.NUnit3.ps1 index e4557644e..539486750 100644 --- a/src/functions/TestResults.NUnit3.ps1 +++ b/src/functions/TestResults.NUnit3.ps1 @@ -1,6 +1,6 @@ # NUnit3 schema docs: https://docs.nunit.org/articles/nunit/technical-notes/usage/Test-Result-XML-Format.html -function Write-NUnit3Report($Result, [System.Xml.XmlWriter] $XmlWriter) { +function Write-NUnit3Report([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) { # Write the XML Declaration $XmlWriter.WriteStartDocument($false) @@ -19,8 +19,13 @@ function Write-NUnit3Report($Result, [System.Xml.XmlWriter] $XmlWriter) { } function Write-NUnit3TestRunAttributes { +<<<<<<< Updated upstream [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')] param($Result, [System.Xml.XmlWriter] $XmlWriter) +======= + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')] + param([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) +>>>>>>> Stashed changes $XmlWriter.WriteAttributeString('id', '0') $XmlWriter.WriteAttributeString('name', $Result.Configuration.TestResult.TestSuiteName.Value) # required attr. in schema, but not in docs or nunit-console output... @@ -42,7 +47,7 @@ function Write-NUnit3TestRunAttributes { function Write-NUnit3TestRunChildNode { param( - $Result, + [Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter ) diff --git a/src/functions/TestResults.ps1 b/src/functions/TestResults.ps1 index b5ff0b3f0..d700754fe 100644 --- a/src/functions/TestResults.ps1 +++ b/src/functions/TestResults.ps1 @@ -20,7 +20,7 @@ function Export-PesterResult { param ( - $Result, + [Pester.Run] $Result, [string] $Path, [string] $Format ) @@ -85,7 +85,7 @@ function Export-NUnitReport { #> param ( [parameter(Mandatory = $true, ValueFromPipeline = $true)] - $Result, + [Pester.Run] $Result, [parameter(Mandatory = $true)] [String] $Path, @@ -135,7 +135,7 @@ function Export-JUnitReport { #> param ( [parameter(Mandatory = $true, ValueFromPipeline = $true)] - $Result, + [Pester.Run] $Result, [parameter(Mandatory = $true)] [String] $Path @@ -147,7 +147,7 @@ function Export-JUnitReport { function Export-XmlReport { param ( [parameter(Mandatory = $true, ValueFromPipeline = $true)] - $Result, + [Pester.Run] $Result, [parameter(Mandatory = $true)] [String] $Path, @@ -272,7 +272,7 @@ function ConvertTo-NUnitReport { #> param ( [parameter(Mandatory = $true, ValueFromPipeline = $true)] - $Result, + [Pester.Run] $Result, [Switch] $AsString, [ValidateSet('NUnit2.5', 'NUnit3')] @@ -362,7 +362,7 @@ function ConvertTo-JUnitReport { #> param ( [parameter(Mandatory = $true, ValueFromPipeline = $true)] - $Result, + [Pester.Run] $Result, [Switch] $AsString ) From 04e4a99230b032767a9a608b0c2037708ec9017a Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sun, 28 Apr 2024 15:41:50 +0000 Subject: [PATCH 4/6] Cleanup unused code --- src/functions/Output.ps1 | 39 ++++++++------------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/src/functions/Output.ps1 b/src/functions/Output.ps1 index a1d70efab..48dedbd14 100644 --- a/src/functions/Output.ps1 +++ b/src/functions/Output.ps1 @@ -200,17 +200,6 @@ function Write-PesterStart { $Context ) process { - # if (-not ( $Context.Show | Has-Flag 'All, Fails, Header')) { - # return - # } - - $OFS = $ReportStrings.MessageOfs - - $hash = @{ - Files = [System.Collections.Generic.List[object]]@() - ScriptBlocks = 0 - } - $moduleInfo = $MyInvocation.MyCommand.ScriptBlock.Module $moduleVersion = $moduleInfo.Version.ToString() if ($moduleInfo.PrivateData.PSData.Prerelease) { @@ -218,18 +207,6 @@ function Write-PesterStart { } $message = $ReportStrings.VersionMessage -f $moduleVersion - # todo write out filters that are applied - # if ($PesterState.TestNameFilter) { - # $message += $ReportStrings.FilterMessage -f "$($PesterState.TestNameFilter)" - # } - # if ($PesterState.ScriptBlockFilter) { - # $m = $(foreach ($m in $PesterState.ScriptBlockFilter) { "$($m.Path):$($m.Line)" }) -join ", " - # $message += $ReportStrings.FilterMessage -f $m - # } - # if ($PesterState.TagFilter) { - # $message += $ReportStrings.TagMessage -f "$($PesterState.TagFilter)" - # } - Write-PesterHostMessage -ForegroundColor $ReportTheme.Discovery $message } } @@ -245,15 +222,15 @@ function ConvertTo-PesterResult { $testResult = @{ Name = $Name Time = $time - FailureMessage = "" - StackTrace = "" + FailureMessage = '' + StackTrace = '' ErrorRecord = $null Success = $false - Result = "Failed" + Result = 'Failed' } if (-not $ErrorRecord) { - $testResult.Result = "Passed" + $testResult.Result = 'Passed' $testResult.Success = $true return $testResult } @@ -270,13 +247,13 @@ function ConvertTo-PesterResult { if (-not $Pester.Strict) { switch ($ErrorRecord.FullyQualifiedErrorID) { PesterTestInconclusive { - $testResult.Result = "Inconclusive"; break; + $testResult.Result = 'Inconclusive'; break; } PesterTestPending { - $testResult.Result = "Pending"; break; + $testResult.Result = 'Pending'; break; } PesterTestSkipped { - $testResult.Result = "Skipped"; break; + $testResult.Result = 'Skipped'; break; } } } @@ -372,7 +349,7 @@ function Write-PesterReport { Write-PesterHostMessage ($ReportStrings.TestsNotRun -f $RunResult.NotRunCount) -Foreground $NotRun if (0 -lt $RunResult.FailedBlocksCount) { - Write-PesterHostMessage ("BeforeAll \ AfterAll failed: {0}" -f $RunResult.FailedBlocksCount) -Foreground $ReportTheme.Fail + Write-PesterHostMessage ('BeforeAll \ AfterAll failed: {0}' -f $RunResult.FailedBlocksCount) -Foreground $ReportTheme.Fail Write-PesterHostMessage ($(foreach ($b in $RunResult.FailedBlocks) { " - $($b.Path -join '.')" }) -join [Environment]::NewLine) -Foreground $ReportTheme.Fail } From a08eb619e6294fbbcc6d7e2d74e3ba16c13dd10d Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sun, 28 Apr 2024 15:45:36 +0000 Subject: [PATCH 5/6] Remove merge conflict artifacts --- src/functions/TestResults.NUnit3.ps1 | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/functions/TestResults.NUnit3.ps1 b/src/functions/TestResults.NUnit3.ps1 index 539486750..6c571de6e 100644 --- a/src/functions/TestResults.NUnit3.ps1 +++ b/src/functions/TestResults.NUnit3.ps1 @@ -19,13 +19,8 @@ function Write-NUnit3Report([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWri } function Write-NUnit3TestRunAttributes { -<<<<<<< Updated upstream - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')] - param($Result, [System.Xml.XmlWriter] $XmlWriter) -======= [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')] param([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) ->>>>>>> Stashed changes $XmlWriter.WriteAttributeString('id', '0') $XmlWriter.WriteAttributeString('name', $Result.Configuration.TestResult.TestSuiteName.Value) # required attr. in schema, but not in docs or nunit-console output... From 20d72f6affc577470983a53c7358f9c05d2b3e14 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sun, 28 Apr 2024 16:27:02 +0000 Subject: [PATCH 6/6] Refactor --- src/csharp/Pester/ToStringConverter.cs | 29 ++++++++------------------ src/functions/TestResults.NUnit3.ps1 | 2 +- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/csharp/Pester/ToStringConverter.cs b/src/csharp/Pester/ToStringConverter.cs index 1b0673da2..7ac6e7d43 100644 --- a/src/csharp/Pester/ToStringConverter.cs +++ b/src/csharp/Pester/ToStringConverter.cs @@ -18,32 +18,21 @@ static string ResultToString(string result) }; } - internal static string ContainerItemToString(string Type, object Item) + internal static string ContainerItemToString(string type, object item) { - string path; - switch (Type) + return type switch { - case Constants.File: - path = Item is FileInfo f ? f.FullName : Item.ToString(); - break; - case Constants.ScriptBlock: - path = ""; - if (Item is ScriptBlock s && !string.IsNullOrWhiteSpace(s.File)) - { - path += $":{s.File}:{s.StartPosition.StartLine}"; - } - break; - default: - path = $"<{Type}>"; - break; - } - - return path; + Constants.File => item is FileInfo f ? f.FullName : item.ToString(), + Constants.ScriptBlock => item is ScriptBlock s && !string.IsNullOrWhiteSpace(s.File) + ? $":{s.File}:{s.StartPosition.StartLine}" + : "", + _ => $"<{type}>" + }; } internal static string ContainerToString(Container container) { - return $"{ResultToString(container.Result)} {ContainerItemToString(container.Type, container.Item)}"; + return $"{ResultToString(container.Result)} {container.Name}"; } internal static string ContainerInfoToString(ContainerInfo containerInfo) diff --git a/src/functions/TestResults.NUnit3.ps1 b/src/functions/TestResults.NUnit3.ps1 index 6c571de6e..ecb5d6fa4 100644 --- a/src/functions/TestResults.NUnit3.ps1 +++ b/src/functions/TestResults.NUnit3.ps1 @@ -19,7 +19,7 @@ function Write-NUnit3Report([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWri } function Write-NUnit3TestRunAttributes { - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')] param([Pester.Run] $Result, [System.Xml.XmlWriter] $XmlWriter) $XmlWriter.WriteAttributeString('id', '0')