Skip to content

Commit a043a32

Browse files
committed
(build) Update tests
.
1 parent 19716a3 commit a043a32

23 files changed

+854
-525
lines changed

Tasks/build.ps1

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
[CmdletBinding()]
22
param(
3-
[switch]
4-
$Bootstrap,
5-
63
[switch]
74
$Compile,
85

96
[switch]
10-
$Test,
11-
12-
[switch]
13-
$CodeCoverage
7+
$Test
148
)
159

1610
# Write information
@@ -30,28 +24,6 @@ Write-Host ''
3024
$RootDir = Join-Path $PSScriptRoot '..'
3125
$OutputDir = Join-Path $RootDir 'Output'
3226

33-
# Bootstrap step
34-
if ($Bootstrap.IsPresent) {
35-
Write-Information "Validate and install missing prerequisits for building ..."
36-
37-
# For testing Pester
38-
if (-not (Get-Module -Name Pester -ListAvailable) -or (Get-Module -Name Pester -ListAvailable)[0].Version -eq [Version]'3.4.0') {
39-
Write-Warning "Module 'Pester' is missing. Installing 'Pester' ..."
40-
Install-Module -Name Pester -Scope CurrentUser -Force -RequiredVersion 4.10.1 -SkipPublisherCheck
41-
}
42-
43-
if ((Get-Module -Name Pester -ListAvailable)[0].Version -ne [Version]'4.10.1') {
44-
Install-Module -Name Pester -Scope CurrentUser -Force -RequiredVersion 4.10.1
45-
}
46-
47-
Import-Module -Name Pester -RequiredVersion 4.10.1
48-
49-
if (-not (Get-Module -Name PSCodeCovIo -ListAvailable)) {
50-
Write-Warning "Module 'PSCodeCovIo' is missing. Installing 'PSCodeCovIo' ..."
51-
Install-Module -Name PSCodeCovIo -Scope CurrentUser -Force
52-
}
53-
}
54-
5527
# Compile step
5628
if ($Compile.IsPresent) {
5729
$CompileDir = Join-Path $OutputDir 'BurntToast'
@@ -65,17 +37,17 @@ if ($Compile.IsPresent) {
6537
$null = New-Item -Path $CompileDir -ItemType Directory
6638

6739
Write-Host "Copying support files ..."
68-
Copy-Item -Path "$RootDir/BurntToast/*" -Filter '*.*' -Exclude '*.ps1', '*.psm1' -Recurse -Destination $CompileDir -Force
40+
Copy-Item -Path "$RootDir/src/*" -Filter '*.*' -Exclude '*.ps1', '*.psm1' -Recurse -Destination $CompileDir -Force
6941
Remove-Item -Path "$CompileDir/Private", "$CompileDir/Public" -Recurse -Force
7042

7143
# Copy Module README file
7244
Copy-Item -Path "$RootDir/README.md" -Destination $CompileDir -Force
7345

7446
Write-Host "Adding Private Functions ..."
75-
Get-ChildItem -Path "$RootDir\BurntToast\Private\*.ps1" -Recurse | Get-Content | Add-Content "$CompileDir/BurntToast.psm1"
47+
Get-ChildItem -Path "$RootDir/src/Private/*.ps1" -Recurse | Get-Content | Add-Content "$CompileDir/BurntToast.psm1"
7648

7749
Write-Host "Adding Public Functions ..."
78-
$Public = @( Get-ChildItem -Path "$RootDir/BurntToast/Public/*.ps1" -ErrorAction SilentlyContinue )
50+
$Public = @( Get-ChildItem -Path "$RootDir/src/Public/*.ps1" -ErrorAction SilentlyContinue )
7951

8052
$Public | Get-Content | Add-Content "$CompileDir/BurntToast.psm1"
8153

@@ -102,34 +74,25 @@ if ($Compile.IsPresent) {
10274

10375
# Test step
10476
if ($Test.IsPresent) {
105-
if (-not (Get-Module -Name Pester -ListAvailable)) {
106-
throw "Cannot find the 'Pester' module. Please specify '-Bootstrap' to install build dependencies."
107-
}
108-
109-
if (-not (Get-Module -Name PSCodeCovIo -ListAvailable)) {
110-
throw "Cannot find the 'PSCodeCovIo' module. Please specify '-Bootstrap' to install build dependencies."
111-
}
77+
Write-Host "Running Pester within CI with code coverage"
11278

113-
if ($ENV:BURNTTOAST_MODULE_ROOT) {
114-
$ModuleDir = (Get-Item -Path $ENV:BURNTTOAST_MODULE_ROOT).Directory.FullName
115-
Write-Host "Using Module Root directory of $ModuleDir"
116-
$RelevantFiles = (Get-ChildItem $ModuleDir -Recurse -Include "*.psm1", "*.ps1").FullName
117-
} else {
118-
$RelevantFiles = (Get-ChildItem ./BurntToast -Recurse -Include "*.psm1", "*.ps1").FullName
119-
}
79+
$PesterConfig = New-PesterConfiguration
12080

121-
if ($env:CI) {
122-
Write-Host "Running Pester within CI with code coverage for $($RelevantFiles.Count) file/s"
123-
$res = Invoke-Pester "./Tests" -OutputFormat JUnitXml -OutputFile TestResults.xml -CodeCoverage $RelevantFiles -CodeCoverageOutputFileFormat 'JaCoCo' -CodeCoverageOutputFile CoverageResults.xml -PassThru
124-
if ($res.FailedCount -gt 0) { throw "$($res.FailedCount) tests failed." }
125-
}
126-
else {
127-
$res = Invoke-Pester "./Tests" -CodeCoverage $RelevantFiles -PassThru
128-
}
81+
$PesterConfig.Run.Path = './tests'
82+
$PesterConfig.Run.PassThru = $true
12983

130-
if ($CodeCoverage.IsPresent) {
131-
Export-CodeCovIoJson -CodeCoverage $res.CodeCoverage -RepoRoot $pwd -Path coverage.json
84+
$PesterConfig.TestResult.Enabled = $true
85+
$PesterConfig.TestResult.OutputFormat = 'JUnitXml'
86+
$PesterConfig.TestResult.OutputPath = 'TestResults.xml'
13287

133-
Invoke-WebRequest -Uri 'https://codecov.io/bash' -OutFile codecov.sh
88+
if ($null -eq $ENV:BURNTTOAST_MODULE_ROOT) {
89+
$PesterConfig.CodeCoverage.Enabled = $true
90+
$PesterConfig.CodeCoverage.OutputFormat = 'JaCoCo'
91+
$PesterConfig.CodeCoverage.OutputPath = 'CoverageResults.xml'
92+
$PesterConfig.CodeCoverage.Path = './src'
93+
$PesterConfig.CodeCoverage.RecursePaths = $true
13494
}
95+
96+
$res = Invoke-Pester -Configuration $PesterConfig
97+
if ($res.FailedCount -gt 0) { throw "$($res.FailedCount) tests failed." }
13598
}

Tests/BurntToast.Tests.ps1

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,58 @@
1-
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
1+
BeforeAll {
2+
if (Get-Module -Name 'BurntToast') {
3+
Remove-Module -Name 'BurntToast'
4+
}
5+
6+
if ($ENV:BURNTTOAST_MODULE_ROOT) {
7+
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
8+
} else {
9+
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
10+
}
11+
}
212

313
Describe 'BurntToast Module' {
414
Context 'meta validation' {
515
It 'should import functions' {
6-
(Get-Module BurntToast).ExportedFunctions.Count | Should -Be 25
16+
(Get-Module BurntToast).ExportedFunctions.Count | Should -Be 20
717
}
818

919
It 'should import aliases' {
1020
(Get-Module BurntToast).ExportedAliases.Count | Should -Be 1
1121
}
1222
}
23+
24+
Context 'Importing on Unsupported Operating Systems' {
25+
It 'throws when importing on operating systems older than Windows 10' {
26+
$env:BurntToastPesterNotWindows10 = $true
27+
28+
if (Get-Module -Name 'BurntToast') {
29+
Remove-Module -Name 'BurntToast'
30+
}
31+
32+
if ($ENV:BURNTTOAST_MODULE_ROOT) {
33+
{Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force} | Should -Throw -ExpectedMessage 'This version of BurntToast will only work on Windows 10*'
34+
} else {
35+
{Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force} | Should -Throw -ExpectedMessage 'This version of BurntToast will only work on Windows 10*'
36+
}
37+
}
38+
39+
It 'throws when importing on Windows 10 builds older than the Anniversary Update' {
40+
$env:BurntToastPesterNotAnniversaryUpdate = $true
41+
42+
if (Get-Module -Name 'BurntToast') {
43+
Remove-Module -Name 'BurntToast'
44+
}
45+
46+
if ($ENV:BURNTTOAST_MODULE_ROOT) {
47+
{Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force} | Should -Throw -ExpectedMessage 'This version of BurntToast will only work on Windows 10 Creators Update (15063) and above*'
48+
} else {
49+
{Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force} | Should -Throw -ExpectedMessage 'This version of BurntToast will only work on Windows 10 Creators Update (15063) and above*'
50+
}
51+
}
52+
53+
AfterEach {
54+
$env:BurntToastPesterNotWindows10 = $null
55+
$env:BurntToastPesterNotAnniversaryUpdate = $null
56+
}
57+
}
1358
}

Tests/Get-BTHeader.Tests.ps1

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
2-
3-
# Helper function needs to be in the global scope so it can be used in mocks
4-
# Note - This may not work in Pester 5+
5-
Function Global:New-MockNotification(
6-
$HeaderId = (New-GUID).ToString(),
7-
$HeaderTitle = (New-GUID).ToString(),
8-
$HeaderArguments = '',
9-
$HeaderActivation = 'protocol'
10-
) {
11-
$Content = @"
1+
BeforeAll {
2+
if (Get-Module -Name 'BurntToast') {
3+
Remove-Module -Name 'BurntToast'
4+
}
5+
6+
if ($ENV:BURNTTOAST_MODULE_ROOT) {
7+
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
8+
} else {
9+
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
10+
}
11+
12+
Function Global:New-MockNotification(
13+
$HeaderId = (New-GUID).ToString(),
14+
$HeaderTitle = (New-GUID).ToString(),
15+
$HeaderArguments = '',
16+
$HeaderActivation = 'protocol'
17+
) {
18+
$Content = @"
1219
<?xml version="1.0" encoding="UTF-8"?>
1320
<toast>
1421
<visual>
@@ -20,9 +27,10 @@ Function Global:New-MockNotification(
2027
<header id="$HeaderId" title="$HeaderTitle" arguments="$HeaderArguments" activationType="$HeaderActivation" />
2128
</toast>
2229
"@
23-
$XMLContent = [Windows.Data.Xml.Dom.XmlDocument]::new()
24-
$XmlContent.LoadXml($Content)
25-
[Windows.UI.Notifications.ToastNotification]::new($XMLContent)
30+
$XMLContent = [Windows.Data.Xml.Dom.XmlDocument]::new()
31+
$XmlContent.LoadXml($Content)
32+
[Windows.UI.Notifications.ToastNotification]::new($XMLContent)
33+
}
2634
}
2735

2836
Describe 'Get-BTHeader' {
@@ -36,14 +44,16 @@ Describe 'Get-BTHeader' {
3644
}
3745

3846
Context 'With multiple duplicate notifications' {
39-
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { $ScheduledToast } {
40-
New-MockNotification -HeaderId 'ID01'
41-
New-MockNotification -HeaderId 'ID01'
42-
}
43-
44-
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { -not $ScheduledToast } {
45-
New-MockNotification -HeaderId 'ID01'
46-
New-MockNotification -HeaderId 'ID01'
47+
BeforeAll {
48+
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { $ScheduledToast } {
49+
New-MockNotification -HeaderId 'ID01'
50+
New-MockNotification -HeaderId 'ID01'
51+
}
52+
53+
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { -not $ScheduledToast } {
54+
New-MockNotification -HeaderId 'ID01'
55+
New-MockNotification -HeaderId 'ID01'
56+
}
4757
}
4858

4959
It 'should ignore duplicate headers' {
@@ -55,14 +65,16 @@ Describe 'Get-BTHeader' {
5565
}
5666

5767
Context 'With multiple unique notifications' {
58-
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { $ScheduledToast } {
59-
New-MockNotification -HeaderId 'ID01' -HeaderTitle 'Title 01'
60-
New-MockNotification -HeaderId 'ID02' -HeaderTitle 'Title 02'
61-
}
62-
63-
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { -not $ScheduledToast } {
64-
New-MockNotification -HeaderId 'ID03' -HeaderTitle 'Title 03'
65-
New-MockNotification -HeaderId 'ID04'
68+
BeforeAll {
69+
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { $ScheduledToast } {
70+
New-MockNotification -HeaderId 'ID01' -HeaderTitle 'Title 01'
71+
New-MockNotification -HeaderId 'ID02' -HeaderTitle 'Title 02'
72+
}
73+
74+
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { -not $ScheduledToast } {
75+
New-MockNotification -HeaderId 'ID03' -HeaderTitle 'Title 03'
76+
New-MockNotification -HeaderId 'ID04'
77+
}
6678
}
6779

6880
It 'should return all headers' {
@@ -101,8 +113,10 @@ Describe 'Get-BTHeader' {
101113
}
102114

103115
Context 'With a single unique notifications' {
104-
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { $ScheduledToast } {
105-
New-MockNotification -HeaderId 'ID01' -HeaderTitle 'Title 01' -HeaderArguments 'arguments' -HeaderProtocol 'protocol'
116+
BeforeAll {
117+
Mock Get-BTHistory -ModuleName BurntToast -Verifiable -ParameterFilter { $ScheduledToast } {
118+
New-MockNotification -HeaderId 'ID01' -HeaderTitle 'Title 01' -HeaderArguments 'arguments' -HeaderProtocol 'protocol'
119+
}
106120
}
107121

108122
It 'should return all header properties' {

Tests/Get-BTHistory.Tests.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
1+
BeforeAll {
2+
if (Get-Module -Name 'BurntToast') {
3+
Remove-Module -Name 'BurntToast'
4+
}
5+
6+
if ($ENV:BURNTTOAST_MODULE_ROOT) {
7+
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
8+
} else {
9+
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
10+
}
11+
}
212

313
Describe 'Get-BTHistory' {
414
if ($PlatformAvailable) {

0 commit comments

Comments
 (0)