Skip to content

Commit 84f433a

Browse files
authored
Merge pull request #1246 from bergmeister/SaveDscDependencyOnLinux
-SaveDscDependency on Linux
2 parents ab0fc4f + 3c56ee1 commit 84f433a

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

Engine/Generic/ModuleDependencyHandler.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#if !PSV3
55
using System;
66
using System.Collections.Generic;
7-
using System.Collections.ObjectModel;
87
using System.IO;
98
using System.Linq;
109
using System.Management.Automation;
1110
using System.Management.Automation.Language;
1211
using System.Management.Automation.Runspaces;
12+
using System.Runtime.InteropServices;
1313

1414
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
1515
{
@@ -75,7 +75,7 @@ private set
7575
#if CORECLR
7676
localAppdataPath
7777
= string.IsNullOrWhiteSpace(value)
78-
? Environment.GetEnvironmentVariable("LOCALAPPDATA")
78+
? Environment.GetEnvironmentVariable(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "LOCALAPPDATA" : "HOME") //Environment.GetEnvironmentVariable("LOCALAPPDATA")
7979
: value;
8080
#else
8181
localAppdataPath
@@ -215,7 +215,7 @@ private string GetTempModulePath(string symLinkPath)
215215
private void SetupPSModulePath()
216216
{
217217
oldPSModulePath = Environment.GetEnvironmentVariable("PSModulePath");
218-
curPSModulePath = oldPSModulePath + ";" + tempModulePath;
218+
curPSModulePath = oldPSModulePath + Path.PathSeparator + tempModulePath;
219219
#if CORECLR
220220
Environment.SetEnvironmentVariable("PSModulePath", curPSModulePath);
221221
#else

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ Describe "Test available parameters" {
7272
It "is a switch parameter" {
7373
$params["SaveDscDependency"].ParameterType.FullName | Should -Be "System.Management.Automation.SwitchParameter"
7474
}
75+
76+
It 'does not throw when being applied against a dummy script with no DSC code' {
77+
Invoke-ScriptAnalyzer -ScriptDefinition 'foo' -SaveDscDependency
78+
}
7579
}
7680
}
7781

Tests/Engine/ModuleDependencyHandler.tests.ps1

+40-17
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ $directory = Split-Path -Parent $MyInvocation.MyCommand.Path
22

33
Describe "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

Comments
 (0)