#!/usr/bin/env pwsh <# .SYNOPSIS Verifies the cherry-pick challenge solution. .DESCRIPTION Checks that the user successfully cherry-picked only the bug fix commits to the main branch without merging the experimental features. #> . "$PSScriptRoot\..\..\util.ps1" $challengeRoot = "$PSScriptRoot\challenge" if (-not (Test-Path $challengeRoot)) { Write-Host "Error: Challenge directory not found. Run setup.ps1 first." -ForegroundColor Red exit 1 } Write-Host "Verifying your solution..." -ForegroundColor Cyan # Check if git repository exists if (-not (Test-Path "$challengeRoot\.git")) { Write-Fail "No git repository found." -ForegroundColor Red exit 1 } $mainBranch = Get-MainBranch Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan # Check current branch $currentBranch = git branch --show-current 2>$null if ($currentBranch -ne $mainBranch) { Write-Fail "You should be on the '$mainBranch' branch." -ForegroundColor Red Write-Host "Current branch: $currentBranch" -ForegroundColor Yellow Write-Hint "Use 'git checkout $mainBranch' to switch to $mainBranch branch" -ForegroundColor Yellow exit 1 } # Check if there's an ongoing cherry-pick if (Test-Path "$challengeRoot\.git\CHERRY_PICK_HEAD") { Write-Fail "Cherry-pick is not complete. There may be unresolved conflicts." -ForegroundColor Red Write-Hint "Resolve any conflicts, then use:" -ForegroundColor Yellow Write-Host " git add " -ForegroundColor White Write-Host " git cherry-pick --continue" -ForegroundColor White Write-Host "Or abort with: git cherry-pick --abort" -ForegroundColor White exit 1 } # Check commit count on main (should be 4: 2 initial + 2 cherry-picked) $mainCommitCount = (git rev-list --count $mainBranch 2>$null) if ($mainCommitCount -ne 4) { Write-Fail "Expected 4 commits on $mainBranch branch, found $mainCommitCount" -ForegroundColor Red if ($mainCommitCount -lt 4) { Write-Hint "You should cherry-pick 2 bug fix commits to $mainBranch" -ForegroundColor Yellow } else { Write-Hint "You should cherry-pick ONLY the 2 bug fix commits, not all commits" -ForegroundColor Yellow } Write-Host "`nExpected commits on ${mainBranch}:" -ForegroundColor Yellow Write-Host " 1. Initial app implementation" -ForegroundColor White Write-Host " 2. Add README" -ForegroundColor White Write-Host " 3. Fix security vulnerability in input validation (cherry-picked)" -ForegroundColor White Write-Host " 4. Fix performance issue with data caching (cherry-picked)" -ForegroundColor White exit 1 } # Check for merge commits (should be none - cherry-pick doesn't create merge commits) $mergeCommits = git log --merges --oneline $mainBranch 2>$null if ($mergeCommits) { Write-Fail "Found merge commits on $mainBranch. You should use cherry-pick, not merge." -ForegroundColor Red Write-Hint "Use 'git cherry-pick ' instead of 'git merge'" -ForegroundColor Yellow exit 1 } # Check that security.py exists (from the security fix commit) if (-not (Test-Path "$challengeRoot\security.py")) { Write-Fail "security.py not found on $mainBranch branch." -ForegroundColor Red Write-Hint "You need to cherry-pick the 'Fix security vulnerability' commit" -ForegroundColor Yellow exit 1 } # Check that security.py has the security fix $securityContent = Get-Content "$challengeRoot\security.py" -Raw if ($securityContent -notmatch "sanitize_input") { Write-Fail "security.py is missing the sanitize_input function." -ForegroundColor Red exit 1 } if ($securityContent -notmatch "validate_token") { Write-Fail "security.py is missing the validate_token function." -ForegroundColor Red exit 1 } # Check that app.py exists if (-not (Test-Path "$challengeRoot\app.py")) { Write-Fail "app.py not found." -ForegroundColor Red exit 1 } # Check that cache.py exists (from performance fix) if (-not (Test-Path "$challengeRoot\cache.py")) { Write-Fail "cache.py not found on $mainBranch branch." -ForegroundColor Red Write-Hint "You need to cherry-pick the 'Fix performance issue' commit" -ForegroundColor Yellow exit 1 } # Check that cache.py has the DataCache class $cacheContent = Get-Content "$challengeRoot\cache.py" -Raw if ($cacheContent -notmatch "DataCache") { Write-Fail "cache.py is missing the DataCache class." -ForegroundColor Red exit 1 } if ($cacheContent -notmatch "def get\(") { Write-Fail "cache.py is missing the get method." -ForegroundColor Red exit 1 } # Check that app.py does NOT have experimental features $appContent = Get-Content "$challengeRoot\app.py" -Raw # Should NOT have experimental features if ($appContent -match "experimental_mode") { Write-Fail "app.py contains experimental features (experimental_mode)." -ForegroundColor Red Write-Hint "You should cherry-pick ONLY the bug fixes, not experimental features" -ForegroundColor Yellow Write-Host " The experimental feature commits should stay on development branch only" -ForegroundColor Yellow exit 1 } if ($appContent -match "beta_features") { Write-Fail "app.py contains experimental features (beta_features)." -ForegroundColor Red Write-Hint "You should cherry-pick ONLY the bug fixes, not experimental features" -ForegroundColor Yellow exit 1 } if ($appContent -match "enable_experimental_features") { Write-Fail "app.py contains experimental features (enable_experimental_features)." -ForegroundColor Red Write-Hint "You should cherry-pick ONLY the bug fixes, not experimental features" -ForegroundColor Yellow exit 1 } # Check commit messages to verify cherry-picks $commits = git log --pretty=format:"%s" $mainBranch 2>$null $commitArray = $commits -split "`n" $hasSecurityFix = $false $hasPerformanceFix = $false foreach ($commit in $commitArray) { if ($commit -match "security vulnerability") { $hasSecurityFix = $true } if ($commit -match "performance issue") { $hasPerformanceFix = $true } } if (-not $hasSecurityFix) { Write-Fail "Security fix commit not found on $mainBranch branch." -ForegroundColor Red Write-Hint "Cherry-pick the 'Fix security vulnerability' commit from development" -ForegroundColor Yellow exit 1 } if (-not $hasPerformanceFix) { Write-Fail "Performance fix commit not found on $mainBranch branch." -ForegroundColor Red Write-Hint "Cherry-pick the 'Fix performance issue' commit from development" -ForegroundColor Yellow exit 1 } # Verify development branch still has all commits $devCommitCount = (git rev-list --count development 2>$null) if ($devCommitCount -ne 6) { Write-Fail "Development branch should still have 6 commits." -ForegroundColor Red Write-Host "Found: $devCommitCount commits" -ForegroundColor Yellow 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 "- Cherry-picked the security vulnerability fix to $mainBranch" -ForegroundColor White Write-Host "- Cherry-picked the performance issue fix to $mainBranch" -ForegroundColor White Write-Host "- Left experimental features on development branch only" -ForegroundColor White Write-Host "- Kept development branch intact with all commits" -ForegroundColor White Write-Host "`nPerfect use of cherry-pick!" -ForegroundColor Green Write-Host "You selectively applied critical fixes without merging unfinished features.`n" -ForegroundColor Green Write-Host "Try 'git log --oneline --graph --all' to see both branches." -ForegroundColor Cyan exit 0