From cec7f3f2f46a91ad564f8b689e6691d1fce1ad17 Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Wed, 26 Jul 2017 15:28:58 -0700 Subject: [PATCH 1/4] Add test coverage for Registry (#4148) Add coverage for Get-Item, Get-ChildItem, Set-Item, and Clear-Item --- .../Registry.Tests.ps1 | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 index 60582002e31..53335a47ebf 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 @@ -13,6 +13,8 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") $testKey2 = "TestKey2" $testPropertyName = "TestEntry" $testPropertyValue = 1 + $defaultPropertyName = "(Default)" + $defaultPropertyValue = "something" } } @@ -44,6 +46,25 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") } Context "Validate basic registry provider Cmdlets" { + It "Verify Get-Item" { + $item = Get-Item $testKey + $item.PSChildName | Should BeExactly $testKey + } + + It "Verify Get-ChildItem" { + $items = Get-ChildItem + $items.Count | Should BeExactly 2 + $Items.PSChildName -contains $testKey | Should Be $true + $Items.PSChildName -contains $testKey2 | Should Be $true + } + + It "Verify Get-ChildItem can get subkey names" { + $items = Get-ChildItem -Name + $items.Count | Should BeExactly 2 + $items -contains $testKey | Should Be $true + $items -contains $testKey2 | Should Be $true + } + It "Verify New-Item" { $newKey = New-Item -Path "NewItemTest" Test-Path "NewItemTest" | Should Be $true @@ -87,6 +108,12 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") $property."$testPropertyName" | Should Be 2 } + It "Verify Set-Item" { + Set-Item -Path $testKey -Value $defaultPropertyValue + $property = Get-ItemProperty -Path $testKey -Name $defaultPropertyName + $property."$defaultPropertyName" | Should BeExactly $defaultPropertyValue + } + It "Verify Get-ItemPropertyValue" { $propertyValue = Get-ItemPropertyValue -Path $testKey -Name $testPropertyName $propertyValue | Should Be $testPropertyValue @@ -125,6 +152,16 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") $property."$testPropertyName" | Should Be 0 } + It "Verity Clear-Item" { + Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue + Set-Item -Path $testKey -Value $defaultPropertyValue + $key = Get-Item -Path $testKey + $key.Property.Length | Should BeExactly 2 + Clear-Item -Path $testKey + $key = Get-Item -Path $testKey + $key.Property.Length | Should BeExactly 0 + } + It "Verify Remove-ItemProperty" { Remove-ItemProperty -Path $testKey -Name $testPropertyName $properties = @(Get-ItemProperty -Path $testKey) From 307d1072fba784b3aa4020d4029b6d11c32afcf9 Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Fri, 28 Jul 2017 11:55:08 -0700 Subject: [PATCH 2/4] Add more tests per code review. --- .../Registry.Tests.ps1 | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 index 53335a47ebf..43e8a97b7cb 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 @@ -15,6 +15,7 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") $testPropertyValue = 1 $defaultPropertyName = "(Default)" $defaultPropertyValue = "something" + $otherPropertyValue = "other" } } @@ -46,6 +47,11 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") } Context "Validate basic registry provider Cmdlets" { + It "Verity Test-Path" { + Test-Path -IsValid Registry::HKCU/Software | Should Be $true + Test-Path -IsValid Registry::foo/Softare | Should Be $false + } + It "Verify Get-Item" { $item = Get-Item $testKey $item.PSChildName | Should BeExactly $testKey @@ -114,6 +120,13 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") $property."$defaultPropertyName" | Should BeExactly $defaultPropertyValue } + It "Verify Set-Item with -WhatIf" { + Set-Item -Path $testKey -Value $defaultPropertyValue + Set-Item -Path $testKey -Value $otherPropertyValue -WhatIf + $property = Get-ItemProperty -Path $testKey -Name $defaultPropertyName + $property."$defaultPropertyName" | Should BeExactly $defaultPropertyValue + } + It "Verify Get-ItemPropertyValue" { $propertyValue = Get-ItemPropertyValue -Path $testKey -Name $testPropertyName $propertyValue | Should Be $testPropertyValue @@ -155,13 +168,19 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") It "Verity Clear-Item" { Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue Set-Item -Path $testKey -Value $defaultPropertyValue - $key = Get-Item -Path $testKey - $key.Property.Length | Should BeExactly 2 Clear-Item -Path $testKey $key = Get-Item -Path $testKey $key.Property.Length | Should BeExactly 0 } + It "Verity Clear-Item with -WhatIf" { + Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue + Set-Item -Path $testKey -Value $defaultPropertyValue + Clear-Item -Path $testKey -WhatIf + $key = Get-Item -Path $testKey + $key.Property.Length | Should BeExactly 2 + } + It "Verify Remove-ItemProperty" { Remove-ItemProperty -Path $testKey -Name $testPropertyName $properties = @(Get-ItemProperty -Path $testKey) From 6f28212fe89b2ec105df693ad3cb002a1a3a81cc Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Fri, 28 Jul 2017 13:30:46 -0700 Subject: [PATCH 3/4] Add test for accessing inaccessible path, per code review. --- .../Microsoft.PowerShell.Management/Registry.Tests.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 index 43e8a97b7cb..f01a023177e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 @@ -57,6 +57,10 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") $item.PSChildName | Should BeExactly $testKey } + It "Verify Get-Item on inaccessible path" { + { Get-Item HKLM:\SAM\SAM -ErrorAction Stop } | ShouldBeErrorId "System.Security.SecurityException,Microsoft.PowerShell.Commands.GetItemCommand" + } + It "Verify Get-ChildItem" { $items = Get-ChildItem $items.Count | Should BeExactly 2 From a946fc5dbb472526992a60fd1df9426cbccd35b2 Mon Sep 17 00:00:00 2001 From: Jeff Bienstadt Date: Fri, 28 Jul 2017 14:12:35 -0700 Subject: [PATCH 4/4] Fix typos. --- .../Microsoft.PowerShell.Management/Registry.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 index f01a023177e..cbcb0f47d40 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 @@ -47,7 +47,7 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") } Context "Validate basic registry provider Cmdlets" { - It "Verity Test-Path" { + It "Verify Test-Path" { Test-Path -IsValid Registry::HKCU/Software | Should Be $true Test-Path -IsValid Registry::foo/Softare | Should Be $false } @@ -169,7 +169,7 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") $property."$testPropertyName" | Should Be 0 } - It "Verity Clear-Item" { + It "Verify Clear-Item" { Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue Set-Item -Path $testKey -Value $defaultPropertyValue Clear-Item -Path $testKey @@ -177,7 +177,7 @@ Describe "Basic Registry Provider Tests" -Tags @("CI", "RequireAdminOnWindows") $key.Property.Length | Should BeExactly 0 } - It "Verity Clear-Item with -WhatIf" { + It "Verify Clear-Item with -WhatIf" { Set-ItemProperty -Path $testKey -Name $testPropertyName -Value $testPropertyValue Set-Item -Path $testKey -Value $defaultPropertyValue Clear-Item -Path $testKey -WhatIf