From 025633def4848debe5d096fd3446b4cdb941a3e6 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Thu, 15 Feb 2018 15:21:31 -0800 Subject: [PATCH 1/6] 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/6] 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/6] 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/6] 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/6] 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 bf672c30ea82548467f9c1aaca9e52a2ef517002 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Tue, 20 Feb 2018 10:42:47 -0800 Subject: [PATCH 6/6] Function Provider tests correction. --- .../FunctionProvider.Tests.ps1 | 13 +++++++------ 1 file changed, 7 insertions(+), 6 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..07da0137650 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1 @@ -27,16 +27,17 @@ 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 -BeExactly "None" Set-Item $existingFunction -Options $newOptions - (Get-Item $existingFunction).Options | Should -Be $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 - (Get-Item $nonExistingFunction).Options | Should -Be $options - (Get-Item $nonExistingFunction).ScriptBlock | Should -Be $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" { @@ -46,12 +47,12 @@ 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) - Invoke-Expression $nonExistingFunction | Should -Be $text + 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 -Be $text + Invoke-Expression $nonExistingFunction | Should -BeExactly $text } It "Throws PSArgumentException when Set-Item is called with incorrect function value" {