Files
git-workshop/01-essentials/06-revert/verify.ps1
2026-01-15 16:23:32 +01:00

174 lines
6.9 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the Module 06 challenge solutions.
.DESCRIPTION
Checks that both revert scenarios have been completed correctly:
- regular-revert: Single commit reverted
- multi-revert: Multiple commits reverted
#>
Write-Host "`n=== Verifying Module 06: Git Revert Solutions ===" -ForegroundColor Cyan
$allChecksPassed = $true
$originalDir = Get-Location
# Check if challenge directory exists
if (-not (Test-Path "challenge")) {
Write-Host "[FAIL] Challenge directory not found. Run setup.ps1 first." -ForegroundColor Red
exit 1
}
Set-Location "challenge"
# Check if git repository exists
if (-not (Test-Path ".git")) {
Write-Host "[FAIL] Not a git repository. Run setup.ps1 first." -ForegroundColor Red
Set-Location $originalDir
exit 1
}
# ============================================================================
# SCENARIO 1: Regular Revert Verification
# ============================================================================
Write-Host "`n=== Scenario 1: Regular Revert ===" -ForegroundColor Cyan
git switch regular-revert 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] regular-revert branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Check that a revert commit exists
$revertCommit = git log --oneline --grep="Revert" 2>$null
if ($revertCommit) {
Write-Host "[PASS] Revert commit found" -ForegroundColor Green
} else {
Write-Host "[FAIL] No revert commit found" -ForegroundColor Red
Write-Host "[HINT] Use: git revert <commit-hash>" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that divide.py is removed (was reverted)
if (-not (Test-Path "divide.py")) {
Write-Host "[PASS] Broken divide.py successfully reverted (file removed)" -ForegroundColor Green
} else {
Write-Host "[FAIL] divide.py still exists (should be reverted)" -ForegroundColor Red
Write-Host "[HINT] The bad commit should be reverted, removing divide.py" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that calculator.py exists and has correct content
if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw
# Check that modulo function still exists (should be preserved)
if ($calcContent -match "def modulo") {
Write-Host "[PASS] modulo function preserved (good commit after bad one)" -ForegroundColor Green
} else {
Write-Host "[FAIL] modulo function missing (should still exist)" -ForegroundColor Red
Write-Host "[HINT] Only revert the bad commit, not the good ones after it" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that multiply function exists (should be preserved)
if ($calcContent -match "def multiply") {
Write-Host "[PASS] multiply function preserved (good commit before bad one)" -ForegroundColor Green
} else {
Write-Host "[FAIL] multiply function missing" -ForegroundColor Red
$allChecksPassed = $false
}
} else {
Write-Host "[FAIL] calculator.py not found" -ForegroundColor Red
$allChecksPassed = $false
}
}
# ============================================================================
# SCENARIO 2: Multi Revert Verification
# ============================================================================
Write-Host "`n=== Scenario 2: Multi Revert ===" -ForegroundColor Cyan
git switch multi-revert 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] multi-revert branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Count revert commits
$revertCommits = git log --oneline --grep="Revert" 2>$null
$revertCount = ($revertCommits | Measure-Object).Count
if ($revertCount -ge 2) {
Write-Host "[PASS] Found $revertCount revert commits (expected at least 2)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Found only $revertCount revert commit(s), need at least 2" -ForegroundColor Red
Write-Host "[HINT] Revert both bad commits: git revert <commit1> <commit2>" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that sqrt.py is removed (reverted)
if (-not (Test-Path "sqrt.py")) {
Write-Host "[PASS] Broken sqrt.py successfully reverted (file removed)" -ForegroundColor Green
} else {
Write-Host "[FAIL] sqrt.py still exists (should be reverted)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check that logarithm.py is removed (reverted)
if (-not (Test-Path "logarithm.py")) {
Write-Host "[PASS] Broken logarithm.py successfully reverted (file removed)" -ForegroundColor Green
} else {
Write-Host "[FAIL] logarithm.py still exists (should be reverted)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check calculator.py content
if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw
# Check that power function still exists (good commit before bad ones)
if ($calcContent -match "def power") {
Write-Host "[PASS] power function preserved" -ForegroundColor Green
} else {
Write-Host "[FAIL] power function missing (should still exist)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check that absolute function still exists (good commit after bad ones)
if ($calcContent -match "def absolute") {
Write-Host "[PASS] absolute function preserved" -ForegroundColor Green
} else {
Write-Host "[FAIL] absolute function missing (should still exist)" -ForegroundColor Red
$allChecksPassed = $false
}
} else {
Write-Host "[FAIL] calculator.py not found" -ForegroundColor Red
$allChecksPassed = $false
}
}
Set-Location $originalDir
# Final summary
Write-Host ""
if ($allChecksPassed) {
Write-Host "=========================================" -ForegroundColor Green
Write-Host " CONGRATULATIONS! ALL SCENARIOS PASSED!" -ForegroundColor Green
Write-Host "=========================================" -ForegroundColor Green
Write-Host "`nYou've mastered git revert!" -ForegroundColor Cyan
Write-Host "You now understand:" -ForegroundColor Cyan
Write-Host " ✓ Reverting commits safely without erasing history" -ForegroundColor White
Write-Host " ✓ Reverting multiple commits at once" -ForegroundColor White
Write-Host " ✓ Preserving all other commits while undoing specific changes" -ForegroundColor White
Write-Host "`nReady for Module 07: Git Reset!" -ForegroundColor Green
Write-Host ""
exit 0
} else {
Write-Host "[SUMMARY] Some checks failed. Review the hints above and try again." -ForegroundColor Red
Write-Host "[INFO] You can run this verification script as many times as needed." -ForegroundColor Yellow
Write-Host ""
exit 1
}