5.1 A change in a hurry
Build it yourself. Read the tutorial 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
This part does not start where the part before it ends, because the walk skips some changes. This command puts the files that this part starts from in place of yours. Run it, and then go on:
git fetch --quiet --tags --force origin; git restore --source step-03 --staged -- :/ && git restore --source step-04 --staged --worktree -- :/If you start at this part, make your clone as Get the project says, and then run this command. It puts in your clone the files that this part starts from. A file that the project deleted before this part stays, so delete it if git check lists it:
git fetch --quiet --tags --force origin; git rm -rq --cached --ignore-unmatch -- :/ && git restore --source step-04 --staged --worktree -- :/just 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.
Apply the patch to src/api-service/api/service.py, from the top of the project:
curl -fsSL https://github.com/rahuldave/cheesecracker/commit/f77650600c1176caa9aaee86e6cddfccc43fc69c.diff | git apply --include=src/api-service/api/service.pyApply the patch to src/api-service/api/utils.py, from the top of the project:
curl -fsSL https://github.com/rahuldave/cheesecracker/commit/f77650600c1176caa9aaee86e6cddfccc43fc69c.diff | git apply --include=src/api-service/api/utils.pyApply both patches. In service.py, read the line import os, the line z=power(x,2)+power(y,2), and the return at
the end. In utils.py, read def power(base,exponent): and the new function unused_function().
just test -qpytest still prints 21 passed. The tests check what the code does, not how it reads.
Check your files against this move. The command prints nothing when they match, and otherwise shows what differs:
git check f77650600c11If you are stuck, this command puts the files of this move in place of yours. A file that you made yourself stays:
git fetch --quiet --tags --force origin; git restore --source step-04 --staged -- :/ && git restore --source f77650600c11 --staged --worktree -- :/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 patch to 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 patch to 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