Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude directories from -Path in Select-String #4829

Merged
merged 2 commits into from
Sep 14, 2017

Conversation

iSazonov
Copy link
Collaborator

Fix #4820.

Select-String can search in files only so we should skip directory
objects.

Select-String can search in files only so we should skip directory
objects.
@@ -1347,6 +1347,11 @@ protected override void ProcessRecord()
{
foreach (string filename in expandedPaths)
{
if (System.IO.Directory.Exists(filename))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a using directive for System.IO.Directory, so any reason to use the full type name here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

It "Select-String does not throw on subdirectory (path with wildcard)" {
{ select-string -Path $pathWithWildcard "noExists" } | Should Not Throw
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current behavior is to throw a non-terminating error, so for this test, it will pass even if it actually failed. see [+] ee 297ms below

PS:122> describe "aa" { It "ee" { { Select-String -Path * hi } | should not throw } }
Describing aa
Select-String : The file F:\tmp\test\pp cannot be read: Access to the path 'F:\tmp\test\pp' is denied.
At line:1 char:29
+ describe "aa" { It "ee" { { Select-String -Path * hi } | should not t ...
+                             ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-String], ArgumentException
    + FullyQualifiedErrorId : ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand

 [+] ee 297ms

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the above should not throw test, you should verify the expected error (ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand)

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not throw might not be a good test here because it doesn't throw with the current behavior -- a non-terminating error is written out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test is fine but you should also verify the reported error (ProcessingFile,Microsoft.PowerShell.Commands.SelectStringCommand) either in the test or in another test.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I skipped '-ErrorAction Stop' - Fixed.

@@ -9,6 +9,11 @@

$fileNameWithDots = $fileName.FullName.Replace("\", "\.\")

$subDirName = Join-Path $TestDrive 'selectSubDir'
New-Item -Path $subDirName -ItemType Directory > $null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest using -ErrorAction Stop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see we usualy use -Force - I added.
Please clarify about -ErrorAction Stop - I don't found such samples in our tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, its all about failure diagnosability. The test is dependent on the directory and if new-item fails, there's no point in the test continuing. Using -EA Stop ensures you get the error at the point of the failure instead of later on when it may be muddled by test or product code.
Its just a suggestion.

@daxian-dbw daxian-dbw merged commit a4cdb80 into PowerShell:master Sep 14, 2017
@iSazonov iSazonov deleted the select-string-dir branch September 15, 2017 03:04
@@ -1347,6 +1347,11 @@ protected override void ProcessRecord()
{
foreach (string filename in expandedPaths)
{
if (Directory.Exists(filename))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I actually think we should only ignore directories if they're among the paths that result from wildcard resolution.

If you specify an effectively literal directory path - whether via -LiteralPath or via -Path and a path that happens not to contain (unescaped) wildcard characters - you should continue to get an error (though arguably a better one than currently).

This will let users know that specifying directories is not supported. With this fix as-is, If you do something like Select-String foo ., you'll get no output and no error, which could be misinterpreted as the string not having been found in any files in the directory.

I think the behavior should be analogous to the following Get-ChildItem behavior:

Get-ChildItem -Directory -LiteralPath /NoSuchDir  # error
Get-ChildItem -Directory -Path /NoSuchDir  # error too, because the path contains no (unescaped) wildcard chars.
Get-ChildItem -Directory -Path /NoSuchDir*  # NO error, because the wildcard expression matched nothing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. We can reactivate #4820 and get the behavior fixed. @iSazonov what do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now Select-String behavior is the same as Unblock-File. There may be other examples (?)
So I believe it is another issue.
I agree that we could fix this with a "breaking change" symptom. If @mklement0 would done the analysis (what cmdlets should we fix?), I'd have solved the Issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants