Exclude directories discovered from '-Path' in Select-String (#4829)

Select-String can search in files only so we should skip directories.
This commit is contained in:
Ilya
2017-09-14 14:57:17 -07:00
committed by Dongbo Wang
parent d804d7fd30
commit a4cdb806f4
2 changed files with 18 additions and 0 deletions
@@ -1347,6 +1347,11 @@ namespace Microsoft.PowerShell.Commands
{
foreach (string filename in expandedPaths)
{
if (Directory.Exists(filename))
{
continue;
}
bool foundMatch = ProcessFile(filename);
if (_quiet && foundMatch)
return;
@@ -9,6 +9,11 @@
$fileNameWithDots = $fileName.FullName.Replace("\", "\.\")
$subDirName = Join-Path $TestDrive 'selectSubDir'
New-Item -Path $subDirName -ItemType Directory -Force > $null
$pathWithoutWildcard = $TestDrive
$pathWithWildcard = Join-Path $TestDrive '*'
$tempFile = New-TemporaryFile
"abc" | Out-File -LiteralPath $tempFile.fullname
"bcd" | Out-File -LiteralPath $tempFile.fullname -Append
@@ -24,6 +29,14 @@
Pop-Location
}
It "Select-String does not throw on subdirectory (path without wildcard)" {
{ select-string -Path $pathWithoutWildcard "noExists" -ErrorAction Stop } | Should Not Throw
}
It "Select-String does not throw on subdirectory (path with wildcard)" {
{ select-string -Path $pathWithWildcard "noExists" -ErrorAction Stop } | Should Not Throw
}
It "LiteralPath with relative path" {
(select-string -LiteralPath (Get-Item -LiteralPath $fileName).Name "b").count | Should Be 2
}