1- if (! (Get-Module PSScriptAnalyzer) -and ! $testingLibraryUsage )
2- {
3- Import-Module PSScriptAnalyzer
4- }
5-
6- if ($testingLibraryUsage )
7- {
8- return
9- }
10-
11- $directory = Split-Path - Parent $MyInvocation.MyCommand.Path
12- $testRootDirectory = Split-Path - Parent $directory
13- Import-Module (Join-Path $testRootDirectory ' PSScriptAnalyzerTestHelper.psm1' )
14-
15- # needs fixing
16- # right now we do not support module dependency handing on Linux
17- if ((Test-PSEditionCoreCLRLinux ))
18- {
19- return
20- }
21-
22- # DSC Module saving is not supported in versions less than PSv5
23- if (($PSVersionTable.PSVersion -lt [Version ]' 5.0.0' ))
24- {
25- return
26- }
27-
28- $violationFileName = ' MissingDSCResource.ps1'
29- $violationFilePath = Join-Path $directory $violationFileName
30-
311Describe " Resolve DSC Resource Dependency" {
2+ BeforeAll {
3+ $skipTest = $false
4+ if ( -not $IsWindows -or $testingLibararyUsage -or ($PSversionTable.PSVersion -lt [Version ]' 5.0.0' ))
5+ {
6+ $skipTest = $true
7+ return
8+ }
9+ $SavedPSModulePath = $env: PSModulePath
10+ $violationFileName = ' MissingDSCResource.ps1'
11+ $violationFilePath = Join-Path $directory $violationFileName
3212
33- Function Test-EnvironmentVariables ( $oldEnv )
34- {
35- $newEnv = Get-Item Env:\ * | Sort-Object - Property Key
36- $newEnv .Count | Should Be $oldEnv .Count
37- foreach ( $index in 1 .. $newEnv .Count )
13+ $directory = Split-Path - Parent $MyInvocation .MyCommand.Path
14+ $testRootDirectory = Split-Path - Parent $directory
15+ Import-Module ( Join-Path $testRootDirectory ' PSScriptAnalyzerTestHelper.psm1 ' )
16+
17+ Function Test-EnvironmentVariables ( $oldEnv )
3818 {
39- $newEnv [$index ].Key | Should Be $oldEnv [$index ].Key
40- $newEnv [$index ].Value | Should Be $oldEnv [$index ].Value
19+ $newEnv = Get-Item Env:\* | Sort-Object - Property Key
20+ $newEnv.Count | Should Be $oldEnv.Count
21+ foreach ($index in 1 .. $newEnv.Count )
22+ {
23+ $newEnv [$index ].Key | Should Be $oldEnv [$index ].Key
24+ $newEnv [$index ].Value | Should Be $oldEnv [$index ].Value
25+ }
4126 }
4227 }
28+ AfterAll {
29+ if ( $skipTest ) { return }
30+ $env: PSModulePath = $SavedPSModulePath
31+ }
4332
4433 Context " Module handler class" {
45- $moduleHandlerType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler ]
46- $oldEnvVars = Get-Item Env:\* | Sort-Object - Property Key
47- $oldPSModulePath = $env: PSModulePath
48- It " Sets defaults correctly" {
34+ BeforeAll {
35+ if ( $skipTest ) { return }
36+ $moduleHandlerType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler ]
37+ $oldEnvVars = Get-Item Env:\* | Sort-Object - Property Key
38+ $oldPSModulePath = $env: PSModulePath
39+ }
40+ It " Sets defaults correctly" - skip:$skipTest {
4941 $rsp = [runspacefactory ]::CreateRunspace()
5042 $rsp.Open ()
5143 $depHandler = $moduleHandlerType ::new($rsp )
@@ -69,21 +61,21 @@ Describe "Resolve DSC Resource Dependency" {
6961 $rsp.Dispose ()
7062 }
7163
72- It " Keeps the environment variables unchanged" {
64+ It " Keeps the environment variables unchanged" - skip: $skipTest {
7365 Test-EnvironmentVariables ($oldEnvVars )
7466 }
7567
76- It " Throws if runspace is null" {
68+ It " Throws if runspace is null" - skip: $skipTest {
7769 {$moduleHandlerType ::new($null )} | Should Throw
7870 }
7971
80- It " Throws if runspace is not opened" {
72+ It " Throws if runspace is not opened" - skip: $skipTest {
8173 $rsp = [runspacefactory ]::CreateRunspace()
8274 {$moduleHandlerType ::new($rsp )} | Should Throw
8375 $rsp.Dispose ()
8476 }
8577
86- It " Extracts 1 module name" {
78+ It " Extracts 1 module name" - skip: $skipTest {
8779 $sb = @"
8880{Configuration SomeConfiguration
8981{
@@ -97,7 +89,7 @@ Describe "Resolve DSC Resource Dependency" {
9789 $resultModuleNames [0 ] | Should Be ' SomeDscModule1'
9890 }
9991
100- It " Extracts more than 1 module names" {
92+ It " Extracts more than 1 module names" - skip: $skipTest {
10193 $sb = @"
10294{Configuration SomeConfiguration
10395{
@@ -114,7 +106,7 @@ Describe "Resolve DSC Resource Dependency" {
114106 }
115107
116108
117- It " Extracts module names when ModuleName parameter is not the first named parameter" {
109+ It " Extracts module names when ModuleName parameter is not the first named parameter" - skip: $skipTest {
118110 $sb = @"
119111{Configuration SomeConfiguration
120112{
@@ -130,64 +122,71 @@ Describe "Resolve DSC Resource Dependency" {
130122 }
131123
132124 Context " Invoke-ScriptAnalyzer without switch" {
133- It " Has parse errors" {
125+ It " Has parse errors" - skip: $skipTest {
134126 $dr = Invoke-ScriptAnalyzer - Path $violationFilePath - ErrorVariable parseErrors - ErrorAction SilentlyContinue
135127 $parseErrors.Count | Should Be 1
136128 }
137129 }
138130
139131 Context " Invoke-ScriptAnalyzer without switch but with module in temp path" {
140- $oldEnvVars = Get-Item Env:\* | Sort-Object - Property Key
141- $moduleName = " MyDscResource"
142- $modulePath = " $ ( Split-Path $directory ) \Rules\DSCResourceModule\DSCResources\$moduleName "
143-
144- # Save the current environment variables
145- $oldLocalAppDataPath = $env: LOCALAPPDATA
146- $oldTempPath = $env: TEMP
147- $oldPSModulePath = $env: PSModulePath
148-
149- # set the environment variables
150- $tempPath = Join-Path $oldTempPath ([guid ]::NewGUID()).ToString()
151- $newLocalAppDataPath = Join-Path $tempPath " LocalAppData"
152- $newTempPath = Join-Path $tempPath " Temp"
153- $env: LOCALAPPDATA = $newLocalAppDataPath
154- $env: TEMP = $newTempPath
155-
156- # create the temporary directories
157- New-Item - Type Directory - Path $newLocalAppDataPath
158- New-Item - Type Directory - Path $newTempPath
159-
160- # create and dispose module dependency handler object
161- # to setup the temporary module
162- $rsp = [runspacefactory ]::CreateRunspace()
163- $rsp.Open ()
164- $depHandler = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler ]::new($rsp )
165- $pssaAppDataPath = $depHandler.PSSAAppDataPath
166- $tempModulePath = $depHandler.TempModulePath
167- $rsp.Dispose ()
168- $depHandler.Dispose ()
169-
170- # copy myresource module to the temporary location
171- # we could let the module dependency handler download it from psgallery
172- Copy-Item - Recurse - Path $modulePath - Destination $tempModulePath
173-
174- It " Doesn't have parse errors" {
132+ BeforeAll {
133+ if ( $skipTest ) { return }
134+ $oldEnvVars = Get-Item Env:\* | Sort-Object - Property Key
135+ $moduleName = " MyDscResource"
136+ $modulePath = " $ ( Split-Path $directory ) \Rules\DSCResourceModule\DSCResources\$moduleName "
137+
138+ # Save the current environment variables
139+ $oldLocalAppDataPath = $env: LOCALAPPDATA
140+ $oldTempPath = $env: TEMP
141+ $oldPSModulePath = $env: PSModulePath
142+
143+ # set the environment variables
144+ $tempPath = Join-Path $oldTempPath ([guid ]::NewGUID()).ToString()
145+ $newLocalAppDataPath = Join-Path $tempPath " LocalAppData"
146+ $newTempPath = Join-Path $tempPath " Temp"
147+ $env: LOCALAPPDATA = $newLocalAppDataPath
148+ $env: TEMP = $newTempPath
149+
150+ # create the temporary directories
151+ New-Item - Type Directory - Path $newLocalAppDataPath
152+ New-Item - Type Directory - Path $newTempPath
153+
154+ # create and dispose module dependency handler object
155+ # to setup the temporary module
156+ $rsp = [runspacefactory ]::CreateRunspace()
157+ $rsp.Open ()
158+ $depHandler = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler ]::new($rsp )
159+ $pssaAppDataPath = $depHandler.PSSAAppDataPath
160+ $tempModulePath = $depHandler.TempModulePath
161+ $rsp.Dispose ()
162+ $depHandler.Dispose ()
163+
164+ # copy myresource module to the temporary location
165+ # we could let the module dependency handler download it from psgallery
166+ Copy-Item - Recurse - Path $modulePath - Destination $tempModulePath
167+ }
168+
169+ AfterAll {
170+ if ( $skipTest ) { return }
171+ # restore environment variables and clean up temporary location
172+ $env: LOCALAPPDATA = $oldLocalAppDataPath
173+ $env: TEMP = $oldTempPath
174+ Remove-Item - Recurse - Path $tempModulePath - Force
175+ Remove-Item - Recurse - Path $tempPath - Force
176+ }
177+
178+ It " Doesn't have parse errors" - skip:$skipTest {
175179 # invoke script analyzer
176180 $dr = Invoke-ScriptAnalyzer - Path $violationFilePath - ErrorVariable parseErrors - ErrorAction SilentlyContinue
177181 $dr.Count | Should Be 0
178182 }
179183
180- It " Keeps PSModulePath unchanged before and after invocation" {
184+ It " Keeps PSModulePath unchanged before and after invocation" - skip: $skipTest {
181185 $dr = Invoke-ScriptAnalyzer - Path $violationFilePath - ErrorVariable parseErrors - ErrorAction SilentlyContinue
182186 $env: PSModulePath | Should Be $oldPSModulePath
183187 }
184- # restore environment variables and clean up temporary location
185- $env: LOCALAPPDATA = $oldLocalAppDataPath
186- $env: TEMP = $oldTempPath
187- Remove-Item - Recurse - Path $tempModulePath - Force
188- Remove-Item - Recurse - Path $tempPath - Force
189188
190- It " Keeps the environment variables unchanged" {
189+ It " Keeps the environment variables unchanged" - skip: $skipTest {
191190 Test-EnvironmentVariables ($oldEnvVars )
192191 }
193192 }
0 commit comments