microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/linting/Invoke-PythonTests.ps1
176lines · modecode
| 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 |
| 44 | $pythonSkills = Get-ChildItem -Path . -Filter 'pyproject.toml' -Recurse -Force -File | |
| 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 globally available (used as fallback when skill has no venv) |
| 57 | $globalPytest = Get-Command pytest -ErrorAction SilentlyContinue |
| 58 | |
| 59 | $results = @{ |
| 60 | success = $true |
| 61 | skillsTested = 0 |
| 62 | passed = 0 |
| 63 | failed = 0 |
| 64 | errors = @() |
| 65 | details = @() |
| 66 | } |
| 67 | |
| 68 | foreach ($skillPath in $pythonSkills) { |
| 69 | Write-Host "`nRunning pytest in $skillPath..." -ForegroundColor Cyan |
| 70 | |
| 71 | Push-Location $skillPath |
| 72 | try { |
| 73 | # Check if tests directory exists |
| 74 | $testsDir = Join-Path $skillPath 'tests' |
| 75 | if (-not (Test-Path $testsDir)) { |
| 76 | Write-Host '⚠ No tests directory found, skipping' -ForegroundColor Yellow |
| 77 | continue |
| 78 | } |
| 79 | |
| 80 | # Resolve pytest: prefer skill venv, fall back to global |
| 81 | $pytestCmd = $null |
| 82 | $venvPytest = Join-Path $skillPath '.venv/bin/pytest' |
| 83 | $venvPytestWin = Join-Path $skillPath '.venv/Scripts/pytest.exe' |
| 84 | if (Test-Path $venvPytest) { |
| 85 | $pytestCmd = $venvPytest |
| 86 | Write-Host ' Using venv pytest' -ForegroundColor Gray |
| 87 | } elseif (Test-Path $venvPytestWin) { |
| 88 | $pytestCmd = $venvPytestWin |
| 89 | Write-Host ' Using venv pytest' -ForegroundColor Gray |
| 90 | } elseif ($globalPytest) { |
| 91 | $pytestCmd = 'pytest' |
| 92 | } |
| 93 | |
| 94 | if (-not $pytestCmd) { |
| 95 | Write-Host '❌ pytest not available (no .venv and not installed globally)' -ForegroundColor Red |
| 96 | $results.success = $false |
| 97 | $results.failed++ |
| 98 | $results.errors += $skillPath |
| 99 | continue |
| 100 | } |
| 101 | |
| 102 | $output = & $pytestCmd tests/ $Verbosity --tb=short 2>&1 |
| 103 | $exitCode = $LASTEXITCODE |
| 104 | |
| 105 | $result = @{ |
| 106 | path = $skillPath |
| 107 | passed = ($exitCode -eq 0) |
| 108 | output = $output | Out-String |
| 109 | } |
| 110 | |
| 111 | $results.details += $result |
| 112 | $results.skillsTested++ |
| 113 | |
| 114 | Write-Host "$output" |
| 115 | |
| 116 | if ($exitCode -ne 0) { |
| 117 | Write-Host '❌ Tests failed' -ForegroundColor Red |
| 118 | $results.success = $false |
| 119 | $results.failed++ |
| 120 | $results.errors += $skillPath |
| 121 | } else { |
| 122 | Write-Host '✓ All tests passed' -ForegroundColor Green |
| 123 | $results.passed++ |
| 124 | } |
| 125 | } catch { |
| 126 | Write-Host "Error running pytest: $_" -ForegroundColor Red |
| 127 | $results.success = $false |
| 128 | $results.failed++ |
| 129 | $results.errors += "$skillPath - error: $_" |
| 130 | } finally { |
| 131 | Pop-Location |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | # Default to logs directory when no OutputPath specified |
| 136 | if (-not $OutputPath) { |
| 137 | $logsDir = Join-Path -Path $RepoRoot -ChildPath 'logs' |
| 138 | if (-not (Test-Path $logsDir)) { |
| 139 | New-Item -ItemType Directory -Path $logsDir -Force | Out-Null |
| 140 | } |
| 141 | $OutputPath = Join-Path -Path $logsDir -ChildPath 'python-test-results.json' |
| 142 | } |
| 143 | $results | ConvertTo-Json -Depth 3 | Out-File $OutputPath -Encoding UTF8 |
| 144 | Write-Host "📊 Results written to: $OutputPath" -ForegroundColor Cyan |
| 145 | |
| 146 | return $results |
| 147 | } finally { |
| 148 | Pop-Location |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | #endregion |
| 153 | |
| 154 | #region Main Execution |
| 155 | |
| 156 | # Don't run main logic if dot-sourced for testing |
| 157 | if ($MyInvocation.InvocationName -ne '.') { |
| 158 | $result = Invoke-PythonTests -RepoRoot $RepoRoot -OutputPath $OutputPath -Verbosity $Verbosity |
| 159 | |
| 160 | Write-Host "`n========================================" -ForegroundColor Cyan |
| 161 | Write-Host 'Test Summary:' -ForegroundColor Cyan |
| 162 | Write-Host " Total: $($result.skillsTested)" -ForegroundColor White |
| 163 | Write-Host " Passed: $($result.passed)" -ForegroundColor Green |
| 164 | Write-Host " Failed: $($result.failed)" -ForegroundColor $(if ($result.failed -gt 0) { 'Red' } else { 'Green' }) |
| 165 | Write-Host '========================================' -ForegroundColor Cyan |
| 166 | |
| 167 | if ($result.success) { |
| 168 | Write-Host '✅ All tests passed' -ForegroundColor Green |
| 169 | exit 0 |
| 170 | } else { |
| 171 | Write-Host '❌ Testing completed with failures' -ForegroundColor Red |
| 172 | exit 1 |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | #endregion Main Execution |
| 177 | |