Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-SaveDscDependency on Linux #1246

Merged
6 changes: 3 additions & 3 deletions Engine/Generic/ModuleDependencyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
57 changes: 40 additions & 17 deletions Tests/Engine/ModuleDependencyHandler.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand All @@ -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
Expand All @@ -41,16 +55,15 @@ 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)

$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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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 |
Expand All @@ -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
}
Expand Down