Clean up analyzer warnings across app search and ISO workflows (#4794)

* Clean up analyzer warnings

* Align analyzer warning cleanup policy

* Run PSScriptAnalyzer directly in unittests workflow

* Bind lazy-rendered buttons to click handlers

* Remove WinUtil performance tracing

* Fix Win11 ISO creator temp directory reuse
This commit is contained in:
Chris Titus
2026-07-02 12:05:25 -05:00
committed by GitHub
parent b72bfb03ba
commit 58d37bb461
45 changed files with 262 additions and 439 deletions
+2
View File
@@ -39,9 +39,11 @@ BeforeAll {
process { }
}
function Get-Package {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidOverwritingBuiltInCmdlets', '', Justification='Test shim is intentionally mocked by Pester.')]
param($Name, $ErrorAction)
}
function Uninstall-Package {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidOverwritingBuiltInCmdlets', '', Justification='Test shim is intentionally mocked by Pester.')]
param(
[Parameter(ValueFromPipeline = $true)]
$InputObject,
-7
View File
@@ -32,11 +32,4 @@ Describe "Rendered asset caching" {
$taskbarScript | Should -Match 'Initialize-WinUtilTaskbarOverlayAssets -IncludeLogo \$false -IncludeStatusAssets \$true'
}
It "records individual taskbar overlay render checkpoints" {
$overlayScript = Get-Content -Path (Join-Path $script:repoRoot "functions\private\Initialize-WinUtilTaskbarOverlayAssets.ps1") -Raw
$overlayScript | Should -Match 'Taskbar logo asset rendered'
$overlayScript | Should -Match 'Taskbar checkmark asset rendered'
$overlayScript | Should -Match 'Taskbar warning asset rendered'
}
}
+4 -13
View File
@@ -21,7 +21,7 @@ Describe "Install app rendering startup contract" {
$renderScript | Should -Match 'Dispatcher\.BeginInvoke'
$renderScript | Should -Match 'Invoke-WinUtilInstallAppRenderNextBatch'
$renderScript | Should -Match 'Initialize-InstallAppEntry'
$renderScript | Should -Match 'Install app entries rendered'
$renderScript | Should -Match '\$sync\.InstallAppEntriesRendered = \$true'
}
It "does not use dispatcher timers for deferred install rendering" {
@@ -41,7 +41,6 @@ Describe "Install app rendering startup contract" {
$previousSync = Get-Variable -Name sync -Scope Global -ErrorAction SilentlyContinue
$previousInitializeAppEntry = Get-Item -Path Function:\Initialize-InstallAppEntry -ErrorAction SilentlyContinue
$previousSearch = Get-Item -Path Function:\Find-AppsByNameOrDescription -ErrorAction SilentlyContinue
$previousCheckpoint = Get-Item -Path Function:\Write-WinUtilPerformanceCheckpoint -ErrorAction SilentlyContinue
$errorCountBefore = $global:Error.Count
try {
@@ -52,7 +51,6 @@ Describe "Install app rendering startup contract" {
$global:sync.InstallAppRenderQueue = [System.Collections.Queue]::new()
$renderedApps = [System.Collections.Generic.List[string]]::new()
$checkpoints = [System.Collections.Generic.List[string]]::new()
function global:Initialize-InstallAppEntry {
param($TargetElement, $AppKey)
@@ -65,11 +63,6 @@ Describe "Install app rendering startup contract" {
throw "Search should not run for an empty search box in this test."
}
function global:Write-WinUtilPerformanceCheckpoint {
param([string]$Name)
$checkpoints.Add($Name)
}
$global:sync.InstallAppRenderQueue.Enqueue([pscustomobject]@{ TargetElement = [pscustomobject]@{}; AppKeys = @("AppA", "AppB") })
$global:sync.InstallAppRenderQueue.Enqueue([pscustomobject]@{ TargetElement = [pscustomobject]@{}; AppKeys = @("AppC") })
@@ -80,8 +73,8 @@ Describe "Install app rendering startup contract" {
$closeTimer = New-Object System.Windows.Threading.DispatcherTimer
$closeTimer.Interval = [TimeSpan]::FromMilliseconds(25)
$closeTimer.Add_Tick({
param($sender)
$timer = [System.Windows.Threading.DispatcherTimer]$sender
param($eventSender)
$timer = [System.Windows.Threading.DispatcherTimer]$eventSender
if ($global:sync.InstallAppEntriesRendered -or $timeout.Elapsed.TotalSeconds -gt 5) {
$timer.Stop()
@@ -95,7 +88,6 @@ Describe "Install app rendering startup contract" {
$global:sync.InstallAppEntriesRendered | Should -BeTrue
$global:sync.InstallAppRenderQueue.Count | Should -Be 0
@($renderedApps) | Should -Be @("AppA", "AppB", "AppC")
@($checkpoints) | Should -Contain "Install app entries rendered"
$global:Error.Count | Should -Be $errorCountBefore
} finally {
if ($previousSync) {
@@ -106,8 +98,7 @@ Describe "Install app rendering startup contract" {
foreach ($functionBackup in @(
@{ Name = "Initialize-InstallAppEntry"; Backup = $previousInitializeAppEntry },
@{ Name = "Find-AppsByNameOrDescription"; Backup = $previousSearch },
@{ Name = "Write-WinUtilPerformanceCheckpoint"; Backup = $previousCheckpoint }
@{ Name = "Find-AppsByNameOrDescription"; Backup = $previousSearch }
)) {
if ($functionBackup.Backup) {
Set-Item -Path "Function:\$($functionBackup.Name)" -Value $functionBackup.Backup.ScriptBlock
+9 -4
View File
@@ -11,9 +11,6 @@ BeforeAll {
function Initialize-WPFUI {
param([string]$TargetGridName)
}
function Write-WinUtilPerformanceCheckpoint {
param([string]$Name)
}
function Invoke-WinUtilISOCheckExistingWork { }
. (Join-Path $script:repoRoot "functions\private\Initialize-WinUtilTabContent.ps1")
@@ -32,7 +29,6 @@ Describe "Initialize-WinUtilTabContent" {
Mock Invoke-WPFUIElements { }
Mock Initialize-WPFUI { }
Mock Write-WinUtilPerformanceCheckpoint { }
}
AfterEach {
@@ -91,4 +87,13 @@ Describe "Startup lazy tab wiring" {
$tabScript | Should -Match 'Initialize-WinUtilTabContent -TabName \$sync\.currentTab'
}
It "binds generated button clicks when lazy panels are rendered" {
$rendererScript = Get-Content -Path (Join-Path $script:repoRoot "functions\public\Invoke-WPFUIElements.ps1") -Raw
$mainScript = Get-Content -Path (Join-Path $script:repoRoot "scripts\main.ps1") -Raw
$rendererScript | Should -Match '(?s)"Button"\s*\{.*\$button\.Add_Click\(\{.*Invoke-WPFButton \$Sender\.name'
$rendererScript | Should -Match '\$sync\.Buttons\.Add\(\$button\.Name\)'
$mainScript | Should -Match '\$sync\.Buttons -notcontains \$psitem'
}
}
-116
View File
@@ -1,116 +0,0 @@
#===========================================================================
# Tests - Performance tracing
#===========================================================================
BeforeAll {
$script:repoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
. (Join-Path $script:repoRoot "functions\private\Write-WinUtilLog.ps1")
. (Join-Path $script:repoRoot "tools\perf\Test-WinUtilPerformanceTrace.ps1")
. (Join-Path $script:repoRoot "tools\perf\Write-WinUtilPerformanceCheckpoint.ps1")
. (Join-Path $script:repoRoot "tools\perf\Start-WinUtilPerformanceTrace.ps1")
. (Join-Path $script:repoRoot "tools\perf\Stop-WinUtilPerformanceTrace.ps1")
}
AfterAll {
& (Join-Path $script:repoRoot "Compile.ps1")
}
Describe "WinUtil performance tracing helpers" {
BeforeEach {
$script:originalPerfEnv = $env:WINUTIL_PERF_LOG
Remove-Item Env:\WINUTIL_PERF_LOG -ErrorAction SilentlyContinue
$script:sync = [Hashtable]::Synchronized(@{})
}
AfterEach {
if ($null -eq $script:originalPerfEnv) {
Remove-Item Env:\WINUTIL_PERF_LOG -ErrorAction SilentlyContinue
} else {
$env:WINUTIL_PERF_LOG = $script:originalPerfEnv
}
Remove-Variable -Name sync -Scope Script -ErrorAction SilentlyContinue
Remove-Variable -Name originalPerfEnv -Scope Script -ErrorAction SilentlyContinue
}
It "is disabled by default" {
Test-WinUtilPerformanceTrace | Should -BeFalse
}
It "can be enabled by environment variable" {
$env:WINUTIL_PERF_LOG = "1"
Test-WinUtilPerformanceTrace | Should -BeTrue
}
It "writes startup checkpoints through the normal WinUtil log helper" {
$env:WINUTIL_PERF_LOG = "1"
Mock Write-WinUtilLog { }
Start-WinUtilPerformanceTrace
Write-WinUtilPerformanceCheckpoint -Name "XAML loaded"
Stop-WinUtilPerformanceTrace
Should -Invoke -CommandName Write-WinUtilLog -Times 3 -Exactly -ParameterFilter {
$Component -eq "StartupPerf" -and $Level -eq "DEBUG"
}
Should -Invoke -CommandName Write-WinUtilLog -Times 1 -Exactly -ParameterFilter {
$Message -like "XAML loaded:*"
}
}
}
Describe "Startup performance checkpoints" {
It "keeps performance tracing out of normal compiled output" {
& (Join-Path $script:repoRoot "Compile.ps1")
$compiledScript = Get-Content -Path (Join-Path $script:repoRoot "winutil.ps1") -Raw
$compiledScript | Should -Not -Match "Test-WinUtilPerformanceTrace"
$compiledScript | Should -Not -Match "Start-WinUtilPerformanceTrace"
$compiledScript | Should -Not -Match "Write-WinUtilPerformanceCheckpoint"
$compiledScript | Should -Not -Match "Stop-WinUtilPerformanceTrace"
$compiledScript | Should -Not -Match "PerformanceTraceEnabled"
}
It "adds config-load checkpoints only to trace compiled output" {
$compileScript = Get-Content -Path (Join-Path $script:repoRoot "Compile.ps1") -Raw
$compileScript | Should -Match '\[switch\]\$Trace'
$compileScript | Should -Match "Start-WinUtilPerformanceTrace"
$compileScript | Should -Match "Config load start"
$compileScript | Should -Match "Config load complete"
$compileScript | Should -Match "Config .* loaded"
& (Join-Path $script:repoRoot "Compile.ps1") -Trace
$compiledScript = Get-Content -Path (Join-Path $script:repoRoot "winutil.ps1") -Raw
$compiledScript | Should -Match "function Test-WinUtilPerformanceTrace"
$compiledScript | Should -Match '\$sync\.PerformanceTraceEnabled = \$true'
$compiledScript | Should -Match "Config load start"
$compiledScript | Should -Match "Config load complete"
}
It "adds runtime checkpoints for startup hotspots" {
$mainScript = Get-Content -Path (Join-Path $script:repoRoot "scripts\main.ps1") -Raw
$lazyTabScript = Get-Content -Path (Join-Path $script:repoRoot "functions\private\Initialize-WinUtilTabContent.ps1") -Raw
$runspaceScript = Get-Content -Path (Join-Path $script:repoRoot "functions\private\Initialize-WinUtilRunspacePool.ps1") -Raw
$overlayScript = Get-Content -Path (Join-Path $script:repoRoot "functions\private\Initialize-WinUtilTaskbarOverlayAssets.ps1") -Raw
$startupText = "$mainScript`n$lazyTabScript`n$runspaceScript`n$overlayScript"
foreach ($checkpoint in @(
"Runspace pool initialized",
"XAML loaded",
"Theme applied",
"Install UI created",
"Tweaks UI created",
"Features UI created",
"AppX UI created",
"Taskbar logo asset rendered",
"First content rendered"
)) {
$startupText | Should -Match ([regex]::Escape($checkpoint))
}
}
}
-6
View File
@@ -12,9 +12,6 @@ Describe "Initialize-WinUtilRunspacePool" {
BeforeEach {
$script:sync = [Hashtable]::Synchronized(@{})
$script:PARAM_OFFLINE = $false
function Write-WinUtilPerformanceCheckpoint { param($Name) }
Mock Write-WinUtilPerformanceCheckpoint { }
}
AfterEach {
@@ -29,9 +26,6 @@ Describe "Initialize-WinUtilRunspacePool" {
$firstPool.RunspacePoolStateInfo.State | Should -Be ([System.Management.Automation.Runspaces.RunspacePoolState]::Opened)
[object]::ReferenceEquals($firstPool, $secondPool) | Should -BeTrue
Should -Invoke -CommandName Write-WinUtilPerformanceCheckpoint -Times 1 -Exactly -ParameterFilter {
$Name -eq "Runspace pool initialized"
}
}
It "closes and removes the active runspace pool" {
-2
View File
@@ -20,8 +20,6 @@ BeforeAll {
$initialSessionState.Variables.Add($syncVariable)
$script:sync.runspace = [runspacefactory]::CreateRunspacePool(1, 2, $initialSessionState, $Host)
$script:sync.runspace.Open()
function Write-WinUtilPerformanceCheckpoint { param($Name) }
}
function script:Clear-WinUtilRunspaceTestContext {
-2
View File
@@ -208,8 +208,6 @@ Describe "Runspace sanity" {
. (Join-Path $script:repoRoot "functions\public\Invoke-WPFRunspace.ps1")
. (Join-Path $script:repoRoot "functions\private\Close-WinUtilRunspacePool.ps1")
. (Join-Path $script:repoRoot "functions\private\Initialize-WinUtilRunspacePool.ps1")
function Write-WinUtilPerformanceCheckpoint { param($Name) }
}
It "returns a single async handle and runs a scriptblock with arguments in the shared runspace pool" {
+22
View File
@@ -35,6 +35,7 @@ Describe "Win11 Creator setup media" {
return $functionAst.Extent.Text
}
$script:modifyFunction = Get-WinUtilFunctionText -Path $script:isoWorkflowPath -FunctionName "Invoke-WinUtilISOModify"
$script:editionIdFunction = Get-WinUtilFunctionText -Path $script:isoWorkflowPath -FunctionName "Get-WinUtilEditionIdFromName"
$script:addDriversFunction = Get-WinUtilFunctionText -Path $script:isoScriptPath -FunctionName "Add-DriversToImage"
$script:answerFileChildElementFunction = Get-WinUtilFunctionText -Path $script:isoScriptPath -FunctionName "Get-WinUtilISOScriptChildElement"
@@ -69,6 +70,27 @@ Describe "Win11 Creator setup media" {
}
}
It "starts each new ISO modification in a fresh working directory" {
foreach ($expectedText in @(
'$workDir = Join-Path $env:TEMP "WinUtil_Win11ISO_$(Get-Date -Format ''yyyyMMdd_HHmmss'')"',
'$workDir = Join-Path $env:TEMP "WinUtil_Win11ISO_$(Get-Date -Format ''yyyyMMdd_HHmmss'')_$(([guid]::NewGuid()).ToString(''N'').Substring(0, 8))"'
)) {
$script:modifyFunction | Should -Match ([regex]::Escape($expectedText))
}
$script:modifyFunction | Should -Not -Match ([regex]::Escape("Reusing existing temp directory"))
}
It "mounts the copied image file that was verified from the ISO" {
foreach ($expectedText in @(
'$sourceImageFileName = Split-Path $wimPath -Leaf',
'$localWim = Join-Path $isoContents "sources\$sourceImageFileName"',
'Copied ISO image file not found: sources\$sourceImageFileName'
)) {
$script:modifyFunction | Should -Match ([regex]::Escape($expectedText))
}
}
It "maps Windows edition names to setup edition IDs" {
. ([scriptblock]::Create($script:editionIdFunction))