Skip to content

Commit 3c56ee1

Browse files
committed
Adapt tests so that they pass the first time when being run the first time. There is more cleanup needed in this class in general but this is the minimum viable change
1 parent c7ac69e commit 3c56ee1

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

Tests/Engine/ModuleDependencyHandler.tests.ps1

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ Describe "Resolve DSC Resource Dependency" {
3333
$newEnv[$index].Value | Should -Be $oldEnv[$index].Value
3434
}
3535
}
36+
37+
Function Get-LocalAppDataFolder
38+
{
39+
if ($IsLinux -or $IsMacOS) { $env:HOME } else { $env:LOCALAPPDATA }
40+
}
3641
}
3742
AfterAll {
3843
if ( $skipTest ) { return }
@@ -58,11 +63,7 @@ Describe "Resolve DSC Resource Dependency" {
5863
$expectedPath = [System.IO.Path]::GetTempPath()
5964
$depHandler.TempPath | Should -Be $expectedPath
6065

61-
$expectedLocalAppDataPath = $env:LOCALAPPDATA
62-
if ($IsLinux -or $IsMacOS) {
63-
$expectedLocalAppDataPath = $env:HOME
64-
}
65-
$depHandler.LocalAppDataPath | Should -Be $expectedLocalAppDataPath
66+
$depHandler.LocalAppDataPath | Should -Be (Get-LocalAppDataFolder)
6667

6768
$expectedModuleRepository = "PSGallery"
6869
$depHandler.ModuleRepository | Should -Be $expectedModuleRepository
@@ -188,16 +189,19 @@ Describe "Resolve DSC Resource Dependency" {
188189
$modulePath = "$(Split-Path $directory)\Rules\DSCResourceModule\DSCResources\$moduleName"
189190

190191
# Save the current environment variables
191-
$oldLocalAppDataPath = $env:LOCALAPPDATA
192+
$oldLocalAppDataPath = Get-LocalAppDataFolder
192193
$oldTempPath = $env:TEMP
193194
$savedPSModulePath = $env:PSModulePath
194195

195196
# set the environment variables
196-
$tempPath = Join-Path $oldTempPath ([guid]::NewGUID()).ToString()
197+
$tempPath = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGUID()).ToString()
197198
$newLocalAppDataPath = Join-Path $tempPath "LocalAppData"
198199
$newTempPath = Join-Path $tempPath "Temp"
199-
$env:LOCALAPPDATA = $newLocalAppDataPath
200-
$env:TEMP = $newTempPath
200+
if (-not ($IsLinux -or $IsMacOS))
201+
{
202+
$env:LOCALAPPDATA = $newLocalAppDataPath
203+
$env:TEMP = $newTempPath
204+
}
201205

202206
# create the temporary directories
203207
New-Item -Type Directory -Path $newLocalAppDataPath -force
@@ -224,7 +228,6 @@ Describe "Resolve DSC Resource Dependency" {
224228
}
225229

226230
It "has a single parse error" -skip:$skipTest {
227-
# invoke script analyzer
228231
$dr = Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable analyzerErrors -ErrorAction SilentlyContinue
229232
$analyzerErrors.Count | Should -Be 0
230233
$dr |
@@ -233,14 +236,22 @@ Describe "Resolve DSC Resource Dependency" {
233236
}
234237

235238
It "Keeps PSModulePath unchanged before and after invocation" -skip:$skipTest {
236-
$dr = Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable parseErrors -ErrorAction SilentlyContinue
239+
Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable parseErrors -ErrorAction SilentlyContinue
237240
$env:PSModulePath | Should -Be $savedPSModulePath
238241
}
239242

240243
if (!$skipTest)
241244
{
242-
$env:LOCALAPPDATA = $oldLocalAppDataPath
243-
$env:TEMP = $oldTempPath
245+
if ($IsLinux -or $IsMacOS)
246+
{
247+
$env:HOME = $oldLocalAppDataPath
248+
# On Linux [System.IO.Path]::GetTempPath() does not use the TEMP env variable unlike on Windows
249+
}
250+
else
251+
{
252+
$env:LOCALAPPDATA = $oldLocalAppDataPath
253+
$env:TEMP = $oldTempPath
254+
}
244255
Remove-Item -Recurse -Path $tempModulePath -Force
245256
Remove-Item -Recurse -Path $tempPath -Force
246257
}

0 commit comments

Comments
 (0)