Tests/Install-OriAzBopPrerequisityForce.tests.ps1
. $PSScriptRoot\..\Private\Invoke-OriAzExrExceptionRetry.ps1 . $PSScriptRoot\..\Private\Test-OriAzExrIsInExceptionList.ps1 . $PSScriptRoot\..\Public\Install-OriAzBopPrerequisityForce.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)) { Invoke-OriAzExrExceptionRetry ` -ListedExceptions @('Access to the path*is denied.') ` -ScriptBlockToRun { Install-Module -Name PowerShellGet -MinimumVersion 2.2.3 -Repository PSGallery -Force -Confirm:$false } } Import-Module -Name PowerShellGet -MinimumVersion 2.2.3 Describe 'OriAzBopBoostrapOriPsgallery\Install-OriAzBopPrerequisityForce' { Context 'Succesfull excution module not exists' { Mock -CommandName Invoke-WebRequest -Verifiable -MockWith { return ";" } Mock -CommandName Get-Module -Verifiable -MockWith { return @{ Name = "PackageManagement" Version = [Version]::new(1,0,0,1) Path = [System.IO.Path]::GetTempPath() } } -ParameterFilter { $Name -eq "PackageManagement" } Mock -CommandName Get-Module -Verifiable -MockWith { return @{ Name = "PowershellGet" Version = [Version]::new(1,0,0,1) Path = [System.IO.Path]::GetTempPath() } } -ParameterFilter { $Name -eq "PowershellGet" } Mock -CommandName New-Item -Verifiable Mock -CommandName Save-Package -Verifiable Mock -CommandName Import-Module -Verifiable Mock -CommandName Write-Debug -Verifiable Mock -CommandName Move-Item -Verifiable it 'Should be succesfully processed' { { Install-OriAzBopPrerequisityForce -Verbose } | Should -Not -Throw } Assert-MockCalled Invoke-WebRequest -Times 1 -Exactly Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PackageManagement" } Assert-MockCalled New-Item -Times 1 -Exactly Assert-MockCalled Move-Item -Times 2 -Exactly Assert-MockCalled Save-Package -Times 2 -Exactly Assert-MockCalled Import-Module -Times 1 -Exactly Assert-MockCalled Write-Debug -Times 1 -Exactly -ParameterFilter { $Message -eq "-- End of Install-OriAzBopPrerequisityForce --" } Assert-VerifiableMock } Context 'Succesfull excution module not exists' { Mock -CommandName Invoke-WebRequest -Verifiable -MockWith { return ";" } Mock -CommandName Get-Module -Verifiable -MockWith { return @{ Name = "PackageManagement" Version = [Version]::new(1,4,6) } } -ParameterFilter { $Name -eq "PackageManagement" } Mock -CommandName Get-Module -Verifiable -MockWith { return @{ Name = "PowershellGet" Version = [Version]::new(2,2,3) } } -ParameterFilter { $Name -eq "PowershellGet" } Mock -CommandName New-Item Mock -CommandName Save-Package Mock -CommandName Import-Module Mock -CommandName Write-Debug -Verifiable Mock -CommandName Move-Item it 'Should be succesfully processed no update required' { { Install-OriAzBopPrerequisityForce -Verbose } | Should -Not -Throw } Assert-MockCalled Invoke-WebRequest -Times 1 -Exactly Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PackageManagement" } Assert-MockCalled New-Item -Times 0 -Exactly Assert-MockCalled Move-Item -Times 0 -Exactly Assert-MockCalled Save-Package -Times 0 -Exactly Assert-MockCalled Import-Module -Times 0 -Exactly Assert-MockCalled Write-Debug -Times 1 -Exactly -ParameterFilter { $Message -eq "-- End of Install-OriAzBopPrerequisityForce --" } Assert-VerifiableMock } Context 'Succesfull excution module not exists' { Mock -CommandName Invoke-WebRequest -Verifiable -MockWith { return ";" } Mock -CommandName Get-Module -Verifiable -MockWith { return $null } -ParameterFilter { $Name -eq "PackageManagement" } Mock -CommandName Get-Module -Verifiable -MockWith { return @{ Name = "PowershellGet" Version = [Version]::new(2,2,3) } } -ParameterFilter { $Name -eq "PowershellGet" } Mock -CommandName New-Item Mock -CommandName Save-Package Mock -CommandName Import-Module Mock -CommandName Write-Debug Mock -CommandName Move-Item it 'Should rise exception' { { Install-OriAzBopPrerequisityForce -Verbose } | Should -Throw "Some version of Powershell module PackageManagement and PowershellGet is required. In this case please copy these modules manually." } Assert-MockCalled Invoke-WebRequest -Times 1 -Exactly Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PackageManagement" } Assert-MockCalled New-Item -Times 0 -Exactly Assert-MockCalled Move-Item -Times 0 -Exactly Assert-MockCalled Save-Package -Times 0 -Exactly Assert-MockCalled Import-Module -Times 0 -Exactly Assert-MockCalled Write-Debug -Times 0 -Exactly -ParameterFilter { $Message -eq "-- End of Install-OriAzBopPrerequisityForce --" } Assert-VerifiableMock } Context 'Succesfull excution module not exists' { Mock -CommandName Invoke-WebRequest -Verifiable -MockWith { return ";" } Mock -CommandName Get-Module -Verifiable -MockWith { return $null } -ParameterFilter { $Name -eq "PowershellGet" } Mock -CommandName Get-Module -Verifiable -MockWith { return @{ Name = "PackageManagement" Version = [Version]::new(1,4,6) } } -ParameterFilter { $Name -eq "PackageManagement" } Mock -CommandName New-Item Mock -CommandName Save-Package Mock -CommandName Import-Module Mock -CommandName Write-Debug Mock -CommandName Move-Item it 'Should rise exception' { { Install-OriAzBopPrerequisityForce -Verbose } | Should -Throw "Some version of Powershell module PackageManagement and PowershellGet is required. In this case please copy these modules manually." } Assert-MockCalled Invoke-WebRequest -Times 1 -Exactly Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PowerShellGet" } Assert-MockCalled Get-Module -Times 1 -Exactly -ParameterFilter { $Name -eq "PackageManagement" } Assert-MockCalled New-Item -Times 0 -Exactly Assert-MockCalled Move-Item -Times 0 -Exactly Assert-MockCalled Save-Package -Times 0 -Exactly Assert-MockCalled Import-Module -Times 0 -Exactly Assert-MockCalled Write-Debug -Times 0 -Exactly -ParameterFilter { $Message -eq "-- End of Install-OriAzBopPrerequisityForce --" } Assert-VerifiableMock } } |