Add -PropertyType argument completer for New-ItemProperty (#21117)

This commit is contained in:
Armaan Mcleod
2024-12-25 17:27:44 +05:00
committed by GitHub
parent 8017195541
commit e127345820
3 changed files with 223 additions and 0 deletions
@@ -931,6 +931,87 @@ ConstructorTestClass(int i, bool b)
}
}
Context 'New-ItemProperty -PropertyType parameter completion' {
BeforeAll {
if ($IsWindows) {
$allRegistryValueKinds = 'String ExpandString Binary DWord MultiString QWord Unknown'
$allRegistryValueKindsWithQuotes = "'String' 'ExpandString' 'Binary' 'DWord' 'MultiString' 'QWord' 'Unknown'"
$dwordValueKind = 'DWord'
$qwordValueKind = 'QWord'
$binaryValueKind = 'Binary'
$multiStringValueKind = 'MultiString'
$registryPath = "HKCU:\test1\sub"
New-Item -Path $registryPath -Force
$registryLiteralPath = "HKCU:\test2\*\sub"
New-Item -Path $registryLiteralPath -Force
$fileSystemPath = "TestDrive:\test1.txt"
New-Item -Path $fileSystemPath -Force
$fileSystemLiteralPathDir = "TestDrive:\[]"
$fileSystemLiteralPath = "$fileSystemLiteralPathDir\test2.txt"
New-Item -Path $fileSystemLiteralPath -Force
}
}
It "Should complete Property Type for '<TextInput>'" -Skip:(!$IsWindows) -TestCases @(
# -Path completions
@{ TextInput = "New-ItemProperty -Path $registryPath -PropertyType "; ExpectedPropertyTypes = $allRegistryValueKinds }
@{ TextInput = "New-ItemProperty -Path $registryPath -PropertyType d"; ExpectedPropertyTypes = $dwordValueKind }
@{ TextInput = "New-ItemProperty -Path $registryPath -PropertyType q"; ExpectedPropertyTypes = $qwordValueKind }
@{ TextInput = "New-ItemProperty -Path $registryPath -PropertyType bin"; ExpectedPropertyTypes = $binaryValueKind }
@{ TextInput = "New-ItemProperty -Path $registryPath -PropertyType multi"; ExpectedPropertyTypes = $multiStringValueKind }
@{ TextInput = "New-ItemProperty -Path $registryPath -PropertyType invalidproptype"; ExpectedPropertyTypes = '' }
@{ TextInput = "New-ItemProperty -Path $fileSystemPath -PropertyType "; ExpectedPropertyTypes = '' }
# -LiteralPath completions
@{ TextInput = "New-ItemProperty -LiteralPath $registryLiteralPath -PropertyType "; ExpectedPropertyTypes = $allRegistryValueKinds }
@{ TextInput = "New-ItemProperty -LiteralPath $registryLiteralPath -PropertyType d"; ExpectedPropertyTypes = $dwordValueKind }
@{ TextInput = "New-ItemProperty -LiteralPath $registryLiteralPath -PropertyType q"; ExpectedPropertyTypes = $qwordValueKind }
@{ TextInput = "New-ItemProperty -LiteralPath $registryLiteralPath -PropertyType bin"; ExpectedPropertyTypes = $binaryValueKind }
@{ TextInput = "New-ItemProperty -LiteralPath $registryLiteralPath -PropertyType multi"; ExpectedPropertyTypes = $multiStringValueKind }
@{ TextInput = "New-ItemProperty -LiteralPath $registryLiteralPath -PropertyType invalidproptype"; ExpectedPropertyTypes = '' }
@{ TextInput = "New-ItemProperty -LiteralPath $fileSystemLiteralPath -PropertyType "; ExpectedPropertyTypes = '' }
# All of these should return no completion since they don't specify -Path/-LiteralPath
@{ TextInput = "New-ItemProperty -PropertyType "; ExpectedPropertyTypes = '' }
@{ TextInput = "New-ItemProperty -PropertyType d"; ExpectedPropertyTypes = '' }
@{ TextInput = "New-ItemProperty -PropertyType q"; ExpectedPropertyTypes = '' }
@{ TextInput = "New-ItemProperty -PropertyType bin"; ExpectedPropertyTypes = '' }
@{ TextInput = "New-ItemProperty -PropertyType multi"; ExpectedPropertyTypes = '' }
@{ TextInput = "New-ItemProperty -PropertyType invalidproptype"; ExpectedPropertyTypes = '' }
# All of these should return completion even with quotes included
@{ TextInput = "New-ItemProperty -Path $registryPath -PropertyType '"; ExpectedPropertyTypes = $allRegistryValueKindsWithQuotes }
@{ TextInput = "New-ItemProperty -Path $registryPath -PropertyType 'bin"; ExpectedPropertyTypes = "'$binaryValueKind'" }
) {
param($TextInput, $ExpectedPropertyTypes)
$res = TabExpansion2 -inputScript $TextInput -cursorColumn $TextInput.Length
$completionText = $res.CompletionMatches.CompletionText
$completionText -join ' ' | Should -BeExactly $ExpectedPropertyTypes
}
It "Test fallback to provider of current location if no path specified" -Skip:(!$IsWindows) {
try {
Push-Location HKCU:\
$textInput = "New-ItemProperty -PropertyType "
$res = TabExpansion2 -inputScript $textInput -cursorColumn $textInput.Length
$completionText = $res.CompletionMatches.CompletionText
$completionText -join ' ' | Should -BeExactly $allRegistryValueKinds
}
finally {
Pop-Location
}
}
AfterAll {
if ($IsWindows) {
Remove-Item -Path $registryPath -Force
Remove-Item -LiteralPath $registryLiteralPath -Force
Remove-Item -Path $fileSystemPath -Force
Remove-Item -LiteralPath $fileSystemLiteralPathDir -Recurse -Force
}
}
}
Context "Format cmdlet's View paramter completion" {
BeforeAll {
$viewDefinition = @'