File size: 5,728 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Basic Function Provider Tests" -Tags "CI" {
BeforeAll {
$existingFunction = "existingFunction"
$nonExistingFunction = "nonExistingFunction"
$text = "Hello World!"
$functionValue = { return $text }
$restoreLocation = Get-Location
$newName = "renamedFunction"
Set-Location Function:
}
AfterAll {
Set-Location -Path $restoreLocation
}
BeforeEach {
Set-Item $existingFunction -Options "None" -Value $functionValue
}
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"
Set-Item $existingFunction -Options $newOptions
(Get-Item $existingFunction).Options | Should -BeExactly $newOptions
}
It "Sets the options and a value of type ScriptBlock for a new function" {
$options = "ReadOnly"
Set-Item $nonExistingFunction -Options $options -Value $functionValue
$getItemResult = Get-Item $nonExistingFunction
$getItemResult.Options | Should -BeExactly $options
$getItemResult.ScriptBlock | Should -BeExactly $functionValue
}
It "Removes existing function if Set-Item has no arguments beside function name" {
Set-Item $existingFunction
$existingFunction | Should -Not -Exist
}
It "Sets a value of type FunctionInfo for a new function" {
Set-Item $nonExistingFunction -Value (Get-Item $existingFunction)
Invoke-Expression $nonExistingFunction | Should -BeExactly $text
}
It "Sets a value of type String for a new function" {
Set-Item $nonExistingFunction -Value "return '$text' "
Invoke-Expression $nonExistingFunction | Should -BeExactly $text
}
It "Throws PSArgumentException when Set-Item is called with incorrect function value" {
{ Set-Item $nonExistingFunction -Value 123 -ErrorAction Stop } | Should -Throw -ErrorId "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 } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
}
It "Fails to remove not existing function" {
{ Remove-Item $nonExistingFunction -ErrorAction Stop } | Should -Throw -ErrorId "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 } | Should -Throw -ErrorId "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 } | Should -Throw -ErrorId "PathNotFound,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 } | Should -Throw -ErrorId "CannotRenameFunction,Microsoft.PowerShell.Commands.RenameItemCommand"
}
It "Fails to rename function which is ReadOnly" {
Set-Item $nonExistingFunction -Options "ReadOnly" -Value $functionValue
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | Should -Throw -ErrorId "CannotRenameFunction,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 } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
(Get-Item $newName).Count | Should -BeExactly 1
}
}
}
|