microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
scripts/linting/Invoke-PythonTests.ps1
158lines · modeblame
0a90f573Jason3 months ago | 1 | #!/usr/bin/env pwsh |
| 2 | # Copyright (c) Microsoft Corporation. | |
| 3 | # SPDX-License-Identifier: MIT | |
| 4 | # | |
| 5 | # Invoke-PythonTests.ps1 | |
| 6 | # | |
| 7 | # Purpose: Dynamically discovers and tests Python skills using pytest | |
| 8 | # Author: HVE Core Team | |
| 9 | | |
| 10 | #Requires -Version 7.0 | |
| 11 | | |
| 12 | [CmdletBinding()] | |
| 13 | param( | |
| 14 | [Parameter(Mandatory = $false)] | |
| 15 | [string]$RepoRoot = (Get-Location).Path, | |
| 16 | | |
| 17 | [Parameter(Mandatory = $false)] | |
| 18 | [string]$OutputPath, | |
| 19 | | |
| 20 | [Parameter(Mandatory = $false)] | |
| 21 | [string]$Verbosity = '-v' | |
| 22 | ) | |
| 23 | | |
| 24 | $ErrorActionPreference = 'Stop' | |
| 25 | | |
| 26 | #region Functions | |
| 27 | | |
| 28 | function Invoke-PythonTests { | |
| 29 | [CmdletBinding()] | |
| 30 | param( | |
| 31 | [Parameter(Mandatory = $true)] | |
| 32 | [string]$RepoRoot, | |
| 33 | | |
| 34 | [Parameter(Mandatory = $false)] | |
| 35 | [string]$OutputPath, | |
| 36 | | |
| 37 | [Parameter(Mandatory = $false)] | |
| 38 | [string]$Verbosity = '-v' | |
| 39 | ) | |
| 40 | | |
| 41 | Push-Location $RepoRoot | |
| 42 | try { | |
| 43 | # Find all directories with pyproject.toml | |
17bbe7a3Bill Berry3 months ago | 44 | $pythonSkills = Get-ChildItem -Path . -Filter 'pyproject.toml' -Recurse -Force -File | |
0a90f573Jason3 months ago | 45 | Where-Object { $_.FullName -notmatch 'node_modules' } | |
| 46 | ForEach-Object { $_.Directory.FullName } | |
| 47 | | |
| 48 | if (-not $pythonSkills) { | |
| 49 | Write-Host 'No Python skills found (no pyproject.toml files detected)' -ForegroundColor Yellow | |
| 50 | return @{ success = $true; skillsTested = 0; passed = 0; failed = 0; errors = @() } | |
| 51 | } | |
| 52 | | |
| 53 | Write-Host "Found $($pythonSkills.Count) Python skill(s):" -ForegroundColor Cyan | |
| 54 | $pythonSkills | ForEach-Object { Write-Host " - $_" -ForegroundColor Gray } | |
| 55 | | |
| 56 | # Check if pytest is available (once, before loop - mirrors ruff check in lint script) | |
| 57 | $pytestAvailable = Get-Command pytest -ErrorAction SilentlyContinue | |
| 58 | if (-not $pytestAvailable) { | |
| 59 | Write-Host '❌ pytest is not installed. Run "uv sync" in a skill directory or install with "uv pip install pytest pytest-cov".' -ForegroundColor Red | |
| 60 | return @{ success = $false; skillsTested = 0; passed = 0; failed = 0; errors = @('pytest not installed') } | |
| 61 | } | |
| 62 | | |
| 63 | $results = @{ | |
| 64 | success = $true | |
| 65 | skillsTested = 0 | |
| 66 | passed = 0 | |
| 67 | failed = 0 | |
| 68 | errors = @() | |
| 69 | details = @() | |
| 70 | } | |
| 71 | | |
| 72 | foreach ($skillPath in $pythonSkills) { | |
| 73 | Write-Host "`nRunning pytest in $skillPath..." -ForegroundColor Cyan | |
| 74 | | |
| 75 | Push-Location $skillPath | |
| 76 | try { | |
| 77 | # Check if tests directory exists | |
| 78 | $testsDir = Join-Path $skillPath 'tests' | |
| 79 | if (-not (Test-Path $testsDir)) { | |
| 80 | Write-Host '⚠ No tests directory found, skipping' -ForegroundColor Yellow | |
| 81 | continue | |
| 82 | } | |
| 83 | | |
| 84 | $output = pytest tests/ $Verbosity --tb=short 2>&1 | |
| 85 | $exitCode = $LASTEXITCODE | |
| 86 | | |
| 87 | $result = @{ | |
| 88 | path = $skillPath | |
| 89 | passed = ($exitCode -eq 0) | |
| 90 | output = $output | Out-String | |
| 91 | } | |
| 92 | | |
| 93 | $results.details += $result | |
| 94 | $results.skillsTested++ | |
| 95 | | |
| 96 | Write-Host "$output" | |
| 97 | | |
| 98 | if ($exitCode -ne 0) { | |
| 99 | Write-Host '❌ Tests failed' -ForegroundColor Red | |
| 100 | $results.success = $false | |
| 101 | $results.failed++ | |
| 102 | $results.errors += $skillPath | |
| 103 | } else { | |
| 104 | Write-Host '✓ All tests passed' -ForegroundColor Green | |
| 105 | $results.passed++ | |
| 106 | } | |
| 107 | } catch { | |
| 108 | Write-Host "Error running pytest: $_" -ForegroundColor Red | |
| 109 | $results.success = $false | |
| 110 | $results.failed++ | |
| 111 | $results.errors += "$skillPath - error: $_" | |
| 112 | } finally { | |
| 113 | Pop-Location | |
| 114 | } | |
| 115 | } | |
| 116 | | |
d337e1d2Bill Berry3 months ago | 117 | # Default to logs directory when no OutputPath specified |
| 118 | if (-not $OutputPath) { | |
| 119 | $logsDir = Join-Path -Path $RepoRoot -ChildPath 'logs' | |
| 120 | if (-not (Test-Path $logsDir)) { | |
| 121 | New-Item -ItemType Directory -Path $logsDir -Force | Out-Null | |
| 122 | } | |
| 123 | $OutputPath = Join-Path -Path $logsDir -ChildPath 'python-test-results.json' | |
0a90f573Jason3 months ago | 124 | } |
d337e1d2Bill Berry3 months ago | 125 | $results | ConvertTo-Json -Depth 3 | Out-File $OutputPath -Encoding UTF8 |
| 126 | Write-Host "📊 Results written to: $OutputPath" -ForegroundColor Cyan | |
0a90f573Jason3 months ago | 127 | |
| 128 | return $results | |
| 129 | } finally { | |
| 130 | Pop-Location | |
| 131 | } | |
| 132 | } | |
| 133 | | |
| 134 | #endregion | |
| 135 | | |
| 136 | #region Main Execution | |
| 137 | | |
| 138 | # Don't run main logic if dot-sourced for testing | |
| 139 | if ($MyInvocation.InvocationName -ne '.') { | |
| 140 | $result = Invoke-PythonTests -RepoRoot $RepoRoot -OutputPath $OutputPath -Verbosity $Verbosity | |
| 141 | | |
| 142 | Write-Host "`n========================================" -ForegroundColor Cyan | |
| 143 | Write-Host 'Test Summary:' -ForegroundColor Cyan | |
| 144 | Write-Host " Total: $($result.skillsTested)" -ForegroundColor White | |
| 145 | Write-Host " Passed: $($result.passed)" -ForegroundColor Green | |
| 146 | Write-Host " Failed: $($result.failed)" -ForegroundColor $(if ($result.failed -gt 0) { 'Red' } else { 'Green' }) | |
| 147 | Write-Host '========================================' -ForegroundColor Cyan | |
| 148 | | |
| 149 | if ($result.success) { | |
| 150 | Write-Host '✅ All tests passed' -ForegroundColor Green | |
| 151 | exit 0 | |
| 152 | } else { | |
| 153 | Write-Host '❌ Testing completed with failures' -ForegroundColor Red | |
| 154 | exit 1 | |
| 155 | } | |
| 156 | } | |
| 157 | | |
| 158 | #endregion Main Execution |