-
Notifications
You must be signed in to change notification settings - Fork 33
feat: adopt hve-core PowerShell CI infrastructure #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,109 @@ | ||
| # Used for Azure DevOps unit test results | ||
| [CmdletBinding()] | ||
| param( | ||
| [switch]$CI, | ||
| [switch]$ChangedOnly, | ||
| [switch]$CodeCoverage, | ||
| [string]$ConfigPath = (Join-Path $PSScriptRoot 'tests/pester.config.ps1'), | ||
| [string]$OutputPath = './test-results', | ||
| [string[]]$Path | ||
| ) | ||
|
|
||
| # To run locally, simply just run Invoke-Pester, no need to run this script | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| param ( | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$Path, | ||
| Import-Module (Join-Path $PSScriptRoot 'ci/Modules/CIHelpers.psm1') -Force | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string]$OutputFile | ||
| ) | ||
| $pesterModule = Get-Module -ListAvailable -Name Pester | | ||
| Where-Object { $_.Version -eq [version]'5.7.1' } | | ||
| Select-Object -First 1 | ||
|
|
||
| function Find-PesterModule { | ||
| param ( | ||
| [string]$ModuleName = "Pester", | ||
| [string]$MinimumVersion = "5.0.0" | ||
| ) | ||
| if (-not $pesterModule) { | ||
| Install-Module -Name Pester -RequiredVersion '5.7.1' -Force -Scope CurrentUser -SkipPublisherCheck | ||
| } | ||
|
|
||
| # Check if the module is installed | ||
| $module = Get-Module -ListAvailable -Name $ModuleName | Sort-Object -Property Version -Descending | Select-Object -First 1 | ||
| Import-Module Pester -RequiredVersion '5.7.1' -Force | ||
|
|
||
| if ($null -eq $module -or $module.Version -lt [version]$MinimumVersion) { | ||
| Write-Host "Installing or updating $ModuleName to version $MinimumVersion or higher..." | ||
| Install-Module -Name $ModuleName -MinimumVersion $MinimumVersion -Force -AllowClobber | ||
| } | ||
| else { | ||
| Write-Host "$ModuleName version $($module.Version) is already installed." | ||
| $configParams = @{} | ||
| if ($CI) { $configParams['CI'] = $true } | ||
| if ($CodeCoverage) { $configParams['CodeCoverage'] = $true } | ||
| if ($Path) { $configParams['Path'] = $Path } | ||
| $configParams['OutputPath'] = $OutputPath | ||
|
|
||
| $config = & $ConfigPath @configParams | ||
|
|
||
| if ($ChangedOnly) { | ||
| $changedTests = & (Join-Path $PSScriptRoot 'tests/Get-ChangedTestFiles.ps1') | ||
| if ($changedTests.Count -eq 0) { | ||
| Write-Host 'No changed test files found.' | ||
| Write-CIStepSummary "## Pester Test Results`n`nNo changed test files to run." | ||
| Set-CIOutput -Name 'test-result' -Value 'passed' | ||
| Set-CIOutput -Name 'test-count' -Value '0' | ||
| Set-CIOutput -Name 'fail-count' -Value '0' | ||
| exit 0 | ||
| } | ||
| $config.Run.Path = $changedTests | ||
| } | ||
|
|
||
| # Ensure Pester module is installed and at least version 5 | ||
| Find-PesterModule -ModuleName "Pester" -MinimumVersion "5.0.0" | ||
|
|
||
| if (-not (Test-Path $OutputPath)) { | ||
| New-Item -ItemType Directory -Path $OutputPath -Force | Out-Null | ||
| } | ||
|
|
||
| Import-Module Pester | ||
| $result = Invoke-Pester -Configuration $config | ||
|
|
||
| $configuration = [PesterConfiguration]@{ | ||
| Run = @{ | ||
| Path = $Path | ||
| } | ||
| Output = @{ | ||
| Verbosity = 'Detailed' | ||
| function Get-FailedTest { | ||
| param([object]$Container) | ||
| $failures = @() | ||
| foreach ($block in $Container.Blocks) { | ||
| foreach ($test in $block.Tests) { | ||
| if ($test.Result -eq 'Failed') { | ||
| $failures += @{ | ||
| Name = $test.ExpandedName | ||
| Error = $test.ErrorRecord.Exception.Message | ||
| File = $test.ScriptBlock.File | ||
| Line = $test.ScriptBlock.StartPosition.StartLine | ||
| } | ||
| } | ||
| } | ||
| if ($block.Blocks.Count -gt 0) { | ||
| $failures += Get-FailedTest -Container $block | ||
| } | ||
| } | ||
| TestResult = @{ | ||
| Enabled = $true | ||
| OutputFormat = "NUnitXml" | ||
| OutputPath = $OutputFile | ||
| return $failures | ||
| } | ||
|
|
||
| $allFailures = @() | ||
| foreach ($container in $result.Containers) { | ||
| $allFailures += Get-FailedTest -Container $container | ||
| } | ||
|
|
||
| $result | Select-Object TotalCount, PassedCount, FailedCount, SkippedCount, Duration | | ||
| ConvertTo-Json | Set-Content (Join-Path $OutputPath 'test-summary.json') | ||
|
|
||
| if ($allFailures.Count -gt 0) { | ||
| $allFailures | ConvertTo-Json -Depth 5 | Set-Content (Join-Path $OutputPath 'test-failures.json') | ||
| foreach ($failure in $allFailures) { | ||
| Write-CIAnnotation -Level 'Error' -Message "Test failed: $($failure.Name) — $($failure.Error)" ` | ||
| -File $failure.File -Line $failure.Line | ||
| } | ||
| } | ||
|
|
||
| # Print out the Path and OutputFile variables | ||
| Write-Host "Test file path: $Path" | ||
| Write-Host "Output test file path: $OutputFile" | ||
| $summary = @" | ||
| ## Pester Test Results | ||
|
|
||
| Invoke-Pester -Configuration $configuration | ||
| | Metric | Value | | ||
| |--------|-------| | ||
| | Total | $($result.TotalCount) | | ||
| | Passed | $($result.PassedCount) | | ||
| | Failed | $($result.FailedCount) | | ||
| | Skipped | $($result.SkippedCount) | | ||
| | Duration | $($result.Duration) | | ||
| "@ | ||
|
|
||
| Write-CIStepSummary $summary | ||
|
|
||
| Set-CIOutput -Name 'test-result' -Value $(if ($result.FailedCount -eq 0) { 'passed' } else { 'failed' }) | ||
| Set-CIOutput -Name 'test-count' -Value $result.TotalCount.ToString() | ||
| Set-CIOutput -Name 'fail-count' -Value $result.FailedCount.ToString() | ||
|
|
||
| if ($CI -and $result.FailedCount -gt 0) { | ||
| exit 1 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.