microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/1637-l2-skill

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/linting/Invoke-PythonTests.ps1

212lines · 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()]
13param(
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
28function 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 # Prefer locked uv environments when available; pytest remains the fallback for unlocked projects.
57 $uvCommand = Get-Command uv -ErrorAction SilentlyContinue
58 $globalPytest = Get-Command pytest -ErrorAction SilentlyContinue
59
60 $results = @{
61 success = $true
62 skillsTested = 0
63 passed = 0
64 failed = 0
65 errors = @()
66 details = @()
67 }
68
69 foreach ($skillPath in $pythonSkills) {
70 Write-Host "`nRunning pytest in $skillPath..." -ForegroundColor Cyan
71
72 Push-Location $skillPath
73 try {
74 # Check if tests directory exists
75 $testsDir = Join-Path $skillPath 'tests'
76 if (-not (Test-Path $testsDir)) {
77 Write-Host '⚠ No tests directory found, skipping' -ForegroundColor Yellow
78 continue
79 }
80
81 $uvLockPath = Join-Path $skillPath 'uv.lock'
82 $runner = 'pytest'
83 $syncOutput = $null
84
85 if ($uvCommand -and (Test-Path $uvLockPath)) {
86 $runner = 'uv'
87 Write-Host ' Using uv locked environment' -ForegroundColor Gray
88
89 $syncOutput = & uv sync --locked --dev 2>&1
90 $syncExitCode = $LASTEXITCODE
91 Write-Host "$syncOutput"
92
93 if ($syncExitCode -ne 0) {
94 $result = @{
95 path = $skillPath
96 passed = $false
97 runner = $runner
98 phase = 'sync'
99 output = $syncOutput | Out-String
100 }
101
102 $results.details += $result
103 $results.skillsTested++
104 $results.success = $false
105 $results.failed++
106 $results.errors += $skillPath
107 Write-Host '❌ uv sync failed' -ForegroundColor Red
108 continue
109 }
110
111 $output = & uv run pytest tests/ $Verbosity --tb=short 2>&1
112 $exitCode = $LASTEXITCODE
113 } else {
114 # Resolve pytest: prefer skill venv, fall back to global
115 $pytestCmd = $null
116 $venvPytest = Join-Path $skillPath '.venv/bin/pytest'
117 $venvPytestWin = Join-Path $skillPath '.venv/Scripts/pytest.exe'
118 if (Test-Path $venvPytest) {
119 $pytestCmd = $venvPytest
120 Write-Host ' Using venv pytest' -ForegroundColor Gray
121 } elseif (Test-Path $venvPytestWin) {
122 $pytestCmd = $venvPytestWin
123 Write-Host ' Using venv pytest' -ForegroundColor Gray
124 } elseif ($globalPytest) {
125 $pytestCmd = 'pytest'
126 }
127
128 if (-not $pytestCmd) {
129 Write-Host '❌ pytest not available (no uv lockfile, no .venv, and not installed globally)' -ForegroundColor Red
130 $results.success = $false
131 $results.failed++
132 $results.errors += $skillPath
133 continue
134 }
135
136 $output = & $pytestCmd tests/ $Verbosity --tb=short 2>&1
137 $exitCode = $LASTEXITCODE
138 }
139
140 $result = @{
141 path = $skillPath
142 passed = ($exitCode -eq 0)
143 runner = $runner
144 output = $output | Out-String
145 }
146
147 $results.details += $result
148 $results.skillsTested++
149
150 Write-Host "$output"
151
152 if ($exitCode -ne 0) {
153 Write-Host '❌ Tests failed' -ForegroundColor Red
154 $results.success = $false
155 $results.failed++
156 $results.errors += $skillPath
157 } else {
158 Write-Host '✓ All tests passed' -ForegroundColor Green
159 $results.passed++
160 }
161 } catch {
162 Write-Host "Error running pytest: $_" -ForegroundColor Red
163 $results.success = $false
164 $results.failed++
165 $results.errors += "$skillPath - error: $_"
166 } finally {
167 Pop-Location
168 }
169 }
170
171 # Default to logs directory when no OutputPath specified
172 if (-not $OutputPath) {
173 $logsDir = Join-Path -Path $RepoRoot -ChildPath 'logs'
174 if (-not (Test-Path $logsDir)) {
175 New-Item -ItemType Directory -Path $logsDir -Force | Out-Null
176 }
177 $OutputPath = Join-Path -Path $logsDir -ChildPath 'python-test-results.json'
178 }
179 $results | ConvertTo-Json -Depth 3 | Out-File $OutputPath -Encoding UTF8
180 Write-Host "📊 Results written to: $OutputPath" -ForegroundColor Cyan
181
182 return $results
183 } finally {
184 Pop-Location
185 }
186}
187
188#endregion
189
190#region Main Execution
191
192# Don't run main logic if dot-sourced for testing
193if ($MyInvocation.InvocationName -ne '.') {
194 $result = Invoke-PythonTests -RepoRoot $RepoRoot -OutputPath $OutputPath -Verbosity $Verbosity
195
196 Write-Host "`n========================================" -ForegroundColor Cyan
197 Write-Host 'Test Summary:' -ForegroundColor Cyan
198 Write-Host " Total: $($result.skillsTested)" -ForegroundColor White
199 Write-Host " Passed: $($result.passed)" -ForegroundColor Green
200 Write-Host " Failed: $($result.failed)" -ForegroundColor $(if ($result.failed -gt 0) { 'Red' } else { 'Green' })
201 Write-Host '========================================' -ForegroundColor Cyan
202
203 if ($result.success) {
204 Write-Host '✅ All tests passed' -ForegroundColor Green
205 exit 0
206 } else {
207 Write-Host '❌ Testing completed with failures' -ForegroundColor Red
208 exit 1
209 }
210}
211
212#endregion Main Execution
213