2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-20 15:00:49 -08:00
|
|
|
# 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
|
2018-02-22 11:26:26 -08:00
|
|
|
$newName = "renamedFunction"
|
2018-02-20 15:00:49 -08:00
|
|
|
Set-Location Function:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AfterAll {
|
|
|
|
|
Set-Location -Path $restoreLocation
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-22 11:26:26 -08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 15:00:49 -08:00
|
|
|
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" {
|
2018-05-17 14:42:04 -07:00
|
|
|
{ Set-Item $nonExistingFunction -Value 123 -ErrorAction Stop } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.SetItemCommand"
|
2018-02-20 15:00:49 -08:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-22 11:26:26 -08:00
|
|
|
|
|
|
|
|
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
|
2018-05-17 14:42:04 -07:00
|
|
|
{ Get-Item $existingFunction -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
|
2018-02-22 11:26:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Fails to remove not existing function" {
|
2018-05-17 14:42:04 -07:00
|
|
|
{ Remove-Item $nonExistingFunction -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand"
|
2018-02-22 11:26:26 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Context "Validate Rename-Item Cmdlet" {
|
|
|
|
|
It "Renames existing function with None options" {
|
|
|
|
|
Rename-Item $existingFunction -NewName $newName
|
2018-05-17 14:42:04 -07:00
|
|
|
{ Get-Item $existingFunction -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
|
2018-02-22 11:26:26 -08:00
|
|
|
(Get-Item $newName).Count | Should -BeExactly 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Fails to rename not existing function" {
|
2018-11-10 03:08:26 +08:00
|
|
|
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.RenameItemCommand"
|
2018-02-22 11:26:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Fails to rename function which is Constant" {
|
|
|
|
|
Set-Item $nonExistingFunction -Options "Constant" -Value $functionValue
|
2018-05-17 14:42:04 -07:00
|
|
|
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | Should -Throw -ErrorId "CannotRenameFunction,Microsoft.PowerShell.Commands.RenameItemCommand"
|
2018-02-22 11:26:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Fails to rename function which is ReadOnly" {
|
2018-11-10 03:08:26 +08:00
|
|
|
Set-Item $nonExistingFunction -Options "ReadOnly" -Value $functionValue
|
|
|
|
|
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | Should -Throw -ErrorId "CannotRenameFunction,Microsoft.PowerShell.Commands.RenameItemCommand"
|
2018-02-22 11:26:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "Renames ReadOnly function when -Force parameter is on" {
|
|
|
|
|
Set-Item $nonExistingFunction -Options "ReadOnly" -Value $functionValue
|
|
|
|
|
Rename-Item $nonExistingFunction -NewName $newName -Force
|
2018-05-17 14:42:04 -07:00
|
|
|
{ Get-Item $nonExistingFunction -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
|
2018-02-22 11:26:26 -08:00
|
|
|
(Get-Item $newName).Count | Should -BeExactly 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-20 15:00:49 -08:00
|
|
|
}
|