Skip to content

Commit b66dfa5

Browse files
CopilotTrenly
andcommitted
Track which stub functions were created in BeforeAll and only remove those in AfterAll
Agent-Logs-Url: https://github.com/Trenly/winget-pkgs/sessions/d5eba23a-35fe-45d0-bef7-537b77b51ba8 Co-authored-by: Trenly <12611259+Trenly@users.noreply.github.com>
1 parent e546495 commit b66dfa5

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Tools/Modules/YamlCreate/YamlCreate.InstallerDetection/YamlCreate.InstallerDetection.Tests.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,39 @@ BeforeAll {
55

66
# Create stub functions for external dependencies that may not be available
77
# These are typically provided by external modules like MSI or Windows SDK tools
8+
$Script:AddedGetMSITable = $false
89
if (-not (Get-Command Get-MSITable -ErrorAction SilentlyContinue)) {
910
function Global:Get-MSITable {
1011
param([string]$Path)
1112
return $null
1213
}
14+
$Script:AddedGetMSITable = $true
1315
}
1416

17+
$Script:AddedGetMSIProperty = $false
1518
if (-not (Get-Command Get-MSIProperty -ErrorAction SilentlyContinue)) {
1619
function Global:Get-MSIProperty {
1720
param([string]$Path, [string]$Property)
1821
return $null
1922
}
23+
$Script:AddedGetMSIProperty = $true
2024
}
2125

26+
$Script:AddedGetWin32ModuleResource = $false
2227
if (-not (Get-Command Get-Win32ModuleResource -ErrorAction SilentlyContinue)) {
2328
function Global:Get-Win32ModuleResource {
2429
param([string]$Path, [switch]$DontLoadResource)
2530
return @()
2631
}
32+
$Script:AddedGetWin32ModuleResource = $true
2733
}
2834
}
2935

3036
AfterAll {
31-
# Clean up stub functions created in BeforeAll to prevent them from persisting in the session
32-
Remove-Item Function:\Get-MSITable -ErrorAction SilentlyContinue
33-
Remove-Item Function:\Get-MSIProperty -ErrorAction SilentlyContinue
34-
Remove-Item Function:\Get-Win32ModuleResource -ErrorAction SilentlyContinue
37+
# Clean up stub functions that were created in BeforeAll to prevent them from persisting in the session
38+
if ($Script:AddedGetMSITable) { Remove-Item Function:\Get-MSITable -ErrorAction SilentlyContinue }
39+
if ($Script:AddedGetMSIProperty) { Remove-Item Function:\Get-MSIProperty -ErrorAction SilentlyContinue }
40+
if ($Script:AddedGetWin32ModuleResource) { Remove-Item Function:\Get-Win32ModuleResource -ErrorAction SilentlyContinue }
3541
}
3642

3743
Describe 'YamlCreate.InstallerDetection Module' {

0 commit comments

Comments
 (0)