Add tests for command globbing (#9180)

Add tests for command globbing
  - Also, add `-noprofile` to places where `pwsh` is launched so they don't fail locally.
This commit is contained in:
Travis Plunk
2019-03-21 14:18:23 -07:00
committed by Travis Plunk
parent 818c7de89c
commit 289ac4f551
2 changed files with 76 additions and 2 deletions
@@ -26,13 +26,13 @@ Describe "TabCompletion" -Tags CI {
}
It 'Should complete abbreviated cmdlet' {
$res = pwsh -settingsfile $configFilePath -c "(TabExpansion2 -inputScript 'i-psdf' -cursorColumn 'pschr'.Length).CompletionMatches.CompletionText"
$res = pwsh -noprofile -settingsfile $configFilePath -c "(TabExpansion2 -inputScript 'i-psdf' -cursorColumn 'pschr'.Length).CompletionMatches.CompletionText"
$res | Should -HaveCount 1
$res | Should -BeExactly 'Import-PowerShellDataFile'
}
It 'Should complete abbreviated function' {
$res = pwsh -settingsfile $configFilePath -c "(TabExpansion2 -inputScript 'pschrl' -cursorColumn 'pschr'.Length).CompletionMatches.CompletionText"
$res = pwsh -noprofile -settingsfile $configFilePath -c "(TabExpansion2 -inputScript 'pschrl' -cursorColumn 'pschr'.Length).CompletionMatches.CompletionText"
$res.Count | Should -BeGreaterOrEqual 1
$res | Should -BeExactly 'PSConsoleHostReadLine'
}
@@ -245,6 +245,51 @@ Describe "TabCompletion" -Tags CI {
$completionText -join ' '| Should -BeExactly 'blg csv tsv'
}
Context "Script name completion" {
BeforeAll {
setup -f 'install-powershell.ps1' -content ""
setup -f 'remove-powershell.ps1' -content ""
$scriptWithWildcardCases = @(
@{
command = '.\install-*.ps1'
expectedCommand = Join-Path -Path '.' -ChildPath 'install-powershell.ps1'
name = "'$(Join-Path -Path '.' -ChildPath 'install-powershell.ps1')'"
}
@{
command = (Join-Path ${TestDrive} -ChildPath 'install-*.ps1')
expectedCommand = (Join-Path ${TestDrive} -ChildPath 'install-powershell.ps1')
name = "'$(Join-Path -Path '.' -ChildPath 'install-powershell.ps1')' by fully qualified path"
}
@{
command = '.\?emove-powershell.ps1'
expectedCommand = Join-Path -Path '.' -ChildPath 'remove-powershell.ps1'
name = "'$(Join-Path -Path '.' -ChildPath '?emove-powershell.ps1')'"
}
@{
# [] cause the parser to create a new token.
# So, the command must be quoted to tab complete.
command = "'.\[ra]emove-powershell.ps1'"
expectedCommand = "'$(Join-Path -Path '.' -ChildPath 'remove-powershell.ps1')'"
name = "'$(Join-Path -Path '.' -ChildPath '[ra]emove-powershell.ps1')'"
}
)
Push-Location ${TestDrive}\
}
AfterAll {
Pop-Location
}
it "Input <name> should successfully complete" -TestCases $scriptWithWildcardCases {
param($command, $expectedCommand)
$res = TabExpansion2 -inputScript $command -cursorColumn $command.Length
$res.CompletionMatches.Count | Should -BeGreaterThan 0
$res.CompletionMatches[0].CompletionText | Should -BeExactly $expectedCommand
}
}
Context "File name completion" {
BeforeAll {
$tempDir = Join-Path -Path $TestDrive -ChildPath "baseDir"
@@ -84,4 +84,33 @@ Describe "Command Discovery tests" -Tags "CI" {
It "Get- is prepended to commands" {
(& 'location').Path | Should -Be (get-location).Path
}
Context "Get-Command should use globbing for scripts" {
BeforeAll {
$firstResult = '[first script]'
$secondResult = 'alt script'
setup -f '[test1].ps1' -content "'$firstResult'"
setup -f '1.ps1' -content "'$secondResult'"
$gcmWithWildcardCases = @(
@{command = '.\?[tb]est1?.ps1'; expectedCommand = '[test1].ps1'; expectedCommandCount =1; name = '''.\?[tb]est1?.ps1'''}
@{command = (Join-Path ${TestDrive} -ChildPath '?[tb]est1?.ps1'); expectedCommand = '[test1].ps1'; expectedCommandCount =1 ; name = '''.\?[tb]est1?.ps1'' by fully qualified path'}
@{command = '.\[test1].ps1'; expectedCommand = '1.ps1'; expectedCommandCount =1; name = '''.\[test1].ps1'''}
@{command = (Join-Path ${TestDrive} -ChildPath '[test1].ps1'); expectedCommand = '1.ps1'; expectedCommandCount =1 ; name = '''.\[test1].ps1'' by fully qualified path'}
)
Push-Location ${TestDrive}\
}
AfterAll {
Pop-Location
}
It "Get-Command <name> should return <expectedCommandCount> command named '<expectedCommand>'" -TestCases $gcmWithWildcardCases {
param($command, $expectedCommand, $expectedCommandCount)
$commands = Get-Command -Name $command
$commands.Count | Should -Be $expectedCommandCount
$commands.Name | Should -BeExactly $expectedCommand
}
}
}