Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress error messages when Powershell Gallery isn't reachable - fixes #18 #36

Merged
merged 4 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Extension/PesterTask/PesterV9/HelperModule.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,32 @@ function Import-Pester {
(Get-PSRepository)) {

try {
$null = Get-PackageProvider -Name NuGet -ErrorAction Stop
}
catch {
try {
Install-PackageProvider -Name Nuget -RequiredVersion 2.8.5.208 -Scope CurrentUser -Force -Confirm:$false -ErrorAction Stop
$null = Get-PackageProvider -Name NuGet -ErrorAction Stop
}
catch {
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."
Import-Module -Name (Join-Path $PSScriptRoot "4.10.1\Pester.psd1") -Force -Verbose:$false -Global
Install-PackageProvider -Name Nuget -RequiredVersion 2.8.5.208 -Scope CurrentUser -Force -Confirm:$false -ErrorAction Stop
}
}

if ($Version -eq "latest") {
$NewestPester = Find-Module -Name Pester -MaximumVersion 4.99.99 | Sort-Object Version -Descending | Select-Object -First 1
if ($Version -eq "latest") {
$NewestPester = Find-Module -Name Pester -MaximumVersion 4.99.99 -ErrorAction Stop | Sort-Object Version -Descending | Select-Object -First 1

If ((Get-Module Pester -ListAvailable | Sort-Object Version -Descending| Select-Object -First 1).Version -lt $NewestPester.Version) {
Install-Module -Name Pester -RequiredVersion $NewestPester.Version -Scope CurrentUser -Force -Repository $NewestPester.Repository -SkipPublisherCheck
}
}
else {
$NewestPester = Find-Module -Name Pester -RequiredVersion $Version -ErrorAction Stop | Select-Object -First 1

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

Install-Module -Name Pester -RequiredVersion $NewestPester.Version -Scope CurrentUser -Force -Repository $NewestPester.Repository -SkipPublisherCheck
Import-Module -Name Pester -RequiredVersion $NewestPester.Version -Verbose:$false -Global
}
catch {
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."
Import-Module -Name (Join-Path $PSScriptRoot "4.10.1\Pester.psd1") -Force -Verbose:$false -Global
}

Import-Module -Name Pester -RequiredVersion $NewestPester.Version -Verbose:$false -Global
}
else {
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."
Expand Down
171 changes: 94 additions & 77 deletions Extension/PesterTask/test/PesterTask/Helper.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Describe "Testing Helper Functions" {

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

Expand Down Expand Up @@ -82,103 +82,120 @@ Describe "Testing Helper Functions" {
}
}

Context "Testing Import-Pester" {
InModuleScope "HelperModule" {
Context "Testing Import-Pester" {

BeforeAll {
Mock -CommandName Import-Module -MockWith { }
Mock -CommandName Install-Module -MockWith { $true }
Mock -CommandName Write-host -MockWith { }
Mock -CommandName Get-PSRepository -MockWith {[PSCustomObject]@{Name = 'PSGallery'}}
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{SkipPublisherCheck='SomeValue'}}} -ParameterFilter {$Name -eq 'Install-Module'}
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{AllowPrerelease='SomeValue'}}} -ParameterFilter {$Name -eq 'Find-Module'}
}

It "Installs the latest version of Pester when on PS5+ and PowerShellGet is available" {
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}}
Mock -CommandName Get-PackageProvider -MockWith { $True }
BeforeAll {
Mock -CommandName Import-Module -MockWith { }
Mock -CommandName Install-Module -MockWith { $true }
Mock -CommandName Write-host -MockWith { }
Mock -CommandName Get-PSRepository -MockWith {[PSCustomObject]@{Name = 'PSGallery'}}
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{SkipPublisherCheck='SomeValue'}}} -ParameterFilter {$Name -eq 'Install-Module'}
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{AllowPrerelease='SomeValue'}}} -ParameterFilter {$Name -eq 'Find-Module'}
}

Import-Pester -Version "latest"

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

It "Installs the latest version of Pester from PSGallery when multiple repositories are available" {
Mock -CommandName Find-Module -MockWith { @(
[PsCustomObject]@{Version=[version]::new(4,3,0);Repository='OtherRepository'}
[PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}
)
Import-Pester -Version "latest"

Assert-MockCalled -CommandName Import-Module -ParameterFilter {$RequiredVersion -eq "9.9.9"} -Scope It -Times 1
}
Mock -CommandName Get-PackageProvider -MockWith { $True }

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

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

It "Installs the required version of NuGet provider when PowerShellGet is available and NuGet isn't already installed" {
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}}
Mock -CommandName Get-PackageProvider -MockWith { throw }
Mock -CommandName Install-PackageProvider -MockWith {}
Assert-MockCalled -CommandName Install-Module -Scope It -ParameterFilter {$Repository -eq 'PSGallery'}
}

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

Assert-MockCalled -CommandName Install-PackageProvider
}
Import-Pester -Version "latest"

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

Import-Pester -Version "latest"
It "Should fall back to build in version of Pester when Find-Module can't find Pester" {
Mock -CommandName Find-Module -MockWith { Write-Error "No match was found for the specified search
criteria and module name 'Pester'"}
Mock -CommandName Get-PackageProvider -MockWith { $True }

Import-Pester -Version "latest"

Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
Assert-MockCalled -CommandName Find-Module -Times 1 -Scope It
Assert-MockCalled -CommandName Write-Host -Times 1 -Scope It -ParameterFilter {
$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."
}
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
}

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

It "Should install and import the specified version of Pester regardless of what is avaialble locally" {
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(4,2,0);Repository='PSGallery'}}
Mock -CommandName Get-PackageProvider -MockWith { $True }
Import-Pester -Version "latest"

Import-Pester -Version 4.2.0
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
}

Assert-MockCalled -CommandName Install-Module -Times 1 -ParameterFilter { $RequiredVersion -eq "4.2.0"}
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$RequiredVersion -eq "4.2.0"}
}
It "Should install and import the specified version of Pester regardless of what is avaialble locally" {
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(4,2,0);Repository='PSGallery'}}
Mock -CommandName Get-PackageProvider -MockWith { $True }

It "Should not Install the latest version of Pester when on PowerShellGet is available but SkipPublisherCheck is not available" {
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=[version]::new(9,9,9);Repository='PSGallery'}}
Mock -CommandName Get-PackageProvider -MockWith { $True }
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{OtherProperty='SomeValue'}} } -ParameterFilter {$Name -eq 'Install-Module'}
Import-Pester -Version 4.2.0

Import-Pester -Version "latest"
Assert-MockCalled -CommandName Install-Module -Times 1 -ParameterFilter { $RequiredVersion -eq "4.2.0"}
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$RequiredVersion -eq "4.2.0"}
}

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

Import-Pester -Version "latest"

Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
}

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

Import-Pester -Version "latest"
Import-Pester -Version "latest"

Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
Assert-MockCalled -CommandName Write-Host -Times 1 -Scope It -ParameterFilter {
$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."
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
Assert-MockCalled -CommandName Write-Host -Times 1 -Scope It -ParameterFilter {
$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."
}
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
}
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
}

<#It "Loads Pester version that ships with task when not on PS5+ or PowerShellGet is unavailable" {
Mock -CommandName Invoke-Pester -MockWith { }
Mock -CommandName Import-Module -MockWith { }
Mock -CommandName Write-Host -MockWith { }
Mock -CommandName Write-Warning -MockWith { }
Mock -CommandName Write-Error -MockWith { }
Mock -CommandName Get-Module -MockWith { }

&$sut -ScriptFolder TestDrive:\ -ResultsFile TestDrive:\output.xml
Assert-MockCalled Import-Module -ParameterFilter { $Name -eq "$pwd\4.10.1\Pester.psd1" }
Assert-MockCalled Invoke-Pester
}#>
}
<#It "Loads Pester version that ships with task when not on PS5+ or PowerShellGet is unavailable" {
Mock -CommandName Invoke-Pester -MockWith { }
Mock -CommandName Import-Module -MockWith { }
Mock -CommandName Write-Host -MockWith { }
Mock -CommandName Write-Warning -MockWith { }
Mock -CommandName Write-Error -MockWith { }
Mock -CommandName Get-Module -MockWith { }

&$sut -ScriptFolder TestDrive:\ -ResultsFile TestDrive:\output.xml
Assert-MockCalled Import-Module -ParameterFilter { $Name -eq "$pwd\4.10.1\Pester.psd1" }
Assert-MockCalled Invoke-Pester
}#>
}
}
}