Skip to content

Commit 8e61d26

Browse files
Merge pull request #36 from jhampson-dbre/master
Suppress error messages when Powershell Gallery isn't reachable - fixes #18
2 parents a44ff1d + 4276bdb commit 8e61d26

File tree

2 files changed

+110
-93
lines changed

2 files changed

+110
-93
lines changed

Extension/PesterTask/PesterV9/HelperModule.psm1

+16-16
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,32 @@ function Import-Pester {
3838
(Get-PSRepository)) {
3939

4040
try {
41-
$null = Get-PackageProvider -Name NuGet -ErrorAction Stop
42-
}
43-
catch {
4441
try {
45-
Install-PackageProvider -Name Nuget -RequiredVersion 2.8.5.208 -Scope CurrentUser -Force -Confirm:$false -ErrorAction Stop
42+
$null = Get-PackageProvider -Name NuGet -ErrorAction Stop
4643
}
4744
catch {
48-
Write-Host "##vso[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."
49-
Import-Module -Name (Join-Path $PSScriptRoot "4.10.1\Pester.psd1") -Force -Verbose:$false -Global
45+
Install-PackageProvider -Name Nuget -RequiredVersion 2.8.5.208 -Scope CurrentUser -Force -Confirm:$false -ErrorAction Stop
5046
}
51-
}
5247

53-
if ($Version -eq "latest") {
54-
$NewestPester = Find-Module -Name Pester -MaximumVersion 4.99.99 | Sort-Object Version -Descending | Select-Object -First 1
48+
if ($Version -eq "latest") {
49+
$NewestPester = Find-Module -Name Pester -MaximumVersion 4.99.99 -ErrorAction Stop | Sort-Object Version -Descending | Select-Object -First 1
50+
51+
If ((Get-Module Pester -ListAvailable | Sort-Object Version -Descending| Select-Object -First 1).Version -lt $NewestPester.Version) {
52+
Install-Module -Name Pester -RequiredVersion $NewestPester.Version -Scope CurrentUser -Force -Repository $NewestPester.Repository -SkipPublisherCheck
53+
}
54+
}
55+
else {
56+
$NewestPester = Find-Module -Name Pester -RequiredVersion $Version -ErrorAction Stop | Select-Object -First 1
5557

56-
If ((Get-Module Pester -ListAvailable | Sort-Object Version -Descending| Select-Object -First 1).Version -lt $NewestPester.Version) {
5758
Install-Module -Name Pester -RequiredVersion $NewestPester.Version -Scope CurrentUser -Force -Repository $NewestPester.Repository -SkipPublisherCheck
5859
}
59-
}
60-
else {
61-
$NewestPester = Find-Module -Name Pester -RequiredVersion $Version | Select-Object -First 1
6260

63-
Install-Module -Name Pester -RequiredVersion $NewestPester.Version -Scope CurrentUser -Force -Repository $NewestPester.Repository -SkipPublisherCheck
61+
Import-Module -Name Pester -RequiredVersion $NewestPester.Version -Verbose:$false -Global
62+
}
63+
catch {
64+
Write-Host "##vso[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."
65+
Import-Module -Name (Join-Path $PSScriptRoot "4.10.1\Pester.psd1") -Force -Verbose:$false -Global
6466
}
65-
66-
Import-Module -Name Pester -RequiredVersion $NewestPester.Version -Verbose:$false -Global
6767
}
6868
else {
6969
Write-Host "##vso[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."

Extension/PesterTask/test/PesterTask/Helper.Tests.ps1

+94-77
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Describe "Testing Helper Functions" {
33

44
BeforeAll {
5-
Import-Module -Name (Resolve-Path "$PSScriptRoot/../../task/HelperModule.psm1") -Force
5+
Import-Module -Name (Resolve-Path "$PSScriptRoot/../../PesterV9/HelperModule.psm1") -Force
66
}
77
Context "Testing Get-HashtableFromString" {
88

@@ -82,103 +82,120 @@ Describe "Testing Helper Functions" {
8282
}
8383
}
8484

85-
Context "Testing Import-Pester" {
85+
InModuleScope "HelperModule" {
86+
Context "Testing Import-Pester" {
8687

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 }
88+
BeforeAll {
89+
Mock -CommandName Import-Module -MockWith { }
90+
Mock -CommandName Install-Module -MockWith { $true }
91+
Mock -CommandName Write-host -MockWith { }
92+
Mock -CommandName Get-PSRepository -MockWith {[PSCustomObject]@{Name = 'PSGallery'}}
93+
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{SkipPublisherCheck='SomeValue'}}} -ParameterFilter {$Name -eq 'Install-Module'}
94+
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{AllowPrerelease='SomeValue'}}} -ParameterFilter {$Name -eq 'Find-Module'}
95+
}
9996

100-
Import-Pester -Version "latest"
101-
102-
Assert-MockCalled -CommandName Import-Module -ParameterFilter {$RequiredVersion -eq "9.9.9"} -Scope It -Times 1
103-
}
97+
It "Installs the latest version of Pester when on PS5+ and PowerShellGet is available" {
98+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}}
99+
Mock -CommandName Get-PackageProvider -MockWith { $True }
104100

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-
)
101+
Import-Pester -Version "latest"
102+
103+
Assert-MockCalled -CommandName Import-Module -ParameterFilter {$RequiredVersion -eq "9.9.9"} -Scope It -Times 1
110104
}
111-
Mock -CommandName Get-PackageProvider -MockWith { $True }
112105

113-
Import-Pester -Version "latest"
106+
It "Installs the latest version of Pester from PSGallery when multiple repositories are available" {
107+
Mock -CommandName Find-Module -MockWith { @(
108+
[PsCustomObject]@{Version=[version]::new(4,3,0);Repository='OtherRepository'}
109+
[PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}
110+
)
111+
}
112+
Mock -CommandName Get-PackageProvider -MockWith { $True }
114113

115-
Assert-MockCalled -CommandName Install-Module -Scope It -ParameterFilter {$Repository -eq 'PSGallery'}
116-
}
114+
Import-Pester -Version "latest"
117115

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 {}
116+
Assert-MockCalled -CommandName Install-Module -Scope It -ParameterFilter {$Repository -eq 'PSGallery'}
117+
}
122118

123-
Import-Pester -Version "latest"
119+
It "Installs the required version of NuGet provider when PowerShellGet is available and NuGet isn't already installed" {
120+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}}
121+
Mock -CommandName Get-PackageProvider -MockWith { throw }
122+
Mock -CommandName Install-PackageProvider -MockWith {}
124123

125-
Assert-MockCalled -CommandName Install-PackageProvider
126-
}
124+
Import-Pester -Version "latest"
127125

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 }
126+
Assert-MockCalled -CommandName Install-PackageProvider
127+
}
131128

132-
Import-Pester -Version "latest"
129+
It "Should fall back to build in version of Pester when Find-Module can't find Pester" {
130+
Mock -CommandName Find-Module -MockWith { Write-Error "No match was found for the specified search
131+
criteria and module name 'Pester'"}
132+
Mock -CommandName Get-PackageProvider -MockWith { $True }
133+
134+
Import-Pester -Version "latest"
135+
136+
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
137+
Assert-MockCalled -CommandName Find-Module -Times 1 -Scope It
138+
Assert-MockCalled -CommandName Write-Host -Times 1 -Scope It -ParameterFilter {
139+
$Object -eq "##vso[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."
140+
}
141+
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
142+
}
133143

134-
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
135-
}
144+
It "Should not install a new version of Pester when the latest is already installed" {
145+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=(Get-Module Pester).Version;Repository='PSGallery'}}
146+
Mock -CommandName Get-PackageProvider -MockWith { $True }
136147

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 }
148+
Import-Pester -Version "latest"
140149

141-
Import-Pester -Version 4.2.0
150+
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
151+
}
142152

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-
}
153+
It "Should install and import the specified version of Pester regardless of what is avaialble locally" {
154+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(4,2,0);Repository='PSGallery'}}
155+
Mock -CommandName Get-PackageProvider -MockWith { $True }
146156

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'}
157+
Import-Pester -Version 4.2.0
151158

152-
Import-Pester -Version "latest"
159+
Assert-MockCalled -CommandName Install-Module -Times 1 -ParameterFilter { $RequiredVersion -eq "4.2.0"}
160+
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$RequiredVersion -eq "4.2.0"}
161+
}
153162

154-
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
155-
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
156-
}
163+
It "Should not Install the latest version of Pester when on PowerShellGet is available but SkipPublisherCheck is not available" {
164+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}}
165+
Mock -CommandName Get-PackageProvider -MockWith { $True }
166+
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{OtherProperty='SomeValue'}} } -ParameterFilter {$Name -eq 'Install-Module'}
167+
168+
Import-Pester -Version "latest"
169+
170+
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
171+
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
172+
}
157173

158-
It "Should fall back to build in version of Pester when no repositories are available" {
159-
Mock -CommandName Get-PSRepository -MockWith {}
160-
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{SkipPublisherCheck='SomeValue'}}} -ParameterFilter {$Name -eq 'Install-Module'}
174+
It "Should fall back to build in version of Pester when no repositories are available" {
175+
Mock -CommandName Get-PSRepository -MockWith {}
176+
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{SkipPublisherCheck='SomeValue'}}} -ParameterFilter {$Name -eq 'Install-Module'}
161177

162-
Import-Pester -Version "latest"
178+
Import-Pester -Version "latest"
163179

164-
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
165-
Assert-MockCalled -CommandName Write-Host -Times 1 -Scope It -ParameterFilter {
166-
$Object -eq "##vso[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."
180+
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
181+
Assert-MockCalled -CommandName Write-Host -Times 1 -Scope It -ParameterFilter {
182+
$Object -eq "##vso[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."
183+
}
184+
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
167185
}
168-
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
169-
}
170186

171-
<#It "Loads Pester version that ships with task when not on PS5+ or PowerShellGet is unavailable" {
172-
Mock -CommandName Invoke-Pester -MockWith { }
173-
Mock -CommandName Import-Module -MockWith { }
174-
Mock -CommandName Write-Host -MockWith { }
175-
Mock -CommandName Write-Warning -MockWith { }
176-
Mock -CommandName Write-Error -MockWith { }
177-
Mock -CommandName Get-Module -MockWith { }
178-
179-
&$sut -ScriptFolder TestDrive:\ -ResultsFile TestDrive:\output.xml
180-
Assert-MockCalled Import-Module -ParameterFilter { $Name -eq "$pwd\4.10.1\Pester.psd1" }
181-
Assert-MockCalled Invoke-Pester
182-
}#>
183-
}
187+
<#It "Loads Pester version that ships with task when not on PS5+ or PowerShellGet is unavailable" {
188+
Mock -CommandName Invoke-Pester -MockWith { }
189+
Mock -CommandName Import-Module -MockWith { }
190+
Mock -CommandName Write-Host -MockWith { }
191+
Mock -CommandName Write-Warning -MockWith { }
192+
Mock -CommandName Write-Error -MockWith { }
193+
Mock -CommandName Get-Module -MockWith { }
194+
195+
&$sut -ScriptFolder TestDrive:\ -ResultsFile TestDrive:\output.xml
196+
Assert-MockCalled Import-Module -ParameterFilter { $Name -eq "$pwd\4.10.1\Pester.psd1" }
197+
Assert-MockCalled Invoke-Pester
198+
}#>
199+
}
200+
}
184201
}

0 commit comments

Comments
 (0)