Skip to content

Commit de28e4f

Browse files
authored
Changes to allow tests to be run outside of CI (#882)
* 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 * Reduce verbosity of build script Update to use Pester version 4.1.1 Change logic in appveyor to invoke pester only once
1 parent c54c8fe commit de28e4f

15 files changed

+148
-154
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ For adding/removing resource strings in the `*.resx` files, it is recommended to
129129
#### Tests
130130
Pester-based ScriptAnalyzer Tests are located in `path/to/PSScriptAnalyzer/Tests` folder.
131131

132-
* Ensure Pester 3.4 is installed on the machine
132+
* Ensure Pester 4.1.1 is installed on the machine
133133
* Copy `path/to/PSScriptAnalyzer/out/PSScriptAnalyzer` to a folder in `PSModulePath`
134134
* Go the Tests folder in your local repository
135135
* Run Engine Tests:

Tests/Engine/CustomizedRule.tests.ps1

+6-9
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

+14-13
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

+89-90
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

+3-3
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

+2-2
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.Major -lt 5)) {
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+
}

0 commit comments

Comments
 (0)