Skip to content

Commit

Permalink
(maint) Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Windos committed Jan 13, 2024
1 parent 19716a3 commit 976da79
Show file tree
Hide file tree
Showing 23 changed files with 292 additions and 107 deletions.
77 changes: 20 additions & 57 deletions Tasks/build.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
[CmdletBinding()]
param(
[switch]
$Bootstrap,

[switch]
$Compile,

[switch]
$Test,

[switch]
$CodeCoverage
$Test
)

# Write information
Expand All @@ -30,28 +24,6 @@ Write-Host ''
$RootDir = Join-Path $PSScriptRoot '..'
$OutputDir = Join-Path $RootDir 'Output'

# Bootstrap step
if ($Bootstrap.IsPresent) {
Write-Information "Validate and install missing prerequisits for building ..."

# For testing Pester
if (-not (Get-Module -Name Pester -ListAvailable) -or (Get-Module -Name Pester -ListAvailable)[0].Version -eq [Version]'3.4.0') {
Write-Warning "Module 'Pester' is missing. Installing 'Pester' ..."
Install-Module -Name Pester -Scope CurrentUser -Force -RequiredVersion 4.10.1 -SkipPublisherCheck
}

if ((Get-Module -Name Pester -ListAvailable)[0].Version -ne [Version]'4.10.1') {
Install-Module -Name Pester -Scope CurrentUser -Force -RequiredVersion 4.10.1
}

Import-Module -Name Pester -RequiredVersion 4.10.1

if (-not (Get-Module -Name PSCodeCovIo -ListAvailable)) {
Write-Warning "Module 'PSCodeCovIo' is missing. Installing 'PSCodeCovIo' ..."
Install-Module -Name PSCodeCovIo -Scope CurrentUser -Force
}
}

# Compile step
if ($Compile.IsPresent) {
$CompileDir = Join-Path $OutputDir 'BurntToast'
Expand All @@ -65,17 +37,17 @@ if ($Compile.IsPresent) {
$null = New-Item -Path $CompileDir -ItemType Directory

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

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

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

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

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

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

# Test step
if ($Test.IsPresent) {
if (-not (Get-Module -Name Pester -ListAvailable)) {
throw "Cannot find the 'Pester' module. Please specify '-Bootstrap' to install build dependencies."
}

if (-not (Get-Module -Name PSCodeCovIo -ListAvailable)) {
throw "Cannot find the 'PSCodeCovIo' module. Please specify '-Bootstrap' to install build dependencies."
}
Write-Host "Running Pester within CI with code coverage"

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

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

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

Invoke-WebRequest -Uri 'https://codecov.io/bash' -OutFile codecov.sh
if ($null -eq $ENV:BURNTTOAST_MODULE_ROOT) {
$PesterConfig.CodeCoverage.Enabled = $true
$PesterConfig.CodeCoverage.OutputFormat = 'JaCoCo'
$PesterConfig.CodeCoverage.OutputPath = 'CoverageResults.xml'
$PesterConfig.CodeCoverage.Path = './src'
$PesterConfig.CodeCoverage.RecursePaths = $true
}

$res = Invoke-Pester -Configuration $PesterConfig
if ($res.FailedCount -gt 0) { throw "$($res.FailedCount) tests failed." }
}
49 changes: 47 additions & 2 deletions Tests/BurntToast.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,58 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'BurntToast Module' {
Context 'meta validation' {
It 'should import functions' {
(Get-Module BurntToast).ExportedFunctions.Count | Should -Be 25
(Get-Module BurntToast).ExportedFunctions.Count | Should -Be 21
}

It 'should import aliases' {
(Get-Module BurntToast).ExportedAliases.Count | Should -Be 1
}
}

Context 'Importing on Unsupported Operating Systems' {
It 'throws when importing on operating systems older than Windows 10' {
$env:BurntToastPesterNotWindows10 = $true

if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
{Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force} | Should -Throw -ExpectedMessage 'This version of BurntToast will only work on Windows 10*'
} else {
{Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force} | Should -Throw -ExpectedMessage 'This version of BurntToast will only work on Windows 10*'
}
}

It 'throws when importing on Windows 10 builds older than the Anniversary Update' {
$env:BurntToastPesterNotAnniversaryUpdate = $true

if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
{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*'
} else {
{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*'
}
}

AfterEach {
$env:BurntToastPesterNotWindows10 = $null
$env:BurntToastPesterNotAnniversaryUpdate = $null
}
}
}
36 changes: 22 additions & 14 deletions Tests/Get-BTHeader.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')

# Helper function needs to be in the global scope so it can be used in mocks
# Note - This may not work in Pester 5+
Function Global:New-MockNotification(
$HeaderId = (New-GUID).ToString(),
$HeaderTitle = (New-GUID).ToString(),
$HeaderArguments = '',
$HeaderActivation = 'protocol'
) {
$Content = @"
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}

Function Global:New-MockNotification(
$HeaderId = (New-GUID).ToString(),
$HeaderTitle = (New-GUID).ToString(),
$HeaderArguments = '',
$HeaderActivation = 'protocol'
) {
$Content = @"
<?xml version="1.0" encoding="UTF-8"?>
<toast>
<visual>
Expand All @@ -20,9 +27,10 @@ Function Global:New-MockNotification(
<header id="$HeaderId" title="$HeaderTitle" arguments="$HeaderArguments" activationType="$HeaderActivation" />
</toast>
"@
$XMLContent = [Windows.Data.Xml.Dom.XmlDocument]::new()
$XmlContent.LoadXml($Content)
[Windows.UI.Notifications.ToastNotification]::new($XMLContent)
$XMLContent = [Windows.Data.Xml.Dom.XmlDocument]::new()
$XmlContent.LoadXml($Content)
[Windows.UI.Notifications.ToastNotification]::new($XMLContent)
}
}

Describe 'Get-BTHeader' {
Expand Down
12 changes: 11 additions & 1 deletion Tests/Get-BTHistory.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'Get-BTHistory' {
if ($PlatformAvailable) {
Expand Down
12 changes: 11 additions & 1 deletion Tests/New-BTAction.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'New-BTAction' {
Context 'running without arguments' {
Expand Down
12 changes: 11 additions & 1 deletion Tests/New-BTAppId.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'New-BTAppId' {
Mock New-Item {} -ModuleName BurntToast
Expand Down
12 changes: 11 additions & 1 deletion Tests/New-BTAudio.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'New-BTAudio' {
Context 'built in audio source' {
Expand Down
12 changes: 11 additions & 1 deletion Tests/New-BTBinding.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'New-BTBinding' {
Context 'loaded with children' {
Expand Down
12 changes: 11 additions & 1 deletion Tests/New-BTButton.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'New-BTButton' {
Context 'system defined dismiss button' {
Expand Down
12 changes: 11 additions & 1 deletion Tests/New-BTColumn.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'New-BTColumn' {
Context 'via New-BurntToastNotification' {
Expand Down
12 changes: 11 additions & 1 deletion Tests/New-BTContent.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'New-BTContent' {
$ModuleRoot = (Get-Item (Get-Module 'BurntToast').Path).Directory.FullName
Expand Down
12 changes: 11 additions & 1 deletion Tests/New-BTContextMenuItem.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'New-BTContextMenuItem' {
Context 'loaded with standard arguments' {
Expand Down
12 changes: 11 additions & 1 deletion Tests/New-BTHeader.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
. (Join-Path -Path $PSScriptRoot -ChildPath '_envPrep.ps1')
BeforeAll {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

if ($ENV:BURNTTOAST_MODULE_ROOT) {
Import-Module $ENV:BURNTTOAST_MODULE_ROOT -Force
} else {
Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
}

Describe 'New-BTHeader' {
Context 'loaded with standard arguments' {
Expand Down
Loading

0 comments on commit 976da79

Please sign in to comment.