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
-
31
1
Describe " 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
32
12
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 )
38
18
{
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
+ }
41
26
}
42
27
}
28
+ AfterAll {
29
+ if ( $skipTest ) { return }
30
+ $env: PSModulePath = $SavedPSModulePath
31
+ }
43
32
44
33
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 {
49
41
$rsp = [runspacefactory ]::CreateRunspace()
50
42
$rsp.Open ()
51
43
$depHandler = $moduleHandlerType ::new($rsp )
@@ -69,21 +61,21 @@ Describe "Resolve DSC Resource Dependency" {
69
61
$rsp.Dispose ()
70
62
}
71
63
72
- It " Keeps the environment variables unchanged" {
64
+ It " Keeps the environment variables unchanged" - skip: $skipTest {
73
65
Test-EnvironmentVariables ($oldEnvVars )
74
66
}
75
67
76
- It " Throws if runspace is null" {
68
+ It " Throws if runspace is null" - skip: $skipTest {
77
69
{$moduleHandlerType ::new($null )} | Should Throw
78
70
}
79
71
80
- It " Throws if runspace is not opened" {
72
+ It " Throws if runspace is not opened" - skip: $skipTest {
81
73
$rsp = [runspacefactory ]::CreateRunspace()
82
74
{$moduleHandlerType ::new($rsp )} | Should Throw
83
75
$rsp.Dispose ()
84
76
}
85
77
86
- It " Extracts 1 module name" {
78
+ It " Extracts 1 module name" - skip: $skipTest {
87
79
$sb = @"
88
80
{Configuration SomeConfiguration
89
81
{
@@ -97,7 +89,7 @@ Describe "Resolve DSC Resource Dependency" {
97
89
$resultModuleNames [0 ] | Should Be ' SomeDscModule1'
98
90
}
99
91
100
- It " Extracts more than 1 module names" {
92
+ It " Extracts more than 1 module names" - skip: $skipTest {
101
93
$sb = @"
102
94
{Configuration SomeConfiguration
103
95
{
@@ -114,7 +106,7 @@ Describe "Resolve DSC Resource Dependency" {
114
106
}
115
107
116
108
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 {
118
110
$sb = @"
119
111
{Configuration SomeConfiguration
120
112
{
@@ -130,64 +122,71 @@ Describe "Resolve DSC Resource Dependency" {
130
122
}
131
123
132
124
Context " Invoke-ScriptAnalyzer without switch" {
133
- It " Has parse errors" {
125
+ It " Has parse errors" - skip: $skipTest {
134
126
$dr = Invoke-ScriptAnalyzer - Path $violationFilePath - ErrorVariable parseErrors - ErrorAction SilentlyContinue
135
127
$parseErrors.Count | Should Be 1
136
128
}
137
129
}
138
130
139
131
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 {
175
179
# invoke script analyzer
176
180
$dr = Invoke-ScriptAnalyzer - Path $violationFilePath - ErrorVariable parseErrors - ErrorAction SilentlyContinue
177
181
$dr.Count | Should Be 0
178
182
}
179
183
180
- It " Keeps PSModulePath unchanged before and after invocation" {
184
+ It " Keeps PSModulePath unchanged before and after invocation" - skip: $skipTest {
181
185
$dr = Invoke-ScriptAnalyzer - Path $violationFilePath - ErrorVariable parseErrors - ErrorAction SilentlyContinue
182
186
$env: PSModulePath | Should Be $oldPSModulePath
183
187
}
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
189
188
190
- It " Keeps the environment variables unchanged" {
189
+ It " Keeps the environment variables unchanged" - skip: $skipTest {
191
190
Test-EnvironmentVariables ($oldEnvVars )
192
191
}
193
192
}
0 commit comments