Files
git-workshop/02_advanced/03-worktrees/verify.ps1
2026-01-07 23:46:32 +01:00

166 lines
5.8 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the worktrees challenge solution.
.DESCRIPTION
Checks that the user successfully used worktrees to fix a bug
while continuing work on a feature.
#>
Set-Location "challenge" -ErrorAction SilentlyContinue
# Check if challenge directory exists
if (-not (Test-Path "../verify.ps1")) {
Write-Host "Error: Please run this script from the module directory" -ForegroundColor Red
exit 1
}
if (-not (Test-Path ".")) {
Write-Host "Error: Challenge directory not found. Run setup.ps1 first." -ForegroundColor Red
Set-Location ..
exit 1
}
Write-Host "Verifying your solution..." -ForegroundColor Cyan
# Check if main-repo exists
if (-not (Test-Path "main-repo")) {
Write-Host "[FAIL] main-repo directory not found." -ForegroundColor Red
Set-Location ..
exit 1
}
Set-Location "main-repo"
# Check if it's a git repository
if (-not (Test-Path ".git")) {
Write-Host "[FAIL] main-repo is not a git repository." -ForegroundColor Red
Set-Location ../..
exit 1
}
# Check if bugfix branch exists
$branches = git branch --all 2>$null
if ($branches -notmatch "bugfix") {
Write-Host "[FAIL] bugfix branch not found." -ForegroundColor Red
Write-Host "Hint: Create a worktree with: git worktree add ../bugfix-worktree -b bugfix" -ForegroundColor Yellow
Set-Location ../..
exit 1
}
Write-Host "[PASS] Bugfix branch exists!" -ForegroundColor Green
# Check bugfix branch for the fix
git checkout bugfix 2>$null | Out-Null
if (-not (Test-Path "calculator.py")) {
Write-Host "[FAIL] calculator.py not found on bugfix branch." -ForegroundColor Red
Set-Location ../..
exit 1
}
$bugfixCalc = Get-Content "calculator.py" -Raw
# Check if division by zero check was added
if ($bugfixCalc -notmatch "b === 0|b == 0|division by zero|divide by zero") {
Write-Host "[FAIL] Division by zero check not found in bugfix branch." -ForegroundColor Red
Write-Host "Hint: Add a check in the divide method to prevent division by zero" -ForegroundColor Yellow
Set-Location ../..
exit 1
}
# Check for commit on bugfix branch
$bugfixCommits = git log --pretty=format:"%s" bugfix 2>$null
if ($bugfixCommits -notmatch "bug|fix|division|divide") {
Write-Host "[FAIL] No bugfix commit found on bugfix branch." -ForegroundColor Red
Write-Host "Hint: Commit your fix with a descriptive message" -ForegroundColor Yellow
Set-Location ../..
exit 1
}
Write-Host "[PASS] Bug fixed on bugfix branch!" -ForegroundColor Green
# Check feature branch for continued work
git checkout feature-advanced-math 2>$null | Out-Null
$featureCalc = Get-Content "calculator.py" -Raw
# Check if square root function was added
if ($featureCalc -notmatch "sqrt|squareRoot") {
Write-Host "[FAIL] Square root function not found on feature branch." -ForegroundColor Red
Write-Host "Hint: Add the square root method to calculator.py on feature-advanced-math branch" -ForegroundColor Yellow
Set-Location ../..
exit 1
}
# Check that feature work was committed
$featureCommits = git log --pretty=format:"%s" feature-advanced-math 2>$null
$featureCommitArray = $featureCommits -split "`n"
# Should have at least 3 commits: initial + README + power + sqrt
if ($featureCommitArray.Count -lt 4) {
Write-Host "[FAIL] Not enough commits on feature branch." -ForegroundColor Red
Write-Host "Expected: initial, README, power, and square root commits" -ForegroundColor Yellow
Write-Host "Found $($featureCommitArray.Count) commits" -ForegroundColor Yellow
Set-Location ../..
exit 1
}
# Check if the latest feature commit is about square root
if ($featureCommitArray[0] -notmatch "sqrt|square|root") {
Write-Host "[FAIL] Latest commit on feature branch should be about square root." -ForegroundColor Red
Write-Host "Latest commit: $($featureCommitArray[0])" -ForegroundColor Yellow
Set-Location ../..
exit 1
}
Write-Host "[PASS] Feature work completed!" -ForegroundColor Green
# Check if worktree was cleaned up (bugfix-worktree should not exist or be removed)
Set-Location ..
$worktreeStillExists = Test-Path "bugfix-worktree"
if ($worktreeStillExists) {
Write-Host "[WARNING] bugfix-worktree directory still exists." -ForegroundColor Yellow
Write-Host "Hint: Clean up with: git worktree remove ../bugfix-worktree" -ForegroundColor Yellow
# Don't fail on this, just warn
}
# Check worktree list
Set-Location "main-repo"
$worktrees = git worktree list 2>$null
# Verify that the concept was understood (they should have created the worktree at some point)
# We can check this by looking for the bugfix branch existence
if ($branches -notmatch "bugfix") {
Write-Host "[FAIL] No evidence of worktree usage." -ForegroundColor Red
Set-Location ../..
exit 1
}
# Success!
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "SUCCESS! Challenge completed!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host "`nYou have successfully:" -ForegroundColor Cyan
Write-Host "- Created a worktree for the bugfix" -ForegroundColor White
Write-Host "- Fixed the division by zero bug" -ForegroundColor White
Write-Host "- Committed the fix on the bugfix branch" -ForegroundColor White
Write-Host "- Continued feature work in parallel" -ForegroundColor White
Write-Host "- Added the square root function" -ForegroundColor White
Write-Host "- Committed the feature work" -ForegroundColor White
if (-not $worktreeStillExists) {
Write-Host "- Cleaned up the worktree" -ForegroundColor White
}
Write-Host "`nYou now understand Git worktrees!" -ForegroundColor Green
Write-Host "`nKey takeaway:" -ForegroundColor Yellow
Write-Host "Worktrees let you work on multiple branches simultaneously" -ForegroundColor White
Write-Host "without stashing, switching, or cloning the repository.`n" -ForegroundColor White
Set-Location ../..
exit 0