Skip to content

Commit 878d0f6

Browse files
committed
Change logic for test execution to handle running on Mac
Mark some tests as pending that weren't actually doing anything Skip DSC tests on Mac/Linux
1 parent d432e00 commit 878d0f6

11 files changed

+124
-133
lines changed

Tests/Engine/CustomizedRule.tests.ps1

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,16 @@ Describe "Test importing correct customized rules" {
9797
$customizedRulePath.Count | Should Be 1
9898
}
9999

100-
It "will show the custom rule when given a rule folder path with trailing backslash" {
100+
It "will show the custom rule when given a rule folder path with trailing backslash" -skip:(!$IsWindows){
101101
# needs fixing for linux
102-
if (-not (Test-PSEditionCoreCLRLinux))
103-
{
104-
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory/samplerule/ | Where-Object {$_.RuleName -eq $measure}
105-
$customizedRulePath.Count | Should Be 1
106-
}
102+
$customizedRulePath = Get-ScriptAnalyzerRule -CustomizedRulePath $directory/samplerule/ | Where-Object {$_.RuleName -eq $measure}
103+
$customizedRulePath.Count | Should Be 1
107104
}
108105

109106
It "will show the custom rules when given a glob" {
110107
# needs fixing for Linux
111108
$expectedNumRules = 4
112-
if (Test-PSEditionCoreCLRLinux)
109+
if ( ! $IsWindows )
113110
{
114111
$expectedNumRules = 3
115112
}
@@ -125,7 +122,7 @@ Describe "Test importing correct customized rules" {
125122
It "will show the custom rules when given glob with recurse switch" {
126123
# needs fixing for Linux
127124
$expectedNumRules = 5
128-
if (Test-PSEditionCoreCLRLinux)
125+
if (! $IsWindows )
129126
{
130127
$expectedNumRules = 4
131128
}
@@ -165,7 +162,7 @@ Describe "Test importing correct customized rules" {
165162
{
166163
It "will show the custom rule in the results when given a rule folder path with trailing backslash" {
167164
# needs fixing for Linux
168-
if (-not (Test-PSEditionCoreCLRLinux))
165+
if ($IsWindows)
169166
{
170167
$customizedRulePath = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule\ | Where-Object {$_.Message -eq $message}
171168
$customizedRulePath.Count | Should Be 1

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -468,33 +468,34 @@ Describe "Test CustomizedRulePath" {
468468

469469
Describe "Test -Fix Switch" {
470470

471-
BeforeEach {
472-
$initialTestScript = Get-Content $directory\TestScriptWithFixableWarnings.ps1 -Raw
471+
BeforeAll {
472+
$scriptName = "TestScriptWithFixableWarnings.ps1"
473+
$testSource = join-path $PSScriptRoot ${scriptName}
474+
$fixedScript = join-path $PSScriptRoot TestScriptWithFixableWarnings_AfterFix.ps1
475+
$expectedScriptContent = Get-Content $fixedScript -raw
476+
$testScript = Join-Path $TESTDRIVE $scriptName
473477
}
474478

475-
AfterEach {
476-
if ($null -ne $initialTestScript)
477-
{
478-
[System.IO.File]::WriteAllText("$($directory)\TestScriptWithFixableWarnings.ps1", $initialTestScript) # Set-Content -NoNewline was only introduced in PS v5 and would therefore not work in CI
479-
}
479+
BeforeEach {
480+
Copy-Item $testSource $TESTDRIVE
480481
}
481482

482483
It "Fixes warnings" {
483484
# we expect the script to contain warnings
484-
$warningsBeforeFix = Invoke-ScriptAnalyzer $directory\TestScriptWithFixableWarnings.ps1
485+
$warningsBeforeFix = Invoke-ScriptAnalyzer $testScript
485486
$warningsBeforeFix.Count | Should Be 5
486487

487488
# fix the warnings and expect that it should not return the fixed warnings
488-
$warningsWithFixSwitch = Invoke-ScriptAnalyzer $directory\TestScriptWithFixableWarnings.ps1 -Fix
489+
$warningsWithFixSwitch = Invoke-ScriptAnalyzer $testScript -Fix
489490
$warningsWithFixSwitch.Count | Should Be 0
490491

491492
# double check that the warnings are really fixed
492-
$warningsAfterFix = Invoke-ScriptAnalyzer $directory\TestScriptWithFixableWarnings.ps1
493+
$warningsAfterFix = Invoke-ScriptAnalyzer $testScript
493494
$warningsAfterFix.Count | Should Be 0
494495

495-
$expectedScriptContentAfterFix = Get-Content $directory\TestScriptWithFixableWarnings_AfterFix.ps1 -Raw
496-
$actualScriptContentAfterFix = Get-Content $directory\TestScriptWithFixableWarnings.ps1 -Raw
497-
$actualScriptContentAfterFix | Should Be $expectedScriptContentAfterFix
496+
# check content to ensure we have what we expect
497+
$actualScriptContentAfterFix = Get-Content $testScript -Raw
498+
$actualScriptContentAfterFix | Should Be $expectedScriptContent
498499
}
499500
}
500501

Tests/Engine/ModuleDependencyHandler.tests.ps1

Lines changed: 89 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,43 @@
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-
311
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
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
}

Tests/Engine/ModuleHelp.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Enter the version of the module to test. This parameter is optional. If you
3333
omit it, the test runs on the latest version of the module in $env:PSModulePath.
3434
3535
.EXAMPLE
36-
.\Module.Help.Tests.ps1 -ModuleName Pester -RequiredVersion 3.4.0
37-
This command runs the tests on the commands in Pester 3.4.0.
36+
.\Module.Help.Tests.ps1 -ModuleName Pester -RequiredVersion 4.1.1
37+
This command runs the tests on the commands in Pester 4.1.1.
3838
3939
.EXAMPLE
4040
.\Module.Help.Tests.ps1 -ModuleName Pester
@@ -63,7 +63,7 @@ Param
6363
$RequiredVersion
6464
)
6565

66-
# #Requires -Module @{ModuleName = 'Pester'; ModuleVersion = '3.4.0'}
66+
# #Requires -Module @{ModuleName = 'Pester'; ModuleVersion = '4.1.1'}
6767

6868
<#
6969
.SYNOPSIS

Tests/Engine/RuleSuppression.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function SuppressPwdParam()
106106
}
107107

108108
Context "Rule suppression within DSC Configuration definition" {
109-
It "Suppresses rule" -skip:((Test-PSEditionCoreCLRLinux) -or ($PSVersionTable.PSVersion -lt [Version]'5.0.0')) {
109+
It "Suppresses rule" -skip:((!$IsWindows) -or ($PSVersionTable.PSVersion -lt [Version]'5.0.0')) {
110110
$suppressedRule = Invoke-ScriptAnalyzer -ScriptDefinition $ruleSuppressionInConfiguration -SuppressedOnly
111111
$suppressedRule.Count | Should Be 1
112112
}
@@ -215,4 +215,4 @@ Describe "RuleSuppressionWithScope" {
215215
$suppressed.Count | Should Be 1
216216
}
217217
}
218-
}
218+
}

Tests/PSScriptAnalyzerTestHelper.psm1

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ Function Test-PSEditionCoreCLR
5656
[bool]$IsCoreCLR
5757
}
5858

59-
Function Test-PSEditionCoreCLRLinux
60-
{
61-
(Test-PSEditionCoreCLR) -and $IsLinux
62-
}
63-
6459
Function Test-PSVersionV3
6560
{
6661
$PSVersionTable.PSVersion.Major -eq 3
@@ -82,7 +77,6 @@ Export-ModuleMember -Function Get-ExtentText
8277
Export-ModuleMember -Function Test-CorrectionExtent
8378
Export-ModuleMember -Function Test-CorrectionExtentFromContent
8479
Export-ModuleMember -Function Test-PSEditionCoreCLR
85-
Export-ModuleMember -Function Test-PSEditionCoreCLRLinux
8680
Export-ModuleMember -Function Test-PSVersionV3
8781
Export-ModuleMember -Function Test-PSVersionV4
88-
Export-ModuleMember -Function Get-Count
82+
Export-ModuleMember -Function Get-Count

0 commit comments

Comments
 (0)