Write an error if argument is a directory in Get-FileHash cmdlet (#11114)
This commit is contained in:
parent
0aaced35ad
commit
8d944fdec2
|
@ -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();
|
||||
|
|
|
@ -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" {
|
||||
|
|
Loading…
Reference in a new issue