Created
January 17, 2025 15:10
-
-
Save SebDeclercq/436596b56101c14911b2a87c6b75db5c to your computer and use it in GitHub Desktop.
pytest + pytest-cov + pytest-bdd + interrogate + mypy in an ugly but working PS script (by Perplexity.AI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[string[]]$commands | |
) | |
# Fonction pour exécuter pytest avec les options spécifiées | |
function Run-Pytest { | |
pytest -vv --gherkin-terminal-reporter --durations=10 --cov-report term-missing --cov-report html --cov=XXXXXX | |
} | |
# Fonction pour exécuter pytest pour les features BDD | |
function Run-Gherkin { | |
pytest -vv --gherkin-terminal-reporter -m feature | |
} | |
# Fonction pour exécuter interrogate sur XXXXXX | |
function Run-Interrogate { | |
interrogate XXXXXX | |
} | |
# Fonction pour exécuter mypy et afficher le résumé de couverture | |
function Run-Mypy { | |
mypy XXXXXX | |
Show-MypyCoverageSummary | |
Remove-MypyCoverageFiles | |
} | |
# Fonction pour afficher le résumé de couverture mypy | |
function Show-MypyCoverageSummary { | |
$filePath = "mypy_coverage\index.txt" | |
if (-Not (Test-Path $filePath)) { | |
Write-Host "mypy coverage report not found at $filePath." | |
return | |
} | |
$totalLine = (Get-Content $filePath | Select-String -Pattern "Total").Line | |
if (-Not $totalLine) { | |
Write-Host "Total line not found in the mypy coverage report." | |
return | |
} | |
$totalLine = $totalLine -replace '\|', '' -replace '\+', '' -replace '\-', '' -replace '\s+', ' ' -replace 'Total', '' | |
$parts = $totalLine.Trim().Split(' ') | |
try { | |
$imprecision = [double]::Parse($parts[0].TrimEnd('%'), [System.Globalization.CultureInfo]::InvariantCulture) | |
$loc = [int]::Parse($parts[2], [System.Globalization.CultureInfo]::InvariantCulture) | |
$impreciseLoc = [math]::Round(($imprecision / 100) * $loc) | |
return "Imprecise: $impreciseLoc/$loc LOC ($imprecision%)" | |
} catch { | |
Write-Host "Error parsing values: $_" | |
} | |
} | |
# Fonction pour supprimer le dossier et le fichier mypy coverage | |
function Remove-MypyCoverageFiles { | |
$filePath = "mypy_coverage\index.txt" | |
$directoryPath = "mypy_coverage" | |
if (Test-Path $filePath) { | |
Remove-Item $filePath -Force | |
} else { | |
Write-Host "File not found: $filePath" | |
} | |
if (Test-Path $directoryPath) { | |
Remove-Item $directoryPath -Recurse -Force | |
} else { | |
Write-Host "Directory not found: $directoryPath" | |
} | |
} | |
# Fonction pour exécuter pytest et extraire les informations nécessaires | |
function Run-Pytest-Report { | |
$pytestOutput = pytest -vv --gherkin-terminal-reporter --durations=10 --cov-report term-missing --cov-report html --cov=XXXXXX 2>&1 | |
$coverageLine = $pytestOutput | Select-String -Pattern "Required test coverage of .* reached. Total coverage: .*" | |
$summaryLine = $pytestOutput | Select-String -Pattern "passed, .* skipped in .*s" | |
Write-Host $coverageLine | |
Write-Host $summaryLine | |
} | |
# Fonction pour exécuter interrogate et extraire les informations nécessaires | |
function Run-Interrogate-Report { | |
$interrogateOutput = interrogate XXXXXX 2>&1 | |
$resultLine = $interrogateOutput | Select-String -Pattern "RESULT: PASSED \(minimum: .*%, actual: .*%\)" | |
if ($resultLine) { | |
Write-Host $resultLine | |
} else { | |
$errorLine = $interrogateOutput | Select-String -Pattern "RESULT: .*" | |
if ($errorLine) { | |
Write-Host $errorLine -ForegroundColor Red | |
} else { | |
Write-Host $interrogateOutput -ForegroundColor Red | |
} | |
} | |
} | |
# Fonction pour exécuter mypy et extraire les informations nécessaires | |
function Run-Mypy-Report { | |
mypy XXXXXX > $null | |
$mypySummary = Show-MypyCoverageSummary | |
if ($mypySummary) { | |
Write-Host $mypySummary | |
} | |
Remove-MypyCoverageFiles | |
} | |
# Fonction pour afficher les commandes disponibles | |
function Show-AvailableCommands { | |
Write-Host "Available commands are: pytest, interrogate, mypy, report" | |
} | |
# Fonction pour colorer le texte | |
function Color-Text { | |
param ( | |
[string]$text, | |
[string]$color | |
) | |
switch ($color) { | |
"green" { Write-Host $text -ForegroundColor Green } | |
"red" { Write-Host $text -ForegroundColor Red } | |
default { Write-Host $text } | |
} | |
} | |
# Exécution des commandes spécifiées | |
foreach ($command in $commands) { | |
switch ($command) { | |
"pytest" { Run-Pytest } | |
"gherkin" { Run-Gherkin } | |
"interrogate" { Run-Interrogate } | |
"mypy" { Run-Mypy } | |
"report" { | |
# Exécution de pytest et extraction des informations nécessaires | |
$pytestOutput = Run-Pytest 2>&1 | |
$coverageLine = $pytestOutput | Select-String -Pattern "Required test coverage of .* reached. Total coverage: .*" | |
$summaryLine = $pytestOutput | Select-String -Pattern "passed, .* skipped in .*s" | |
# Affichage des résultats de pytest | |
$pytestSuccess = $false | |
if ($coverageLine -match "Required test coverage of (\d+\.\d+)% .* Total coverage: (\d+\.\d+)%") { | |
$requiredCoverage = [double]$matches[1] | |
$totalCoverage = [double]$matches[2] | |
if ($totalCoverage -ge $requiredCoverage) { | |
Color-Text ("1. PYTEST-COV: [SUCCESS] " + $coverageLine) "green" | |
$pytestSuccess = $true | |
} else { | |
Color-Text ("1. PYTEST-COV: [FAILURE] " + $coverageLine) "red" | |
} | |
} else { | |
Write-Host ("1. PYTEST-COV: [FAILURE] " + $coverageLine) | |
} | |
$pytestCovSuccess = $false | |
if ($summaryLine -match "(\d+) passed, (\d+) skipped") { | |
if ($summaryLine -notmatch "(\d+) failed" -and $summaryLine -notmatch "(\d+) error") { | |
Color-Text ("2. PYTEST: [SUCCESS] " + $summaryLine) "green" | |
$pytestCovSuccess = $true | |
} else { | |
Color-Text ("2. PYTEST: [FAILURE] " + $summaryLine) "red" | |
} | |
} else { | |
Color-Text ("2. PYTEST: [FAILURE] " + $summaryLine) "red" | |
} | |
# Exécution d'interrogate et extraction des informations nécessaires | |
$interrogateSuccess = $false | |
$interrogateOutput = Run-Interrogate 2>&1 | |
$resultLine = $interrogateOutput | Select-String -Pattern "RESULT: PASSED \(minimum: .*%, actual: .*%\)" | |
if ($resultLine) { | |
Color-Text ("3. INTERROGATE: [SUCCESS] " + $resultLine) "green" | |
$interrogateSuccess = $true | |
} else { | |
$errorLine = $interrogateOutput | Select-String -Pattern "RESULT: .*" | |
if ($errorLine) { | |
Color-Text ("3. INTERROGATE: [FAILURE] " + $errorLine) "red" | |
} else { | |
Write-Host ("3. INTERROGATE: [FAILURE] " + $interrogateOutput) -ForegroundColor Red | |
} | |
} | |
# Exécution de mypy et extraction des informations nécessaires | |
$mypySuccess = $false | |
mypy XXXXXX > $null | |
$mypySummary = Show-MypyCoverageSummary | |
if ($mypySummary -match "Imprecise: (\d+)/(\d+) LOC \((\d+\.\d+)%\)") { | |
$imprecision = [double]$matches[3] | |
if ($imprecision -lt 10.0) { | |
Color-Text ("4. MYPY: [SUCCESS] " + $mypySummary) "green" | |
$mypySuccess = $true | |
} else { | |
Color-Text ("4. MYPY: [FAILURE] " + $mypySummary) "red" | |
} | |
} else { | |
Write-Host ("4. MYPY: [FAILURE] " + $mypySummary) | |
} | |
Remove-MypyCoverageFiles | |
# Rapport final | |
if ($pytestSuccess -and $pytestCovSuccess -and $interrogateSuccess -and $mypySuccess) { | |
Color-Text ([char]0xD83E + [char]0xDD73 + [char]0xD83E + [char]0xDD73 + [char]0xD83E + [char]0xDD73 + [char]0xD83E + [char]0xDD73 + [char]0xD83E + [char]0xDD73 + [char]0xD83E + [char]0xDD73 + [char]0xD83E + [char]0xDD73 + " [SUCCESS] " + [char]0xD83E + [char]0xDD73 + [char]0xD83E + [char]0xDD73 + [char]0xD83E + [char]0xDD73) "green" | |
} else { | |
Color-Text ([char]0xD83D + [char]0xDE2D + [char]0xD83D + [char]0xDE2D + [char]0xD83D + [char]0xDE2D + [char]0xD83D + [char]0xDE2D + [char]0xD83D + [char]0xDE2D + [char]0xD83D + [char]0xDE2D + [char]0xD83D + [char]0xDE2D + " [FAILURE] " + [char]0xD83D + [char]0xDE2D + [char]0xD83D + [char]0xDE2D + [char]0xD83D + [char]0xDE2D) "red" | |
} | |
} | |
"all" { Run-Pytest; Run-Interrogate; Run-Mypy } | |
default { | |
Write-Host "Commande inconnue : $command" | |
Show-AvailableCommands | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment