Files
git-workshop/01_essentials/05-revert/verify.ps1

227 lines
9.4 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the Module 05 challenge solutions.
.DESCRIPTION
Checks that all three revert scenarios have been completed correctly:
- regular-revert: Single commit reverted
- merge-revert: Merge commit reverted with -m flag
- multi-revert: Multiple commits reverted
#>
Write-Host "`n=== Verifying Module 05: 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 calculator.py exists
if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw
# Check that divide function is NOT in the code (was reverted)
if ($calcContent -notmatch "def divide") {
Write-Host "[PASS] Broken divide function successfully reverted" -ForegroundColor Green
} else {
Write-Host "[FAIL] divide function still exists (should be reverted)" -ForegroundColor Red
Write-Host "[HINT] The bad commit should be reverted, removing the divide function" -ForegroundColor Yellow
$allChecksPassed = $false
}
# 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: Merge Revert Verification
# ============================================================================
Write-Host "`n=== Scenario 2: Merge Revert ===" -ForegroundColor Cyan
git switch merge-revert 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] merge-revert branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Check that a revert commit for the merge exists
$revertMerge = git log --oneline --grep="Revert.*Merge" 2>$null
if ($revertMerge) {
Write-Host "[PASS] Merge revert commit found" -ForegroundColor Green
} else {
Write-Host "[FAIL] No merge revert commit found" -ForegroundColor Red
Write-Host "[HINT] Use: git revert -m 1 <merge-commit-hash>" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that the original merge commit still exists (revert doesn't erase it)
$mergeCommit = git log --merges --oneline --grep="Merge feature-auth" 2>$null
if ($mergeCommit) {
Write-Host "[PASS] Original merge commit still in history (not erased)" -ForegroundColor Green
} else {
Write-Host "[INFO] Original merge commit not found (this is OK if you used a different approach)" -ForegroundColor Yellow
}
# Check that auth.py no longer exists or its effects are reverted
if (-not (Test-Path "auth.py")) {
Write-Host "[PASS] auth.py removed (merge reverted successfully)" -ForegroundColor Green
} else {
Write-Host "[INFO] auth.py still exists (check if merge was fully reverted)" -ForegroundColor Yellow
}
# Check that calculator.py exists
if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw
# After reverting the merge, calculator shouldn't import auth
if ($calcContent -notmatch "from auth import") {
Write-Host "[PASS] Auth integration reverted from calculator.py" -ForegroundColor Green
} else {
Write-Host "[FAIL] calculator.py still imports auth (merge not fully reverted)" -ForegroundColor Red
Write-Host "[HINT] Reverting the merge should remove the auth integration" -ForegroundColor Yellow
$allChecksPassed = $false
}
}
}
# ============================================================================
# SCENARIO 3: Multi Revert Verification
# ============================================================================
Write-Host "`n=== Scenario 3: 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 calculator.py content
if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw
# Check that square_root is NOT in code (reverted)
if ($calcContent -notmatch "def square_root") {
Write-Host "[PASS] Broken square_root function reverted" -ForegroundColor Green
} else {
Write-Host "[FAIL] square_root function still exists (should be reverted)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check that logarithm is NOT in code (reverted)
if ($calcContent -notmatch "def logarithm") {
Write-Host "[PASS] Broken logarithm function reverted" -ForegroundColor Green
} else {
Write-Host "[FAIL] logarithm function still exists (should be reverted)" -ForegroundColor Red
$allChecksPassed = $false
}
# 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 regular commits safely" -ForegroundColor White
Write-Host " ✓ Reverting merge commits with -m flag" -ForegroundColor White
Write-Host " ✓ Reverting multiple commits at once" -ForegroundColor White
Write-Host " ✓ Preserving history while undoing changes" -ForegroundColor White
Write-Host "`nReady for Module 06: 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
}