microsoft/hve-core

Public

mirrored from https://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7226b4c1944c0773b20eeb1b3be5a4aebf11d4e9

Branches

Tags

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

Clone

HTTPS

Download ZIP

scripts/linting/Invoke-PythonTests.ps1

176lines · modeblame

0a90f573Jason3 months ago1#!/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()]
30param(
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
41Push-Location $RepoRoot
42try {
43# Find all directories with pyproject.toml
17bbe7a3Bill Berry3 months ago44$pythonSkills = Get-ChildItem -Path . -Filter 'pyproject.toml' -Recurse -Force -File |
0a90f573Jason3 months ago45Where-Object { $_.FullName -notmatch 'node_modules' } |
46ForEach-Object { $_.Directory.FullName }
47
48if (-not $pythonSkills) {
49Write-Host 'No Python skills found (no pyproject.toml files detected)' -ForegroundColor Yellow
50return @{ success = $true; skillsTested = 0; passed = 0; failed = 0; errors = @() }
51}
52
53Write-Host "Found $($pythonSkills.Count) Python skill(s):" -ForegroundColor Cyan
54$pythonSkills | ForEach-Object { Write-Host " - $_" -ForegroundColor Gray }
55
3b922278Bill Berry3 months ago56# Check if pytest is globally available (used as fallback when skill has no venv)
57$globalPytest = Get-Command pytest -ErrorAction SilentlyContinue
0a90f573Jason3 months ago58
59$results = @{
60success = $true
61skillsTested = 0
62passed = 0
63failed = 0
64errors = @()
65details = @()
66}
67
68foreach ($skillPath in $pythonSkills) {
69Write-Host "`nRunning pytest in $skillPath..." -ForegroundColor Cyan
70
71Push-Location $skillPath
72try {
73# Check if tests directory exists
74$testsDir = Join-Path $skillPath 'tests'
75if (-not (Test-Path $testsDir)) {
76Write-Host '⚠ No tests directory found, skipping' -ForegroundColor Yellow
77continue
78}
79
3b922278Bill Berry3 months ago80# 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'
84if (Test-Path $venvPytest) {
85$pytestCmd = $venvPytest
86Write-Host ' Using venv pytest' -ForegroundColor Gray
87} elseif (Test-Path $venvPytestWin) {
88$pytestCmd = $venvPytestWin
89Write-Host ' Using venv pytest' -ForegroundColor Gray
90} elseif ($globalPytest) {
91$pytestCmd = 'pytest'
92}
93
94if (-not $pytestCmd) {
95Write-Host '❌ pytest not available (no .venv and not installed globally)' -ForegroundColor Red
96$results.success = $false
97$results.failed++
98$results.errors += $skillPath
99continue
100}
101
102$output = & $pytestCmd tests/ $Verbosity --tb=short 2>&1
0a90f573Jason3 months ago103$exitCode = $LASTEXITCODE
104
105$result = @{
106path = $skillPath
107passed = ($exitCode -eq 0)
108output = $output | Out-String
109}
110
111$results.details += $result
112$results.skillsTested++
113
114Write-Host "$output"
115
116if ($exitCode -ne 0) {
117Write-Host '❌ Tests failed' -ForegroundColor Red
118$results.success = $false
119$results.failed++
120$results.errors += $skillPath
121} else {
122Write-Host '✓ All tests passed' -ForegroundColor Green
123$results.passed++
124}
125} catch {
126Write-Host "Error running pytest: $_" -ForegroundColor Red
127$results.success = $false
128$results.failed++
129$results.errors += "$skillPath - error: $_"
130} finally {
131Pop-Location
132}
133}
134
d337e1d2Bill Berry3 months ago135# Default to logs directory when no OutputPath specified
136if (-not $OutputPath) {
137$logsDir = Join-Path -Path $RepoRoot -ChildPath 'logs'
138if (-not (Test-Path $logsDir)) {
139New-Item -ItemType Directory -Path $logsDir -Force | Out-Null
140}
141$OutputPath = Join-Path -Path $logsDir -ChildPath 'python-test-results.json'
0a90f573Jason3 months ago142}
d337e1d2Bill Berry3 months ago143$results | ConvertTo-Json -Depth 3 | Out-File $OutputPath -Encoding UTF8
144Write-Host "📊 Results written to: $OutputPath" -ForegroundColor Cyan
0a90f573Jason3 months ago145
146return $results
147} finally {
148Pop-Location
149}
150}
151
152#endregion
153
154#region Main Execution
155
156# Don't run main logic if dot-sourced for testing
157if ($MyInvocation.InvocationName -ne '.') {
158$result = Invoke-PythonTests -RepoRoot $RepoRoot -OutputPath $OutputPath -Verbosity $Verbosity
159
160Write-Host "`n========================================" -ForegroundColor Cyan
161Write-Host 'Test Summary:' -ForegroundColor Cyan
162Write-Host " Total: $($result.skillsTested)" -ForegroundColor White
163Write-Host " Passed: $($result.passed)" -ForegroundColor Green
164Write-Host " Failed: $($result.failed)" -ForegroundColor $(if ($result.failed -gt 0) { 'Red' } else { 'Green' })
165Write-Host '========================================' -ForegroundColor Cyan
166
167if ($result.success) {
168Write-Host '✅ All tests passed' -ForegroundColor Green
169exit 0
170} else {
171Write-Host '❌ Testing completed with failures' -ForegroundColor Red
172exit 1
173}
174}
175
176#endregion Main Execution