From 7e2324d4d7d2caa1e3dfe9b333fb89722342aa3e Mon Sep 17 00:00:00 2001 From: iSazonov Date: Wed, 13 Sep 2017 14:19:12 +0300 Subject: [PATCH 1/2] Exclude directories from -Path in Select-String Select-String can search in files only so we should skip directory objects. --- .../commands/utility/MatchString.cs | 5 +++++ .../Microsoft.PowerShell.Utility/string.tests.ps1 | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 5cc6d26738b..7645efc2026 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -1347,6 +1347,11 @@ protected override void ProcessRecord() { foreach (string filename in expandedPaths) { + if (System.IO.Directory.Exists(filename)) + { + continue; + } + bool foundMatch = ProcessFile(filename); if (_quiet && foundMatch) return; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 index f31b2d1fdc2..4c08fcbac69 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 @@ -9,6 +9,11 @@ $fileNameWithDots = $fileName.FullName.Replace("\", "\.\") + $subDirName = Join-Path $TestDrive 'selectSubDir' + New-Item -Path $subDirName -ItemType Directory > $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" } | Should Not Throw + } + + It "Select-String does not throw on subdirectory (path with wildcard)" { + { select-string -Path $pathWithWildcard "noExists" } | Should Not Throw + } + It "LiteralPath with relative path" { (select-string -LiteralPath (Get-Item -LiteralPath $fileName).Name "b").count | Should Be 2 } From f601ebf16de286f21f43b0b1b7bd9e91fdd11c30 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 14 Sep 2017 07:47:11 +0300 Subject: [PATCH 2/2] Minor fixes --- .../commands/utility/MatchString.cs | 2 +- .../Modules/Microsoft.PowerShell.Utility/string.tests.ps1 | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 7645efc2026..88fac123a4a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -1347,7 +1347,7 @@ protected override void ProcessRecord() { foreach (string filename in expandedPaths) { - if (System.IO.Directory.Exists(filename)) + if (Directory.Exists(filename)) { continue; } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 index 4c08fcbac69..4d3eb7df81c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 @@ -10,7 +10,7 @@ $fileNameWithDots = $fileName.FullName.Replace("\", "\.\") $subDirName = Join-Path $TestDrive 'selectSubDir' - New-Item -Path $subDirName -ItemType Directory > $null + New-Item -Path $subDirName -ItemType Directory -Force > $null $pathWithoutWildcard = $TestDrive $pathWithWildcard = Join-Path $TestDrive '*' @@ -30,11 +30,11 @@ } It "Select-String does not throw on subdirectory (path without wildcard)" { - { select-string -Path $pathWithoutWildcard "noExists" } | Should Not Throw + { 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" } | Should Not Throw + { select-string -Path $pathWithWildcard "noExists" -ErrorAction Stop } | Should Not Throw } It "LiteralPath with relative path" {