From 4e43780cd6624936d1c4b90d1f9b1fd634015163 Mon Sep 17 00:00:00 2001 From: PowerShell Team Bot <69177312+pwshBot@users.noreply.github.com> Date: Tue, 16 Dec 2025 12:40:31 -0800 Subject: [PATCH] [release/v7.6] Fix the DSC test by skipping `AfterAll` cleanup if the initial setup in `BeforeAll` failed (#26622) --- .../dsc/dsc.profileresource.Tests.ps1 | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/powershell/dsc/dsc.profileresource.Tests.ps1 b/test/powershell/dsc/dsc.profileresource.Tests.ps1 index f361e67ea8..cb357c7350 100644 --- a/test/powershell/dsc/dsc.profileresource.Tests.ps1 +++ b/test/powershell/dsc/dsc.profileresource.Tests.ps1 @@ -4,8 +4,10 @@ Describe "DSC PowerShell Profile Resource Tests" -Tag "CI" { BeforeAll { $DSC_ROOT = $env:DSC_ROOT + $skipCleanup = $false if (-not (Test-Path -Path $DSC_ROOT)) { + $skipCleanup = $true throw "DSC_ROOT environment variable is not set or path does not exist." } @@ -40,6 +42,10 @@ Describe "DSC PowerShell Profile Resource Tests" -Tag "CI" { New-Item -Path $testProfilePathCurrentUserAllHosts -Value $testProfileContent -Force -ItemType File } AfterAll { + if ($skipCleanup) { + return + } + # Restore original profile $testProfilePathCurrentUserCurrentHost = $PROFILE.CurrentUserCurrentHost if (Test-Path "$TestDrive/currentuser-currenthost-profile.bak") { @@ -118,16 +124,19 @@ Describe "DSC PowerShell Profile Resource Tests" -Tag "CI" { Describe "DSC PowerShell Profile resource elevated tests" -Tag "CI", 'RequireAdminOnWindows', 'RequireSudoOnUnix' { BeforeAll { $DSC_ROOT = $env:DSC_ROOT + $skipCleanup = $false if (-not (Test-Path -Path $DSC_ROOT)) { + $skipCleanup = $true throw "DSC_ROOT environment variable is not set or path does not exist." } Write-Verbose "DSC_ROOT is set to $DSC_ROOT" -Verbose + + $originalPath = $env:PATH + $pathSeparator = [System.IO.Path]::PathSeparator - $env:PATH += "$pathSeparator$DSC_ROOT" - $env:PATH += "$pathSeparator$PSHome" Write-Verbose "Updated PATH to include DSC_ROOT: $env:PATH" -Verbose @@ -151,11 +160,12 @@ Describe "DSC PowerShell Profile resource elevated tests" -Tag "CI", 'RequireAdm $testProfilePathAllUsersAllHosts = $PROFILE.AllUsersAllHosts Copy-Item -Path $testProfilePathAllUsersAllHosts -Destination "$TestDrive/allusers-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue New-Item -Path $testProfilePathAllUsersAllHosts -Value $testProfileContent -Force -ItemType File - - $originalPath = $env:PATH - $env:PATH += "$pathSeparator$PSHome" } AfterAll { + if ($skipCleanup) { + return + } + $env:PATH = $originalPath $testProfilePathAllUsersCurrentHost = $PROFILE.AllUsersCurrentHost