Skip to content

Commit b14cd03

Browse files
authored
Fix the DSC test by skipping AfterAll cleanup if the initial setup in BeforeAll failed (#26621)
1 parent cd06286 commit b14cd03

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

test/powershell/dsc/dsc.profileresource.Tests.ps1

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
Describe "DSC PowerShell Profile Resource Tests" -Tag "CI" {
55
BeforeAll {
66
$DSC_ROOT = $env:DSC_ROOT
7+
$skipCleanup = $false
78

89
if (-not (Test-Path -Path $DSC_ROOT)) {
10+
$skipCleanup = $true
911
throw "DSC_ROOT environment variable is not set or path does not exist."
1012
}
1113

@@ -40,6 +42,10 @@ Describe "DSC PowerShell Profile Resource Tests" -Tag "CI" {
4042
New-Item -Path $testProfilePathCurrentUserAllHosts -Value $testProfileContent -Force -ItemType File
4143
}
4244
AfterAll {
45+
if ($skipCleanup) {
46+
return
47+
}
48+
4349
# Restore original profile
4450
$testProfilePathCurrentUserCurrentHost = $PROFILE.CurrentUserCurrentHost
4551
if (Test-Path "$TestDrive/currentuser-currenthost-profile.bak") {
@@ -118,16 +124,19 @@ Describe "DSC PowerShell Profile Resource Tests" -Tag "CI" {
118124
Describe "DSC PowerShell Profile resource elevated tests" -Tag "CI", 'RequireAdminOnWindows', 'RequireSudoOnUnix' {
119125
BeforeAll {
120126
$DSC_ROOT = $env:DSC_ROOT
127+
$skipCleanup = $false
121128

122129
if (-not (Test-Path -Path $DSC_ROOT)) {
130+
$skipCleanup = $true
123131
throw "DSC_ROOT environment variable is not set or path does not exist."
124132
}
125133

126134
Write-Verbose "DSC_ROOT is set to $DSC_ROOT" -Verbose
127-
$pathSeparator = [System.IO.Path]::PathSeparator
128135

129-
$env:PATH += "$pathSeparator$DSC_ROOT"
136+
$originalPath = $env:PATH
130137

138+
$pathSeparator = [System.IO.Path]::PathSeparator
139+
$env:PATH += "$pathSeparator$DSC_ROOT"
131140
$env:PATH += "$pathSeparator$PSHome"
132141

133142
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
151160
$testProfilePathAllUsersAllHosts = $PROFILE.AllUsersAllHosts
152161
Copy-Item -Path $testProfilePathAllUsersAllHosts -Destination "$TestDrive/allusers-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue
153162
New-Item -Path $testProfilePathAllUsersAllHosts -Value $testProfileContent -Force -ItemType File
154-
155-
$originalPath = $env:PATH
156-
$env:PATH += "$pathSeparator$PSHome"
157163
}
158164
AfterAll {
165+
if ($skipCleanup) {
166+
return
167+
}
168+
159169
$env:PATH = $originalPath
160170

161171
$testProfilePathAllUsersCurrentHost = $PROFILE.AllUsersCurrentHost

0 commit comments

Comments
 (0)