Tests/Install-OriAzBopPrerequisity.tests.ps1
. $PSScriptRoot\..\Private\Invoke-OriAzExrExceptionRetry.ps1 . $PSScriptRoot\..\Private\Test-OriAzExrIsInExceptionList.ps1 . $PSScriptRoot\..\Public\Install-OriAzBopPrerequisity.ps1 $InstalledPowerShellGet = Get-Module -Name PowerShellGet -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1 if(![string]::IsNullOrEmpty($InstalledPowerShellGet) ` -and ![string]::IsNullOrEmpty($InstalledPowerShellGet.Version) ` -and $InstalledPowerShellGet.Version -lt [Version]::new(2,2,3)) { Write-Verbose "PowerShellGet is already installed in version $($InstalledPowerShellGet.Version)" Invoke-OriAzExrExceptionRetry ` -ListedExceptions @('Access to the path*is denied.') ` -ScriptBlockToRun { Install-Module -Name PowerShellGet -MinimumVersion 2.2.3 -Repository PSGallery -Force -SkipPublisherCheck -Confirm:$false } } Import-Module -Name PowerShellGet -MinimumVersion 2.2.3 Describe 'OriAzBopBoostrapOriPsgallery\Install-OriAzBopPrerequisity' { Context 'Succesfull execution module not exists' { Mock -CommandName Get-Module -Verifiable -MockWith { return $null } Mock -CommandName Invoke-OriAzExrExceptionRetry -Verifiable -MockWith { $ScriptBlockToRun.Invoke(); } Mock -CommandName Install-Module -Verifiable Mock -CommandName Import-Module -Verifiable Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { { Install-OriAzBopPrerequisity -Name PowerShellGet -MinimumVersion 2.2.3 } | Should -Not -Throw } Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Invoke-OriAzExrExceptionRetry -Times 1 -Exactly Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" -and $MinimumVersion -eq [Version]::new(2,2,3) } Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -ilike "-- End of Install-OriAzBopPrerequisity --" } Assert-VerifiableMock } Context 'Succesfull execution module exists but under Minimum version' { Mock -CommandName Get-Module -Verifiable -MockWith { return @{ Version = [Version]::new(2,2,1) } } Mock -CommandName Invoke-OriAzExrExceptionRetry -Verifiable -MockWith { $ScriptBlockToRun.Invoke(); } Mock -CommandName Install-Module -Verifiable Mock -CommandName Import-Module -Verifiable Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { { Install-OriAzBopPrerequisity -Name PowerShellGet -MinimumVersion 2.2.3 } | Should -Not -Throw } Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Invoke-OriAzExrExceptionRetry -Times 1 -Exactly Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" -and $MinimumVersion -eq [Version]::new(2,2,3) } Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq "-- End of Install-OriAzBopPrerequisity --" } Assert-VerifiableMock } Context 'Succesfull execution module exists same as Minimum version' { Mock -CommandName Get-Module -Verifiable -MockWith { return @{ Version = [Version]::new(2,2,3) } } Mock -CommandName Invoke-OriAzExrExceptionRetry Mock -CommandName Install-Module Mock -CommandName Import-Module Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { { Install-OriAzBopPrerequisity -Name PowerShellGet -MinimumVersion 2.2.3 } | Should -Not -Throw } Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Invoke-OriAzExrExceptionRetry -Times 0 -Exactly Assert-MockCalled Install-Module -Times 0 -Exactly Assert-MockCalled Import-Module -Times 1 -Exactly Assert-MockCalled Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq "-- End of Install-OriAzBopPrerequisity --" } Assert-VerifiableMock } Context 'Succesfull execution module exists higer than Minimum version' { Mock -CommandName Get-Module -Verifiable -MockWith { return @{ Version = [Version]::new(2,2,4) } } Mock -CommandName Invoke-OriAzExrExceptionRetry Mock -CommandName Install-Module Mock -CommandName Import-Module Mock -CommandName Write-Verbose -Verifiable it 'Should be succesfully processed' { { Install-OriAzBopPrerequisity -Name PowerShellGet -MinimumVersion 2.2.3 } | Should -Not -Throw } Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Invoke-OriAzExrExceptionRetry -Times 0 -Exactly Assert-MockCalled Install-Module -Times 0 -Exactly Assert-MockCalled Import-Module -Times 1 -Exactly Assert-MockCalled Write-Verbose -Times 1 -Exactly -ParameterFilter { $Message -eq "-- End of Install-OriAzBopPrerequisity --" } Assert-VerifiableMock } } |