Files
git-workshop/01-essentials/02-history/verify.ps1
2026-01-21 10:51:59 +01:00

121 lines
4.9 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the Module 02 challenge solution.
.DESCRIPTION
This script checks that:
- The challenge directory exists
- A Git repository exists
- answers.txt exists with correct information about commit history
#>
. "$PSScriptRoot\..\..\util.ps1"
Write-Host "`n=== Verifying Module 02 Solution ===" -ForegroundColor Cyan
$allChecksPassed = $true
$challengeRoot = "$PSScriptRoot\challenge"
# Check if challenge directory exists
if (-not (Test-Path $challengeRoot)) {
Write-Fail "Challenge directory not found. Did you run setup.ps1?" -ForegroundColor Red
exit 1
}
# Check if git repository exists
if (-not (Test-Path "$challengeRoot\.git")) {
Write-Fail "Not a git repository. Did you run setup.ps1?" -ForegroundColor Red
exit 1
}
# Check if answers.md exists
if (-not (Test-Path "$challengeRoot\answers.md")) {
Write-Fail "answers.md not found. Did you run setup.ps1?" -ForegroundColor Red
Write-Hint "The setup script should have created answers.md for you" -ForegroundColor Yellow
$allChecksPassed = $false
} else {
Write-Pass "answers.md exists" -ForegroundColor Green
# Read the answers file
$answers = Get-Content "$challengeRoot\answers.md" -Raw
$answersLower = $answers.ToLower()
# Check 1: Contains "5" or "five" for commit count
if ($answersLower -match "5|five|fem") {
Write-Pass "Correct commit count found" -ForegroundColor Green
} else {
Write-Fail "Commit count not found or incorrect" -ForegroundColor Red
Write-Hint "Use 'git log --oneline' to count commits easily" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check 2: Contains "database" keyword for third commit
if ($answersLower -match "database") {
Write-Pass "Third commit message identified" -ForegroundColor Green
} else {
Write-Fail "Third commit message not found" -ForegroundColor Red
Write-Hint "Use 'git log' to see commit messages in order" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check 3: Contains both "auth" and "database" keywords for files modified in bug fix
$hasAuth = $answersLower -match "auth"
$hasDatabase = $answersLower -match "database"
if ($hasAuth -and $hasDatabase) {
Write-Pass "Both files identified for bug fix commit" -ForegroundColor Green
} elseif ($hasAuth -or $hasDatabase) {
Write-Host "[PARTIAL] Only one file found - there are TWO files modified in this commit" -ForegroundColor Yellow
Write-Hint "Use 'git log --stat' or 'git show <commit-hash> --name-only' to see ALL files changed" -ForegroundColor Yellow
$allChecksPassed = $false
} else {
Write-Fail "Files modified in bug fix commit not found" -ForegroundColor Red
Write-Hint "Use 'git log --stat' to see which files were changed in each commit" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check 4: Contains "config" keyword for staged file
if ($answersLower -match "config.py") {
Write-Pass "Staged file identified" -ForegroundColor Green
} else {
Write-Fail "Staged file not identified" -ForegroundColor Red
Write-Hint "Use 'git status' or 'git diff --staged' to see staged changes" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check 5: Contains "unicorn" for the secret code
if ($answersLower -match "unicorn") {
Write-Pass "Secret code found!" -ForegroundColor Green
} else {
Write-Fail "Secret code not found" -ForegroundColor Red
Write-Hint "Use 'git diff <commit3> <commit4> database.py' to find the secret code" -ForegroundColor Yellow
$allChecksPassed = $false
}
}
# Final summary
if ($allChecksPassed) {
Write-Host "`n" -NoNewline
Write-Host "=====================================" -ForegroundColor Green
Write-Host " CONGRATULATIONS! CHALLENGE PASSED!" -ForegroundColor Green
Write-Host "=====================================" -ForegroundColor Green
Write-Host "`nYou've successfully explored Git commit history!" -ForegroundColor Cyan
Write-Host "You now know how to:" -ForegroundColor Cyan
Write-Host " - View commit history with git log" -ForegroundColor White
Write-Host " - Find specific commits and their messages" -ForegroundColor White
Write-Host " - See which files changed in commits" -ForegroundColor White
Write-Host " - Check staged changes with git diff --staged" -ForegroundColor White
Write-Host " - Compare changes between specific commits with git diff" -ForegroundColor White
Write-Host "`nReady for the next module!" -ForegroundColor Green
Write-Host ""
} else {
Write-Host "`n[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
}