Skip to content

Commit 99a7d38

Browse files
JamesWTruheradityapatwardhan
authored andcommitted
Update test framework and tests to support 4x version of Pester (PowerShell#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
1 parent fa40a87 commit 99a7d38

25 files changed

Lines changed: 120 additions & 128 deletions

build.psm1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,8 @@ function Restore-PSModuleToBuild
662662

663663
if($CI.IsPresent)
664664
{
665-
Restore-PSPester -Destination $modulesDir
665+
# take the latest version of pester and install it so it may be used
666+
Save-Module -Path $modulesDir -Name Pester
666667
}
667668
}
668669

@@ -987,7 +988,7 @@ function Start-PSPester {
987988
Write-Warning @"
988989
Pester module not found.
989990
Restore the module to '$Pester' by running:
990-
Restore-PSPester
991+
Save-Module Pester -Path '$binDir/Modules'
991992
"@
992993
return;
993994
}

docs/testing-guidelines/WritingPesterTests.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
### Writing Pester Tests
2-
Note that this document does not replace the documents found in the [Pester](https://github.com/pester/pester "Pester") project. This is just
3-
some quick tips and suggestions for creating Pester tests for this project. The Pester community is vibrant and active, if you have questions
4-
about Pester or creating tests, the [Pester Wiki](https://github.com/pester/pester/wiki) has a lot of great information.
2+
Note that this document does not replace the documents found in the [Pester](https://github.com/pester/pester "Pester") project.
3+
This is just some quick tips and suggestions for creating Pester tests for this project.
4+
The Pester community is vibrant and active, if you have questions about Pester or creating tests, the [Pester Wiki](https://github.com/pester/pester/wiki) has a lot of great information.
5+
As of January 2018, PowerShell Core is using Pester version 4 which has some changes from earlier versions.
6+
See [Migrating from Pester 3 to Pester 4](https://github.com/pester/Pester/wiki/Migrating-from-Pester-3-to-Pester-4) for more information.
7+
8+
59

610
When creating tests, keep the following in mind:
711
* Tests should not be overly complicated and test too many things
@@ -16,7 +20,7 @@ Here's the simplest of tests
1620
Describe "A variable can be assigned and retrieved" {
1721
It "Create a variable and make sure its value is correct" {
1822
$a = 1
19-
$a | Should be 1
23+
$a | Should Be 1
2024
}
2125
}
2226
```
@@ -27,7 +31,7 @@ If you need to do type checking, that can be done as well
2731
Describe "One is really one" {
2832
It "Compare 1 to 1" {
2933
$a = 1
30-
$a | Should be 1
34+
$a | Should Be 1
3135
}
3236
It "1 is really an int" {
3337
$i = 1
@@ -42,7 +46,7 @@ alternatively, you could do the following:
4246
Describe "One is really one" {
4347
It "Compare 1 to 1" {
4448
$a = 1
45-
$a | Should be 1
49+
$a | Should Be 1
4650
}
4751
It "1 is really an int" {
4852
$i = 1
@@ -136,7 +140,7 @@ $testCases = @(
136140
Describe "A test" {
137141
It "<a> -xor <b> should be <expectedresult>" -testcase $testcases {
138142
param ($a, $b, $ExpectedResult)
139-
$a -xor $b | Should be $ExpectedResult
143+
$a -xor $b | Should Be $ExpectedResult
140144
}
141145
}
142146
```
@@ -151,7 +155,7 @@ The following example illustrates simple use:
151155
Context "Get-Random is not random" {
152156
Mock Get-Random { return 3 }
153157
It "Get-Random returns 3" {
154-
Get-Random | Should be 3
158+
Get-Random | Should Be 3
155159
}
156160
}
157161
```

test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
It "should only load the specified version" {
4444
Import-Module TestModule -RequiredVersion 1.1
45-
(Get-Module TestModule).Version | Should Be "1.1"
45+
(Get-Module TestModule).Version | Should BeIn "1.1"
4646
}
4747
}
4848

@@ -192,7 +192,7 @@ Describe "Import-Module should be case insensitive" -Tags 'CI' {
192192
Import-Module testMODULE
193193
$m = Get-Module TESTmodule
194194
$m | Should BeOfType "System.Management.Automation.PSModuleInfo"
195-
$m.Name | Should Be "TESTMODULE"
195+
$m.Name | Should BeIn "TESTMODULE"
196196
mytest | Should BeExactly "hello"
197197
Remove-Module TestModule
198198
Get-Module tESTmODULE | Should BeNullOrEmpty

test/powershell/Modules/Microsoft.PowerShell.Core/Out-Default.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Describe "Out-Default Tests" -tag CI {
1515
"@
1616

1717
& $powershell -noprofile -command $script | Should BeExactly 'bye'
18-
"TestDrive:\transcript.txt" | Should Contain 'hello'
18+
"TestDrive:\transcript.txt" | Should FileContentMatch 'hello'
1919
}
2020

2121
It "Out-Default reverts transcription state when used more than once in a pipeline" {

test/powershell/Modules/Microsoft.PowerShell.Management/Clear-Content.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Describe "Clear-Content cmdlet tests" -Tags "CI" {
6464
# we could suppress the WhatIf output here if we use the testhost, but it's not necessary
6565
It "The filesystem provider supports should process" -skip:(!$IsWindows) {
6666
clear-content TESTDRIVE:\$file2 -WhatIf
67-
"TESTDRIVE:\$file2" | should contain "This is content"
67+
"TESTDRIVE:\$file2" | should FileContentMatch "This is content"
6868
}
6969

7070
It "The filesystem provider should support ShouldProcess (reference ProviderSupportsShouldProcess member)" {

test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ Describe "Handling of globbing patterns" -Tags "CI" {
378378
BeforeEach {
379379
$file = New-Item -ItemType File -Path $filePath -Force
380380
}
381-
AfterEach
382-
{
381+
AfterEach {
383382
Remove-Item -Force -Recurse -Path $dirPath -ErrorAction SilentlyContinue
384383
Remove-Item -Force -LiteralPath $newPath -ErrorAction SilentlyContinue
385384
}
@@ -1392,7 +1391,7 @@ Describe "UNC paths" -Tags 'CI' {
13921391
$testPath = Join-Path "\\localhost" $systemDrive
13931392
& $cmdlet $testPath
13941393
Get-Location | Should BeExactly "Microsoft.PowerShell.Core\FileSystem::$testPath"
1395-
$children = { Get-ChildItem -ErrorAction Stop } | Should Not Throw
1394+
$children = Get-ChildItem -ErrorAction Stop
13961395
$children.Count | Should BeGreaterThan 0
13971396
}
13981397
finally {

test/powershell/Modules/Microsoft.PowerShell.Management/Move-Item.Tests.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Describe "Move-Item tests" -Tag "CI" {
99
Move-Item $source $target
1010
test-path $source | Should be $false
1111
test-path $target | Should be $true
12-
"$target" | Should ContainExactly "This is content"
12+
"$target" | Should FileContentMatchExactly "This is content"
1313
}
1414

1515
Context "Move-Item with filters" {
@@ -49,7 +49,7 @@ Describe "Move-Item tests" -Tag "CI" {
4949
$newBarPath | Should Exist
5050
$booPath | Should Exist
5151
$fooPath | Should Exist
52-
$newBarPath | Should ContainExactly $barContent
52+
$newBarPath | Should FileContentMatchExactly $barContent
5353
}
5454
It "Can move to different directory, filtered with -Exclude" {
5555
Move-Item -Path $filePath -Destination $moveToPath -Exclude "b*" -ErrorVariable e -ErrorAction SilentlyContinue
@@ -58,7 +58,7 @@ Describe "Move-Item tests" -Tag "CI" {
5858
$newFooPath | Should Exist
5959
$booPath | Should Exist
6060
$barPath | Should Exist
61-
$newFooPath | Should ContainExactly $fooContent
61+
$newFooPath | Should FileContentMatchExactly $fooContent
6262
}
6363
It "Can move to different directory, filtered with -Filter" {
6464
Move-Item -Path $filePath -Destination $moveToPath -Filter "bo*" -ErrorVariable e -ErrorAction SilentlyContinue
@@ -67,7 +67,7 @@ Describe "Move-Item tests" -Tag "CI" {
6767
$newBooPath | Should Exist
6868
$barPath | Should Exist
6969
$fooPath | Should Exist
70-
$newBooPath | Should ContainExactly $booContent
70+
$newBooPath | Should FileContentMatchExactly $booContent
7171
}
7272

7373
It "Can rename via move, filtered with -Include" {
@@ -77,7 +77,7 @@ Describe "Move-Item tests" -Tag "CI" {
7777
$barPath | Should Not Exist
7878
$booPath | Should Exist
7979
$fooPath | Should Exist
80-
$renameToPath | Should ContainExactly $barContent
80+
$renameToPath | Should FileContentMatchExactly $barContent
8181
}
8282
It "Can rename via move, filtered with -Exclude" {
8383
Move-Item -Path $filePath -Destination $renameToPath -Exclude "b*" -ErrorVariable e -ErrorAction SilentlyContinue
@@ -86,7 +86,7 @@ Describe "Move-Item tests" -Tag "CI" {
8686
$fooPath | Should Not Exist
8787
$booPath | Should Exist
8888
$barPath | Should Exist
89-
$renameToPath | Should ContainExactly $fooContent
89+
$renameToPath | Should FileContentMatchExactly $fooContent
9090
}
9191
It "Can rename via move, filtered with -Filter" {
9292
Move-Item -Path $filePath -Destination $renameToPath -Filter "bo*" -ErrorVariable e -ErrorAction SilentlyContinue
@@ -95,7 +95,7 @@ Describe "Move-Item tests" -Tag "CI" {
9595
$booPath | Should Not Exist
9696
$fooPath | Should Exist
9797
$barPath | Should Exist
98-
$renameToPath | Should ContainExactly $booContent
98+
$renameToPath | Should FileContentMatchExactly $booContent
9999
}
100100
}
101101
}

test/powershell/Modules/Microsoft.PowerShell.Management/Rename-Item.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Describe "Rename-Item tests" -Tag "CI" {
99
Rename-Item $source $target
1010
test-path $source | Should be $false
1111
test-path $target | Should be $true
12-
"$target" | Should ContainExactly "This is content"
12+
"$target" | Should FileContentMatchExactly "This is content"
1313
}
1414
}

test/powershell/Modules/Microsoft.PowerShell.Management/TimeZone.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ Describe "Get-Timezone test cases" -Tags "CI" {
6363
It "Call with ListAvailable switch returns a list containing TimeZoneInfo.Local" {
6464
$observedIdList = Get-TimeZone -ListAvailable | Select-Object -ExpandProperty BaseUtcOffset
6565
$oneExpectedOffset = ([System.TimeZoneInfo]::Local).BaseUtcOffset
66-
$observedIdList -eq $oneExpectedOffset | Should Be $oneExpectedOffset
66+
$oneExpectedOffset | Should BeIn $observedIdList
6767
}
6868

6969
## The local time zone could be set to UTC or GMT*. In this case, the .NET API returns the region ID
7070
## and not UTC. To avoid a string matching error, we compare the BaseUtcOffset instead.
7171
It "Call with ListAvailable switch returns a list containing one returned by Get-TimeZone" {
7272
$observedIdList = Get-TimeZone -ListAvailable | Select-Object -ExpandProperty BaseUtcOffset
7373
$oneExpectedOffset = (Get-TimeZone).BaseUtcOffset
74-
$observedIdList -eq $oneExpectedOffset | Should Be $oneExpectedOffset
74+
$oneExpectedOffset | Should BeIn $observedIdList
7575
}
7676

7777
It "Call Get-TimeZone using ID param and single item" {

test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-StringData.Tests.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ bazz = 2
6565
It "Should work for multiple lines" {
6666
{ ConvertFrom-StringData -StringData $sampleData } | Should Not Throw
6767

68-
$(ConvertFrom-StringData -StringData $sampleData).Keys | Should Be "foo", "bar", "bazz"
68+
# keys are not order guaranteed
69+
$(ConvertFrom-StringData -StringData $sampleData).Keys | Should BeIn @("foo", "bar", "bazz")
6970

70-
$(ConvertFrom-StringData -StringData $sampleData).Values | Should Be "0","1","2"
71+
$(ConvertFrom-StringData -StringData $sampleData).Values | Should BeIn @("0","1","2")
7172
}
7273
}

0 commit comments

Comments
 (0)