Skip to content

Commit ee5a5c9

Browse files
committed
Upgrade the Pester Tests to 5.x
1 parent b73651d commit ee5a5c9

14 files changed

+888
-786
lines changed

Build.build.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ param(
1616
# Add the clean task before the default build
1717
[switch]$Clean,
1818

19-
# Collect code coverage when tests are run
20-
[switch]$CollectCoverage,
19+
# A minimum code coverage percentage to accept as a double: 0.85
20+
[double]$RequiredCodeCoverage = 0.85,
2121

2222
# Which projects to build
2323
[Alias("Projects")]

Tests/Integration/Source1.Tests.ps1

Lines changed: 86 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
. $PSScriptRoot\..\Convert-FolderSeparator.ps1
33

44
Describe "When we call Build-Module" -Tag Integration {
5-
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
6-
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
5+
BeforeAll {
6+
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
7+
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
8+
$Metadata = Import-Metadata $Output.Path
9+
}
710

811
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
912
$Module | Should -Not -FileContentMatch '^Source$'
1013
}
1114

12-
$Metadata = Import-Metadata $Output.Path
1315

1416
It "Should update FunctionsToExport in the manifest" {
1517
$Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
@@ -29,14 +31,16 @@ Describe "When we call Build-Module" -Tag Integration {
2931
}
3032

3133
Describe "Regression test for #55: I can pass SourceDirectories" -Tag Integration, Regression {
32-
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -Passthru
33-
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
34+
BeforeAll {
35+
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -Passthru
36+
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
37+
$Metadata = Import-Metadata $Output.Path
38+
}
3439

3540
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
3641
$Module | Should -Not -FileContentMatch '^Source$'
3742
}
3843

39-
$Metadata = Import-Metadata $Output.Path
4044

4145
It "Should not have any FunctionsToExport if SourceDirectories don't match the PublicFilter" {
4246
$Metadata.FunctionsToExport | Should -Be @()
@@ -80,27 +84,30 @@ Describe "Regression test for #55: I can pass SourceDirectories and PublicFilter
8084
}
8185

8286
Describe "Regression test for #84: Multiple Aliases per command will Export" -Tag Integration, Regression {
83-
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
84-
85-
$Metadata = Import-Metadata $Output.Path
87+
BeforeAll {
88+
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
89+
$Metadata = Import-Metadata $Output.Path
90+
}
8691

8792
It "Should update AliasesToExport in the manifest" {
8893
$Metadata.AliasesToExport | Should -Be @("Get-MyAlias","GS","GSou", "SS", "SSou")
8994
}
9095
}
9196

9297
Describe "Supports building without a build.psd1" -Tag Integration {
93-
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
94-
# This is the old build, with a build.psd1
95-
$Output = Build-Module TestDrive:\Source1\build.psd1 -Passthru
96-
$ManifestContent = Get-Content $Output.Path
97-
$ModuleContent = Get-Content ([IO.Path]::ChangeExtension($Output.Path, ".psm1"))
98-
Remove-Item (Split-Path $Output.Path) -Recurse
98+
BeforeAll {
99+
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
100+
# This is the old build, with a build.psd1
101+
$Output = Build-Module TestDrive:\Source1\build.psd1 -Passthru
102+
$ManifestContent = Get-Content $Output.Path
103+
$ModuleContent = Get-Content ([IO.Path]::ChangeExtension($Output.Path, ".psm1"))
104+
Remove-Item (Split-Path $Output.Path) -Recurse
99105

100-
# Then remove the build.psd1 and rebuild it
101-
Remove-Item TestDrive:\Source1\build.psd1
106+
# Then remove the build.psd1 and rebuild it
107+
Remove-Item TestDrive:\Source1\build.psd1
102108

103-
$Build = @{ }
109+
$Build = @{ }
110+
}
104111

105112
It "No longer fails if there's no build.psd1" {
106113
$BuildParameters = @{
@@ -158,17 +165,19 @@ Describe "Supports building without a build.psd1" -Tag Integration {
158165
}
159166

160167
Describe "Defaults to VersionedOutputDirectory" -Tag Integration {
161-
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
162-
# This is the old build, with a build.psd1
163-
$Output = Build-Module TestDrive:\Source1\build.psd1 -Passthru
164-
$ManifestContent = Get-Content $Output.Path
165-
$ModuleContent = Get-Content ([IO.Path]::ChangeExtension($Output.Path, ".psm1"))
166-
Remove-Item (Split-Path $Output.Path) -Recurse
168+
BeforeAll {
169+
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
170+
# This is the old build, with a build.psd1
171+
$Output = Build-Module TestDrive:\Source1\build.psd1 -Passthru
172+
$ManifestContent = Get-Content $Output.Path
173+
$ModuleContent = Get-Content ([IO.Path]::ChangeExtension($Output.Path, ".psm1"))
174+
Remove-Item (Split-Path $Output.Path) -Recurse
167175

168-
# Then remove the build.psd1 and rebuild it
169-
Remove-Item TestDrive:\Source1\build.psd1
176+
# Then remove the build.psd1 and rebuild it
177+
Remove-Item TestDrive:\Source1\build.psd1
170178

171-
$Build = @{ }
179+
$Build = @{ }
180+
}
172181

173182
It "Builds into a folder with version by default" {
174183
$BuildParameters = @{
@@ -207,20 +216,22 @@ Describe "Defaults to VersionedOutputDirectory" -Tag Integration {
207216
}
208217

209218
Describe "Supports building discovering the module without a build.psd1" -Tag Integration {
210-
Copy-Item $PSScriptRoot\Source1 TestDrive:\source -Recurse
219+
BeforeAll {
220+
Copy-Item $PSScriptRoot\Source1 TestDrive:\source -Recurse
211221

212-
# This is the old build, with a build.psd1
213-
$Output = Build-Module TestDrive:\source\build.psd1 -Passthru
214-
$ManifestContent = Get-Content $Output.Path
215-
$ModuleContent = Get-Content ([IO.Path]::ChangeExtension($Output.Path, ".psm1"))
216-
Remove-Item (Split-Path $Output.Path) -Recurse
222+
# This is the old build, with a build.psd1
223+
$Output = Build-Module TestDrive:\source\build.psd1 -Passthru
224+
$ManifestContent = Get-Content $Output.Path
225+
$ModuleContent = Get-Content ([IO.Path]::ChangeExtension($Output.Path, ".psm1"))
226+
Remove-Item (Split-Path $Output.Path) -Recurse
217227

218-
# Then remove the build.psd1 and rebuild it
219-
Remove-Item TestDrive:\source\build.psd1
228+
# Then remove the build.psd1 and rebuild it
229+
Remove-Item TestDrive:\source\build.psd1
220230

221-
Push-Location -StackName 'IntegrationTest' -Path TestDrive:\
231+
Push-Location -StackName 'IntegrationTest' -Path TestDrive:\
222232

223-
$Build = @{ }
233+
$Build = @{ }
234+
}
224235

225236
It "No longer fails if there's no build.psd1" {
226237
$Build.Output = Build-Module -Passthru
@@ -240,13 +251,16 @@ Describe "Supports building discovering the module without a build.psd1" -Tag In
240251
$Build.Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
241252
}
242253

243-
Pop-Location -StackName 'IntegrationTest'
254+
AfterAll {
255+
Pop-Location -StackName 'IntegrationTest'
256+
}
244257
}
245258

246259
Describe "Regression test for #88 not copying prefix files" -Tag Integration, Regression {
247-
$Output = Build-Module $PSScriptRoot\build.psd1 -Passthru
248-
249-
$Metadata = Import-Metadata $Output.Path
260+
BeforeAll {
261+
$Output = Build-Module $PSScriptRoot\build.psd1 -Passthru
262+
$Metadata = Import-Metadata $Output.Path
263+
}
250264

251265
It "Should update AliasesToExport in the manifest" {
252266
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
@@ -256,20 +270,22 @@ Describe "Regression test for #88 not copying prefix files" -Tag Integration, Re
256270
}
257271

258272
Describe "Regression test for #40.2 not copying suffix if prefix" -Tag Integration, Regression {
259-
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
273+
BeforeAll {
274+
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
260275

261-
New-Item TestDrive:\Source1\_GlobalScope.ps1 -Value '$Global:Module = "Testing"'
276+
New-Item TestDrive:\Source1\_GlobalScope.ps1 -Value '$Global:Module = "Testing"'
262277

263-
$metadata = Import-Metadata TestDrive:\Source1\build.psd1
264-
$metadata += @{
265-
Prefix = "./_GlobalScope.ps1"
266-
Suffix = "./_GlobalScope.ps1"
267-
}
268-
$metadata | Export-Metadata TestDrive:\Source1\build.psd1
278+
$metadata = Import-Metadata TestDrive:\Source1\build.psd1
279+
$metadata += @{
280+
Prefix = "./_GlobalScope.ps1"
281+
Suffix = "./_GlobalScope.ps1"
282+
}
283+
$metadata | Export-Metadata TestDrive:\Source1\build.psd1
269284

270-
$Output = Build-Module TestDrive:\Source1 -Passthru
285+
$Output = Build-Module TestDrive:\Source1 -Passthru
271286

272-
$Metadata = Import-Metadata $Output.Path
287+
$Metadata = Import-Metadata $Output.Path
288+
}
273289

274290
It "Should inject the content of the _GlobalScope file at the TOP and BOTTOM" {
275291
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
@@ -288,10 +304,12 @@ Describe "Regression test for #40.2 not copying suffix if prefix" -Tag Integrati
288304
# There's no such thing as a drive root on unix
289305
if ($PSVersionTable.Platform -eq "Win32NT") {
290306
Describe "Able to build from the drive root" {
291-
$null = New-ModuleManifest "TestDrive:/MyModule.psd1" -ModuleVersion "1.0.0" -Author "Tester"
292-
$null = New-Item "TestDrive:/Public/Test.ps1" -Type File -Value 'MATCHING TEST CONTENT' -Force
307+
BeforeAll {
308+
$null = New-ModuleManifest "TestDrive:/MyModule.psd1" -ModuleVersion "1.0.0" -Author "Tester"
309+
$null = New-Item "TestDrive:/Public/Test.ps1" -Type File -Value 'MATCHING TEST CONTENT' -Force
293310

294-
$Result = Build-Module -SourcePath 'TestDrive:/MyModule.psd1' -Version "1.0.0" -OutputDirectory './output' -Encoding UTF8 -SourceDirectories @('Public') -Target Build -Passthru
311+
$Result = Build-Module -SourcePath 'TestDrive:/MyModule.psd1' -Version "1.0.0" -OutputDirectory './output' -Encoding UTF8 -SourceDirectories @('Public') -Target Build -Passthru
312+
}
295313

296314
It "Builds the Module in the designated output folder" {
297315
$Result.ModuleBase | Convert-FolderSeparator | Should -Be (Convert-FolderSeparator "TestDrive:/Output/MyModule/1.0.0")
@@ -301,20 +319,21 @@ if ($PSVersionTable.Platform -eq "Win32NT") {
301319
}
302320

303321
Describe "Copies additional items specified in CopyPaths" {
322+
BeforeAll {
323+
$null = New-Item "TestDrive:/build.psd1" -Type File -Force -Value "@{
324+
SourcePath = 'TestDrive:/MyModule.psd1'
325+
SourceDirectories = @('Public')
326+
OutputDirectory = './output'
327+
CopyPaths = './lib', './MyModule.format.ps1xml'
328+
}"
329+
$null = New-ModuleManifest "TestDrive:/MyModule.psd1" -ModuleVersion "1.0.0" -Author "Tester"
330+
$null = New-Item "TestDrive:/Public/Test.ps1" -Type File -Value 'MATCHING TEST CONTENT' -Force
331+
$null = New-Item "TestDrive:/MyModule.format.ps1xml" -Type File -Value '<Configuration />' -Force
332+
$null = New-Item "TestDrive:/lib/imaginary1.dll" -Type File -Value '1' -Force
333+
$null = New-Item "TestDrive:/lib/subdir/imaginary2.dll" -Type File -Value '2' -Force
304334

305-
$null = New-Item "TestDrive:/build.psd1" -Type File -Force -Value "@{
306-
SourcePath = 'TestDrive:/MyModule.psd1'
307-
SourceDirectories = @('Public')
308-
OutputDirectory = './output'
309-
CopyPaths = './lib', './MyModule.format.ps1xml'
310-
}"
311-
$null = New-ModuleManifest "TestDrive:/MyModule.psd1" -ModuleVersion "1.0.0" -Author "Tester"
312-
$null = New-Item "TestDrive:/Public/Test.ps1" -Type File -Value 'MATCHING TEST CONTENT' -Force
313-
$null = New-Item "TestDrive:/MyModule.format.ps1xml" -Type File -Value '<Configuration />' -Force
314-
$null = New-Item "TestDrive:/lib/imaginary1.dll" -Type File -Value '1' -Force
315-
$null = New-Item "TestDrive:/lib/subdir/imaginary2.dll" -Type File -Value '2' -Force
316-
317-
$Result = Build-Module -SourcePath 'TestDrive:/build.psd1' -OutputDirectory './output' -Version '1.0.0' -Passthru -Target Build
335+
$Result = Build-Module -SourcePath 'TestDrive:/build.psd1' -OutputDirectory './output' -Version '1.0.0' -Passthru -Target Build
336+
}
318337

319338
It "Copies single files that are in CopyPaths" {
320339
(Convert-FolderSeparator $Result.ModuleBase) | Should -Be (Convert-FolderSeparator "$TestDrive/output/MyModule/1.0.0")

Tests/Private/ConvertToAst.Tests.ps1

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
Describe "ConvertToAst" {
33

44
Context "It returns a ParseResult for file paths" {
5-
$ParseResult = InModuleScope ModuleBuilder {
6-
ConvertToAst $PSCommandPath
5+
BeforeAll {
6+
$ParseResult = InModuleScope ModuleBuilder {
7+
ConvertToAst $PSCommandPath
8+
}
79
}
810

911
It "Returns a ParseResult object" {
@@ -22,8 +24,10 @@ Describe "ConvertToAst" {
2224
}
2325

2426
Context "It parses piped in commands" {
25-
$ParseResult = InModuleScope ModuleBuilder {
26-
Get-Command ConvertToAst | ConvertToAst
27+
BeforeAll {
28+
$ParseResult = InModuleScope ModuleBuilder {
29+
Get-Command ConvertToAst | ConvertToAst
30+
}
2731
}
2832

2933
It "Returns a ParseResult object with the AST" {
@@ -33,8 +37,10 @@ Describe "ConvertToAst" {
3337
}
3438

3539
Context "It parses piped in modules" {
36-
$ParseResult = InModuleScope ModuleBuilder {
37-
Get-Module ModuleBuilder | ConvertToAst
40+
BeforeAll {
41+
$ParseResult = InModuleScope ModuleBuilder {
42+
Get-Module ModuleBuilder | ConvertToAst
43+
}
3844
}
3945

4046
It "Returns a ParseResult object with the AST" {

Tests/Private/CopyReadMe.Tests.ps1

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#requires -Module ModuleBuilder
22
Describe "Copy ReadMe" {
3-
. $PSScriptRoot\..\Convert-FolderSeparator.ps1
3+
BeforeAll {
4+
. $PSScriptRoot\..\Convert-FolderSeparator.ps1
5+
}
46

57
Context "There's no ReadMe" {
68
# It should not even call Test-Path
@@ -30,32 +32,36 @@ Describe "Copy ReadMe" {
3032
}
3133

3234
Context "There is a ReadMe" {
33-
# Nothing is actually created when this test runs
34-
Mock New-Item -ModuleName ModuleBuilder
35-
Mock Copy-Item -ModuleName ModuleBuilder
35+
BeforeAll {
36+
# Nothing is actually created when this test runs
37+
Mock New-Item -ModuleName ModuleBuilder
38+
Mock Copy-Item -ModuleName ModuleBuilder
3639

37-
# Test-Path returns true only for the source document
38-
${global:Test Script Path} = Join-Path $PSScriptRoot CopyReadMe.Tests.ps1
39-
Mock Test-Path { $Path -eq ${global:Test Script Path} } -ModuleName ModuleBuilder
40+
# Test-Path returns true only for the source document
41+
${global:Test Script Path} = Join-Path $PSScriptRoot CopyReadMe.Tests.ps1
42+
Mock Test-Path { $Path -eq ${global:Test Script Path} } -ModuleName ModuleBuilder
4043

41-
Remove-Item TestDrive:\En -Recurse -Force -ErrorAction SilentlyContinue
44+
Remove-Item TestDrive:\En -Recurse -Force -ErrorAction SilentlyContinue
4245

43-
InModuleScope ModuleBuilder {
44-
CopyReadMe -ReadMe ${global:Test Script Path} -Module ModuleBuilder -OutputDirectory TestDrive:\ -Culture "En"
46+
InModuleScope ModuleBuilder {
47+
CopyReadMe -ReadMe ${global:Test Script Path} -Module ModuleBuilder -OutputDirectory TestDrive:\ -Culture "En"
48+
}
4549
}
4650

4751
It "Creates a language path in the output" {
4852
Assert-MockCalled New-Item -ModuleName ModuleBuilder -ParameterFilter {
4953
(Convert-FolderSeparator "$Path") -eq (Convert-FolderSeparator "TestDrive:\En")
50-
}
54+
} -Scope Context
5155
}
5256

5357
It "Copies the readme as about_module.help.txt" {
5458
Assert-MockCalled Copy-Item -ModuleName ModuleBuilder -ParameterFilter {
5559
(Convert-FolderSeparator $Destination) -eq (Convert-FolderSeparator "TestDrive:\En\about_ModuleBuilder.help.txt")
56-
}
60+
} -Scope Context
5761
}
5862

59-
Remove-Item TestDrive:\En -Recurse -Force -ErrorAction SilentlyContinue
63+
AfterAll {
64+
Remove-Item TestDrive:\En -Recurse -Force -ErrorAction SilentlyContinue
65+
}
6066
}
6167
}

0 commit comments

Comments
 (0)