Skip to content

Commit 9701f7f

Browse files
committed
(chocolateyGH-148) Updates Tests to work in Pester 5.x
There were some compatibility issues when running the existing tests in Pester 5, and it was noted that it would be good to update them. This commit does so. Tested using Pester 5.3.1.
1 parent 7ba5c25 commit 9701f7f

8 files changed

+402
-297
lines changed

AppVeyor/AppVeyorInstall.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Write-Host "Installed NuGet version '$($pkg.version)'"
3434
# Install Modules #
3535
#---------------------------------#
3636
[version]$ScriptAnalyzerVersion = '1.8.1'
37-
[version]$PesterVersion = '4.10.1'
37+
[version]$PesterVersion = '5.99.99'
3838
Install-Module -Name 'PSScriptAnalyzer' -Repository PSGallery -Force -ErrorAction Stop -MaximumVersion $ScriptAnalyzerVersion
3939
Install-Module -Name 'Pester' -SkipPublisherCheck -Repository PSGallery -Force -ErrorAction Stop -MaximumVersion $PesterVersion
4040
Install-Module -Name 'xDSCResourceDesigner' -Repository PSGallery -Force -ErrorAction Stop

AppVeyor/AppVeyorTest.ps1

+18-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,27 @@ Write-Host "Current working directory: $pwd"
2323
# Run Pester Tests #
2424
#---------------------------------#
2525
$resultsFile = '.\TestsResults.xml'
26-
$testFiles = Get-ChildItem "$pwd\tests" | Where-Object {$_.FullName -match 'Tests.ps1$'} | Select-Object -ExpandProperty FullName
27-
$results = Invoke-Pester -Script $testFiles -OutputFormat NUnitXml -OutputFile $resultsFile -PassThru
26+
$PesterConfiguration = New-PesterConfiguration @{
27+
Run = @{
28+
Path = "$PSScriptRoot\..\tests"
29+
PassThru = $true
30+
}
31+
TestResult = @{
32+
Enabled = $true
33+
OutputFormat = "NUnitXml"
34+
OutputPath = $resultsFile
35+
}
36+
Output = @{
37+
Verbosity = "Detailed"
38+
}
39+
}
40+
$results = Invoke-Pester -Configuration $PesterConfiguration
2841

2942
Write-Host 'Uploading results'
3043
try {
31-
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $resultsFile))
44+
if ($env:APPVEYOR_JOB_ID) {
45+
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $resultsFile))
46+
}
3247
} catch {
3348
throw "Upload failed."
3449
}

Tests/cChocoConfig.Tests.ps1

+86-50
Original file line numberDiff line numberDiff line change
@@ -12,93 +12,129 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
16-
$ResourceName = ((Split-Path $MyInvocation.MyCommand.Path -Leaf) -split '_')[0]
17-
$ResourceFile = (Get-DscResource -Name $ResourceName).Path
18-
19-
$TestsPath = (split-path -path $MyInvocation.MyCommand.Path -Parent)
20-
$ResourceFile = Get-ChildItem -Recurse $TestsPath\.. -File | Where-Object {$_.name -eq "$ResourceName.psm1"}
21-
22-
Import-Module -Name $ResourceFile.FullName
23-
24-
2515
#---------------------------------#
2616
# Pester tests for cChocoConfig #
2717
#---------------------------------#
2818
Describe "Testing cChocoConfig" {
19+
BeforeAll {
20+
$ModuleUnderTest = "cChocoConfig"
2921

30-
Context "Test-TargetResource" {
22+
Import-Module $PSScriptRoot\..\DSCResources\$($ModuleUnderTest)\$($ModuleUnderTest).psm1 -Force
3123

32-
mock -ModuleName cChocoConfig -CommandName Get-Content -MockWith {'<?xml version="1.0" encoding="utf-8"?>
33-
<chocolatey xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
34-
<config>
35-
<add key="commandExecutionTimeoutSeconds" value="1339" description="Default timeout for command execution. for infinite (starting in 0.10.4)." />
36-
<add key="proxy" value="" description="Explicit proxy location. Available in 0.9.9.9+." />
37-
</config>
38-
<sources>
39-
<source id="chocolatey" value="https://chocolatey.org/api/v2/" disabled="false" bypassProxy="false" selfService="false" adminOnly="false" priority="0" />
40-
</sources>
41-
</chocolatey>'
42-
} -Verifiable
24+
if (-not $env:ChocolateyInstall -and -not (Test-Path $env:ChocolateyInstall -ErrorAction SilentlyContinue)) {
25+
# Chocolatey doesn't need to be installed for these tests, but the resource tests for it
26+
$env:ChocolateyInstall = "C:\ProgramData\chocolatey"
27+
}
4328

44-
it 'Test-TargetResource returns true when Present and Configured.' {
45-
Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Present' -Value '1339' | Should be $true
29+
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
30+
function global:choco {}
4631
}
4732

48-
it 'Test-TargetResource returns false when Present and Not configured' {
49-
Test-TargetResource -ConfigName 'proxy' -Ensure 'Present' -Value 'http://myproxy.url' | Should be $false
33+
Mock Get-Item -ModuleName $ModuleUnderTest -ParameterFilter {
34+
$Path.StartsWith($env:ChocolateyInstall)
35+
} -MockWith {
36+
$true
5037
}
5138

52-
it 'Test-TargetResource returns false when Present and Unknown' {
53-
Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value 'MyValue' | Should be $false
39+
Mock Get-ChildItem -ModuleName $ModuleUnderTest -ParameterFilter {
40+
$Path -eq (Join-Path $env:ChocolateyInstall "config")
41+
} -MockWith {
42+
@{
43+
Name = "chocolatey.config"
44+
FullName = Join-Path $env:ChocolateyInstall "config/chocolatey.config"
45+
}
46+
}
47+
}
48+
49+
AfterAll {
50+
Remove-Module $ModuleUnderTest
51+
}
52+
53+
Context "Test-TargetResource" {
54+
BeforeAll {
55+
Mock Get-Content -ModuleName $ModuleUnderTest -ParameterFilter {
56+
$Path.EndsWith('chocolatey.config')
57+
} -MockWith {
58+
'<?xml version="1.0" encoding="utf-8"?>
59+
<chocolatey xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
60+
<config>
61+
<add key="commandExecutionTimeoutSeconds" value="1339" description="Default timeout for command execution. for infinite (starting in 0.10.4)." />
62+
<add key="proxy" value="" description="Explicit proxy location. Available in 0.9.9.9+." />
63+
</config>
64+
<sources>
65+
<source id="chocolatey" value="https://chocolatey.org/api/v2/" disabled="false" bypassProxy="false" selfService="false" adminOnly="false" priority="0" />
66+
</sources>
67+
</chocolatey>'
68+
}
69+
}
70+
71+
It 'Test-TargetResource returns true when Present and Configured.' {
72+
Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Present' -Value '1339' | Should -Be $true
5473
}
5574

56-
it 'Test-TargetResource throws when Present and no value' {
75+
It 'Test-TargetResource returns false when Present and Not configured' {
76+
Test-TargetResource -ConfigName 'proxy' -Ensure 'Present' -Value 'http://myproxy.url' | Should -Be $false
77+
}
78+
79+
It 'Test-TargetResource returns false when Present and Unknown' {
80+
Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value 'MyValue' | Should -Be $false
81+
}
82+
83+
It 'Test-TargetResource throws when Present and no value' {
5784
{ Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' } | Should -Throw "Missing parameter 'Value' when ensuring config is present!"
5885
}
5986

60-
it 'Test-TargetResource throws when Present and no value' {
87+
It 'Test-TargetResource throws when Present and no value' {
6188
{ Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value '' } | Should -Throw "Missing parameter 'Value' when ensuring config is present!"
6289
}
6390

64-
it 'Test-TargetResource throws when Present and no value' {
91+
It 'Test-TargetResource throws when Present and no value' {
6592
{ Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value $null } | Should -Throw "Missing parameter 'Value' when ensuring config is present!"
6693
}
6794

68-
it 'Test-TargetResource returns false when Absent and Configured' {
69-
Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Absent' | Should be $false
95+
It 'Test-TargetResource returns false when Absent and Configured' {
96+
Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Absent' | Should -Be $false
7097
}
7198

72-
it 'Test-TargetResource returns true when Absent and Not configured' {
73-
Test-TargetResource -ConfigName 'proxy' -Ensure 'Absent' | Should be $true
99+
It 'Test-TargetResource returns true when Absent and Not configured' {
100+
Test-TargetResource -ConfigName 'proxy' -Ensure 'Absent' | Should -Be $true
74101
}
75102

76-
it 'Test-TargetResource returns true when Absent and Unknown' {
77-
Test-TargetResource -ConfigName 'MyParam' -Ensure 'Absent' | Should be $true
103+
It 'Test-TargetResource returns true when Absent and Unknown' {
104+
Test-TargetResource -ConfigName 'MyParam' -Ensure 'Absent' | Should -Be $true
78105
}
79-
80106
}
81107

82108
Context "Set-TargetResource" {
83-
84-
InModuleScope -ModuleName cChocoConfig -ScriptBlock {
85-
function choco {}
86-
mock choco {}
109+
BeforeAll {
110+
Mock choco -ModuleName $ModuleUnderTest
87111
}
88112

89-
Set-TargetResource -ConfigName "TestConfig" -Ensure "Present" -Value "MyValue"
113+
Context "Setting a config value when Present" {
114+
BeforeAll {
115+
Set-TargetResource -ConfigName "TestConfig" -Ensure "Present" -Value "MyValue"
116+
}
90117

91-
it "Present - Should have called choco, with set" {
92-
Assert-MockCalled -CommandName choco -ModuleName cChocoConfig -ParameterFilter {
93-
$args -contains "'MyValue'"
118+
It "Present - Should have called choco, to set the specified ConfigName with the specified Value" {
119+
Assert-MockCalled choco -ModuleName $ModuleUnderTest -ParameterFilter {
120+
$args[0] -eq 'config' -and
121+
$args -match "\bset\b" -and
122+
$args -match "'MyValue'"
123+
} -Scope Context
94124
}
95125
}
96126

97-
Set-TargetResource -ConfigName "TestConfig" -Ensure "Absent"
127+
Context "Removing a config value when Absent" {
128+
BeforeAll {
129+
Set-TargetResource -ConfigName "TestConfig" -Ensure "Absent"
130+
}
98131

99-
it "Absent - Should have called choco, with unset" {
100-
Assert-MockCalled -CommandName choco -ModuleName cChocoConfig -ParameterFilter {
101-
$args -contains "unset"
132+
It "Absent - Should have called choco, to unset the specified ConfigName" {
133+
Assert-MockCalled choco -ModuleName $ModuleUnderTest -ParameterFilter {
134+
$args[0] -eq "config" -and
135+
$args -match "\bunset\b" -and
136+
$args -match "'TestConfig'"
137+
} -Scope Context
102138
}
103139
}
104140
}

Tests/cChocoFeature.Tests.ps1

+56-43
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,87 @@
1313
# limitations under the License.
1414

1515

16-
$ResourceName = ((Split-Path $MyInvocation.MyCommand.Path -Leaf) -split '_')[0]
17-
$ResourceFile = (Get-DscResource -Name $ResourceName).Path
18-
19-
$TestsPath = (split-path -path $MyInvocation.MyCommand.Path -Parent)
20-
$ResourceFile = Get-ChildItem -Recurse $TestsPath\.. -File | Where-Object {$_.name -eq "$ResourceName.psm1"}
21-
22-
Import-Module -Name $ResourceFile.FullName
23-
24-
2516
#---------------------------------#
2617
# Pester tests for cChocoInstall #
2718
#---------------------------------#
2819
Describe "Testing cChocoFeature" {
20+
BeforeAll {
21+
$ModuleUnderTest = "cChocoFeature"
2922

30-
Context "Test-TargetResource" {
31-
32-
mock -ModuleName cChocoFeature -CommandName Get-ChocoFeature -MockWith {
33-
@([pscustomobject]@{
34-
Name = "allowGlobalConfirmation"
35-
State = "Enabled"
36-
Description = "blah"
37-
},
38-
[pscustomobject]@{
39-
Name = "powershellhost"
40-
State = "Disabled"
41-
Description = "blah"
42-
} )| Where-Object { $_.Name -eq $FeatureName }
43-
} -Verifiable
23+
Import-Module $PSScriptRoot\..\DSCResources\$($ModuleUnderTest)\$($ModuleUnderTest).psm1 -Force
4424

25+
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
26+
function global:choco {}
27+
}
28+
}
29+
30+
AfterAll {
31+
Remove-Module $ModuleUnderTest
32+
}
4533

46-
it 'Test-TargetResource returns true when Present and Enabled.' {
47-
Test-TargetResource -FeatureName 'allowGlobalConfirmation' -Ensure 'Present' | should be $true
34+
Context "Test-TargetResource" {
35+
BeforeAll {
36+
Mock -CommandName Get-ChocoFeature -ModuleName $ModuleUnderTest -MockWith {
37+
@([pscustomobject]@{
38+
Name = "allowGlobalConfirmation"
39+
State = "Enabled"
40+
Description = "blah"
41+
},
42+
[pscustomobject]@{
43+
Name = "powershellhost"
44+
State = "Disabled"
45+
Description = "blah"
46+
} ) | Where-Object { $_.Name -eq $FeatureName }
47+
} -Verifiable
4848
}
4949

50-
it 'Test-TargetResource returns false when Present and Disabled' {
51-
Test-TargetResource -FeatureName 'powershellhost' -Ensure 'Present' | should be $false
50+
It 'Test-TargetResource returns true when Present and Enabled.' {
51+
Test-TargetResource -FeatureName 'allowGlobalConfirmation' -Ensure 'Present' | Should -Be $true
5252
}
5353

54-
it 'Test-TargetResource returns false when Absent and Enabled' {
55-
Test-TargetResource -FeatureName 'allowGlobalConfirmation' -Ensure 'Absent' | Should be $false
54+
It 'Test-TargetResource returns false when Present and Disabled' {
55+
Test-TargetResource -FeatureName 'powershellhost' -Ensure 'Present' | Should -Be $false
5656
}
5757

58-
it 'Test-TargetResource returns true when Absent and Disabled' {
59-
Test-TargetResource -FeatureName 'powershellhost' -Ensure 'Absent' | should be $true
58+
It 'Test-TargetResource returns false when Absent and Enabled' {
59+
Test-TargetResource -FeatureName 'allowGlobalConfirmation' -Ensure 'Absent' | Should -Be $false
6060
}
6161

62+
It 'Test-TargetResource returns true when Absent and Disabled' {
63+
Test-TargetResource -FeatureName 'powershellhost' -Ensure 'Absent' | Should -Be $true
64+
}
6265
}
6366

6467
Context "Set-TargetResource" {
65-
66-
InModuleScope -ModuleName cChocoFeature -ScriptBlock {
67-
function choco {}
68-
mock choco {}
68+
BeforeAll {
69+
Mock choco -ModuleName $ModuleUnderTest
6970
}
7071

71-
Set-TargetResource -FeatureName "TestFeature" -Ensure "Present"
72+
Context "Enabling a Feature" {
73+
BeforeAll {
74+
Set-TargetResource -FeatureName "TestFeature" -Ensure "Present"
75+
}
7276

73-
it "Present - Should have called choco, with enable" {
74-
Assert-MockCalled -CommandName choco -ModuleName cChocoFeature -ParameterFilter {
75-
$args -contains "enable"
77+
It "Present - Should have called choco, with enable, and the specified FeatureName" {
78+
Assert-MockCalled choco -ModuleName cChocoFeature -ParameterFilter {
79+
$args[0] -eq "feature" -and
80+
$args[1] -eq "enable" -and
81+
$args -contains "TestFeature"
82+
} -Scope Context
7683
}
7784
}
7885

79-
Set-TargetResource -FeatureName "TestFeature" -Ensure "Absent"
86+
Context "Disabling a Feature" {
87+
BeforeAll {
88+
Set-TargetResource -FeatureName "TestFeature" -Ensure "Absent"
89+
}
8090

81-
it "Absent - Should have called choco, with disable" {
82-
Assert-MockCalled -CommandName choco -ModuleName cChocoFeature -ParameterFilter {
83-
$args -contains "disable"
91+
It "Absent - Should have called choco, with disable, and the specified FeatureName" {
92+
Assert-MockCalled choco -ModuleName cChocoFeature -ParameterFilter {
93+
$args[0] -eq "feature" -and
94+
$args[1] -eq "disable" -and
95+
$args -contains "TestFeature"
96+
} -Scope Context
8497
}
8598
}
8699
}

Tests/cChocoInstaller.Tests.ps1

+15-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@
1616
#---------------------------------#
1717
# Pester tests for cChocoInstall #
1818
#---------------------------------#
19-
$ResourceName = ((Split-Path $MyInvocation.MyCommand.Path -Leaf) -split '_')[0]
20-
$ResourceFile = (Get-DscResource -Name $ResourceName).Path
19+
Describe "Testing cChocoInstaller" {
20+
BeforeAll {
21+
$ModuleUnderTest = "cChocoInstaller"
22+
23+
Import-Module $PSScriptRoot\..\DSCResources\$($ModuleUnderTest)\$($ModuleUnderTest).psm1 -Force
24+
25+
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
26+
function global:choco {}
27+
}
28+
}
29+
30+
AfterAll {
31+
Remove-Module $ModuleUnderTest
32+
}
2133

22-
Describe "Testing $ResourceName loaded from $ResourceFile" {
2334
Context Testing 'Get-TargetResource' {
2435
It 'DummyTest $true should be $true' {
25-
$true | Should Be $true
36+
$true | Should -Be $true
2637
}
2738
}
2839
}

0 commit comments

Comments
 (0)