From 025633def4848debe5d096fd3446b4cdb941a3e6 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Thu, 15 Feb 2018 15:21:31 -0800 Subject: [PATCH 1/9] Tests for Set-Item Cmdlet for Function Provider --- .../FuntionProvider.Tests.ps1 | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 new file mode 100644 index 00000000000..898ed7e8ca7 --- /dev/null +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 @@ -0,0 +1,64 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +Describe "Basic Function Provider Tests" -Tags "CI" { + BeforeAll { + $existingFunction = "prompt" + $nonExistingFunction = "nonExistingFunction" + $text = "Hello World!" + $functionValue = { return $text } + $restoreLocation = Get-Location + } + + AfterAll { + # Restore the previous location. + Set-Location -Path $restoreLocation + } + + Context "Validate Set-Item Cmdlet" { + + BeforeAll { + Set-Location Function: + } + + AfterEach { + # Removes $nonExistingFunction in case it was added. + Set-Item $nonExistingFunction + $nonExistingFunction | Should -Not -Exist + } + + It "Sets the new options in existing function" { + (Get-Item $existingFunction).Options | Should -be "None" + Set-Item $existingFunction -Options "AllScope" + (Get-Item $existingFunction).Options | Should -be "AllScope" + } + + It "Sets the options and a value of type ScriptBlock for a new function" { + Set-Item $nonExistingFunction -Options "AllScope" -value $functionValue + (Get-Item $nonExistingFunction).Options | Should -be "AllScope" + (Get-Item $nonExistingFunction).ScriptBlock | Should -BeLike $functionValue + } + + It "Removes existing function if Set-Item has no arguments beside function name" { + $existingFunction | Should -Exist + Set-Item $existingFunction + $existingFunction | Should -Not -Exist + } + + It "Sets a value of type FunctionInfo for a new function" { + Set-Item $nonExistingFunction -value $functionValue + $tmpFunction = "tmpFunction" + Set-Item $tmpFunction -value (Get-Item $nonExistingFunction) + Invoke-Expression $tmpFunction | Should -Be $text + } + + It "Sets a value of type String for a new function" { + Set-Item $nonExistingFunction -value "return '$text' " + Invoke-Expression $nonExistingFunction | Should -Be $text + } + + It "Throws PSArgumentException when Set-Item is called with incorrect function value" { + Set-Item $nonExistingFunction -Value 123 -errorvariable x -erroraction silentlycontinue + $x.CategoryInfo | Should -Match "PSArgumentException" + } + } +} \ No newline at end of file From aa5e120af4ce7f389ba9bcf3419d13ff4cc691be Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Thu, 15 Feb 2018 15:25:46 -0800 Subject: [PATCH 2/9] Adding new line at the end of FunctionProvider test file. --- .../Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 index 898ed7e8ca7..cdd208e0f95 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 @@ -61,4 +61,4 @@ Describe "Basic Function Provider Tests" -Tags "CI" { $x.CategoryInfo | Should -Match "PSArgumentException" } } -} \ No newline at end of file +} From 282e15390f3db66bf00ee0c2e53d8b4d0809f099 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Thu, 15 Feb 2018 18:06:51 -0800 Subject: [PATCH 3/9] Creating a new function for tests instead of using preexisting one. --- ...r.Tests.ps1 => FunctionProvider.Tests.ps1} | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) rename test/powershell/Modules/Microsoft.PowerShell.Management/{FuntionProvider.Tests.ps1 => FunctionProvider.Tests.ps1} (67%) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 similarity index 67% rename from test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 rename to test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 index cdd208e0f95..04eadf05545 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FuntionProvider.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 @@ -2,11 +2,12 @@ # Licensed under the MIT License. Describe "Basic Function Provider Tests" -Tags "CI" { BeforeAll { - $existingFunction = "prompt" + $existingFunction = "existingFunction" $nonExistingFunction = "nonExistingFunction" $text = "Hello World!" $functionValue = { return $text } $restoreLocation = Get-Location + Set-Location Function: } AfterAll { @@ -15,40 +16,37 @@ Describe "Basic Function Provider Tests" -Tags "CI" { } Context "Validate Set-Item Cmdlet" { - - BeforeAll { - Set-Location Function: + BeforeEach { + Set-Item $existingFunction -Options "None" -Value $functionValue } AfterEach { - # Removes $nonExistingFunction in case it was added. - Set-Item $nonExistingFunction - $nonExistingFunction | Should -Not -Exist + Remove-Item $existingFunction -ErrorAction SilentlyContinue -Force + Remove-Item $nonexistingFunction -ErrorAction SilentlyContinue -Force } It "Sets the new options in existing function" { + $newOptions = "ReadOnly, AllScope" (Get-Item $existingFunction).Options | Should -be "None" - Set-Item $existingFunction -Options "AllScope" - (Get-Item $existingFunction).Options | Should -be "AllScope" + Set-Item $existingFunction -Options $newOptions + (Get-Item $existingFunction).Options | Should -be $newOptions } It "Sets the options and a value of type ScriptBlock for a new function" { - Set-Item $nonExistingFunction -Options "AllScope" -value $functionValue - (Get-Item $nonExistingFunction).Options | Should -be "AllScope" + $options = "ReadOnly" + Set-Item $nonExistingFunction -Options $options -value $functionValue + (Get-Item $nonExistingFunction).Options | Should -be $options (Get-Item $nonExistingFunction).ScriptBlock | Should -BeLike $functionValue } It "Removes existing function if Set-Item has no arguments beside function name" { - $existingFunction | Should -Exist Set-Item $existingFunction $existingFunction | Should -Not -Exist } It "Sets a value of type FunctionInfo for a new function" { - Set-Item $nonExistingFunction -value $functionValue - $tmpFunction = "tmpFunction" - Set-Item $tmpFunction -value (Get-Item $nonExistingFunction) - Invoke-Expression $tmpFunction | Should -Be $text + Set-Item $nonExistingFunction -value (Get-Item $existingFunction) + Invoke-Expression $nonExistingFunction | Should -Be $text } It "Sets a value of type String for a new function" { From 9df6d3eec047e8d1d957ae7eb3907c7110116eb4 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Fri, 16 Feb 2018 11:24:29 -0800 Subject: [PATCH 4/9] Correcting tests for Set-Item FunctionProvider --- .../FunctionProvider.Tests.ps1 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 index 04eadf05545..29d10bc7e2e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 @@ -1,5 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + Describe "Basic Function Provider Tests" -Tags "CI" { BeforeAll { $existingFunction = "existingFunction" @@ -11,7 +12,6 @@ Describe "Basic Function Provider Tests" -Tags "CI" { } AfterAll { - # Restore the previous location. Set-Location -Path $restoreLocation } @@ -27,15 +27,15 @@ Describe "Basic Function Provider Tests" -Tags "CI" { It "Sets the new options in existing function" { $newOptions = "ReadOnly, AllScope" - (Get-Item $existingFunction).Options | Should -be "None" + (Get-Item $existingFunction).Options | Should -Be "None" Set-Item $existingFunction -Options $newOptions - (Get-Item $existingFunction).Options | Should -be $newOptions + (Get-Item $existingFunction).Options | Should -Be $newOptions } It "Sets the options and a value of type ScriptBlock for a new function" { $options = "ReadOnly" - Set-Item $nonExistingFunction -Options $options -value $functionValue - (Get-Item $nonExistingFunction).Options | Should -be $options + Set-Item $nonExistingFunction -Options $options -Value $functionValue + (Get-Item $nonExistingFunction).Options | Should -Be $options (Get-Item $nonExistingFunction).ScriptBlock | Should -BeLike $functionValue } @@ -45,18 +45,18 @@ Describe "Basic Function Provider Tests" -Tags "CI" { } It "Sets a value of type FunctionInfo for a new function" { - Set-Item $nonExistingFunction -value (Get-Item $existingFunction) + Set-Item $nonExistingFunction -Value (Get-Item $existingFunction) Invoke-Expression $nonExistingFunction | Should -Be $text } It "Sets a value of type String for a new function" { - Set-Item $nonExistingFunction -value "return '$text' " + Set-Item $nonExistingFunction -Value "return '$text' " Invoke-Expression $nonExistingFunction | Should -Be $text } It "Throws PSArgumentException when Set-Item is called with incorrect function value" { - Set-Item $nonExistingFunction -Value 123 -errorvariable x -erroraction silentlycontinue - $x.CategoryInfo | Should -Match "PSArgumentException" + Set-Item $nonExistingFunction -Value 123 -ErrorVariable x -ErrorAction silentlycontinue + $x.FullyQualifiedErrorId | Should -Match "Argument,Microsoft.PowerShell.Commands.SetItemCommand" } } } From 6fda634f708991966e4d34d1ceb2390124ec2122 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Fri, 16 Feb 2018 12:34:23 -0800 Subject: [PATCH 5/9] Using ShouldBeErrorId for Set-Item error. --- .../FunctionProvider.Tests.ps1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 index 29d10bc7e2e..09f2d528c46 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 @@ -36,7 +36,7 @@ Describe "Basic Function Provider Tests" -Tags "CI" { $options = "ReadOnly" Set-Item $nonExistingFunction -Options $options -Value $functionValue (Get-Item $nonExistingFunction).Options | Should -Be $options - (Get-Item $nonExistingFunction).ScriptBlock | Should -BeLike $functionValue + (Get-Item $nonExistingFunction).ScriptBlock | Should -Be $functionValue } It "Removes existing function if Set-Item has no arguments beside function name" { @@ -55,8 +55,7 @@ Describe "Basic Function Provider Tests" -Tags "CI" { } It "Throws PSArgumentException when Set-Item is called with incorrect function value" { - Set-Item $nonExistingFunction -Value 123 -ErrorVariable x -ErrorAction silentlycontinue - $x.FullyQualifiedErrorId | Should -Match "Argument,Microsoft.PowerShell.Commands.SetItemCommand" + { Set-Item $nonExistingFunction -Value 123 -ErrorAction Stop } | ShouldBeErrorId "Argument,Microsoft.PowerShell.Commands.SetItemCommand" } } } From 8b6078934d90d7278566dd712ef08c47cea1e374 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Fri, 16 Feb 2018 17:10:38 -0800 Subject: [PATCH 6/9] Function provider Cmdlets tests --- .../FunctionProvider.Tests.ps1 | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 index 09f2d528c46..a464025325e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 @@ -15,16 +15,16 @@ 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 + } + Context "Validate Set-Item Cmdlet" { It "Sets the new options in existing function" { $newOptions = "ReadOnly, AllScope" (Get-Item $existingFunction).Options | Should -Be "None" @@ -58,4 +58,34 @@ 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" { + { Get-Item $existingFunction } | Should -Not -Throw + (Get-Item $existingFunction).Name | Should -Be $existingFunction + (Get-Item $existingFunction).Options | Should -Be "None" + (Get-Item $existingFunction).ScriptBlock | Should -Be $functionValue + } + + It "Matches regex with stars to the function names" { + { Get-Item "ex*on" } | Should -Not -Throw + (Get-Item "ex*on").Name | Should -Be $existingFunction + + # Stars representing empty string. + { Get-Item "*existingFunction*" } | Should -Not -Throw + (Get-Item "*existingFunction*").Name | Should -Be $existingFunction + + # Finds 2 functions that match the regex. + Set-Item $nonExistingFunction -Value $functionValue + { Get-Item "*Function" } | Should -Not -Throw + (Get-Item "*Function*").Count | Should -Be 2 + } + } + + Context "Validate Remove-Item Cmdlet" { + It "Removes function" { + Remove-Item $existingFunction + $existingFunction | Should -Not -Exist + } + } } From ea89d1ba8783f457f23e97bb3414bfd5c548df6e Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Wed, 21 Feb 2018 13:20:43 -0800 Subject: [PATCH 7/9] Tests style corrections --- .../FunctionProvider.Tests.ps1 | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 index e431d37688a..f210ce9a1ea 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 @@ -62,24 +62,24 @@ Describe "Basic Function Provider Tests" -Tags "CI" { Context "Validate Get-Item Cmdlet" { It "Gets existing functions by name" { - { Get-Item $existingFunction } | Should -Not -Throw - (Get-Item $existingFunction).Name | Should -Be $existingFunction - (Get-Item $existingFunction).Options | Should -Be "None" - (Get-Item $existingFunction).ScriptBlock | Should -Be $functionValue + $getItemResult = Get-Item $existingFunction + $getItemResult.Name | Should -Be $existingFunction + $getItemResult.Options | Should -Be "None" + $getItemResult.ScriptBlock | Should -Be $functionValue } It "Matches regex with stars to the function names" { - { Get-Item "ex*on" } | Should -Not -Throw - (Get-Item "ex*on").Name | Should -Be $existingFunction + $getItemResult = Get-Item "ex*on" + $getItemResult.Name | Should -Be $existingFunction # Stars representing empty string. - { Get-Item "*existingFunction*" } | Should -Not -Throw - (Get-Item "*existingFunction*").Name | Should -Be $existingFunction + $getItemResult = Get-Item "*existingFunction*" + $getItemResult.Name | Should -Be $existingFunction # Finds 2 functions that match the regex. Set-Item $nonExistingFunction -Value $functionValue - { Get-Item "*Function" } | Should -Not -Throw - (Get-Item "*Function*").Count | Should -Be 2 + $getItemResults = Get-Item "*Function" + $getItemResults.Count | Should -BeGreaterThan 1 } } From 5e364d3c006dd2e4d5f271dd9b9afcc732e99081 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Wed, 21 Feb 2018 14:42:31 -0800 Subject: [PATCH 8/9] Adding Rename-Item cmdlet test --- .../FunctionProvider.Tests.ps1 | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 index f210ce9a1ea..d0d6e8bb797 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 @@ -21,7 +21,7 @@ Describe "Basic Function Provider Tests" -Tags "CI" { AfterEach { Remove-Item $existingFunction -ErrorAction SilentlyContinue -Force - Remove-Item $nonexistingFunction -ErrorAction SilentlyContinue -Force + Remove-Item $nonExistingFunction -ErrorAction SilentlyContinue -Force } Context "Validate Set-Item Cmdlet" { @@ -86,7 +86,15 @@ Describe "Basic Function Provider Tests" -Tags "CI" { Context "Validate Remove-Item Cmdlet" { It "Removes function" { Remove-Item $existingFunction - $existingFunction | Should -Not -Exist + { Get-Item $existingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand" + } + } + + Context "Validate Rename-Item Cmdlet" { + It "Renames function" { + Rename-Item -path $existingFunction -NewName $nonExistingFunction + { Get-Item $existingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand" + { Get-Item $nonExistingFunction } | Should -Not -Throw } } } From 8465368f63d3606875bc2ee51bd37aecb10b33a1 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Wed, 21 Feb 2018 16:16:16 -0800 Subject: [PATCH 9/9] More tests for Rename-Item cmdlet --- .../FunctionProvider.Tests.ps1 | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 index d0d6e8bb797..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: } @@ -22,6 +23,7 @@ Describe "Basic Function Provider Tests" -Tags "CI" { AfterEach { Remove-Item $existingFunction -ErrorAction SilentlyContinue -Force Remove-Item $nonExistingFunction -ErrorAction SilentlyContinue -Force + Remove-Item $newName -ErrorAction SilentlyContinue -Force } Context "Validate Set-Item Cmdlet" { @@ -63,18 +65,18 @@ Describe "Basic Function Provider Tests" -Tags "CI" { Context "Validate Get-Item Cmdlet" { It "Gets existing functions by name" { $getItemResult = Get-Item $existingFunction - $getItemResult.Name | Should -Be $existingFunction - $getItemResult.Options | Should -Be "None" - $getItemResult.ScriptBlock | Should -Be $functionValue + $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 -Be $existingFunction + $getItemResult.Name | Should -BeExactly $existingFunction # Stars representing empty string. $getItemResult = Get-Item "*existingFunction*" - $getItemResult.Name | Should -Be $existingFunction + $getItemResult.Name | Should -BeExactly $existingFunction # Finds 2 functions that match the regex. Set-Item $nonExistingFunction -Value $functionValue @@ -88,13 +90,38 @@ Describe "Basic Function Provider Tests" -Tags "CI" { 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 function" { - Rename-Item -path $existingFunction -NewName $nonExistingFunction + 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 $nonExistingFunction } | Should -Not -Throw + (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 } } }