Skip to content

Commit 790ef44

Browse files
CopilotTravisEz13
andcommitted
Add demonstration test for GitHub Actions annotations feature
- Added test in ciModule.Tests.ps1 to demonstrate the annotation feature - Test intentionally fails to show annotations in CI output - Verifies relative path conversion and test log links - Can be removed after feature is verified in actual CI runs Co-authored-by: TravisEz13 <10873629+TravisEz13@users.noreply.github.com>
1 parent 9b85763 commit 790ef44

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

test/infrastructure/ciModule.Tests.ps1

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,89 @@ Describe "Install-CIPester" {
244244
}
245245
}
246246

247+
Describe "Show-PSPesterError GitHub Actions Annotations" {
248+
BeforeAll {
249+
# Import the build module which contains Show-PSPesterError
250+
Import-Module "$PSScriptRoot/../../build.psm1" -Force
251+
252+
# Set up GitHub Actions environment to trigger annotation feature
253+
$script:originalGitHubWorkflow = $env:GITHUB_WORKFLOW
254+
$script:originalGitHubWorkspace = $env:GITHUB_WORKSPACE
255+
$script:originalGitHubServerUrl = $env:GITHUB_SERVER_URL
256+
$script:originalGitHubRepository = $env:GITHUB_REPOSITORY
257+
$script:originalGitHubRunId = $env:GITHUB_RUN_ID
258+
259+
$env:GITHUB_WORKFLOW = 'test-workflow'
260+
# Set workspace to repo root (two levels up from test/infrastructure)
261+
$env:GITHUB_WORKSPACE = Resolve-Path "$PSScriptRoot/../.."
262+
$env:GITHUB_SERVER_URL = 'https://github.com'
263+
$env:GITHUB_REPOSITORY = 'PowerShell/PowerShell'
264+
$env:GITHUB_RUN_ID = '12345'
265+
}
266+
267+
AfterAll {
268+
# Restore original environment
269+
if ($null -eq $script:originalGitHubWorkflow) {
270+
Remove-Item Env:\GITHUB_WORKFLOW -ErrorAction SilentlyContinue
271+
} else {
272+
$env:GITHUB_WORKFLOW = $script:originalGitHubWorkflow
273+
}
274+
if ($null -eq $script:originalGitHubWorkspace) {
275+
Remove-Item Env:\GITHUB_WORKSPACE -ErrorAction SilentlyContinue
276+
} else {
277+
$env:GITHUB_WORKSPACE = $script:originalGitHubWorkspace
278+
}
279+
if ($null -eq $script:originalGitHubServerUrl) {
280+
Remove-Item Env:\GITHUB_SERVER_URL -ErrorAction SilentlyContinue
281+
} else {
282+
$env:GITHUB_SERVER_URL = $script:originalGitHubServerUrl
283+
}
284+
if ($null -eq $script:originalGitHubRepository) {
285+
Remove-Item Env:\GITHUB_REPOSITORY -ErrorAction SilentlyContinue
286+
} else {
287+
$env:GITHUB_REPOSITORY = $script:originalGitHubRepository
288+
}
289+
if ($null -eq $script:originalGitHubRunId) {
290+
Remove-Item Env:\GITHUB_RUN_ID -ErrorAction SilentlyContinue
291+
} else {
292+
$env:GITHUB_RUN_ID = $script:originalGitHubRunId
293+
}
294+
}
295+
296+
Context "When test failure occurs in GitHub Actions" {
297+
It "Should generate GitHub Actions annotation with relative file path - DEMONSTRATION TEST (will fail)" {
298+
# This test intentionally fails to demonstrate the GitHub Actions annotation feature
299+
# It can be removed once we verify the feature works in actual CI
300+
301+
# Create a mock test failure XML with absolute path
302+
$absolutePath = Join-Path $env:GITHUB_WORKSPACE "test/infrastructure/ciModule.Tests.ps1"
303+
$xmlContent = @"
304+
<?xml version="1.0" encoding="utf-8"?>
305+
<test-results>
306+
<test-case description="Demo Context" name="Demo test that fails" result="Failure">
307+
<failure>
308+
<message>This is a demonstration failure to show GitHub Actions annotations</message>
309+
<stack-trace>at line: 42 in $absolutePath</stack-trace>
310+
</failure>
311+
</test-case>
312+
</test-results>
313+
"@
314+
315+
$xml = [xml]$xmlContent
316+
$testCase = $xml.'test-results'.'test-case'
317+
318+
# Capture the output from Show-PSPesterError (Write-Host goes to stream 6)
319+
$output = Show-PSPesterError -testFailure $testCase 6>&1 | Out-String
320+
321+
# Verify the annotation is generated with relative path (not absolute)
322+
$output | Should -Match '::error file=test/infrastructure/ciModule.Tests.ps1,line=42'
323+
324+
# Verify the test log link is included
325+
$output | Should -Match 'Test logs:.*github.com/PowerShell/PowerShell/actions/runs/12345'
326+
327+
# This line intentionally fails to demonstrate the annotation in CI output
328+
1 | Should -Be 2 -Because "This test demonstrates GitHub Actions annotations for test failures. Remove this test after verifying the feature works in CI."
329+
}
330+
}
331+
}
332+

0 commit comments

Comments
 (0)