microsoft/hve-core

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
hve-core-v3.2.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

scripts/linting/Invoke-PythonTests.ps1

158lines · 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
56# Check if pytest is available (once, before loop - mirrors ruff check in lint script)
57$pytestAvailable = Get-Command pytest -ErrorAction SilentlyContinue
58if (-not $pytestAvailable) {
59Write-Host '❌ pytest is not installed. Run "uv sync" in a skill directory or install with "uv pip install pytest pytest-cov".' -ForegroundColor Red
60return @{ success = $false; skillsTested = 0; passed = 0; failed = 0; errors = @('pytest not installed') }
61}
62
63$results = @{
64success = $true
65skillsTested = 0
66passed = 0
67failed = 0
68errors = @()
69details = @()
70}
71
72foreach ($skillPath in $pythonSkills) {
73Write-Host "`nRunning pytest in $skillPath..." -ForegroundColor Cyan
74
75Push-Location $skillPath
76try {
77# Check if tests directory exists
78$testsDir = Join-Path $skillPath 'tests'
79if (-not (Test-Path $testsDir)) {
80Write-Host '⚠ No tests directory found, skipping' -ForegroundColor Yellow
81continue
82}
83
84$output = pytest tests/ $Verbosity --tb=short 2>&1
85$exitCode = $LASTEXITCODE
86
87$result = @{
88path = $skillPath
89passed = ($exitCode -eq 0)
90output = $output | Out-String
91}
92
93$results.details += $result
94$results.skillsTested++
95
96Write-Host "$output"
97
98if ($exitCode -ne 0) {
99Write-Host '❌ Tests failed' -ForegroundColor Red
100$results.success = $false
101$results.failed++
102$results.errors += $skillPath
103} else {
104Write-Host '✓ All tests passed' -ForegroundColor Green
105$results.passed++
106}
107} catch {
108Write-Host "Error running pytest: $_" -ForegroundColor Red
109$results.success = $false
110$results.failed++
111$results.errors += "$skillPath - error: $_"
112} finally {
113Pop-Location
114}
115}
116
d337e1d2Bill Berry3 months ago117# Default to logs directory when no OutputPath specified
118if (-not $OutputPath) {
119$logsDir = Join-Path -Path $RepoRoot -ChildPath 'logs'
120if (-not (Test-Path $logsDir)) {
121New-Item -ItemType Directory -Path $logsDir -Force | Out-Null
122}
123$OutputPath = Join-Path -Path $logsDir -ChildPath 'python-test-results.json'
0a90f573Jason3 months ago124}
d337e1d2Bill Berry3 months ago125$results | ConvertTo-Json -Depth 3 | Out-File $OutputPath -Encoding UTF8
126Write-Host "📊 Results written to: $OutputPath" -ForegroundColor Cyan
0a90f573Jason3 months ago127
128return $results
129} finally {
130Pop-Location
131}
132}
133
134#endregion
135
136#region Main Execution
137
138# Don't run main logic if dot-sourced for testing
139if ($MyInvocation.InvocationName -ne '.') {
140$result = Invoke-PythonTests -RepoRoot $RepoRoot -OutputPath $OutputPath -Verbosity $Verbosity
141
142Write-Host "`n========================================" -ForegroundColor Cyan
143Write-Host 'Test Summary:' -ForegroundColor Cyan
144Write-Host " Total: $($result.skillsTested)" -ForegroundColor White
145Write-Host " Passed: $($result.passed)" -ForegroundColor Green
146Write-Host " Failed: $($result.failed)" -ForegroundColor $(if ($result.failed -gt 0) { 'Red' } else { 'Green' })
147Write-Host '========================================' -ForegroundColor Cyan
148
149if ($result.success) {
150Write-Host '✅ All tests passed' -ForegroundColor Green
151exit 0
152} else {
153Write-Host '❌ Testing completed with failures' -ForegroundColor Red
154exit 1
155}
156}
157
158#endregion Main Execution