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 12, 2024
1 parent f6c49e9 commit ad3e8b6
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 68 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." }
}
58 changes: 58 additions & 0 deletions Tests/_BurntToast.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Describe 'BurntToast Module' {
Context 'Importing on Supported Operating System' {
It 'doesn''t throw' {
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast'
}

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

It 'should import functions' {
(Get-Module BurntToast).ExportedFunctions.Count | Should -Be 6
}

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
}
}
}
14 changes: 3 additions & 11 deletions Tests/_envPrep.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
if (Get-Module -Name 'BurntToast') {
Remove-Module -Name 'BurntToast' -Force
}

try {
[Windows.UI.Notifications.ToastNotificationManager]::History.Clear('{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe')
$PlatformAvailable = $true
} catch {
$PlatformAvailable = $false
Remove-Module -Name 'BurntToast'
}

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

Import-Module "$PSScriptRoot/../src/BurntToast.psd1" -Force
}
4 changes: 4 additions & 0 deletions Tests/_pester.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$PesterResult = Invoke-Pester -PassThru
if ($PesterResult.FailedCount -ne 0) {
throw "There were $($PesterResult.FailedCount) failed tests."
}
12 changes: 12 additions & 0 deletions Tests/_pssa.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$ExcludedRules = @()

$PssaSplat = @{
Path = ".\src"
Recurse = $true
EnableExit = $true
ReportSummary = $true
ExcludeRule = $ExcludedRules
#Severity = 'Error'
}

Invoke-ScriptAnalyzer @PssaSplat

0 comments on commit ad3e8b6

Please sign in to comment.