From acaa82941ebdc61ce28c64c3c5b04b4cf7660186 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 14:46:31 +0000 Subject: [PATCH] Fix coverage collapsing to 2% on Windows PowerShell 5.1 The new per-function test files imported the source module with -Force while the staged build was already loaded. Windows PowerShell 5.1 can't load Watch.types.ps1xml over the existing type data ("The member ToString is already present"), so every one of those files failed Discovery and only Watch.Tests.ps1 ran: coverage 2.3%. - Remove any loaded Watch module before importing it in each test file - Help.Tests.ps1: reuse the already loaded module instead of re-importing it - Test task: fail the build on failed containers/blocks, not just failed tests, so a Discovery failure can't pass silently again Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MFNjHmNmZhLMjcqfcoYzw6 --- Build/Tasks/Test.ps1 | 6 ++++++ Tests/Common/Help.Tests.ps1 | 3 ++- Tests/Private/Get-KeyedChange.Tests.ps1 | 3 +++ Tests/Private/Get-PipelineScriptBlock.Tests.ps1 | 3 +++ Tests/Private/Get-PipelineText.Tests.ps1 | 3 +++ Tests/Private/Get-PropertyChange.Tests.ps1 | 3 +++ Tests/Private/Get-UpstreamPipelineText.Tests.ps1 | 3 +++ Tests/Private/Get-WatchedProperty.Tests.ps1 | 3 +++ Tests/Private/Invoke-WatchedCommand.Tests.ps1 | 3 +++ Tests/Private/New-WildcardMatcher.Tests.ps1 | 3 +++ Tests/Private/Resolve-WatchedProperty.Tests.ps1 | 3 +++ Tests/Private/Skip-ExcludedItem.Tests.ps1 | 3 +++ Tests/Public/Watch-Command.Tests.ps1 | 3 +++ 13 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Build/Tasks/Test.ps1 b/Build/Tasks/Test.ps1 index d5bc29b..e0434e2 100644 --- a/Build/Tasks/Test.ps1 +++ b/Build/Tasks/Test.ps1 @@ -27,6 +27,12 @@ Task 'Test' -Depends 'ImportStagingModule' { throw "Failed '$($TestResults.FailedCount)' tests, build failed" } + # Also fail if a test file or block failed outside a test (e.g. during Discovery). Those tests never run, + # so FailedCount misses them and the only symptom would otherwise be a collapsed coverage figure. + if ($TestResults.FailedContainersCount -gt 0 -or $TestResults.FailedBlocksCount -gt 0) { + throw "Failed '$($TestResults.FailedContainersCount)' test files and '$($TestResults.FailedBlocksCount)' blocks, build failed" + } + # Surface the coverage result as a pipeline output variable so a later stage (which runs in a # separate, discarded workspace) can apply it to README.md without re-running the tests. if ($env:GITHUB_OUTPUT) { diff --git a/Tests/Common/Help.Tests.ps1 b/Tests/Common/Help.Tests.ps1 index 9002c2c..1a1d8f2 100644 --- a/Tests/Common/Help.Tests.ps1 +++ b/Tests/Common/Help.Tests.ps1 @@ -11,7 +11,8 @@ if (-not $env:BHModulePath) { $env:BHModulePath = Join-Path $env:BHProjectPath $ if (-not $env:BHPSModuleManifest) { $env:BHPSModuleManifest = Join-Path $env:BHModulePath "$env:BHProjectName.psd1" } # Import module -if (-not (Get-Module -Name $env:BHProjectName -ListAvailable)) { +# Reuse the module if it's already loaded (e.g. the staged build), as re-importing it fails on Windows PowerShell 5.1. +if (-not (Get-Module -Name $env:BHProjectName)) { Import-Module -Name $env:BHPSModuleManifest -ErrorAction 'Stop' -Force } $commands = Get-Command -Module $env:BHProjectName -CommandType Cmdlet, Function -ErrorAction 'Stop' # Not alias diff --git a/Tests/Private/Get-KeyedChange.Tests.ps1 b/Tests/Private/Get-KeyedChange.Tests.ps1 index 3be1023..6184682 100644 --- a/Tests/Private/Get-KeyedChange.Tests.ps1 +++ b/Tests/Private/Get-KeyedChange.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Get-KeyedChange PS$PSVersion" { diff --git a/Tests/Private/Get-PipelineScriptBlock.Tests.ps1 b/Tests/Private/Get-PipelineScriptBlock.Tests.ps1 index 6a74df5..c2c8ecf 100644 --- a/Tests/Private/Get-PipelineScriptBlock.Tests.ps1 +++ b/Tests/Private/Get-PipelineScriptBlock.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Get-PipelineScriptBlock PS$PSVersion" { diff --git a/Tests/Private/Get-PipelineText.Tests.ps1 b/Tests/Private/Get-PipelineText.Tests.ps1 index 32b8988..35896f0 100644 --- a/Tests/Private/Get-PipelineText.Tests.ps1 +++ b/Tests/Private/Get-PipelineText.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Get-PipelineText PS$PSVersion" { diff --git a/Tests/Private/Get-PropertyChange.Tests.ps1 b/Tests/Private/Get-PropertyChange.Tests.ps1 index f34aa3e..d0fc145 100644 --- a/Tests/Private/Get-PropertyChange.Tests.ps1 +++ b/Tests/Private/Get-PropertyChange.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Get-PropertyChange PS$PSVersion" { diff --git a/Tests/Private/Get-UpstreamPipelineText.Tests.ps1 b/Tests/Private/Get-UpstreamPipelineText.Tests.ps1 index c9996e3..e2cae1b 100644 --- a/Tests/Private/Get-UpstreamPipelineText.Tests.ps1 +++ b/Tests/Private/Get-UpstreamPipelineText.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Get-UpstreamPipelineText PS$PSVersion" { diff --git a/Tests/Private/Get-WatchedProperty.Tests.ps1 b/Tests/Private/Get-WatchedProperty.Tests.ps1 index 22f113c..8bc70fe 100644 --- a/Tests/Private/Get-WatchedProperty.Tests.ps1 +++ b/Tests/Private/Get-WatchedProperty.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Get-WatchedProperty PS$PSVersion" { diff --git a/Tests/Private/Invoke-WatchedCommand.Tests.ps1 b/Tests/Private/Invoke-WatchedCommand.Tests.ps1 index da5abd7..fc315d7 100644 --- a/Tests/Private/Invoke-WatchedCommand.Tests.ps1 +++ b/Tests/Private/Invoke-WatchedCommand.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Invoke-WatchedCommand PS$PSVersion" { diff --git a/Tests/Private/New-WildcardMatcher.Tests.ps1 b/Tests/Private/New-WildcardMatcher.Tests.ps1 index 4e6e5ec..1c994b6 100644 --- a/Tests/Private/New-WildcardMatcher.Tests.ps1 +++ b/Tests/Private/New-WildcardMatcher.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "New-WildcardMatcher PS$PSVersion" { diff --git a/Tests/Private/Resolve-WatchedProperty.Tests.ps1 b/Tests/Private/Resolve-WatchedProperty.Tests.ps1 index cd6cc25..6103ed6 100644 --- a/Tests/Private/Resolve-WatchedProperty.Tests.ps1 +++ b/Tests/Private/Resolve-WatchedProperty.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Resolve-WatchedProperty PS$PSVersion" { diff --git a/Tests/Private/Skip-ExcludedItem.Tests.ps1 b/Tests/Private/Skip-ExcludedItem.Tests.ps1 index da65717..a926e25 100644 --- a/Tests/Private/Skip-ExcludedItem.Tests.ps1 +++ b/Tests/Private/Skip-ExcludedItem.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Skip-ExcludedItem PS$PSVersion" { diff --git a/Tests/Public/Watch-Command.Tests.ps1 b/Tests/Public/Watch-Command.Tests.ps1 index 64ca186..a6edcbd 100644 --- a/Tests/Public/Watch-Command.Tests.ps1 +++ b/Tests/Public/Watch-Command.Tests.ps1 @@ -1,5 +1,8 @@ $PSVersion = $PSVersionTable.PSVersion.Major +# Remove any copy already loaded (e.g. the staged build) first: Windows PowerShell 5.1 can't re-import +# Watch.types.ps1xml over an existing one ("The member ToString is already present"), which fails Discovery. +Get-Module Watch | Remove-Module -Force Import-Module "$PSScriptRoot/../../Watch" -Force Describe "Watch-Command PS$PSVersion" {