. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" Import-Module $functionPath -Force $moduleForMock = "" Describe "Update-AWSPowerShellModule" { Import-Module PowerShellGet Mock Get-Module -ModuleName $moduleForMock -MockWith {} Mock Install-Module -ModuleName $moduleForMock -MockWith {} Mock Uninstall-Module -ModuleName $moduleForMock -MockWith {} Mock Find-Module -ModuleName $moduleForMock -MockWith {} Mock Test-Path -ModuleName $moduleForMock -MockWith {} Mock Remove-FileSystemItem -ModuleName $moduleForMock -MockWith {} Mock Get-PSRepository -ModuleName $moduleForMock -MockWith {} Mock Set-PSRepository -ModuleName $moduleForMock -MockWith {} Mock Register-PSRepository -ModuleName $moduleForMock -MockWith {} Context "Error Handling" { It "Returns Early When No Remote Module Found" { Mock Write-Warning -ModuleName $moduleForMock -MockWith {} Update-AWSPowerShellModule Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -ParameterFilter {$Message -match "Could not find module on remote repository" } Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-Module -Times 0 -Exactly -Scope It } It "Returns Early When a Module Name that Would Cause Wildcard Matches is Passed" { Mock Write-Warning -ModuleName $moduleForMock -MockWith {} Update-AWSPowerShellModule -ModuleName "AWS*" Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -ParameterFilter {$Message -match "Wildcard module names are not allowed in this function" } Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-Module -Times 0 -Exactly -Scope It } It "Writes a Warning and Uses the Highest Version Module For Comparison When Multiple Modules Matched" { Mock Get-Module -ModuleName $moduleForMock -MockWith { $localModuleOne = New-Object PSObject -Property @{ ModuleType = "Binary"; Version = "8.8.8.8"; Name = "AWSPowerShell"; ExportedCommands = @("Submit-ResignationLetter") } $localModuleTwo = New-Object PSObject -Property @{ ModuleType = "Binary"; Version = "8.8.4.4"; Name = "AWSPowerShell"; ExportedCommands = @("Submit-ResignationLetter") } return @($localModuleOne, $localModuleTwo) } Mock Find-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ Version = "8.8.4.4"; Name = "AWSPowerShell"; Repository = "FakeOlFeed"; Description = "This is Fake Homie"; } } Mock Write-Warning -ModuleName $moduleForMock -MockWith {} Update-AWSPowerShellModule Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -ParameterFilter {$Message -match "More than one module version found" } Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-Module -Times 1 -Exactly -Scope It } } Context "Function Logic" { It "Updates the PSRepository Source Location to the Value Provided via Parameter if it is Incorrect" { Mock Find-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ Version = "9.9.9.9"; Name = "AWSPowerShell"; Repository = "FakeOlFeed"; Description = "This is Fake Homie"; } } Mock Get-PSRepository -ModuleName $moduleForMark -MockWith { return New-Object PSObject -Property @{ Name = "FakeOlFeed"; InstallationPolicy = "Untrusted"; SourceLocation = "https://stackoverflow.com/nuget/ThisShouldBeUpdatedByTheFunction"; } } $fakeFeedName = "FakeOlFeed" $fakeFeed = "https://stackoverflow.com/nuget/PesterTest" Update-AWSPowerShellModule -FeedName $fakeFeedName -SourceLocation $fakeFeed Assert-MockCalled -ModuleName $moduleForMock -CommandName Set-PSRepository -ParameterFilter {$SourceLocation -eq $fakeFeed -and $Name -eq $fakeFeedName } } It "Adds the PSRepository if it is Not Found" { Mock Find-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ Version = "9.9.9.9"; Name = "AWSPowerShell"; Repository = "FakeOlFeed"; Description = "This is Fake Homie"; } } Mock Get-PSRepository -ModuleName $moduleForMark -MockWith { return $null } $fakeFeedName = "FakeOlFeed" $fakeFeed = "https://stackoverflow.com/nuget/PesterTest" Update-AWSPowerShellModule -FeedName $fakeFeedName -SourceLocation $fakeFeed Assert-MockCalled -ModuleName $moduleForMock -CommandName Register-PSRepository -ParameterFilter {$SourceLocation -eq $fakeFeed -and $Name -eq $fakeFeedName } } It "Exits Early if the Specified Version is Already Installed" { Mock Get-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ ModuleType = "Binary"; Version = "9.0.9.0"; Name = "AWSPowerShell"; ExportedCommands = @("Format-HardDrive") } } Update-AWSPowerShellModule -TargetVersion "9.0.9.0" Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-Module -Times 0 -Exactly -Scope It } It "Exits Early if No Version Specified and Latest Version is Already Installed" { Mock Get-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ ModuleType = "Binary"; Version = "9.1.9.1"; Name = "AWSPowerShell"; ExportedCommands = @("Format-HardDrive") } } Mock Find-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ Version = "9.1.9.1"; Name = "AWSPowerShell"; Repository = "FakeOlFeed"; Description = "This is Fake Homie"; } } Update-AWSPowerShellModule Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-Module -Times 0 -Exactly -Scope It } It "Uninstalls Current Version if Latest Remote Version Doesn't Match" { Mock Get-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ ModuleType = "Binary"; Version = "9.2.9.2"; Name = "AWSPowerShell"; ExportedCommands = @("Format-HardDrive") } } Mock Find-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ Version = "9.3.9.3"; Name = "AWSPowerShell"; Repository = "FakeOlFeed"; Description = "This is Fake Homie"; } } Update-AWSPowerShellModule Assert-MockCalled -ModuleName $moduleForMock -CommandName Uninstall-Module -Times 1 -Exactly -Scope It -ParameterFilter { $Name -eq "AWSPowerShell"} } It "Removes the DeprecatedModuleFolder If it Exists" { Mock Get-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ ModuleType = "Binary"; Version = "9.2.9.2"; Name = "AWSPowerShell"; ExportedCommands = @("Format-HardDrive") } } Mock Find-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ Version = "9.3.9.3"; Name = "AWSPowerShell"; Repository = "FakeOlFeed"; Description = "This is Fake Homie"; } } Mock Test-Path -ModuleName $moduleForMock -MockWith { return $true } $fakePath = "Z:\Temp\PesterTestCase" Update-AWSPowerShellModule -DeprecatedModuleFolder $fakePath Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-Path -Times 1 -Exactly -Scope It -ParameterFilter { $Path -eq $fakePath } Assert-MockCalled -ModuleName $moduleForMock -CommandName Remove-FileSystemItem -Times 1 -Exactly -Scope It -ParameterFilter { $Path -eq $fakePath } } It "Uses the Module Name From the Parameters" { Mock Get-Module -ModuleName $moduleForMock -MockWith { return New-Object PSObject -Property @{ ModuleType = "Binary"; Version = "9.4.9.4"; Name = "BogusModule"; ExportedCommands = @("Submit-ResignationLetter") } } Mock Find-Module -ModuleName $moduleForMock -MockWith { $fakeLocalModule1 = New-Object PSObject -Property @{ Version = "9.5.9.5"; Name = "AWSPowerShell"; Repository = "FakeOlFeed"; Description = "This is Fake Homie"; } $fakeLocalModule2 = New-Object PSObject -Property @{ Versoin = "9.6.9.6"; Name = "BogusModule"; Repository = "FakeOlFeed"; Description = "This is also fake, homeslice"; } return @( $fakeLocalModule1, $fakeLocalModule2 ) } $fakeModuleName = "BogusModule" Update-AWSPowerShellModule -ModuleName $fakeModuleName Assert-MockCalled -ModuleName $moduleForMock -CommandName Find-Module -Times 1 -Exactly -Scope It -ParameterFilter { $Name -eq $fakeModuleName} Assert-MockCalled -ModuleName $moduleForMock -CommandName Uninstall-Module -Times 1 -Exactly -Scope It -ParameterFilter { $Name -eq $fakeModuleName} Assert-MockCalled -ModuleName $moduleForMock -CommandName Install-Module -Times 1 -Exactly -Scope It -ParameterFilter { $Name -eq $fakeModuleName} } } }