mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user