5.1 A change in a hurry
Read the tutorial. Build it yourself is the other way to read this part.
This walk goes through rahuldave/cheesecracker on GitHub. This part starts from the code at step-04. The code before this part
To look at this code on your machine, run these commands. The first one makes a clone, if this folder has none:
[ -d cheesecracker ] || git clone https://github.com/rahuldave/cheesecracker
cd cheesecracker
git fetch --quiet --tags --force origin
git switch --detach step-04just setupThe whole tutorial then adds pytest-cov, a tool that shows which lines of the code the tests run, and a recipe,
just cov, that runs it. The code of the server does not change.
Then someone changes the code in a hurry. It still works, and it reads worse.
5.1 a change in a hurry: the code still works, and reads worse
A change made in a hurry leaves code that works and reads badly. The change adds an import that nothing uses, and takes out spaces and blank lines. It also makes one line 187 characters long, and adds a function that nothing calls.
± See the change to src/api-service/api/service.py
± See the change to src/api-service/api/utils.py
service.py now imports os, which nothing uses. /health follows / with no blank line. The line
z=power(x,2)+power(y,2) has no spaces, and the return at the end is 187 characters long. utils.py has a missing
space, three blank lines, and unused_function().
just test -qpytest still prints 21 passed. The tests check what the code does, not how it reads.
What changed
src/api-service/api/service.py: an import that nothing uses, missing spaces and blank lines, and a long line. 3 lines added, 5 removed. The file- look at
z=power(x,2)+power(y,2)
- look at
src/api-service/api/utils.py: a missing space, extra blank lines, and a function that nothing calls. 6 lines added, 1 removed. The file- look at
def unused_function():
- look at
The diff of src/api-service/api/service.py
@@ -5,6 +5,7 @@ Cheese App API Service
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
import math
+import os
from api.utils import power
# Setup FastAPI app
@@ -19,13 +20,10 @@ app.add_middleware(
allow_headers=["*"],
)
-
# Routes
@app.get("/")
async def get_index():
return {"message": "Welcome to the Cheese App CI/CD Tutorial!"}
-
-
@app.get("/health")
async def health_check():
return {"status": "healthy"}
@@ -36,6 +34,6 @@ async def calculate_euclidean_distance(x: float = 1, y: float = 2):
"""Calculate the Euclidean distance from origin: sqrt(x^2 + y^2)
Returns the distance of a point (x, y) from the origin (0, 0)
"""
- z = power(x, 2) + power(y, 2)
+ z=power(x,2)+power(y,2)
result = math.sqrt(z)
- return {"x": x, "y": y, "distance": result}
+ return {"x": x, "y": y, "distance": result, "message": "This is a very long line that exceeds 120 characters and Black will complain about it so we need to fix this formatting issue"}The diff of src/api-service/api/utils.py
@@ -4,6 +4,11 @@ Used by the Cheese App API Service
"""
-def power(base, exponent):
+def power(base,exponent):
"""Calculate base raised to the power of exponent"""
return base**exponent
+
+
+
+def unused_function():
+ passThis part ends at the code at f77650600c11, partway through step-05. The code after this part