From 819a8c845248dd051e8695d0ded598b7f7b38ea3 Mon Sep 17 00:00:00 2001 From: Joel Timothy Oh Date: Fri, 10 May 2024 19:00:46 +0000 Subject: [PATCH] Docs (samples): Update `test.ps1` entrypoint script --- docs/samples/test/test.ps1 | 71 +++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/docs/samples/test/test.ps1 b/docs/samples/test/test.ps1 index 9d19e8b..8cc057f 100644 --- a/docs/samples/test/test.ps1 +++ b/docs/samples/test/test.ps1 @@ -1,43 +1,66 @@ [CmdletBinding()] -param() +param ( + [string]$Tag = '' +) +$moduleItem = Get-Item "$PSScriptRoot/../src/*/*.psm1" +$MODULE_PATH = $moduleItem.FullName +$MODULE_DIR = $moduleItem.Directory +$MODULE_NAME = $moduleItem.BaseName Set-StrictMode -Version Latest -$ErrorView = 'NormalView' $VerbosePreference = 'Continue' $script:PesterDebugPreference_ShowFullErrors = $true try { Push-Location $PSScriptRoot - # Install test dependencies - "Installing test dependencies" | Write-Host - & "$PSScriptRoot\scripts\dep\Install-TestDependencies.ps1" > $null - - # Run unit tests - "Running unit tests" | Write-Host - $testFailed = $false - $unitResult = Invoke-Pester -Script "$PSScriptRoot\..\src\MyPowershellModule" -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-Host - $integratedFailedCount = & "$PSScriptRoot\scripts\integration\Run-IntegrationTests.ps1" - if ($integratedFailedCount -gt 0) { - $testFailed = $true - } + # Import the project module + Import-Module $MODULE_PATH -Force - "Listing test artifacts" | Write-Host - 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 + } + + # 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 + } - "End of tests" | Write-Host - if ($testFailed) { - throw "One or more tests failed." + if (!($res.PassedCount -eq $res.TotalCount) -or !($res2.PassedCount -eq $res2.TotalCount)) { + throw + } } + }catch { throw }finally { + "Listing test artifacts" | Write-Host + git ls-files --others --exclude-standard Pop-Location }