181 lines
6.9 KiB
PowerShell
181 lines
6.9 KiB
PowerShell
#!/usr/bin/env pwsh
|
|
|
|
<#
|
|
.SYNOPSIS
|
|
Verifies the stash challenge solution.
|
|
|
|
.DESCRIPTION
|
|
Checks that the user successfully stashed work, fixed the bug on main,
|
|
and completed the feature on the feature branch.
|
|
#>
|
|
|
|
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 git repository exists
|
|
if (-not (Test-Path ".git")) {
|
|
Write-Host "[FAIL] No git repository found." -ForegroundColor Red
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
# Check current branch
|
|
$currentBranch = git branch --show-current 2>$null
|
|
if ($currentBranch -ne "feature-login") {
|
|
Write-Host "[FAIL] You should be on the 'feature-login' branch." -ForegroundColor Red
|
|
Write-Host "Current branch: $currentBranch" -ForegroundColor Yellow
|
|
Write-Host "Hint: Switch back to feature-login after completing the challenge" -ForegroundColor Yellow
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
# Check for uncommitted changes on feature-login
|
|
$status = git status --porcelain 2>$null
|
|
if ($status) {
|
|
Write-Host "[FAIL] You have uncommitted changes on feature-login." -ForegroundColor Red
|
|
Write-Host "Hint: After restoring from stash, you should complete and commit the feature" -ForegroundColor Yellow
|
|
git status --short
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
# Verify main branch has the security fix
|
|
Write-Host "`nChecking main branch for bug fix..." -ForegroundColor Cyan
|
|
git checkout main 2>$null | Out-Null
|
|
|
|
# Check for bug fix commit
|
|
$mainCommits = git log --pretty=format:"%s" main 2>$null
|
|
if ($mainCommits -notmatch "security bug|Fix.*bug|security fix") {
|
|
Write-Host "[FAIL] No security bug fix commit found on main branch." -ForegroundColor Red
|
|
Write-Host "Hint: After stashing, switch to main and commit a bug fix" -ForegroundColor Yellow
|
|
git checkout feature-login 2>$null | Out-Null
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
# Check that app.js has been fixed
|
|
if (-not (Test-Path "app.js")) {
|
|
Write-Host "[FAIL] app.js not found on main branch." -ForegroundColor Red
|
|
git checkout feature-login 2>$null | Out-Null
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
$appContent = Get-Content "app.js" -Raw
|
|
|
|
# The bug was "return true" in authenticate - it should be fixed now
|
|
# We'll check that the buggy comment is gone or the implementation is improved
|
|
if ($appContent -match "allows unauthenticated access") {
|
|
Write-Host "[FAIL] The security bug comment still exists in app.js." -ForegroundColor Red
|
|
Write-Host "Hint: Remove the bug from app.js and commit the fix" -ForegroundColor Yellow
|
|
git checkout feature-login 2>$null | Out-Null
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "[PASS] Security bug fixed on main!" -ForegroundColor Green
|
|
|
|
# Switch back to feature-login
|
|
Write-Host "`nChecking feature-login branch..." -ForegroundColor Cyan
|
|
git checkout feature-login 2>$null | Out-Null
|
|
|
|
# Check for completed feature commit
|
|
$featureCommits = git log --pretty=format:"%s" feature-login 2>$null
|
|
$commitCount = ($featureCommits -split "`n").Count
|
|
|
|
if ($commitCount -lt 3) {
|
|
Write-Host "[FAIL] Expected at least 3 commits on feature-login." -ForegroundColor Red
|
|
Write-Host "Hint: You should have the initial commits plus your completed feature commit" -ForegroundColor Yellow
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
# Check that login.js exists
|
|
if (-not (Test-Path "login.js")) {
|
|
Write-Host "[FAIL] login.js not found on feature-login branch." -ForegroundColor Red
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
$loginContent = Get-Content "login.js" -Raw
|
|
|
|
# Check that login method exists and is implemented
|
|
if ($loginContent -notmatch "login\(username, password\)") {
|
|
Write-Host "[FAIL] login.js should have a login method." -ForegroundColor Red
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
# Check that TODOs are completed (no TODO comments should remain)
|
|
if ($loginContent -match "TODO") {
|
|
Write-Host "[FAIL] login.js still contains TODO comments." -ForegroundColor Red
|
|
Write-Host "Hint: Complete all the TODOs in login.js before committing" -ForegroundColor Yellow
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
# Check that password verification is implemented
|
|
if ($loginContent -notmatch "password") {
|
|
Write-Host "[FAIL] login method should verify the password." -ForegroundColor Red
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
# Check that the feature has been committed (not just in working directory)
|
|
$lastCommit = git log -1 --pretty=format:"%s" 2>$null
|
|
if ($lastCommit -notmatch "login|feature|complete|implement") {
|
|
Write-Host "[FAIL] Your completed feature should be committed." -ForegroundColor Red
|
|
Write-Host "Last commit: $lastCommit" -ForegroundColor Yellow
|
|
Write-Host "Hint: After popping the stash and completing the TODOs, commit the feature" -ForegroundColor Yellow
|
|
Set-Location ..
|
|
exit 1
|
|
}
|
|
|
|
# Check that no stashes remain (optional check - they should have used pop)
|
|
$stashCount = (git stash list 2>$null | Measure-Object).Count
|
|
if ($stashCount -gt 0) {
|
|
Write-Host "[WARNING] You have $stashCount stash(es) remaining." -ForegroundColor Yellow
|
|
Write-Host "Hint: Use 'git stash pop' instead of 'git stash apply' to automatically remove the stash" -ForegroundColor Yellow
|
|
Write-Host " Or clean up with 'git stash drop'" -ForegroundColor Yellow
|
|
# Don't fail on this, just warn
|
|
}
|
|
|
|
# Verify the login implementation is complete
|
|
if ($loginContent -notmatch "logout|session") {
|
|
Write-Host "[PARTIAL] login.js is missing logout or session functionality." -ForegroundColor Yellow
|
|
Write-Host "Consider adding logout method for a complete implementation" -ForegroundColor Yellow
|
|
# Don't fail - this is just a suggestion
|
|
}
|
|
|
|
# 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 "- Stashed your work in progress" -ForegroundColor White
|
|
Write-Host "- Switched to main branch cleanly" -ForegroundColor White
|
|
Write-Host "- Fixed the critical security bug" -ForegroundColor White
|
|
Write-Host "- Returned to your feature branch" -ForegroundColor White
|
|
Write-Host "- Restored your stashed work" -ForegroundColor White
|
|
Write-Host "- Completed and committed the login feature" -ForegroundColor White
|
|
Write-Host "`nYou now understand how to use git stash!" -ForegroundColor Green
|
|
Write-Host "`nKey takeaway:" -ForegroundColor Yellow
|
|
Write-Host "Stash lets you save incomplete work without committing," -ForegroundColor White
|
|
Write-Host "making it easy to context-switch and handle interruptions.`n" -ForegroundColor White
|
|
|
|
Set-Location ..
|
|
exit 0
|