refactor: use python instead of javascript
This commit is contained in:
@@ -28,21 +28,16 @@ git config user.email "user@workshop.local" | Out-Null
|
||||
|
||||
# Create initial commits on main
|
||||
$app = @"
|
||||
class Calculator {
|
||||
add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
|
||||
subtract(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Calculator;
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
"@
|
||||
|
||||
Set-Content -Path "calculator.js" -Value $app
|
||||
git add calculator.js
|
||||
Set-Content -Path "calculator.py" -Value $app
|
||||
git add calculator.py
|
||||
git commit -m "Initial calculator implementation" | Out-Null
|
||||
|
||||
$readme = @"
|
||||
@@ -59,54 +54,41 @@ git commit -m "Add README" | Out-Null
|
||||
git checkout -b local-feature | Out-Null
|
||||
|
||||
$appWithMultiply = @"
|
||||
class Calculator {
|
||||
add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
|
||||
subtract(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
multiply(a, b) {
|
||||
return a * b;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Calculator;
|
||||
def multiply(self, a, b):
|
||||
return a * b
|
||||
"@
|
||||
|
||||
Set-Content -Path "calculator.js" -Value $appWithMultiply
|
||||
git add calculator.js
|
||||
Set-Content -Path "calculator.py" -Value $appWithMultiply
|
||||
git add calculator.py
|
||||
git commit -m "Add multiply function" | Out-Null
|
||||
|
||||
# Add a bad commit that should be removed with reset
|
||||
$appWithBadCode = @"
|
||||
class Calculator {
|
||||
add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
|
||||
subtract(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
multiply(a, b) {
|
||||
return a * b;
|
||||
}
|
||||
def multiply(self, a, b):
|
||||
return a * b
|
||||
|
||||
// BUG: This is broken and should never have been committed!
|
||||
divide(a, b) {
|
||||
// Forgot to check for division by zero
|
||||
return a / b; // This will return Infinity or NaN for zero!
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Calculator;
|
||||
# BUG: This is broken and should never have been committed!
|
||||
def divide(self, a, b):
|
||||
# Forgot to check for division by zero
|
||||
return a / b # This will raise ZeroDivisionError for zero!
|
||||
"@
|
||||
|
||||
Set-Content -Path "calculator.js" -Value $appWithBadCode
|
||||
git add calculator.js
|
||||
Set-Content -Path "calculator.py" -Value $appWithBadCode
|
||||
git add calculator.py
|
||||
git commit -m "Add broken divide function - DO NOT KEEP" | Out-Null
|
||||
|
||||
# Switch back to main for shared-feature branch
|
||||
@@ -116,87 +98,70 @@ git checkout main | Out-Null
|
||||
git checkout -b shared-feature | Out-Null
|
||||
|
||||
$appWithPower = @"
|
||||
class Calculator {
|
||||
add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
|
||||
subtract(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
power(a, b) {
|
||||
return Math.pow(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Calculator;
|
||||
def power(self, a, b):
|
||||
return a ** b
|
||||
"@
|
||||
|
||||
Set-Content -Path "calculator.js" -Value $appWithPower
|
||||
git add calculator.js
|
||||
Set-Content -Path "calculator.py" -Value $appWithPower
|
||||
git add calculator.py
|
||||
git commit -m "Add power function" | Out-Null
|
||||
|
||||
# Add a bad commit that should be reverted (not reset)
|
||||
$appWithBrokenFeature = @"
|
||||
class Calculator {
|
||||
add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
import math
|
||||
|
||||
subtract(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
|
||||
power(a, b) {
|
||||
return Math.pow(a, b);
|
||||
}
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
// BUG: This breaks the calculator!
|
||||
squareRoot(a) {
|
||||
// This implementation is wrong for negative numbers
|
||||
return Math.sqrt(a); // Returns NaN for negative numbers without warning!
|
||||
}
|
||||
}
|
||||
def power(self, a, b):
|
||||
return a ** b
|
||||
|
||||
module.exports = Calculator;
|
||||
# BUG: This breaks the calculator!
|
||||
def square_root(self, a):
|
||||
# This implementation is wrong for negative numbers
|
||||
return math.sqrt(a) # Raises ValueError for negative numbers without warning!
|
||||
"@
|
||||
|
||||
Set-Content -Path "calculator.js" -Value $appWithBrokenFeature
|
||||
git add calculator.js
|
||||
Set-Content -Path "calculator.py" -Value $appWithBrokenFeature
|
||||
git add calculator.py
|
||||
git commit -m "Add broken feature" | Out-Null
|
||||
|
||||
# Add another good commit after the bad one (to show that revert preserves subsequent commits)
|
||||
$appWithMoreFeatures = @"
|
||||
class Calculator {
|
||||
add(a, b) {
|
||||
return a + b;
|
||||
}
|
||||
import math
|
||||
|
||||
subtract(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
|
||||
power(a, b) {
|
||||
return Math.pow(a, b);
|
||||
}
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
// BUG: This breaks the calculator!
|
||||
squareRoot(a) {
|
||||
// This implementation is wrong for negative numbers
|
||||
return Math.sqrt(a); // Returns NaN for negative numbers without warning!
|
||||
}
|
||||
def power(self, a, b):
|
||||
return a ** b
|
||||
|
||||
modulo(a, b) {
|
||||
return a % b;
|
||||
}
|
||||
}
|
||||
# BUG: This breaks the calculator!
|
||||
def square_root(self, a):
|
||||
# This implementation is wrong for negative numbers
|
||||
return math.sqrt(a) # Raises ValueError for negative numbers without warning!
|
||||
|
||||
module.exports = Calculator;
|
||||
def modulo(self, a, b):
|
||||
return a % b
|
||||
"@
|
||||
|
||||
Set-Content -Path "calculator.js" -Value $appWithMoreFeatures
|
||||
git add calculator.js
|
||||
Set-Content -Path "calculator.py" -Value $appWithMoreFeatures
|
||||
git add calculator.py
|
||||
git commit -m "Add modulo function" | Out-Null
|
||||
|
||||
# Switch to local-feature for the challenge start
|
||||
|
||||
@@ -49,26 +49,26 @@ if ($localCommitCount -ne 3) {
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that calculator.js exists
|
||||
if (-not (Test-Path "calculator.js")) {
|
||||
Write-Host "[FAIL] calculator.js not found." -ForegroundColor Red
|
||||
# Check that calculator.py exists
|
||||
if (-not (Test-Path "calculator.py")) {
|
||||
Write-Host "[FAIL] calculator.py not found." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check calculator.js on local-feature
|
||||
$localCalcContent = Get-Content "calculator.js" -Raw
|
||||
# Check calculator.py on local-feature
|
||||
$localCalcContent = Get-Content "calculator.py" -Raw
|
||||
|
||||
# Should have multiply function
|
||||
if ($localCalcContent -notmatch "multiply") {
|
||||
Write-Host "[FAIL] calculator.js should have the multiply function." -ForegroundColor Red
|
||||
Write-Host "[FAIL] calculator.py should have the multiply function." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Should NOT have divide function (it was in the bad commit that should be reset)
|
||||
if ($localCalcContent -match "divide") {
|
||||
Write-Host "[FAIL] calculator.js should NOT have the divide function." -ForegroundColor Red
|
||||
Write-Host "[FAIL] calculator.py should NOT have the divide function." -ForegroundColor Red
|
||||
Write-Host "Hint: Use 'git reset --hard HEAD~1' to remove the bad commit" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
@@ -115,27 +115,27 @@ if ($sharedCommits -notmatch "Revert") {
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check calculator.js on shared-feature
|
||||
$sharedCalcContent = Get-Content "calculator.js" -Raw
|
||||
# Check calculator.py on shared-feature
|
||||
$sharedCalcContent = Get-Content "calculator.py" -Raw
|
||||
|
||||
# Should have power function
|
||||
if ($sharedCalcContent -notmatch "power") {
|
||||
Write-Host "[FAIL] calculator.js should have the power function." -ForegroundColor Red
|
||||
Write-Host "[FAIL] calculator.py should have the power function." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Should have modulo function (commits after the reverted one should be preserved)
|
||||
if ($sharedCalcContent -notmatch "modulo") {
|
||||
Write-Host "[FAIL] calculator.js should have the modulo function." -ForegroundColor Red
|
||||
Write-Host "[FAIL] calculator.py should have the modulo function." -ForegroundColor Red
|
||||
Write-Host "Hint: Reverting should preserve commits made after the bad one" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Should NOT have squareRoot function (it was in the bad commit that should be reverted)
|
||||
if ($sharedCalcContent -match "squareRoot") {
|
||||
Write-Host "[FAIL] calculator.js should NOT have the squareRoot function." -ForegroundColor Red
|
||||
# Should NOT have square_root function (it was in the bad commit that should be reverted)
|
||||
if ($sharedCalcContent -match "square_root") {
|
||||
Write-Host "[FAIL] calculator.py should NOT have the square_root function." -ForegroundColor Red
|
||||
Write-Host "Hint: The 'Add broken feature' commit should be reverted" -ForegroundColor Yellow
|
||||
Write-Host " Use: git revert <commit-hash-of-broken-feature>" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
@@ -146,7 +146,7 @@ if ($sharedCalcContent -match "squareRoot") {
|
||||
$revertCommitMessage = git log --grep="Revert" --pretty=format:"%s" -n 1 2>$null
|
||||
if ($revertCommitMessage -notmatch "broken feature") {
|
||||
Write-Host "[FAIL] The revert commit should mention 'broken feature'." -ForegroundColor Red
|
||||
Write-Host "Hint: Make sure you reverted the correct commit (the one that added squareRoot)" -ForegroundColor Yellow
|
||||
Write-Host "Hint: Make sure you reverted the correct commit (the one that added square_root)" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user