diff --git a/Engine/Generic/ModuleDependencyHandler.cs b/Engine/Generic/ModuleDependencyHandler.cs index 6023cd82c..347b9a9a1 100644 --- a/Engine/Generic/ModuleDependencyHandler.cs +++ b/Engine/Generic/ModuleDependencyHandler.cs @@ -4,12 +4,12 @@ #if !PSV3 using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Management.Automation; using System.Management.Automation.Language; using System.Management.Automation.Runspaces; +using System.Runtime.InteropServices; namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic { @@ -75,7 +75,7 @@ private set #if CORECLR localAppdataPath = string.IsNullOrWhiteSpace(value) - ? Environment.GetEnvironmentVariable("LOCALAPPDATA") + ? Environment.GetEnvironmentVariable(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "LOCALAPPDATA" : "HOME") //Environment.GetEnvironmentVariable("LOCALAPPDATA") : value; #else localAppdataPath @@ -215,7 +215,7 @@ private string GetTempModulePath(string symLinkPath) private void SetupPSModulePath() { oldPSModulePath = Environment.GetEnvironmentVariable("PSModulePath"); - curPSModulePath = oldPSModulePath + ";" + tempModulePath; + curPSModulePath = oldPSModulePath + Path.PathSeparator + tempModulePath; #if CORECLR Environment.SetEnvironmentVariable("PSModulePath", curPSModulePath); #else diff --git a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 index 158af4603..3cc9351af 100644 --- a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 +++ b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 @@ -72,6 +72,10 @@ Describe "Test available parameters" { It "is a switch parameter" { $params["SaveDscDependency"].ParameterType.FullName | Should -Be "System.Management.Automation.SwitchParameter" } + + It 'does not throw when being applied against a dummy script with no DSC code' { + Invoke-ScriptAnalyzer -ScriptDefinition 'foo' -SaveDscDependency + } } } diff --git a/Tests/Engine/ModuleDependencyHandler.tests.ps1 b/Tests/Engine/ModuleDependencyHandler.tests.ps1 index a76624e4b..a392150f5 100644 --- a/Tests/Engine/ModuleDependencyHandler.tests.ps1 +++ b/Tests/Engine/ModuleDependencyHandler.tests.ps1 @@ -2,12 +2,21 @@ $directory = Split-Path -Parent $MyInvocation.MyCommand.Path Describe "Resolve DSC Resource Dependency" { BeforeAll { - $skipTest = $false - if ($IsLinux -or $IsMacOS -or $testingLibararyUsage -or ($PSversionTable.PSVersion -lt [Version]'5.0.0')) + $skipTest = $false # Test that require DSC to be installed + if ($testingLibararyUsage -or ($PSversionTable.PSVersion -lt [Version]'5.0.0')) { $skipTest = $true return } + if ($IsLinux -or $IsMacOS) + { + $dscIsInstalled = Test-Path /etc/opt/omi/conf/dsc/configuration + if (-not $dscIsInstalled) + { + $skipTest = $true + } + } + $savedPSModulePath = $env:PSModulePath $violationFileName = 'MissingDSCResource.ps1' $violationFilePath = Join-Path $directory $violationFileName @@ -24,6 +33,11 @@ Describe "Resolve DSC Resource Dependency" { $newEnv[$index].Value | Should -Be $oldEnv[$index].Value } } + + Function Get-LocalAppDataFolder + { + if ($IsLinux -or $IsMacOS) { $env:HOME } else { $env:LOCALAPPDATA } + } } AfterAll { if ( $skipTest ) { return } @@ -32,7 +46,7 @@ Describe "Resolve DSC Resource Dependency" { Context "Module handler class" { BeforeAll { - if ( $skipTest ) { return } + if ($PSversionTable.PSVersion -lt [Version]'5.0.0') { return } $moduleHandlerType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler] $oldEnvVars = Get-Item Env:\* | Sort-Object -Property Key $savedPSModulePath = $env:PSModulePath @@ -41,7 +55,7 @@ Describe "Resolve DSC Resource Dependency" { if ( $skipTest ) { return } $env:PSModulePath = $savedPSModulePath } - It "Sets defaults correctly" -skip:$skipTest { + It "Sets defaults correctly" -Skip:($PSversionTable.PSVersion -lt [Version]'5.0.0') { $rsp = [runspacefactory]::CreateRunspace() $rsp.Open() $depHandler = $moduleHandlerType::new($rsp) @@ -49,8 +63,7 @@ Describe "Resolve DSC Resource Dependency" { $expectedPath = [System.IO.Path]::GetTempPath() $depHandler.TempPath | Should -Be $expectedPath - $expectedLocalAppDataPath = $env:LOCALAPPDATA - $depHandler.LocalAppDataPath | Should -Be $expectedLocalAppDataPath + $depHandler.LocalAppDataPath | Should -Be (Get-LocalAppDataFolder) $expectedModuleRepository = "PSGallery" $depHandler.ModuleRepository | Should -Be $expectedModuleRepository @@ -65,15 +78,15 @@ Describe "Resolve DSC Resource Dependency" { $rsp.Dispose() } - It "Keeps the environment variables unchanged" -skip:$skipTest { + It "Keeps the environment variables unchanged" -Skip:($PSversionTable.PSVersion -lt [Version]'5.0.0') { Test-EnvironmentVariables($oldEnvVars) } - It "Throws if runspace is null" -skip:$skipTest { + It "Throws if runspace is null" -Skip:($PSversionTable.PSVersion -lt [Version]'5.0.0') { {$moduleHandlerType::new($null)} | Should -Throw } - It "Throws if runspace is not opened" -skip:$skipTest { + It "Throws if runspace is not opened" -Skip:($PSversionTable.PSVersion -lt [Version]'5.0.0') { $rsp = [runspacefactory]::CreateRunspace() {$moduleHandlerType::new($rsp)} | Should -Throw $rsp.Dispose() @@ -176,16 +189,19 @@ Describe "Resolve DSC Resource Dependency" { $modulePath = "$(Split-Path $directory)\Rules\DSCResourceModule\DSCResources\$moduleName" # Save the current environment variables - $oldLocalAppDataPath = $env:LOCALAPPDATA + $oldLocalAppDataPath = Get-LocalAppDataFolder $oldTempPath = $env:TEMP $savedPSModulePath = $env:PSModulePath # set the environment variables - $tempPath = Join-Path $oldTempPath ([guid]::NewGUID()).ToString() + $tempPath = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGUID()).ToString() $newLocalAppDataPath = Join-Path $tempPath "LocalAppData" $newTempPath = Join-Path $tempPath "Temp" - $env:LOCALAPPDATA = $newLocalAppDataPath - $env:TEMP = $newTempPath + if (-not ($IsLinux -or $IsMacOS)) + { + $env:LOCALAPPDATA = $newLocalAppDataPath + $env:TEMP = $newTempPath + } # create the temporary directories New-Item -Type Directory -Path $newLocalAppDataPath -force @@ -212,7 +228,6 @@ Describe "Resolve DSC Resource Dependency" { } It "has a single parse error" -skip:$skipTest { - # invoke script analyzer $dr = Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable analyzerErrors -ErrorAction SilentlyContinue $analyzerErrors.Count | Should -Be 0 $dr | @@ -221,14 +236,22 @@ Describe "Resolve DSC Resource Dependency" { } It "Keeps PSModulePath unchanged before and after invocation" -skip:$skipTest { - $dr = Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable parseErrors -ErrorAction SilentlyContinue + Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable parseErrors -ErrorAction SilentlyContinue $env:PSModulePath | Should -Be $savedPSModulePath } if (!$skipTest) { - $env:LOCALAPPDATA = $oldLocalAppDataPath - $env:TEMP = $oldTempPath + if ($IsLinux -or $IsMacOS) + { + $env:HOME = $oldLocalAppDataPath + # On Linux [System.IO.Path]::GetTempPath() does not use the TEMP env variable unlike on Windows + } + else + { + $env:LOCALAPPDATA = $oldLocalAppDataPath + $env:TEMP = $oldTempPath + } Remove-Item -Recurse -Path $tempModulePath -Force Remove-Item -Recurse -Path $tempPath -Force }