From 4161055150b6c49e55a97e643b0c62caa6c52a64 Mon Sep 17 00:00:00 2001 From: Hampson Jared Date: Wed, 10 Jun 2020 15:49:16 -0500 Subject: [PATCH 1/4] Update Helper.Tests.ps1 to fix HelperModule path and get all tests passing before making any other changes --- .../test/PesterTask/Helper.Tests.ps1 | 156 +++++++++--------- 1 file changed, 79 insertions(+), 77 deletions(-) diff --git a/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 b/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 index 1fc2e5b..0317e39 100644 --- a/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 +++ b/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 @@ -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") -Forc } Context "Testing Get-HashtableFromString" { @@ -82,103 +82,105 @@ 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'} - } + 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 } + 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 } - 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 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 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-Module -Times 0 -Scope It - } + Import-Pester -Version "latest" - 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 } + Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It + } - Import-Pester -Version 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 } - 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"} - } + Import-Pester -Version 4.2.0 - 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'} + 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"} + } - Import-Pester -Version "latest" + 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'} - Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It - Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'} - } + 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 + }#> + } + } } From 1d547bb74972854a357e2166ff25e44521603826 Mon Sep 17 00:00:00 2001 From: Hampson Jared Date: Wed, 10 Jun 2020 15:50:48 -0500 Subject: [PATCH 2/4] Add test case for when Find-Package cannot find Pester module --- .../PesterTask/test/PesterTask/Helper.Tests.ps1 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 b/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 index 0317e39..7baa6d6 100644 --- a/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 +++ b/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 @@ -126,6 +126,21 @@ Describe "Testing Helper Functions" { Assert-MockCalled -CommandName Install-PackageProvider } + 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'} + } + 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 } From b49736d9c0b7b6c9949c99f838c3f0d943613d1c Mon Sep 17 00:00:00 2001 From: Hampson Jared Date: Wed, 10 Jun 2020 16:08:49 -0500 Subject: [PATCH 3/4] Update HelperModule.psm1 to fix #18 When PowershellGallery is not reachable (e.g. on an airgapped build server), the Find-Module command writes an error message stating that it cannot find the given module (in this case, Pester). This error message caused the Pester extension task to fail when used in an Azure DevOps pipeline, even though the tests would be executed as expected. To fix this, we set errors from Find-Module to be terminating errors so they can be handled by the try/catch block and fall back to the bundled Pester module. --- .../PesterTask/PesterV9/HelperModule.psm1 | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Extension/PesterTask/PesterV9/HelperModule.psm1 b/Extension/PesterTask/PesterV9/HelperModule.psm1 index 5a74d57..784de16 100644 --- a/Extension/PesterTask/PesterV9/HelperModule.psm1 +++ b/Extension/PesterTask/PesterV9/HelperModule.psm1 @@ -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." From 4276bdbc454052a6b367f0e4d5419b7ae0241af7 Mon Sep 17 00:00:00 2001 From: jhampson-dbre Date: Wed, 10 Jun 2020 17:52:40 -0500 Subject: [PATCH 4/4] Update Helper.Tests.ps1 Fix typo on HelperModule import --- Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 b/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 index 7baa6d6..28e33ef 100644 --- a/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 +++ b/Extension/PesterTask/test/PesterTask/Helper.Tests.ps1 @@ -2,7 +2,7 @@ Describe "Testing Helper Functions" { BeforeAll { - Import-Module -Name (Resolve-Path "$PSScriptRoot/../../PesterV9/HelperModule.psm1") -Forc + Import-Module -Name (Resolve-Path "$PSScriptRoot/../../PesterV9/HelperModule.psm1") -Force } Context "Testing Get-HashtableFromString" {