Skip to content

Commit feb6793

Browse files
committed
Update tests for Pester 5.x
1 parent 31afabe commit feb6793

23 files changed

Lines changed: 619 additions & 498 deletions

Build.build.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ param(
1919
# Collect code coverage when tests are run
2020
[switch]$CollectCoverage,
2121

22+
# The PesterFilter from New-PesterConfiguration.
23+
# Supports specifying any of:
24+
# Tag: Tags of Describe, Context or It to be run.
25+
# ExcludeTag: Tags of Describe, Context or It to be excluded from the run.
26+
# Line: Filter by file and scriptblock start line, useful to run parsed tests programmatically to avoid problems with expanded names. Example: 'C:\tests\file1.Tests.ps1:37'
27+
# ExcludeLine: Exclude by file and scriptblock start line, takes precedence over Line.
28+
# FullName: Full name of test with -like wildcards, joined by dot. Example: '*.describe Get-Item.test1'
29+
[hashtable]$PesterFilter,
30+
2231
# Which projects to build
2332
[Alias("Projects")]
2433
$dotnetProjects = @(),
@@ -39,6 +48,7 @@ $ErrorView = 'DetailedView'
3948

4049
# The name of the module to publish
4150
$script:PSModuleName = "TerminalBlocks"
51+
$script:RequiredCodeCoverage = 0.85
4252
# Use Env because Earthly can override it
4353
$Env:OUTPUT_ROOT ??= Join-Path $BuildRoot Modules
4454

RequiredModules.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@{
33
Configuration = "[1.5.0, 2.0)"
44
Metadata = "[1.5.1, 2.0)"
5-
Pester = "[4.10.1,5.0)"
5+
Pester = "[5.6.1, 6.0)"
66
ModuleBuilder = "[3.0.0, 4.0)"
77
PSScriptAnalyzer = "[1.21.0,2.0)"
88
PowerShellGet = "2.0.4"

Source/Private/CopyReadMe.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
function CopyReadMe {
2+
<#
3+
.SYNOPSIS
4+
Copy the readme file as an about_ help file
5+
#>
26
[CmdletBinding()]
37
param(
48
# The path to the ReadMe document to copy
@@ -22,19 +26,18 @@ function CopyReadMe {
2226
[Switch]$Force
2327
)
2428
process {
25-
# Copy the readme file as an about_ help file
26-
Write-Verbose "Test for ReadMe: $Pwd/$($ReadMe)"
29+
Write-Verbose "Test for ReadMe: $ReadMe"
2730
if ($ReadMe -and (Test-Path $ReadMe -PathType Leaf)) {
2831
# Make sure there's a language path
2932
$LanguagePath = Join-Path $OutputDirectory $Culture
3033
if (!(Test-Path $LanguagePath -PathType Container)) {
34+
Write-Verbose "Create language path: $LanguagePath"
3135
$null = New-Item $LanguagePath -Type Directory -Force
3236
}
33-
Write-Verbose "Copy ReadMe to: $LanguagePath"
3437

3538
$about_module = Join-Path $LanguagePath "about_$($ModuleName).help.txt"
3639
if (!(Test-Path $about_module)) {
37-
Write-Verbose "Turn readme into about_module"
40+
Write-Verbose "Copy $ReadMe to: $about_module"
3841
Copy-Item -LiteralPath $ReadMe -Destination $about_module -Force:$Force
3942
}
4043
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
#requires -Module ModuleBuilder
21
. $PSScriptRoot\..\Convert-FolderSeparator.ps1
32

43
Describe "Parameters.Set in build manifest" -Tag Integration {
54
BeforeAll {
5+
# This file should not be deleted by the build, because VersionedOutput defaults true now
6+
# So the actual Build output will be ../Result3/Parameters/3.0.0
67
New-Item $PSScriptRoot\Result3\Parameters\ReadMe.md -ItemType File -Force
8+
79
$Output = Build-Module $PSScriptRoot\Parameters\build.psd1
810
if ($Output) {
911
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
1012
$Metadata = Import-Metadata $Output.Path
1113
}
1214
}
1315

14-
It "Passthru works" {
16+
It "Passthru can be set from build.psd1" {
1517
$Output | Should -Not -BeNullOrEmpty
1618
}
1719

18-
It "The Target is Build" {
19-
"$PSScriptRoot\Result3\Parameters\ReadMe.md" | Should -Exist
20+
It "The version can be set from build.psd1" {
21+
$Metadata.ModuleVersion | Should -Be "3.0.0"
2022
}
2123

22-
It "The version is set" {
23-
$Metadata.ModuleVersion | Should -Be "3.0.0"
24+
It "Files outside the Output should not be cleaned up or overwritten" {
25+
"$PSScriptRoot\Result3\Parameters\ReadMe.md" | Should -Exist
2426
}
2527

26-
It "The PreRelease is set" {
28+
It "The PreRelease is set properly even when set from build.psd1" {
2729
$Metadata.PrivateData.PSData.Prerelease | Should -Be 'alpha001'
2830
}
2931
}

Tests/Integration/Source1.Tests.ps1

Lines changed: 89 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
#requires -Module ModuleBuilder
21
. $PSScriptRoot\..\Convert-FolderSeparator.ps1
2+
$ProgressPreference = "SilentlyContinue"
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+
$Metadata = Import-Metadata $Output.Path
8+
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
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
13-
1415
It "Should update FunctionsToExport in the manifest" {
1516
$Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
1617
}
@@ -29,15 +30,16 @@ Describe "When we call Build-Module" -Tag Integration {
2930
}
3031

3132
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")
33+
BeforeAll {
34+
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -Passthru
35+
$Metadata = Import-Metadata $Output.Path
36+
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
37+
}
3438

3539
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
3640
$Module | Should -Not -FileContentMatch '^Source$'
3741
}
3842

39-
$Metadata = Import-Metadata $Output.Path
40-
4143
It "Should not have any FunctionsToExport if SourceDirectories don't match the PublicFilter" {
4244
$Metadata.FunctionsToExport | Should -Be @()
4345
}
@@ -54,8 +56,8 @@ Describe "Regression test for #55: I can pass SourceDirectories" -Tag Integratio
5456
Describe "Regression test for #55: I can pass SourceDirectories and PublicFilter" -Tag Integration, Regression {
5557
BeforeAll {
5658
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -PublicFilter "Pub*\*" -Passthru
57-
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
5859
$Metadata = Import-Metadata $Output.Path
60+
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
5961
}
6062

6163
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
@@ -80,27 +82,30 @@ Describe "Regression test for #55: I can pass SourceDirectories and PublicFilter
8082
}
8183

8284
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
85+
BeforeAll {
86+
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
87+
$Metadata = Import-Metadata $Output.Path
88+
}
8689

8790
It "Should update AliasesToExport in the manifest" {
8891
$Metadata.AliasesToExport | Should -Be @("Get-MyAlias","GS","GSou", "SS", "SSou")
8992
}
9093
}
9194

9295
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
96+
BeforeAll {
97+
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
98+
# This is the old build, with a build.psd1
99+
$Output = Build-Module TestDrive:\Source1\build.psd1 -Passthru
100+
$ManifestContent = Get-Content $Output.Path
101+
$ModuleContent = Get-Content ([IO.Path]::ChangeExtension($Output.Path, ".psm1"))
102+
Remove-Item (Split-Path $Output.Path) -Recurse
99103

100-
# Then remove the build.psd1 and rebuild it
101-
Remove-Item TestDrive:\Source1\build.psd1
104+
# Then remove the build.psd1 and rebuild it
105+
Remove-Item TestDrive:\Source1\build.psd1
102106

103-
$Build = @{ }
107+
$Build = @{ }
108+
}
104109

105110
It "No longer fails if there's no build.psd1" {
106111
$BuildParameters = @{
@@ -158,17 +163,19 @@ Describe "Supports building without a build.psd1" -Tag Integration {
158163
}
159164

160165
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
166+
BeforeAll {
167+
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
168+
# This is the old build, with a build.psd1
169+
$Output = Build-Module TestDrive:\Source1\build.psd1 -Passthru
170+
$ManifestContent = Get-Content $Output.Path
171+
$ModuleContent = Get-Content ([IO.Path]::ChangeExtension($Output.Path, ".psm1"))
172+
Remove-Item (Split-Path $Output.Path) -Recurse
167173

168-
# Then remove the build.psd1 and rebuild it
169-
Remove-Item TestDrive:\Source1\build.psd1
174+
# Then remove the build.psd1 and rebuild it
175+
Remove-Item TestDrive:\Source1\build.psd1
170176

171-
$Build = @{ }
177+
$Build = @{ }
178+
}
172179

173180
It "Builds into a folder with version by default" {
174181
$BuildParameters = @{
@@ -207,20 +214,22 @@ Describe "Defaults to VersionedOutputDirectory" -Tag Integration {
207214
}
208215

209216
Describe "Supports building discovering the module without a build.psd1" -Tag Integration {
210-
Copy-Item $PSScriptRoot\Source1 TestDrive:\source -Recurse
217+
BeforeAll {
218+
Copy-Item $PSScriptRoot\Source1 TestDrive:\source -Recurse
211219

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
220+
# This is the old build, with a build.psd1
221+
$Output = Build-Module TestDrive:\source\build.psd1 -Passthru
222+
$ManifestContent = Get-Content $Output.Path
223+
$ModuleContent = Get-Content ([IO.Path]::ChangeExtension($Output.Path, ".psm1"))
224+
Remove-Item (Split-Path $Output.Path) -Recurse
217225

218-
# Then remove the build.psd1 and rebuild it
219-
Remove-Item TestDrive:\source\build.psd1
226+
# Then remove the build.psd1 and rebuild it
227+
Remove-Item TestDrive:\source\build.psd1
220228

221-
Push-Location -StackName 'IntegrationTest' -Path TestDrive:\
229+
Push-Location -StackName 'IntegrationTest' -Path TestDrive:\
222230

223-
$Build = @{ }
231+
$Build = @{ }
232+
}
224233

225234
It "No longer fails if there's no build.psd1" {
226235
$Build.Output = Build-Module -Passthru
@@ -239,38 +248,41 @@ Describe "Supports building discovering the module without a build.psd1" -Tag In
239248
It "Should update FunctionsToExport in the manifest" {
240249
$Build.Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
241250
}
242-
243-
Pop-Location -StackName 'IntegrationTest'
251+
AfterAll {
252+
Pop-Location -StackName 'IntegrationTest'
253+
}
244254
}
245255

246256
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
257+
BeforeAll {
258+
$Output = Build-Module $PSScriptRoot\build.psd1 -Passthru
259+
$Metadata = Import-Metadata $Output.Path
260+
}
250261

251-
It "Should update AliasesToExport in the manifest" {
262+
It "Should update the top of the module with that prefix" {
252263
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
253264
$ModuleInfo = Get-Content $Module
254265
$ModuleInfo[0] | Should -be "using module Configuration"
255266
}
256267
}
257268

258269
Describe "Regression test for #40.2 not copying suffix if prefix" -Tag Integration, Regression {
259-
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
260-
261-
New-Item TestDrive:\Source1\_GlobalScope.ps1 -Value '$Global:Module = "Testing"'
270+
BeforeAll {
271+
Copy-Item $PSScriptRoot\Source1 TestDrive:\Source1 -Recurse
262272

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
273+
New-Item TestDrive:\Source1\_GlobalScope.ps1 -Value '$Global:Module = "Testing"'
269274

270-
$Output = Build-Module TestDrive:\Source1 -Passthru
275+
$metadata = Import-Metadata TestDrive:\Source1\build.psd1
276+
$metadata += @{
277+
Prefix = "./_GlobalScope.ps1"
278+
Suffix = "./_GlobalScope.ps1"
279+
}
280+
$metadata | Export-Metadata TestDrive:\Source1\build.psd1
271281

272-
$Metadata = Import-Metadata $Output.Path
282+
$Output = Build-Module TestDrive:\Source1 -Passthru
273283

284+
$Metadata = Import-Metadata $Output.Path
285+
}
274286
It "Should inject the content of the _GlobalScope file at the TOP and BOTTOM" {
275287
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
276288
$Code = Get-Content $Module
@@ -288,10 +300,12 @@ Describe "Regression test for #40.2 not copying suffix if prefix" -Tag Integrati
288300
# There's no such thing as a drive root on unix
289301
if ($PSVersionTable.Platform -eq "Win32NT") {
290302
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
303+
BeforeAll {
304+
$null = New-ModuleManifest "TestDrive:/MyModule.psd1" -ModuleVersion "1.0.0" -Author "Tester"
305+
$null = New-Item "TestDrive:/Public/Test.ps1" -Type File -Value 'MATCHING TEST CONTENT' -Force
293306

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

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

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

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
331+
$Result = Build-Module -SourcePath 'TestDrive:/build.psd1' -OutputDirectory './output' -Version '1.0.0' -Passthru -Target Build
332+
}
318333

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

0 commit comments

Comments
 (0)