diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 index 07da0137650..f287c404cf6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 @@ -8,6 +8,7 @@ Describe "Basic Function Provider Tests" -Tags "CI" { $text = "Hello World!" $functionValue = { return $text } $restoreLocation = Get-Location + $newName = "renamedFunction" Set-Location Function: } @@ -15,16 +16,17 @@ Describe "Basic Function Provider Tests" -Tags "CI" { Set-Location -Path $restoreLocation } - Context "Validate Set-Item Cmdlet" { - BeforeEach { - Set-Item $existingFunction -Options "None" -Value $functionValue - } + BeforeEach { + Set-Item $existingFunction -Options "None" -Value $functionValue + } - AfterEach { - Remove-Item $existingFunction -ErrorAction SilentlyContinue -Force - Remove-Item $nonexistingFunction -ErrorAction SilentlyContinue -Force - } + AfterEach { + Remove-Item $existingFunction -ErrorAction SilentlyContinue -Force + Remove-Item $nonExistingFunction -ErrorAction SilentlyContinue -Force + Remove-Item $newName -ErrorAction SilentlyContinue -Force + } + Context "Validate Set-Item Cmdlet" { It "Sets the new options in existing function" { $newOptions = "ReadOnly, AllScope" (Get-Item $existingFunction).Options | Should -BeExactly "None" @@ -59,4 +61,67 @@ Describe "Basic Function Provider Tests" -Tags "CI" { { Set-Item $nonExistingFunction -Value 123 -ErrorAction Stop } | ShouldBeErrorId "Argument,Microsoft.PowerShell.Commands.SetItemCommand" } } + + Context "Validate Get-Item Cmdlet" { + It "Gets existing functions by name" { + $getItemResult = Get-Item $existingFunction + $getItemResult.Name | Should -BeExactly $existingFunction + $getItemResult.Options | Should -BeExactly "None" + $getItemResult.ScriptBlock | Should -BeExactly $functionValue + } + + It "Matches regex with stars to the function names" { + $getItemResult = Get-Item "ex*on" + $getItemResult.Name | Should -BeExactly $existingFunction + + # Stars representing empty string. + $getItemResult = Get-Item "*existingFunction*" + $getItemResult.Name | Should -BeExactly $existingFunction + + # Finds 2 functions that match the regex. + Set-Item $nonExistingFunction -Value $functionValue + $getItemResults = Get-Item "*Function" + $getItemResults.Count | Should -BeGreaterThan 1 + } + } + + Context "Validate Remove-Item Cmdlet" { + It "Removes function" { + Remove-Item $existingFunction + { Get-Item $existingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand" + } + + It "Fails to remove not existing function" { + { Remove-Item $nonExistingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand" + } + } + + Context "Validate Rename-Item Cmdlet" { + It "Renames existing function with None options" { + Rename-Item $existingFunction -NewName $newName + { Get-Item $existingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand" + (Get-Item $newName).Count | Should -BeExactly 1 + } + + It "Fails to rename not existing function" { + { Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | ShouldBeErrorId "InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand" + } + + It "Fails to rename function which is Constant" { + Set-Item $nonExistingFunction -Options "Constant" -Value $functionValue + { Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | ShouldBeErrorId "CannotRenameFunction,Microsoft.PowerShell.Commands.RenameItemCommand" + } + + It "Fails to rename function which is ReadOnly" { + Set-Item $nonExistingFunction -Options "ReadOnly" + { Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | ShouldBeErrorId "InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand" + } + + It "Renames ReadOnly function when -Force parameter is on" { + Set-Item $nonExistingFunction -Options "ReadOnly" -Value $functionValue + Rename-Item $nonExistingFunction -NewName $newName -Force + { Get-Item $nonExistingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand" + (Get-Item $newName).Count | Should -BeExactly 1 + } + } }