@@ -2,12 +2,21 @@ $directory = Split-Path -Parent $MyInvocation.MyCommand.Path
22
33Describe " Resolve DSC Resource Dependency" {
44 BeforeAll {
5- $skipTest = $false
6- if ($IsLinux -or $IsMacOS -or $ testingLibararyUsage -or ($PSversionTable.PSVersion -lt [Version ]' 5.0.0' ))
5+ $skipTest = $false # Test that require DSC to be installed
6+ if ($testingLibararyUsage -or ($PSversionTable.PSVersion -lt [Version ]' 5.0.0' ))
77 {
88 $skipTest = $true
99 return
1010 }
11+ if ($IsLinux -or $IsMacOS )
12+ {
13+ $dscIsInstalled = Test-Path / etc/ opt/ omi/ conf/ dsc/ configuration
14+ if (-not $dscIsInstalled )
15+ {
16+ $skipTest = $true
17+ }
18+ }
19+
1120 $savedPSModulePath = $env: PSModulePath
1221 $violationFileName = ' MissingDSCResource.ps1'
1322 $violationFilePath = Join-Path $directory $violationFileName
@@ -24,6 +33,11 @@ Describe "Resolve DSC Resource Dependency" {
2433 $newEnv [$index ].Value | Should - Be $oldEnv [$index ].Value
2534 }
2635 }
36+
37+ Function Get-LocalAppDataFolder
38+ {
39+ if ($IsLinux -or $IsMacOS ) { $env: HOME } else { $env: LOCALAPPDATA }
40+ }
2741 }
2842 AfterAll {
2943 if ( $skipTest ) { return }
@@ -32,7 +46,7 @@ Describe "Resolve DSC Resource Dependency" {
3246
3347 Context " Module handler class" {
3448 BeforeAll {
35- if ( $skipTest ) { return }
49+ if ($PSversionTable .PSVersion -lt [ Version ] ' 5.0.0 ' ) { return }
3650 $moduleHandlerType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler ]
3751 $oldEnvVars = Get-Item Env:\* | Sort-Object - Property Key
3852 $savedPSModulePath = $env: PSModulePath
@@ -41,16 +55,15 @@ Describe "Resolve DSC Resource Dependency" {
4155 if ( $skipTest ) { return }
4256 $env: PSModulePath = $savedPSModulePath
4357 }
44- It " Sets defaults correctly" - skip: $skipTest {
58+ It " Sets defaults correctly" - Skip:( $PSversionTable .PSVersion -lt [ Version ] ' 5.0.0 ' ) {
4559 $rsp = [runspacefactory ]::CreateRunspace()
4660 $rsp.Open ()
4761 $depHandler = $moduleHandlerType ::new($rsp )
4862
4963 $expectedPath = [System.IO.Path ]::GetTempPath()
5064 $depHandler.TempPath | Should - Be $expectedPath
5165
52- $expectedLocalAppDataPath = $env: LOCALAPPDATA
53- $depHandler.LocalAppDataPath | Should - Be $expectedLocalAppDataPath
66+ $depHandler.LocalAppDataPath | Should - Be (Get-LocalAppDataFolder )
5467
5568 $expectedModuleRepository = " PSGallery"
5669 $depHandler.ModuleRepository | Should - Be $expectedModuleRepository
@@ -65,15 +78,15 @@ Describe "Resolve DSC Resource Dependency" {
6578 $rsp.Dispose ()
6679 }
6780
68- It " Keeps the environment variables unchanged" - skip: $skipTest {
81+ It " Keeps the environment variables unchanged" - Skip:( $PSversionTable .PSVersion -lt [ Version ] ' 5.0.0 ' ) {
6982 Test-EnvironmentVariables ($oldEnvVars )
7083 }
7184
72- It " Throws if runspace is null" - skip: $skipTest {
85+ It " Throws if runspace is null" - Skip:( $PSversionTable .PSVersion -lt [ Version ] ' 5.0.0 ' ) {
7386 {$moduleHandlerType ::new($null )} | Should - Throw
7487 }
7588
76- It " Throws if runspace is not opened" - skip: $skipTest {
89+ It " Throws if runspace is not opened" - Skip:( $PSversionTable .PSVersion -lt [ Version ] ' 5.0.0 ' ) {
7790 $rsp = [runspacefactory ]::CreateRunspace()
7891 {$moduleHandlerType ::new($rsp )} | Should - Throw
7992 $rsp.Dispose ()
@@ -176,16 +189,19 @@ Describe "Resolve DSC Resource Dependency" {
176189 $modulePath = " $ ( Split-Path $directory ) \Rules\DSCResourceModule\DSCResources\$moduleName "
177190
178191 # Save the current environment variables
179- $oldLocalAppDataPath = $ env: LOCALAPPDATA
192+ $oldLocalAppDataPath = Get-LocalAppDataFolder
180193 $oldTempPath = $env: TEMP
181194 $savedPSModulePath = $env: PSModulePath
182195
183196 # set the environment variables
184- $tempPath = Join-Path $oldTempPath ([guid ]::NewGUID()).ToString()
197+ $tempPath = Join-Path ([ System.IO.Path ]::GetTempPath()) ([guid ]::NewGUID()).ToString()
185198 $newLocalAppDataPath = Join-Path $tempPath " LocalAppData"
186199 $newTempPath = Join-Path $tempPath " Temp"
187- $env: LOCALAPPDATA = $newLocalAppDataPath
188- $env: TEMP = $newTempPath
200+ if (-not ($IsLinux -or $IsMacOS ))
201+ {
202+ $env: LOCALAPPDATA = $newLocalAppDataPath
203+ $env: TEMP = $newTempPath
204+ }
189205
190206 # create the temporary directories
191207 New-Item - Type Directory - Path $newLocalAppDataPath - force
@@ -212,7 +228,6 @@ Describe "Resolve DSC Resource Dependency" {
212228 }
213229
214230 It " has a single parse error" - skip:$skipTest {
215- # invoke script analyzer
216231 $dr = Invoke-ScriptAnalyzer - Path $violationFilePath - ErrorVariable analyzerErrors - ErrorAction SilentlyContinue
217232 $analyzerErrors.Count | Should - Be 0
218233 $dr |
@@ -221,14 +236,22 @@ Describe "Resolve DSC Resource Dependency" {
221236 }
222237
223238 It " Keeps PSModulePath unchanged before and after invocation" - skip:$skipTest {
224- $dr = Invoke-ScriptAnalyzer - Path $violationFilePath - ErrorVariable parseErrors - ErrorAction SilentlyContinue
239+ Invoke-ScriptAnalyzer - Path $violationFilePath - ErrorVariable parseErrors - ErrorAction SilentlyContinue
225240 $env: PSModulePath | Should - Be $savedPSModulePath
226241 }
227242
228243 if (! $skipTest )
229244 {
230- $env: LOCALAPPDATA = $oldLocalAppDataPath
231- $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+ }
232255 Remove-Item - Recurse - Path $tempModulePath - Force
233256 Remove-Item - Recurse - Path $tempPath - Force
234257 }
0 commit comments