From bf7a5650dbc343ff0933c4f6419489742d402571 Mon Sep 17 00:00:00 2001 From: Joel Timothy Oh Date: Sat, 11 May 2024 19:24:54 +0000 Subject: [PATCH] Test (entrypoint): Improve test entrypoint script - Includes necessary variables - Integrates installation of Pester dependencies - Fixes throwing of error when Pester tests do not have a full passed count - Add listing of test artifacts at the end of tests --- test/Install-TestDependencies.ps1 | 25 ---------- test/test.ps1 | 77 +++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 50 deletions(-) delete mode 100644 test/Install-TestDependencies.ps1 diff --git a/test/Install-TestDependencies.ps1 b/test/Install-TestDependencies.ps1 deleted file mode 100644 index 0af2ccb..0000000 --- a/test/Install-TestDependencies.ps1 +++ /dev/null @@ -1,25 +0,0 @@ -[CmdletBinding()] -param() - -$ErrorActionPreference = 'Stop' - -try { - Push-Location $PSScriptRoot - - # Install Pester if needed - "Checking Pester version" | Write-Host - $pesterMinimumVersion = [version]'4.0.0' - $pesterMaximumVersion = [version]'4.10.1' - $pester = Get-Module 'Pester' -ListAvailable -ErrorAction SilentlyContinue - if (!$pester -or !($pester | ? { $_.Version -ge $pesterMinimumVersion -and $_.Version -le $pesterMaximumVersion })) { - "Installing Pester" | Write-Host - Install-Module -Name 'Pester' -Repository 'PSGallery' -MinimumVersion $pesterMinimumVersion -MaximumVersion $pesterMaximumVersion -Scope CurrentUser -Force - } - Get-Module Pester -ListAvailable | Out-String | Write-Verbose - Import-Module -Name 'Pester' -MinimumVersion $pesterMinimumVersion -MaximumVersion $pesterMaximumVersion -Force # Force import to ensure environment uses the correct version of Pester - -}catch { - throw -}finally{ - Pop-Location -} diff --git a/test/test.ps1 b/test/test.ps1 index 7841407..83132a6 100644 --- a/test/test.ps1 +++ b/test/test.ps1 @@ -1,43 +1,70 @@ [CmdletBinding()] -param() +param ( + [Parameter(Mandatory=$false)] + [ValidateNotNullOrEmpty()] + [string]$Tag = '' +) Set-StrictMode -Version Latest $VerbosePreference = 'Continue' -$global:PesterDebugPreference_ShowFullErrors = $true +$script:PesterDebugPreference_ShowFullErrors = $true try { + # Initialize variables + $moduleItem = Get-Item "$PSScriptRoot/../src/*/*.psm1" + $MODULE_PATH = $moduleItem.FullName + $MODULE_DIR = $moduleItem.Directory + $MODULE_NAME = $moduleItem.BaseName + Push-Location $PSScriptRoot - # Install test dependencies - "Installing test dependencies" | Write-Verbose - & "$PSScriptRoot\Install-TestDependencies.ps1" > $null - - # Run unit tests - "Running unit tests" | Write-Verbose - $testFailed = $false - $unitResult = Invoke-Pester -Script "$PSScriptRoot\..\src\PSRepositoryReleaseManager" -Tag 'Unit' -PassThru - if ($unitResult.FailedCount -gt 0) { - "$($unitResult.FailedCount) tests failed." | Write-Warning - $testFailed = $true + # Install Pester if needed + "Checking Pester version" | Write-Host + $pesterMinimumVersion = [version]'4.0.0' + $pesterMaximumVersion = [version]'4.10.1' + $pester = Get-Module 'Pester' -ListAvailable -ErrorAction SilentlyContinue + if (!$pester -or !($pester | ? { $_.Version -ge $pesterMinimumVersion -and $_.Version -le $pesterMaximumVersion })) { + "Installing Pester" | Write-Host + Install-Module -Name 'Pester' -Repository 'PSGallery' -MinimumVersion $pesterMinimumVersion -MaximumVersion $pesterMaximumVersion -Scope CurrentUser -Force } + $pester = Get-Module Pester -ListAvailable + $pester | Out-String | Write-Verbose + $pester | ? { $_.Version -ge $pesterMinimumVersion -and $_.Version -le $pesterMaximumVersion } | Select-Object -First 1 | Import-Module # Force import the latest version within the defined range to ensure environment uses the correct version of Pester - # Run integration tests - "Running integration tests" | Write-Verbose - $integratedResult = Invoke-Pester -Script "$PSScriptRoot\..\src\PSRepositoryReleaseManager" -Tag 'Integration' -PassThru - if ($integratedResult.FailedCount -gt 0) { - "$($integratedResult.FailedCount) tests failed." | Write-Warning - $testFailed = $true - } + # Import the project module + Import-Module $MODULE_PATH -Force - "Listing test artifacts" | Write-Verbose - git ls-files --others --exclude-standard + if ($Tag) { + # Run Unit Tests + $res = Invoke-Pester -Script $MODULE_DIR -Tag $Tag -PassThru -ErrorAction Stop + if (!($res.PassedCount -eq $res.TotalCount)) { + "$($res.TotalCount - $res.PassedCount) unit tests did not pass." | Write-Host + } + if (!($res.PassedCount -eq $res.TotalCount)) { + throw + } + }else { + # Run Unit Tests + $res = Invoke-Pester -Script $MODULE_DIR -Tag 'Unit' -PassThru -ErrorAction Stop + if (!($res.PassedCount -eq $res.TotalCount)) { + "$($res.TotalCount - $res.PassedCount) integration tests did not pass." | Write-Host + } - "End of tests" | Write-Verbose - if ($testFailed) { - throw "One or more tests failed." + # Run Integration Tests + $res2 = Invoke-Pester -Script $MODULE_DIR -Tag 'Integration' -PassThru -ErrorAction Stop + if (!($res2.PassedCount -eq $res2.TotalCount)) { + "$($res2.TotalCount - $res2.PassedCount) integration tests did not pass." | Write-Host + } + + if (!($res.PassedCount -eq $res.TotalCount) -or !($res2.PassedCount -eq $res2.TotalCount)) { + throw + } } + }catch { throw }finally { Pop-Location + "Listing test artifacts" | Write-Host + git ls-files --others --exclude-standard }