Skip to content

Commit 0ac01b7

Browse files
kalgiziSazonov
authored andcommitted
Add tests for *-Item Cmdlets in Function Provider (#6172)
1 parent 11d2e2d commit 0ac01b7

1 file changed

Lines changed: 73 additions & 8 deletions

File tree

test/powershell/Modules/Microsoft.PowerShell.Management/FunctionProvider.Tests.ps1

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ Describe "Basic Function Provider Tests" -Tags "CI" {
88
$text = "Hello World!"
99
$functionValue = { return $text }
1010
$restoreLocation = Get-Location
11+
$newName = "renamedFunction"
1112
Set-Location Function:
1213
}
1314

1415
AfterAll {
1516
Set-Location -Path $restoreLocation
1617
}
1718

18-
Context "Validate Set-Item Cmdlet" {
19-
BeforeEach {
20-
Set-Item $existingFunction -Options "None" -Value $functionValue
21-
}
19+
BeforeEach {
20+
Set-Item $existingFunction -Options "None" -Value $functionValue
21+
}
2222

23-
AfterEach {
24-
Remove-Item $existingFunction -ErrorAction SilentlyContinue -Force
25-
Remove-Item $nonexistingFunction -ErrorAction SilentlyContinue -Force
26-
}
23+
AfterEach {
24+
Remove-Item $existingFunction -ErrorAction SilentlyContinue -Force
25+
Remove-Item $nonExistingFunction -ErrorAction SilentlyContinue -Force
26+
Remove-Item $newName -ErrorAction SilentlyContinue -Force
27+
}
2728

29+
Context "Validate Set-Item Cmdlet" {
2830
It "Sets the new options in existing function" {
2931
$newOptions = "ReadOnly, AllScope"
3032
(Get-Item $existingFunction).Options | Should -BeExactly "None"
@@ -59,4 +61,67 @@ Describe "Basic Function Provider Tests" -Tags "CI" {
5961
{ Set-Item $nonExistingFunction -Value 123 -ErrorAction Stop } | ShouldBeErrorId "Argument,Microsoft.PowerShell.Commands.SetItemCommand"
6062
}
6163
}
64+
65+
Context "Validate Get-Item Cmdlet" {
66+
It "Gets existing functions by name" {
67+
$getItemResult = Get-Item $existingFunction
68+
$getItemResult.Name | Should -BeExactly $existingFunction
69+
$getItemResult.Options | Should -BeExactly "None"
70+
$getItemResult.ScriptBlock | Should -BeExactly $functionValue
71+
}
72+
73+
It "Matches regex with stars to the function names" {
74+
$getItemResult = Get-Item "ex*on"
75+
$getItemResult.Name | Should -BeExactly $existingFunction
76+
77+
# Stars representing empty string.
78+
$getItemResult = Get-Item "*existingFunction*"
79+
$getItemResult.Name | Should -BeExactly $existingFunction
80+
81+
# Finds 2 functions that match the regex.
82+
Set-Item $nonExistingFunction -Value $functionValue
83+
$getItemResults = Get-Item "*Function"
84+
$getItemResults.Count | Should -BeGreaterThan 1
85+
}
86+
}
87+
88+
Context "Validate Remove-Item Cmdlet" {
89+
It "Removes function" {
90+
Remove-Item $existingFunction
91+
{ Get-Item $existingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
92+
}
93+
94+
It "Fails to remove not existing function" {
95+
{ Remove-Item $nonExistingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand"
96+
}
97+
}
98+
99+
Context "Validate Rename-Item Cmdlet" {
100+
It "Renames existing function with None options" {
101+
Rename-Item $existingFunction -NewName $newName
102+
{ Get-Item $existingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
103+
(Get-Item $newName).Count | Should -BeExactly 1
104+
}
105+
106+
It "Fails to rename not existing function" {
107+
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | ShouldBeErrorId "InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand"
108+
}
109+
110+
It "Fails to rename function which is Constant" {
111+
Set-Item $nonExistingFunction -Options "Constant" -Value $functionValue
112+
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | ShouldBeErrorId "CannotRenameFunction,Microsoft.PowerShell.Commands.RenameItemCommand"
113+
}
114+
115+
It "Fails to rename function which is ReadOnly" {
116+
Set-Item $nonExistingFunction -Options "ReadOnly"
117+
{ Rename-Item $nonExistingFunction -NewName $newName -ErrorAction Stop } | ShouldBeErrorId "InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand"
118+
}
119+
120+
It "Renames ReadOnly function when -Force parameter is on" {
121+
Set-Item $nonExistingFunction -Options "ReadOnly" -Value $functionValue
122+
Rename-Item $nonExistingFunction -NewName $newName -Force
123+
{ Get-Item $nonExistingFunction -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
124+
(Get-Item $newName).Count | Should -BeExactly 1
125+
}
126+
}
62127
}

0 commit comments

Comments
 (0)