Write an error if argument is a directory in Get-FileHash cmdlet (#11114)

This commit is contained in:
Ilya 2019-11-21 15:15:19 +05:00 committed by GitHub
parent 0aaced35ad
commit 8d944fdec2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View file

@ -140,12 +140,22 @@ namespace Microsoft.PowerShell.Commands
}
catch (FileNotFoundException ex)
{
ErrorRecord errorRecord = new ErrorRecord(ex,
var errorRecord = new ErrorRecord(
ex,
"FileNotFound",
ErrorCategory.ObjectNotFound,
path);
WriteError(errorRecord);
}
catch (UnauthorizedAccessException ex)
{
var errorRecord = new ErrorRecord(
ex,
"UnauthorizedAccessError",
ErrorCategory.InvalidData,
path);
WriteError(errorRecord);
}
finally
{
openfilestream?.Dispose();

View file

@ -18,10 +18,16 @@ Describe "Get-FileHash" -Tags "CI" {
Context "Default result tests" {
It "Should default to correct algorithm, hash and path" {
$result = Get-FileHash $testDocument
$result.Algorithm | Should Be "SHA256"
$result.Algorithm | Should -Be "SHA256"
$result.Hash | Should -Be "41620f6c9f3531722efe90aed9abbc1d1b31788aa9141982030d3dde199f770c"
$result.Path | Should -Be $testDocument
}
It "Should write non-terminating error if argument is a folder" {
$result = $pshome, "${pshome}\pwsh.dll" | Get-FileHash -ErrorVariable errorVariable
$result.Count | Should -Be 1
$errorVariable.FullyQualifiedErrorId | Should -BeExactly "UnauthorizedAccessError,Microsoft.PowerShell.Commands.GetFileHashCommand"
}
}
Context "Algorithm tests" {