Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function Test-UnblockFile {
try {
Get-Content -Path $testfilepath -Stream Zone.Identifier -ErrorAction Stop | Out-Null
}
catch {
if ($_.FullyQualifiedErrorId -eq "GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand") {
return $true
}
}

return $false
}

Describe "Unblock-File" -Tags "CI" {

BeforeAll {
if ( ! $IsWindows )
{
$origDefaults = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues['it:skip'] = $true

} else {
$testfilepath = Join-Path -Path $TestDrive -ChildPath testunblockfile.ttt
}
}

AfterAll {
if ( ! $IsWindows ){
$global:PSDefaultParameterValues = $origDefaults
}
}

BeforeEach {
if ( $IsWindows ){
Set-Content -Value "[ZoneTransfer]`r`nZoneId=4" -Path $testfilepath -Stream Zone.Identifier
}
}

It "With '-Path': no file exist" {
try {
Unblock-File -Path nofileexist.ttt -ErrorAction Stop
throw "No Exception!"
Copy link
Member

Choose a reason for hiding this comment

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

Same as the other comment.

Copy link
Member

Choose a reason for hiding this comment

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

I am fine with this being kept as "No Exception"

}
catch {
$_.FullyQualifiedErrorId | Should Be "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"
}
}

It "With -LiteralPath': no file exist" {
try {
Unblock-File -LiteralPath nofileexist.ttt -ErrorAction Stop
throw "No Exception!"
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer this to be following so that the logs on failure have good error message.

throw "No Exception thrown when FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand was expected"

Copy link
Collaborator Author

@iSazonov iSazonov Jan 6, 2017

Choose a reason for hiding this comment

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

I have two "cons" (for all comments).
I believe we should follow the documentation (throw "No Exception!" ) and preserve consistent in all the tests that currently use this docs template. If a template is bad, then we should change it and bring all the tests to the new template.

If Unblock-File test returns FileNotFound, then this will not help our understanding of the problem's root and why the exception throw. (We should mention a Zone.Identify stream and give a link to a MSDN article in the message.) I believe this is redundant, since in any case we will always start with the analysis of the test code and do more manual tests. So I prefer to see Expected : {True} But was : {False}. that simple means that a file is not unlocked.

Copy link
Member

Choose a reason for hiding this comment

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

I am fine with this being kept as "No Exception"

But as of the function returning a $true or $false, I would prefer it to be like below as the error will give us the line number where the should is located :

function Test-UnblockFile {
    try {
        Get-Content -Path $testfilepath -Stream Zone.Identifier -ErrorAction Stop | Out-Null
        throw "No Exception"
    }
    catch {
        $_.FullyQualifiedErrorId | Should Be "GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand"        
    }
}

It "With '-Path': file exist" {
        Unblock-File -Path $testfilepath
        Test-UnblockFile 
    }

    It "With '-LiteralPath': file exist" {
        Unblock-File -LiteralPath $testfilepath
        Test-UnblockFile 
    }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we move Should to a function we get line number in the function, don't we? It seems this is not what we want.
And I am waiting #2973

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@adityapatwardhan Issue #2973 was closed. Please continue the review.

}
catch {
$_.FullyQualifiedErrorId | Should Be "FileNotFound,Microsoft.PowerShell.Commands.UnblockFileCommand"
}
}

It "With '-Path': file exist" {
Unblock-File -Path $testfilepath
Test-UnblockFile | Should Be $true
Copy link
Member

Choose a reason for hiding this comment

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

If this test fails, the error message would be Expected : {True} But was : {False}. This does not help in debugging the failure. I would prefer this to be like:

Unblock-File -Path $testfilepath
try {
    Get-Content -Path $testfilepath -Stream Zone.Identifier -ErrorAction Stop | Out-Null
     throw "No exception thrown when GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand was expected"|
}
catch {
    $_.FullyQualifiedErrorId | Should Be "GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand"
}

}

It "With '-LiteralPath': file exist" {
Unblock-File -LiteralPath $testfilepath
Test-UnblockFile | Should Be $true
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

}
}