Update test framework and tests to support 4x version of Pester (#6064)

* Test fixes and changes needed to support Pester 4.0.8

* Replace 'Should Contain' with new 'Should FileContentMatch' assertion

Explicitly check for string creation with write-output

* Use the current version of pester and install it in modulesDir

* Simplify logic for relative path test.

Multiple '..' is not needed for a relative path, a single one will do. Also, on multi-drive systems using split-path -noqualifier will probably do the wrong thing with regard to constructing a correct path.
Remove extraneous Should Not Throw test, if this throws, the test will fail, we don't need to explicitly assert the not throw

* In some environments it is possible that computer name is 'localhost', so that should be allowed

* [feature] Add link for migrating tests from Pester v3 to v4

Fix up capitalization and white space issues
Change one test to check FullyQualifiedErrorId rather than just `Should Throw`

* [feature] update invoke-item test to handle the case where multiple notepad processes are running

* Fix spelling issue with Pester 4x, calling it Pester 4 should be sufficient
This commit is contained in:
James Truher [MSFT] authored and Aditya Patwardhan committed 2018-02-05 11:58:25 -08:00
1 parent fa40a87bd7
commit 99a7d38cae
25 files changed
+120 -128

No files matched your search

@@ -65,8 +65,9 @@ bazz = 2
It "Should work for multiple lines" {
{ ConvertFrom-StringData -StringData $sampleData } | Should Not Throw
$(ConvertFrom-StringData -StringData $sampleData).Keys | Should Be "foo", "bar", "bazz"
# keys are not order guaranteed
$(ConvertFrom-StringData -StringData $sampleData).Keys | Should BeIn @("foo", "bar", "bazz")
$(ConvertFrom-StringData -StringData $sampleData).Values | Should Be "0","1","2"
$(ConvertFrom-StringData -StringData $sampleData).Values | Should BeIn @("0","1","2")
}
}
@@ -138,7 +138,9 @@ After the object
It "Test ConvertTo-HTML meta with invalid properties should throw warning" {
$parms = @{"authors"="John Doe";"keywords"="PowerShell,PSv6"}
($customObject | ConvertTo-HTML -Meta $parms 3>&1) -match $parms["authors"] | Should Be $true
# make this a string, rather than an array of string so match will behave
[string]$observedProperties = $customObject | ConvertTo-HTML -Meta $parms 3>&1
$observedProperties | Should Match $parms["authors"]
}
It "Test ConvertTo-HTML charset"{
@@ -63,46 +63,46 @@ Describe "Export-Alias DRT Unit Tests" -Tags "CI" {
It "Export-Alias for Default"{
Export-Alias $fulltestpath abcd01 -passthru
$fulltestpath| Should ContainExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should FileContentMatchExactly '"abcd01","efgh01","","None"'
}
It "Export-Alias As CSV"{
Export-Alias $fulltestpath abcd01 -As CSV -passthru
$fulltestpath| Should ContainExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should FileContentMatchExactly '"abcd01","efgh01","","None"'
}
It "Export-Alias As CSV With Description"{
Export-Alias $fulltestpath abcd01 -As CSV -description "My Aliases" -passthru
$fulltestpath| Should ContainExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should ContainExactly "My Aliases"
$fulltestpath| Should FileContentMatchExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should FileContentMatchExactly "My Aliases"
}
It "Export-Alias As CSV With Multiline Description"{
Export-Alias $fulltestpath abcd01 -As CSV -description "My Aliases\nYour Aliases\nEveryones Aliases" -passthru
$fulltestpath| Should ContainExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should ContainExactly "My Aliases"
$fulltestpath| Should ContainExactly "Your Aliases"
$fulltestpath| Should ContainExactly "Everyones Aliases"
$fulltestpath| Should FileContentMatchExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should FileContentMatchExactly "My Aliases"
$fulltestpath| Should FileContentMatchExactly "Your Aliases"
$fulltestpath| Should FileContentMatchExactly "Everyones Aliases"
}
It "Export-Alias As Script"{
Export-Alias $fulltestpath abcd01 -As Script -passthru
$fulltestpath| Should ContainExactly 'set-alias -Name:"abcd01" -Value:"efgh01" -Description:"" -Option:"None"'
$fulltestpath| Should FileContentMatchExactly 'set-alias -Name:"abcd01" -Value:"efgh01" -Description:"" -Option:"None"'
}
It "Export-Alias As Script With Multiline Description"{
Export-Alias $fulltestpath abcd01 -As Script -description "My Aliases\nYour Aliases\nEveryones Aliases" -passthru
$fulltestpath| Should ContainExactly 'set-alias -Name:"abcd01" -Value:"efgh01" -Description:"" -Option:"None"'
$fulltestpath| Should ContainExactly "My Aliases"
$fulltestpath| Should ContainExactly "Your Aliases"
$fulltestpath| Should ContainExactly "Everyones Aliases"
$fulltestpath| Should FileContentMatchExactly 'set-alias -Name:"abcd01" -Value:"efgh01" -Description:"" -Option:"None"'
$fulltestpath| Should FileContentMatchExactly "My Aliases"
$fulltestpath| Should FileContentMatchExactly "Your Aliases"
$fulltestpath| Should FileContentMatchExactly "Everyones Aliases"
}
It "Export-Alias for Force Test"{
Export-Alias $fulltestpath abcd01
Export-Alias $fulltestpath abcd02 -force
$fulltestpath| Should Not ContainExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should ContainExactly '"abcd02","efgh02","","None"'
$fulltestpath| Should Not FileContentMatchExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should FileContentMatchExactly '"abcd02","efgh02","","None"'
}
It "Export-Alias for Force ReadOnly Test" {
@@ -124,9 +124,9 @@ Describe "Export-Alias DRT Unit Tests" -Tags "CI" {
$_.FullyQualifiedErrorId | Should be "FileOpenFailure,Microsoft.PowerShell.Commands.ExportAliasCommand"
}
Export-Alias $fulltestpath abcd03 -force
$fulltestpath| Should Not ContainExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should Not ContainExactly '"abcd02","efgh02","","None"'
$fulltestpath| Should ContainExactly '"abcd03","efgh03","","None"'
$fulltestpath| Should Not FileContentMatchExactly '"abcd01","efgh01","","None"'
$fulltestpath| Should Not FileContentMatchExactly '"abcd02","efgh02","","None"'
$fulltestpath| Should FileContentMatchExactly '"abcd03","efgh03","","None"'
if ( $IsWindows )
{
@@ -145,7 +145,8 @@ Describe "Get-Variable" -Tags "CI" {
It "Should not be able to clear a global scope variable using the local switch" {
New-Variable globalVar -Value 1 -Scope global -Force
Get-Variable -Name globalVar -Scope local -ErrorAction SilentlyContinue | Should Throw
Get-Variable -Name globalVar -Scope local -ErrorAction SilentlyContinue -ErrorVariable removeGlobalAsLocal
$removeGlobalAsLocal.FullyQualifiedErrorId | Should Be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"
}
It "Should be able to get a global variable when there's one in the script scope" {
@@ -67,7 +67,8 @@ Describe "Invoke-Item basic tests" -Tags "Feature" {
Get-Process -Name $notepadProcessName | Stop-Process -Force
Invoke-Item -Path $notepad
$notepadProcess = Get-Process -Name $notepadProcessName
$notepadProcess.Name | Should Be $notepadProcessName
# we need BeIn because multiple notepad processes could be running
$notepadProcess.Name | Should BeIn $notepadProcessName
Stop-Process -InputObject $notepadProcess
}
} else {
@@ -85,8 +85,9 @@ Describe "New-Variable" -Tags "CI" {
}
It "Should not be able to set the name of a new variable to that of an old variable within same scope when the Force switch is missing" {
New-Variable var1
(New-Variable var1 -ErrorAction SilentlyContinue) | Should Throw
New-Variable var1
New-Variable var1 -ErrorAction SilentlyContinue -ErrorVariable newWhenExists
$newWhenExists.FullyQualifiedErrorId | Should Be "VariableAlreadyExists,Microsoft.PowerShell.Commands.NewVariableCommand"
}
It "Should change the value of an already existing variable using the Force switch" {
@@ -11,8 +11,8 @@ Describe "Out-File DRT Unit Tests" -Tags "CI" {
$tempFile = Join-Path -Path $TestDrive -ChildPath "outfileAppendTest.txt"
{ 'This is first line.' | out-file $tempFile } | Should Not Throw
{ 'This is second line.' | out-file -append $tempFile } | Should Not Throw
$tempFile |Should Contain "first"
$tempFile |Should Contain "second"
$tempFile |Should FileContentMatch "first"
$tempFile |Should FileContentMatch "second"
Remove-Item $tempFile -Force
}
}
@@ -12,7 +12,7 @@ Describe "Remove-Event" -Tags "CI" {
It "Should remove an event given a sourceidentifier" {
{ Remove-Event -sourceidentifier PesterTimer }
{ Get-Event -ErrorAction SilentlyContinue | Should Not Contain PesterTimer }
{ Get-Event -ErrorAction SilentlyContinue | Should Not FileMatchContent PesterTimer }
}
It "Should remove an event given an event identifier" {
@@ -20,19 +20,19 @@ Describe "Remove-Event" -Tags "CI" {
{ $events = $events.EventIdentifier }
{ Remove-Event -EventIdentifier $events }
{ $events = Get-Event -ErrorAction SilentlyContinue}
{ $events.SourceIdentifier | Should Not Contain "PesterTimer" }
{ $events.SourceIdentifier | Should Not FileMatchContent "PesterTimer" }
}
It "Should be able to remove an event given a pipe from Get-Event" {
{ Get-Event -sourceidentifier PesterTimer | Remove-Event }
{ Get-Event -ErrorAction SilentlyContinue | Should Not Contain "PesterTimer" }
{ Get-Event -ErrorAction SilentlyContinue | Should Not FileMatchContent "PesterTimer" }
}
It "Should NOT remove an event given the whatif flag" {
{ Remove-Event -sourceidentifier PesterTimer -whatif }
{ $events = Get-Event }
{ $events.SourceIdentifier | Should Contain "PesterTimer" }
{ $events.SourceIdentifier | Should FileContentMatch "PesterTimer" }
}
}
}
@@ -5,7 +5,8 @@ Describe "Remove-Variable" -Tags "CI" {
It "Should throw an error when a dollar sign is used in the variable name place" {
New-Variable -Name var1 -Value 4
Remove-Variable $var1 -ErrorAction SilentlyContinue | Should Throw
Remove-Variable $var1 -ErrorAction SilentlyContinue -ErrorVariable err
$err.FullyQualifiedErrorId | Should Be "VariableNotFound,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}
It "Should not throw error when used without the Name field, and named variable is properly specified and exists" {
@@ -25,7 +26,7 @@ Describe "Remove-Variable" -Tags "CI" {
}
It "Should throw error when used with Name field, and named variable does not exist" {
Remove-Variable -Name nonexistentVariable -ErrorAction SilentlyContinue | Should Throw
{ Remove-Variable -Name nonexistentVariable -ErrorAction Stop } | Should Throw
}
It "Should be able to remove a set of variables using wildcard characters" {
@@ -87,7 +88,7 @@ Describe "Remove-Variable" -Tags "CI" {
It "Should throw an error when attempting to remove a read-only variable and the Force switch is not used" {
New-Variable -Name var1 -Value 2 -Option ReadOnly
Remove-Variable -Name var1 -ErrorAction SilentlyContinue | Should Throw
{ Remove-Variable -Name var1 -ErrorAction Stop } | Should Throw
$var1 | Should Be 2
@@ -99,7 +100,7 @@ Describe "Remove-Variable" -Tags "CI" {
Remove-Variable -Name var1 -Force
$var1 | Should Be # Nothing. It should be nothing at all.
$var1 | Should BeNullOrEmpty
}
Context "Scope Tests" {
@@ -108,73 +109,73 @@ Describe "Remove-Variable" -Tags "CI" {
Remove-Variable -Name var1 -Scope global
$var1 | Should Be #Nothing.
$var1 | Should BeNullOrEmpty
}
It "Should not be able to clear a global variable using the local switch" {
New-Variable -Name var1 -Value "context" -Scope global
Remove-Variable -Name var1 -Scope local -ErrorAction SilentlyContinue | Should Throw
{ Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should Throw
$var1 | Should Be "context"
Remove-Variable -Name var1 -Scope global
$var1 | Should Be # Nothing
$var1 | Should BeNullOrEmpty
}
It "Should not be able to clear a global variable using the script switch" {
New-Variable -Name var1 -Value "context" -Scope global
Remove-Variable -Name var1 -Scope local -ErrorAction SilentlyContinue | Should Throw
{ Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should Throw
$var1 | Should Be "context"
Remove-Variable -Name var1 -Scope global
$var1 | Should Be # Nothing
$var1 | Should BeNullOrEmpty
}
It "Should be able to remove an item locally using the local switch" {
New-Variable -Name var1 -Value "context" -Scope local
Remove-Variable -Name var1 -Scope local -ErrorAction SilentlyContinue | Should Throw
{ Remove-Variable -Name var1 -Scope local -ErrorAction Stop } | Should Throw
$var1 | Should Be # Nothing
$var1 | Should Be context
}
It "Should be able to remove an item locally using the global switch" {
New-Variable -Name var1 -Value "context" -Scope local
Remove-Variable -Name var1 -Scope global -ErrorAction SilentlyContinue | Should Throw
{ Remove-Variable -Name var1 -Scope global -ErrorAction Stop } | Should Throw
$var1 | Should Be "context"
Remove-Variable -Name var1 -Scope local
$var1 | Should Be # Nothing
$var1 | Should BeNullOrEmpty
}
It "Should be able to remove a local variable using the script scope switch" {
New-Variable -Name var1 -Value "context" -Scope local
Remove-Variable -Name var1 -Scope script -ErrorAction SilentlyContinue | Should Throw
{ Remove-Variable -Name var1 -Scope script -ErrorAction Stop } | Should Throw
$var1 | Should Be "context"
Remove-Variable -Name var1 -Scope local
$var1 | Should Be # Nothing
$var1 | Should BeNullOrEmpty
}
It "Should be able to remove a script variable created using the script switch" {
New-Variable -Name var1 -Value "context" -Scope script
Remove-Variable -Name var1 -Scope script | Should Throw
{ Remove-Variable -Name var1 -Scope script } | Should Not Throw
$var1 | Should Be # Nothing
$var1 | Should BeNullOrEmpty
}
It "Should not be able to remove a global script variable that was created using the script scope switch" {
New-Variable -Name var1 -Value "context" -Scope script
Remove-Variable -Name var1 -Scope global -ErrorAction SilentlyContinue | Should Throw
{ Remove-Variable -Name var1 -Scope global -ErrorAction Stop } | Should Throw
$var1 | Should Be "context"
}
@@ -200,7 +201,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo | Should Match "SessionStateUnauthorizedAccessException"
$_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}
}
@@ -214,7 +215,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo| Should Match "SessionStateUnauthorizedAccessException"
$_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}
Remove-Variable foo -Force
$var1 = Get-Variable -Name foo -EA SilentlyContinue
@@ -231,7 +232,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo | Should Match "SessionStateUnauthorizedAccessException"
$_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}
try
@@ -242,7 +243,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo | Should Match "SessionStateUnauthorizedAccessException"
$_.FullyQualifiedErrorId | Should be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand"
}
}
@@ -258,7 +259,7 @@ Describe "Remove-Variable basic functionality" -Tags "CI" {
catch
{
$_.CategoryInfo | Should Match "ItemNotFoundException"
$_.FullyQualifiedErrorId | Should be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"
$_.FullyQualifiedErrorId | Should Be "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"
}
}
@@ -104,7 +104,7 @@ Describe "Select-String" -Tags "CI" {
It "Should return the name of the file and the string that 'string' is found if there is only one lines that has a match" {
$expected = $testInputFile + ":1:This is a text string, and another string"
Select-String $(Split-Path $testInputFile -NoQualifier) -Pattern "string" | Should Be $expected
Select-String $testInputFile -Pattern "string" | Should Be $expected
}
It "Should return all strings where 'second' is found in testfile1 if there is only one lines that has a match" {
@@ -148,8 +148,7 @@ Describe "Select-String" -Tags "CI" {
$expected = "testfile1.txt:3:This is the third line"
$relativePath = Join-Path -Path $testDirectory -ChildPath ".."
$relativePath = Join-Path -Path $relativePath -ChildPath ".."
$relativePath = Join-Path -Path $relativePath -ChildPath (Split-Path $testDirectory -NoQualifier)
$relativePath = Join-Path -Path $relativePath -ChildPath $TestDirectory.Name
$relativePath = Join-Path -Path $relativePath -ChildPath testfile1.txt
Select-String third $relativePath | Should Match $expected
}
@@ -67,8 +67,8 @@ set-psbreakpoint -command foo
It "-script and -line can take multiple items" {
$brk = sbp -line 11,12,13 -column 1 -script $scriptFileName,$scriptFileName
$brk.Line | Should Be 11,12,13
$brk.Column | Should Be 1
$brk.Line | Should BeIn 11,12,13
$brk.Column | Should BeIn 1
Remove-PSBreakPoint -Id $brk.Id
}
@@ -75,14 +75,12 @@ Describe "Write-Error Tests" -Tags "CI" {
$e.CategoryInfo.GetMessage() | Should Be 'NotSpecified: (fooTargetName:fooTargetType) [fooAct], fooReason'
}
It "Should be able to throw" {
Write-Error "test throw" -ErrorAction SilentlyContinue | Should Throw
It "Should be able to throw with -ErrorAction stop" {
{ Write-Error "test throw" -ErrorAction Stop } | Should Throw
}
It "Should throw a non-terminating error" {
Write-Error "test throw" -ErrorAction SilentlyContinue
1 + 1 | Should Be 2
{ Write-Error "test throw" -ErrorAction SilentlyContinue } | Should Not Throw
}
It "Should trip an exception using the exception switch" {
@@ -106,17 +104,17 @@ Describe "Write-Error Tests" -Tags "CI" {
$theError = "Error: Too many input values."
write-error -message $theError -category InvalidArgument -ErrorAction SilentlyContinue
$error[0]| Should Be $theError
[string]$error[0]| Should Be $theError
}
It "ErrorRecord should not be truncated" {
It "ErrorRecord should not be truncated or have inserted newlines when redirected from another process" {
$longtext = "0123456789"
while ($longtext.Length -lt [console]::WindowWidth) {
$longtext += $longtext
}
pwsh -c Write-Error -Message $longtext 2>&1 > $testdrive\error.txt
$e = Get-Content -Path $testdrive\error.txt
$e.Count | Should BeExactly 4
$e[0] | Should Match $longtext
$pwsh = $pshome + "/pwsh"
$result = & $pwsh -c Write-Error -Message $longtext 2>&1
$result.Count | Should BeExactly 4
$result[0] | Should Match $longtext
}
}
@@ -16,7 +16,8 @@ Describe "Write-Output DRT Unit Tests" -Tags "CI" {
It "Works with NoEnumerate switch" {
$objectWritten = 1, 2.2, @("John", "Smith", 10), "abc"
Write-Output $objectWritten -NoEnumerate 6>&1 | Should be '1 2.2 System.Object[] abc'
[string]$s = Write-Output $objectWritten -NoEnumerate 6>&1
$s | Should be '1 2.2 System.Object[] abc'
}
}
@@ -15,15 +15,6 @@ Describe "Stream writer tests" -Tags "CI" {
}
function Get-OutputResults
{
# Get the contents of the targetfile.
# Make the array a string for less brittle testing
$output = $(Get-Content $args[0])
[String]::Join([Environment]::NewLine, $output )
return $output
}
Context "Redirect Stream Tests" {
# These tests validate that a stream is actually being written to by redirecting the output of that stream
@@ -36,28 +27,20 @@ Describe "Stream writer tests" -Tags "CI" {
It "Should write error messages to the error stream" {
Write-Error "Testing Error" 2>&1 > $targetfile
$result = Get-OutputResults $targetfile
# The contents of the error stream should contain the expected text
$result -match ": Testing Error" | Should Be $true
$targetfile | Should FileContentMatch ": Testing Error"
}
It "Should write debug messages to the debug stream" {
Write-Messages -Debug -EA SilentlyContinue 5>&1 > $targetfile
$result = Get-OutputResults $targetfile
# The contents of the debug stream should contain the expected text
$result -match "Debug Message" | Should Be $true
$targetfile | Should FileContentMatch "Debug Message"
}
It "Should write messages to the verbose stream" {
Write-Messages -Verbose 4>&1 > $targetfile
$result = Get-OutputResults $targetfile
# The contents of the debug stream should contain the expected text
$result -match "Verbose Message" | Should Be $true
$targetfile | Should FileContentMatch "Verbose Message"
}
}
@@ -83,7 +66,7 @@ Describe "Stream writer tests" -Tags "CI" {
$ps.Dispose()
}
It "Write-Information outputs an information object" -Pending:($IsMacOS) {
It "Write-Information outputs an information object" {
# redirect the streams is sufficient
$result = Write-Information "Test Message" *>&1
$result.NativeThreadId | Should Not Be 0
@@ -91,7 +74,7 @@ Describe "Stream writer tests" -Tags "CI" {
$result | Should BeOfType System.Management.Automation.InformationRecord
# Use Match instead of Be so we can avoid dealing with a potential domain name
$result.Computer | Should Match "^($([environment]::MachineName)){1}(\.[a-zA-Z0-9]+)*$"
$result.Computer | Should Match "^($([environment]::MachineName)){1}(\.[a-zA-Z0-9]+)*$|^localhost$"
if ($IsWindows)
{
$result.User | Should Match ".*${env:USERNAME}"