Skip to content

Commit 4161055

Browse files
Hampson JaredHampson Jared
Hampson Jared
authored and
Hampson Jared
committed
Update Helper.Tests.ps1 to fix HelperModule path and get all tests passing before making any other changes
1 parent a44ff1d commit 4161055

File tree

1 file changed

+79
-77
lines changed

1 file changed

+79
-77
lines changed

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

+79-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") -Forc
66
}
77
Context "Testing Get-HashtableFromString" {
88

@@ -82,103 +82,105 @@ 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-
}
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+
}
9596

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 }
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 }
99100

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-
)
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 not install a new version of Pester when the latest is already installed" {
130+
Mock -CommandName Find-Module -MockWith { [PsCustomObject]@{Version=(Get-Module Pester).Version;Repository='PSGallery'}}
131+
Mock -CommandName Get-PackageProvider -MockWith { $True }
133132

134-
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
135-
}
133+
Import-Pester -Version "latest"
136134

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 }
135+
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
136+
}
140137

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

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-
}
142+
Import-Pester -Version 4.2.0
146143

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'}
144+
Assert-MockCalled -CommandName Install-Module -Times 1 -ParameterFilter { $RequiredVersion -eq "4.2.0"}
145+
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$RequiredVersion -eq "4.2.0"}
146+
}
151147

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

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-
}
153+
Import-Pester -Version "latest"
154+
155+
Assert-MockCalled -CommandName Install-Module -Times 0 -Scope It
156+
Assert-MockCalled -CommandName Import-Module -Times 1 -ParameterFilter {$Name -like '*\4.10.1\Pester.psd1'}
157+
}
157158

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'}
159+
It "Should fall back to build in version of Pester when no repositories are available" {
160+
Mock -CommandName Get-PSRepository -MockWith {}
161+
Mock -CommandName Get-Command -MockWith { [PsCustomObject]@{Parameters=@{SkipPublisherCheck='SomeValue'}}} -ParameterFilter {$Name -eq 'Install-Module'}
161162

162-
Import-Pester -Version "latest"
163+
Import-Pester -Version "latest"
163164

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

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-
}
172+
<#It "Loads Pester version that ships with task when not on PS5+ or PowerShellGet is unavailable" {
173+
Mock -CommandName Invoke-Pester -MockWith { }
174+
Mock -CommandName Import-Module -MockWith { }
175+
Mock -CommandName Write-Host -MockWith { }
176+
Mock -CommandName Write-Warning -MockWith { }
177+
Mock -CommandName Write-Error -MockWith { }
178+
Mock -CommandName Get-Module -MockWith { }
179+
180+
&$sut -ScriptFolder TestDrive:\ -ResultsFile TestDrive:\output.xml
181+
Assert-MockCalled Import-Module -ParameterFilter { $Name -eq "$pwd\4.10.1\Pester.psd1" }
182+
Assert-MockCalled Invoke-Pester
183+
}#>
184+
}
185+
}
184186
}

0 commit comments

Comments
 (0)