Skip to content

Commit 85940be

Browse files
committed
Refactor pester install and import to separate function
1 parent b283d32 commit 85940be

File tree

4 files changed

+191
-182
lines changed

4 files changed

+191
-182
lines changed

Extension/task/HelperModule.psm1

+45
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,48 @@ function Get-HashtableFromString
2525
}
2626
}
2727
}
28+
29+
function Import-Pester {
30+
[cmdletbinding()]
31+
param (
32+
[string]$Version
33+
)
34+
35+
36+
if ((Get-Module -Name PowerShellGet -ListAvailable) -and
37+
(Get-Command Install-Module).Parameters.ContainsKey('SkipPublisherCheck')) {
38+
39+
try {
40+
$null = Get-PackageProvider -Name NuGet -ErrorAction Stop
41+
}
42+
catch {
43+
try {
44+
Install-PackageProvider -Name Nuget -RequiredVersion 2.8.5.201 -Scope CurrentUser -Force -Confirm:$false -ErrorAction Stop
45+
}
46+
catch {
47+
Write-Host "##vos[task.logissue type=warning]Falling back to version of Pester shipped with extension. To use a newer version please update the version of PowerShellGet available on this machine."
48+
Import-Module "$PSScriptRoot\4.6.0\Pester.psd1" -force -Verbose:$false
49+
}
50+
}
51+
52+
if ($Version -eq "latest") {
53+
$NewestPester = Find-Module -Name Pester | Sort-Object Version -Descending | Select-Object -First 1
54+
55+
If ((Get-Module Pester -ListAvailable | Sort-Object Version -Descending| Select-Object -First 1).Version -lt $NewestPester.Version) {
56+
Install-Module -Name Pester -RequiredVersion $NewestPester.Version -Scope CurrentUser -Force -Repository $NewestPester.Repository -SkipPublisherCheck
57+
}
58+
}
59+
else {
60+
$NewestPester = Find-Module -Name Pester -RequiredVersion $Version | Select-Object -First 1
61+
62+
Install-Module -Name Pester -RequiredVersion $NewestPester.Version -Scope CurrentUser -Force -Repository $NewestPester.Repository -SkipPublisherCheck
63+
}
64+
65+
Import-Module -Name Pester -RequiredVersion $NewestPester.Version -Verbose:$false
66+
}
67+
else {
68+
Write-Host "##vos[task.logissue type=warning]Falling back to version of Pester shipped with extension. To use a newer version please update the version of PowerShellGet available on this machine."
69+
Import-Module "$PSScriptRoot\4.6.0\Pester.psd1" -Force -Verbose:$false
70+
}
71+
72+
}

Extension/task/Pester.ps1

+4-26
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ param
3838

3939
[string]$CodeCoverageFolder,
4040

41-
[string]$ScriptBlock
41+
[string]$ScriptBlock,
42+
43+
[string]$TargetPesterVersion = "latest"
4244
)
4345

4446
Write-Host "scriptFolder $scriptFolder"
@@ -52,6 +54,7 @@ Write-Host "CodeCoverageFolder $CodeCoverageFolder"
5254
Write-Host "ScriptBlock $ScriptBlock"
5355

5456
Import-Module -Name "$PSScriptRoot\HelperModule.psm1" -Force
57+
Import-Pester -Version $TargetPesterVersion
5558

5659
if ($run32Bit -eq $true -and $env:Processor_Architecture -ne "x86") {
5760
# Get the command parameters
@@ -78,31 +81,6 @@ if ($PSBoundParameters.ContainsKey('additionalModulePath')) {
7881
$env:PSModulePath = $additionalModulePath + ';' + $env:PSModulePath
7982
}
8083

81-
if ((Get-Module -Name PowerShellGet -ListAvailable) -and
82-
(Get-Command Install-Module).Parameters.ContainsKey('SkipPublisherCheck')) {
83-
84-
try {
85-
$null = Get-PackageProvider -Name NuGet -ErrorAction Stop
86-
}
87-
catch {
88-
try {
89-
Install-PackageProvider -Name Nuget -RequiredVersion 2.8.5.201 -Scope CurrentUser -Force -Confirm:$false -ErrorAction Stop
90-
}
91-
catch {
92-
Write-Host "##vos[task.logissue type=warning]Falling back to version of Pester shipped with extension. To use a newer version please update the version of PowerShellGet available on this machine."
93-
Import-Module "$PSScriptRoot\4.6.0\Pester.psd1" -force
94-
}
95-
}
96-
$NewestPester = Find-Module -Name Pester | Sort-Object Version -Descending | Select-Object -First 1
97-
If ((Get-Module Pester -ListAvailable | Sort-Object Version -Descending| Select-Object -First 1).Version -lt $NewestPester.Version) {
98-
Install-Module -Name Pester -Scope CurrentUser -Force -Repository $NewestPester.Repository -SkipPublisherCheck
99-
}
100-
Import-Module -Name Pester
101-
}
102-
else {
103-
Write-Host "##vos[task.logissue type=warning]Falling back to version of Pester shipped with extension. To use a newer version please update the version of PowerShellGet available on this machine."
104-
Import-Module "$PSScriptRoot\4.6.0\Pester.psd1" -force
105-
}
10684

10785
$Parameters = @{
10886
PassThru = $True
+98-13
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
11

2-
3-
Import-Module -Name "$PSScriptRoot\..\..\Task\HelperModule.psm1" -Force
4-
52
Describe "Testing Helper Functions" {
63

4+
BeforeAll {
5+
Import-Module -Name "$PSScriptRoot\..\..\Task\HelperModule.psm1" -Force
6+
}
77
Context "Testing Get-HashtableFromString" {
88

9-
it "Can parse empty block" {
9+
It "Can parse empty block" {
1010
$actual = Get-HashtableFromString -line ""
1111
$actual.GetType() | Should -Be @{}.GetType()
1212
$actual.count | Should -Be 0
1313
}
1414

15-
it "Can parse block with no values but delimiter" {
15+
It "Can parse block with no values but delimiter" {
1616
$actual = Get-HashtableFromString -line ";"
1717
$actual.GetType() | Should -Be @{}.GetType()
1818
$actual.count | Should -Be 0
1919
}
2020

21-
it "Cannot parse block with invalid string" {
21+
It "Cannot parse block with invalid string" {
2222
$actual = Get-HashtableFromString -line ";"
2323
$actual.GetType() | Should -Be @{}.GetType()
2424
$actual.count | Should -Be 0
2525
}
2626

27-
it "Can parse two part block" {
27+
It "Can parse two part block" {
2828
$actual = Get-HashtableFromString -line "@{Path='C:\path\123'; Parameters=@{param1='111'; param2='222'}}"
2929
$actual.Path | Should -Be "C:\path\123"
3030
$actual.Parameters.param1 | Should -Be "111"
3131
$actual.Parameters.param2 | Should -Be "222"
3232
}
3333

34-
it "Can parse two hashtable part block" {
34+
It "Can parse two hashtable part block" {
3535
$actual = Get-HashtableFromString -line "@{Parameters1=@{param1='111'; param2='222'}; Parameters2=@{param1='111'; param2='222'}}"
3636
$actual.Parameters1.param1 | Should -Be "111"
3737
$actual.Parameters1.param2 | Should -Be "222"
3838
$actual.Parameters2.param1 | Should -Be "111"
3939
$actual.Parameters2.param2 | Should -Be "222"
4040
}
4141

42-
it "Can parse block with trailing ;" {
42+
It "Can parse block with trailing ;" {
4343
$actual = Get-HashtableFromString -line "@{Path='C:\path\123'; Parameters=@{param1='111'; param2='222'}};"
4444
$actual.Path | Should -Be "C:\path\123"
4545
$actual.Parameters.param1 | Should -Be "111"
4646
$actual.Parameters.param2 | Should -Be "222"
4747
}
4848

49-
it "Can parse three part block 1" {
49+
It "Can parse three part block 1" {
5050
$actual = Get-HashtableFromString -line "@{Path='C:\path\123'; x='y'; Parameters=@{param1='111'; param2='222'}}"
5151
$actual.Path | Should -Be "C:\path\123"
5252
$actual.Parameters.param1 | Should -Be "111"
5353
$actual.Parameters.param2 | Should -Be "222"
5454
$actual.x | Should -Be "y"
5555
}
5656

57-
it "Can parse three part block 2" {
57+
It "Can parse three part block 2" {
5858
$actual = Get-HashtableFromString -line "@{Path='C:\path\123'; Parameters=@{param1='111'; param2='222'}; x='y'}"
5959
$actual.Path | Should -Be "C:\path\123"
6060
$actual.Parameters.param1 | Should -Be "111"
6161
$actual.Parameters.param2 | Should -Be "222"
6262
$actual.x | Should -Be "y"
6363
}
64-
it "Can parse hashtable with ; in a value" {
64+
It "Can parse hashtable with ; in a value" {
6565
$actual = Get-HashtableFromString -line "@{Path='.\tests\script.tests.ps1'; Parameters=@{someVar='this'}},@{Path='.\tests\script2.tests.ps1'; Parameters=@{otherparam='foo.txt;bar.txt'}}"
6666
$actual.GetType().BaseType | Should -Be "Array"
6767
$actual[0].Path | Should -Be '.\tests\script.tests.ps1'
6868
$actual[0].Parameters.SomeVar | Should -Be 'this'
6969
$actual[1].Path | Should -Be '.\tests\script2.tests.ps1'
7070
$actual[1].Parameters.otherparam | Should -Be 'foo.txt;bar.txt'
7171
}
72-
it "Can parse hashtable with commas in a value" {
72+
It "Can parse hashtable with commas in a value" {
7373
$actual = Get-HashtableFromString -line "@{Path='.\tests\script.tests.ps1'; Parameters=@{someVar='this'}},@{Path='.\tests\script2.tests.ps1'; Parameters=@{otherparam='foo.txt;bar.txt';Param2='ValueGoesHere'}},@{path='.\tests\script3.tests.ps1';Parameters=@{inputvar='var,this,string'}}"
7474
$actual.GetType().BaseType | Should -Be "Array"
7575
$actual[0].Path | Should -Be '.\tests\script.tests.ps1'
@@ -80,7 +80,92 @@ Describe "Testing Helper Functions" {
8080
$actual[2].Path | Should -Be '.\tests\script3.tests.ps1'
8181
$actual[2].Parameters.inputvar | Should -Be 'var,this,string'
8282
}
83+
}
84+
85+
Context "Testing Import-Pester" {
86+
87+
BeforeAll {
88+
Mock -CommandName Import-Module -MockWith { }
89+
Mock -CommandName Install-Module -MockWith { $true }
90+
Mock -CommandName Write-host -MockWith { }
91+
Mock -CommandName Get-PSRepository -MockWith {[PSCustomObject]@{Name = 'PSGallery'}}
92+
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{SkipPublisherCheck='SomeValue'}}} -ParameterFilter {$Name -eq 'Install-Module'}
93+
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{AllowPrerelease='SomeValue'}}} -ParameterFilter {$Name -eq 'Find-Module'}
94+
}
95+
96+
It "Installs the latest version of Pester when on PS5+ and PowerShellGet is available" {
97+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}}
98+
Mock -CommandName Get-PackageProvider -MockWith { $True }
99+
100+
Import-Pester -Version "latest"
101+
102+
Assert-MockCalled -CommandName Import-Module -ParameterFilter {$RequiredVersion -eq "9.9.9"} -Scope It -Times 1
103+
}
104+
105+
It "Installs the latest version of Pester from PSGallery when multiple repositories are available" {
106+
Mock -CommandName Find-Module -MockWith { @(
107+
[PsCustomObject]@{Version=[version]::new(4,3,0);Repository='OtherRepository'}
108+
[PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}
109+
)
110+
}
111+
Mock -CommandName Get-PackageProvider -MockWith { $True }
112+
113+
Import-Pester -Version "latest"
114+
115+
Assert-MockCalled -CommandName Install-Module -Scope It -ParameterFilter {$Repository -eq 'PSGallery'}
116+
}
117+
118+
It "Installs the required version of NuGet provider when PowerShellGet is available and NuGet isn't already installed" {
119+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}}
120+
Mock -CommandName Get-PackageProvider -MockWith { throw }
121+
Mock -CommandName Install-PackageProvider -MockWith {}
122+
123+
Import-Pester -Version "latest"
124+
125+
Assert-MockCalled -CommandName Install-PackageProvider
126+
}
127+
128+
It "Should not install a new version of Pester when the latest is already installed" {
129+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=(Get-Module Pester).Version;Repository='PSGallery'}}
130+
Mock -CommandName Get-PackageProvider -MockWith { $True }
131+
132+
Import-Pester -Version "latest"
133+
134+
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
135+
}
136+
137+
It "Should install and import the specified version of Pester regardless of what is avaialble locally" {
138+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(4,2,0);Repository='PSGallery'}}
139+
Mock -CommandName Get-PackageProvider -MockWith { $True }
140+
141+
Import-Pester -Version 4.2.0
142+
143+
Assert-MockCalled -CommandName Install-Module -Times 1 -ParameterFilter { $RequiredVersion -eq "4.2.0"}
144+
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$RequiredVersion -eq "4.2.0"}
145+
}
146+
147+
It "Should not Install the latest version of Pester when on PowerShellGet is available but SkipPublisherCheck is not available" {
148+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}}
149+
Mock -CommandName Get-PackageProvider -MockWith { $True }
150+
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{OtherProperty='SomeValue'}} } -ParameterFilter {$Name -eq 'Install-Module'}
151+
152+
Import-Pester -Version "latest"
153+
154+
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
155+
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.6.0\Pester.psd1'}
156+
}
83157

158+
<#It "Loads Pester version that ships with task when not on PS5+ or PowerShellGet is unavailable" {
159+
Mock -CommandName Invoke-Pester -MockWith { }
160+
Mock -CommandName Import-Module -MockWith { }
161+
Mock -CommandName Write-Host -MockWith { }
162+
Mock -CommandName Write-Warning -MockWith { }
163+
Mock -CommandName Write-Error -MockWith { }
164+
Mock -CommandName Get-Module -MockWith { }
84165
166+
&$sut -ScriptFolder TestDrive:\ -ResultsFile TestDrive:\output.xml
167+
Assert-MockCalled Import-Module -ParameterFilter { $Name -eq "$pwd\4.6.0\Pester.psd1" }
168+
Assert-MockCalled Invoke-Pester
169+
}#>
85170
}
86171
}

0 commit comments

Comments
 (0)