# Create a feature branch
git checkout -b feature/add-calculator
# Add a new file
mkdir -p src/calculator
cat > src/calculator/operations.py << EOF
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
EOF
# Commit the new file
git add src/calculator/operations.py
git commit -m "Add basic calculator operations"
# Add tests
cat > test_calculator.py << EOF
from src.calculator.operations import add, subtract, multiply, divide
import pytest
def test_add():
assert add(1, 2) == 3
assert add(-1, 1) == 0
def test_subtract():
assert subtract(5, 3) == 2
assert subtract(3, 5) == -2
def test_multiply():
assert multiply(2, 3) == 6
assert multiply(-2, 3) == -6
def test_divide():
assert divide(6, 3) == 2
assert divide(5, 2) == 2.5
with pytest.raises(ValueError):
divide(1, 0)
EOF
# Commit the tests
git add test_calculator.py
git commit -m "Add tests for calculator operations"
# Update the main file to use the calculator
cat > src/main.py << EOF
from calculator.operations import add, subtract, multiply, divide
def hello():
return 'Hello, world!'
def calculate(operation, a, b):
if operation == 'add':
return add(a, b)
elif operation == 'subtract':
return subtract(a, b)
elif operation == 'multiply':
return multiply(a, b)
elif operation == 'divide':
return divide(a, b)
else:
raise ValueError(f"Unknown operation: {operation}")
EOF
# Commit the update
git add src/main.py
git commit -m "Update main to use calculator operations"
# Merge the feature branch
git checkout main
git merge feature/add-calculator -m "Merge feature/add-calculator"
# Create another feature branch
git checkout -b feature/add-power-operation
# Add power operation
cat >> src/calculator/operations.py << EOF
def power(a, b):
return a ** b
EOF
# Commit the new operation
git add src/calculator/operations.py
git commit -m "Add power operation"
# Add test for power operation
cat >> test_calculator.py << EOF
def test_power():
assert power(2, 3) == 8
assert power(3, 2) == 9
EOF
# Fix the import in the test file
sed -i '' '1s/from src.calculator.operations import add, subtract, multiply, divide/from src.calculator.operations import add, subtract, multiply, divide, power/' test_calculator.py
# Commit the test
git add test_calculator.py
git commit -m "Add test for power operation"
# Update main to use power operation
sed -i '' 's/from calculator.operations import add, subtract, multiply, divide/from calculator.operations import add, subtract, multiply, divide, power/' src/main.py
sed -i '' '/ return divide(a, b)/a\ elif operation == '\''power'\'':\n return power(a, b)' src/main.py
# Commit the update
git add src/main.py
git commit -m "Update main to use power operation"
# Merge the feature branch
git checkout main
git merge feature/add-power-operation -m "Merge feature/add-power-operation"